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.plugin.views.inputBox.actions;
13  
14  import org.eclipse.jface.viewers.IInputProvider;
15  import org.eclipse.ui.IWorkbenchPart;
16  import org.w3c.dom.Node;
17  
18  import sk.baka.xml.gene.ExportException;
19  import sk.uniba.euromath.document.DocumentModifier;
20  import sk.uniba.euromath.editor.textEditor.actions.AtCaretAction;
21  import sk.uniba.euromath.plugin.views.inputBox.lang.Messages;
22  
23  /***
24   * @author Tomáš Studva 20.7.2005
25   */
26  public class InsertTextAction extends AtCaretAction {
27  
28      /***
29       * Id.
30       */
31      public static final String id = InsertTextAction.class.toString();
32  
33      /***
34       * Constructor.
35       */
36      public InsertTextAction(IWorkbenchPart part) {
37          super(part);
38          setId(id);
39          setText(Messages.getString("InsertTextAction.Text")); //$NON-NLS-1$
40      }
41  
42      @Override
43      public void run() {
44          DocumentModifier dm = getXMLAccess().getModifier();
45          try {
46              dm.startModify();
47              dm.insertText(getPointer(), getInputText(), Node.TEXT_NODE);
48              dm.endModify();
49          } catch (ExportException te) {
50              handleExportException(te);
51          }
52      }
53  
54      protected String getInputText() {
55          return (String) ((IInputProvider) (getWorkbenchPart()
56                  .getAdapter(IInputProvider.class))).getInput();
57      }
58  
59      @Override
60      protected boolean calculateEnabled() {
61          return super.calculateEnabled() && (getInputText().length() > 0);
62      }
63  
64  }