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.dnd.TextTransfer;
15  import org.eclipse.swt.dnd.Transfer;
16  import org.w3c.dom.DOMException;
17  import sk.baka.ikslibs.levelmapper.NodeListID;
18  import sk.baka.xml.gene.ExportException;
19  import sk.uniba.euromath.document.DocumentModifier;
20  import sk.uniba.euromath.editor.IEditor;
21  import sk.uniba.euromath.editor.textEditor.TextPieceInfoImpl;
22  import sk.uniba.euromath.editor.textEditor.actions.lang.Messages;
23  
24  /***
25   * Standart cut action.
26   * 
27   * @author Tomáš Studva 8.7.2005
28   */
29  public class CutAction extends CopyAction {
30  
31      /***
32       * Id of action.
33       */
34      @SuppressWarnings("hiding")
35      public static final String id = CutAction.class.toString();
36  
37      /***
38       * Constructor.
39       */
40      public CutAction() {
41          super();
42          setId(id);
43          setText(Messages.getString("CutAction.Text")); //$NON-NLS-1$
44      }
45  
46      @Override
47      protected boolean calculateEnabled() {
48          return super.calculateEnabled();// TODO Studva
49      }
50  
51      @Override
52      public void run() {
53          // TODO Studva review, refactor
54          // is selected text
55          if (!getTextIds().isEmpty()) {
56              String text = new String();
57              IEditor editor = (IEditor) getWorkbenchPart().getAdapter(
58                      IEditor.class);
59              if (editor != null)
60                  throw new IllegalStateException();
61              editor.sortIds(getTextIds());
62              DocumentModifier dm = getXMLAccess().getModifier();
63              dm.startModify();
64  
65              for (String fullID : getTextIds()) {
66  
67                  String nodeID = TextPieceInfoImpl.getNodeID(fullID);
68                  int offset = TextPieceInfoImpl.getOffset(fullID);
69                  int length = TextPieceInfoImpl.getLength(fullID);
70  
71                  try {
72                      NodeListID nodeList = getIdManager().getNode(nodeID);
73                      text = text
74                              + nodeList.getTextValue().substring(offset,
75                                      offset + length);
76                      if (nodeList.isDeletable(offset, offset + length)) {
77                          dm.delete(nodeList, offset, offset + length);
78                      }
79  
80                  } catch (DOMException e) {
81                      handleDOMException(e);
82                  }
83              }
84  
85              try {
86                  dm.endModify();
87              } catch (ExportException e) {
88                  handleExportException(e);
89              }
90              TextTransfer textTransfer = TextTransfer.getInstance();
91              Transfer[] transfer = new Transfer[] { textTransfer };
92              String[] textA = new String[] { text };
93              getClipboard().setContents(textA, transfer);
94          }
95  
96      }
97  
98  }