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.HashSet;
16  import java.util.List;
17  import java.util.Set;
18  import org.eclipse.ui.IWorkbenchPart;
19  import org.w3c.dom.DOMException;
20  import org.w3c.dom.Element;
21  import org.w3c.dom.Node;
22  import sk.baka.ikslibs.levelmapper.NodeListID;
23  import sk.baka.xml.gene.ExportException;
24  import sk.uniba.euromath.document.DocumentModifier;
25  import sk.uniba.euromath.editor.textEditor.TextPieceInfoImpl;
26  import sk.uniba.euromath.editor.textEditor.actions.lang.Messages;
27  import sk.uniba.euromath.editor.xmlEditor.actions.XMLAccessModifyAction;
28  
29  /***
30   * Deletes selected text and elements.
31   * 
32   * @author Tomáš Studva 10.6.2005
33   */
34  public class DeleteMixedAction extends XMLAccessModifyAction {
35  
36      /***
37       * Default id of action.
38       */
39      public static final String id = DeleteMixedAction.class.toString();
40  
41      /***
42       * Stores selected elements.
43       * 
44       * @see #processSelection()
45       */
46      private final Set<Element> elements = new HashSet<Element>();
47  
48      /***
49       * Stores text ids from selection.
50       * 
51       * @see #processSelection()
52       */
53      private final List<String> textIds = new ArrayList<String>();
54  
55      /***
56       * Constructor.
57       */
58      public DeleteMixedAction() {
59          this(null);
60      }
61  
62      /***
63       * Constructor.
64       * @param part
65       */
66      public DeleteMixedAction(IWorkbenchPart part) {
67          super(part);
68          setId(this.getClass().toString());
69          setText(Messages.getString("DeleteMixedAction.Text")); //$NON-NLS-1$
70      }
71  
72      @Override
73      protected boolean calculateEnabled() {
74          return super.calculateEnabled()
75                  && ((!getElements().isEmpty()) || !getTextIds().isEmpty());
76      }
77  
78      /***
79       * Filters selected elements from selection to variable elements.
80       */
81      @Override
82      protected void processSelection() {
83          super.processSelection();
84  
85          for (String id : getSelection().getContentIds(getIdManager())) {
86              // if it is not text
87              if (id.equals(TextPieceInfoImpl.getNodeID(id))) {
88                  try {
89                      NodeListID nodeList = getIdManager().getNode(id);
90                      // it is element
91                      if (nodeList.item(0).getNodeType() == Node.ELEMENT_NODE) {
92                          getElements().add((Element) (nodeList.item(0)));
93                      }
94                  } catch (DOMException e) {
95                      handleDOMException(e);
96                  }
97  
98              } else {
99                  // it is text
100                 getTextIds().add(id);
101             }
102         }
103     }
104 
105     @Override
106     protected void clear() {
107         super.clear();
108         getElements().clear();
109         getTextIds().clear();
110     }
111 
112     @Override
113     public void run() {
114         // TODO Studva review, refactor
115         // FIXME Studva bug
116         // TODO Studva if selected more with one id
117         DocumentModifier dm = getXMLAccess().getModifier();
118         dm.startModify();
119         for (String fullID : getTextIds()) {
120 
121             String nodeID = TextPieceInfoImpl.getNodeID(fullID);
122             int offset = TextPieceInfoImpl.getOffset(fullID);
123             int length = TextPieceInfoImpl.getLength(fullID);
124             try {
125                 NodeListID nodeList = getIdManager().getNode(nodeID);
126                 if (nodeList.isDeletable(offset, offset + length)) {
127                     dm.delete(nodeList, offset, offset + length);
128                 }
129             } catch (DOMException e) {
130                 handleDOMException(e);
131                 return;
132             }
133         }
134 
135         try {
136             dm.endModify();
137             getModifyHelper().deleteElements(getShell(), getElements());
138         } catch (ExportException e) {
139             handleExportException(e);
140         }
141 
142     }
143 
144     /***
145      * @return Returns the elements.
146      */
147     protected Set<Element> getElements() {
148         return this.elements;
149     }
150 
151     /***
152      * @return Returns the textIds.
153      */
154     protected List<String> getTextIds() {
155         return this.textIds;
156     }
157 
158 }