View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
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 }