1
2
3
4
5
6
7
8
9
10
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"));
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
87 if (id.equals(TextPieceInfoImpl.getNodeID(id))) {
88 try {
89 NodeListID nodeList = getIdManager().getNode(id);
90
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
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
115
116
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 }