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 java.util.ArrayList;
15  import java.util.List;
16  
17  import org.eclipse.swt.dnd.TextTransfer;
18  import org.eclipse.swt.dnd.Transfer;
19  import org.w3c.dom.DOMException;
20  
21  import sk.baka.ikslibs.levelmapper.NodeListID;
22  import sk.uniba.euromath.editor.IEditor;
23  import sk.uniba.euromath.editor.textEditor.TextPieceInfoImpl;
24  import sk.uniba.euromath.editor.textEditor.actions.lang.Messages;
25  
26  /***
27   * Standart copy action.
28   * 
29   * @author Tomáš Studva 8.7.2005
30   */
31  public class CopyAction extends ClipboardAction {
32  
33      /***
34       * Id of action.
35       */
36      public static final String id = CopyAction.class.toString();
37  
38      /***
39       * Holds ids of selected text
40       */
41      private final List<String> textIds = new ArrayList<String>();
42  
43      /***
44       * Holds selected element ids
45       */
46      private final List<String> elementIds = new ArrayList<String>();
47  
48      /***
49       * Constructor.
50       */
51      public CopyAction() {
52          super(null);
53          setId(id);
54          setText(Messages.getString("CopyAction.Text")); //$NON-NLS-1$
55      }
56  
57      @Override
58      protected boolean calculateEnabled() {
59          return super.calculateEnabled() && (!getSelection().isEmpty())
60                  && (getTextIds().isEmpty() || getElementIds().isEmpty());
61      }
62  
63      @Override
64      protected void processSelection() {
65          super.processSelection();
66          for (String id : getSelection().getContentIds(getIdManager())) {
67              if (!id.equals(TextPieceInfoImpl.getNodeID(id)))
68                  getTextIds().add(id);
69              else
70                  getElementIds().add(id);
71          }
72  
73      }
74  
75      @Override
76      protected void clear() {
77          super.clear();
78          getTextIds().clear();
79          getElementIds().clear();
80      }
81  
82      @Override
83      public void run() {
84          // is selected text
85          if (!getTextIds().isEmpty()) {
86              String text = new String();
87              IEditor editor = (IEditor) getWorkbenchPart().getAdapter(
88                      IEditor.class);
89              if (editor == null)
90                  throw new IllegalStateException();
91  
92              editor.sortIds(getTextIds());
93  
94              for (String fullID : getTextIds()) {
95  
96                  String nodeID = TextPieceInfoImpl.getNodeID(fullID);
97                  int offset = TextPieceInfoImpl.getOffset(fullID);
98                  int length = TextPieceInfoImpl.getLength(fullID);
99  
100                 try {
101                     NodeListID nodeList = getIdManager().getNode(nodeID);
102                     text = text
103                             + nodeList.getTextValue().substring(offset,
104                                     offset + length);
105 
106                 } catch (DOMException e) {
107                     handleDOMException(e);
108                 }
109             }
110             TextTransfer textTransfer = TextTransfer.getInstance();
111             Transfer[] transfer = new Transfer[] { textTransfer };
112             String[] textA = new String[] { text };
113             getClipboard().setContents(textA, transfer);
114         }
115 
116     }
117 
118     /***
119      * @return Returns the textIds.
120      */
121     protected List<String> getTextIds() {
122         return this.textIds;
123     }
124 
125     /***
126      * @return Returns the eIds.
127      */
128     protected List<String> getElementIds() {
129         return this.elementIds;
130     }
131 
132 }