View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor.textEditor.commands;
13  
14  import org.eclipse.core.runtime.IStatus;
15  import org.eclipse.gef.commands.Command;
16  import org.w3c.dom.Node;
17  import sk.baka.ikslibs.levelmapper.NodeListID;
18  import sk.baka.ikslibs.ptr.DomPointer;
19  import sk.baka.xml.gene.ExportException;
20  import sk.uniba.euromath.EuroMath;
21  import sk.uniba.euromath.document.XMLAccess;
22  import sk.uniba.euromath.editor.textEditor.ITextPieceKeeper;
23  
24  /***
25   * Command to insert text. Only to delete. Use canExecute to test if deletin can
26   * be performed.
27   * 
28   * @author Tomáš Studva 1.10.2005
29   */
30  public class InsertTextCommand extends Command {
31  
32      /***
33       * Text to insert.
34       */
35      private final String text;
36  
37      /***
38       * XMLAccess instance.
39       */
40      private final XMLAccess xmlAccess;
41  
42      /***
43       * Pointer to xml, where to insert text.
44       */
45      private final DomPointer insertPointer;
46  
47      /***
48       * Constructor. Target of insertion = piece keeper's text.
49       * 
50       * @param pieceKeeper
51       *            pieceKeeper holding text where insertion will be done
52       * @param position
53       *            position of insertion in <b>pieceKeeper's text</b>
54       * @param textToInsert
55       *            text to insert
56       * @param xmlAccess
57       *            XMLAccess instance
58       */
59      public InsertTextCommand(ITextPieceKeeper pieceKeeper, int position,
60              String textToInsert, XMLAccess xmlAccess) {
61          if ((position < 0) || (position > pieceKeeper.getText().length()))
62              throw new IllegalArgumentException("position"); //$NON-NLS-1$
63          this.text = textToInsert;
64          this.xmlAccess = xmlAccess;
65  
66          String id = pieceKeeper.getTextPieceInfo().getNodeID();
67          // get XML text node(s)
68          NodeListID textNode = this.xmlAccess.getIdManager().getNodeNull(id);
69          assert textNode != null;
70          assert textNode.isTextual();
71  
72          // resolve position to the XML text(s) position
73          String prevText = pieceKeeper.getWholeTextToPosition(position);
74          int nodeTextPosition = textNode.resolveIndex(prevText, prevText
75                  .length());
76          // get the insertion point
77          this.insertPointer = textNode.getPointer(nodeTextPosition, true, true);
78  
79      }
80  
81      /***
82       * Constructor. Target of insertion = textual node's text.
83       * 
84       * @param id
85       *            of textual node holding text where insertion will be done
86       * @param position
87       *            position of insertion in <b>textual node's text</c>
88       * @param textToInsert
89       *            text to insert
90       * @param xmlAccess
91       *            XMLAccess instance
92       */
93      public InsertTextCommand(String id, int position, String textToInsert,
94              XMLAccess xmlAccess) {
95          this.text = textToInsert;
96          this.xmlAccess = xmlAccess;
97          // get XML text node(s)
98          NodeListID textNode = this.xmlAccess.getIdManager().getNodeNull(id);
99          assert textNode != null;
100         assert textNode.isTextual();
101         // get the insertion point
102         this.insertPointer = textNode.getPointer(position, true, true);
103 
104     }
105 
106     @Override
107     public void execute() {
108         if (!canExecute())
109             throw new IllegalArgumentException(
110                     "Illegal call of execute method. Command cannot be executed."); //$NON-NLS-1$
111         // modify the document
112         this.xmlAccess.getModifier().startModify();
113         this.xmlAccess.getModifier().insertText(this.insertPointer.ip,
114                 this.insertPointer.parentElement, this.text, Node.TEXT_NODE);
115         // transform the document
116         try {
117             this.xmlAccess.getModifier().endModify();
118         } catch (ExportException ex) {
119             EuroMath.log(IStatus.ERROR, 0, "Error during transformation", ex); //$NON-NLS-1$
120         }
121     }
122 
123     @Override
124     public boolean canExecute() {
125         return !this.insertPointer.inEntity();
126 
127     }
128 }