1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.textEditor.actions;
13
14 import org.eclipse.swt.widgets.Caret;
15 import org.eclipse.ui.IWorkbenchPart;
16 import sk.baka.ikslibs.ptr.DomPointer;
17 import sk.baka.ikslibs.ptr.DomPointer;
18 import sk.uniba.euromath.editor.textEditor.ICaretProvider;
19 import sk.uniba.euromath.editor.xmlEditor.actions.XMLAccessModifyAction;
20
21 /***
22 * Common action for actions using caret. Enabled if caret is active in EditPart
23 * with id and nothing is selected.
24 *
25 * @author Tomáš Studva 11.6.2005
26 */
27 public abstract class AtCaretAction extends XMLAccessModifyAction {
28
29 /***
30 * Reference for caret provider. If not null, this caret provider is used,
31 * instead caret provider is serched by IAdaptable from wokbench part.
32 */
33 private ICaretProvider caretProvider;
34
35 /***
36 * Creates a <code>AtCaretAction</code> and associates it with the given
37 * editor.
38 *
39 * @param part
40 * The workbench part associated with this action
41 */
42 public AtCaretAction(IWorkbenchPart part) {
43 super(part);
44 }
45
46 /***
47 * Creates a <code>AtCaretAction</code> and associates it with the given
48 * editor.
49 *
50 * @param part
51 * The workbench part associated with this action
52 *
53 * @param style
54 * the style for this action
55 */
56 public AtCaretAction(IWorkbenchPart part, int style) {
57 super(part, style);
58 }
59
60 /***
61 * Enabled if cursor is active in EditPart with id and nothig is selected.
62 */
63 @Override
64 protected boolean calculateEnabled() {
65 return super.calculateEnabled() && (getSelection().getContentIds(getIdManager()).size() == 0)
66 && (getCaretProvider() != null) && (getPointer() != null);
67 }
68
69 /***
70 * @return Caret
71 */
72 protected Caret getCaret() {
73 if (getCaretProvider() == null)
74 return null;
75 return getCaretProvider().getCaret();
76 }
77
78 /***
79 * Returns pointer to place in DOM tree where is caret.
80 *
81 * @return pointer in DOM
82 */
83 protected DomPointer getPointer() {
84 if (getCaretProvider() == null)
85 return null;
86 return getCaretProvider().getPointer();
87 }
88
89 /***
90 * Returns caret provider.
91 *
92 * @return caret provider
93 */
94 protected ICaretProvider getCaretProvider() {
95 if (this.caretProvider != null)
96 return this.caretProvider;
97 return (ICaretProvider) (getWorkbenchPart()
98 .getAdapter(ICaretProvider.class));
99 }
100
101 /***
102 * @param caretProvider
103 * The caretProvider to set.
104 */
105 public void setCaretProvider(ICaretProvider caretProvider) {
106 this.caretProvider = caretProvider;
107 }
108 }