1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.xmlEditor.actions;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.gef.commands.Command;
16 import org.eclipse.gef.ui.actions.WorkbenchPartAction;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IWorkbenchPart;
20
21 import sk.baka.ikslibs.ids.IDManager;
22 import sk.baka.ikslibs.interval.DOMIntervalSet;
23 import sk.baka.ikslibs.ptr.DomPointer;
24 import sk.baka.xml.gene.ExportException;
25 import sk.uniba.euromath.EuroMath;
26 import sk.uniba.euromath.document.DocumentException;
27 import sk.uniba.euromath.document.DocumentModifyHelper;
28 import sk.uniba.euromath.document.XMLAccess;
29 import sk.uniba.euromath.editor.lang.ErrorMessages;
30 import sk.uniba.euromath.editor.selections.IDOMSelectionProvider;
31 import sk.uniba.euromath.editor.textEditor.CaretManager;
32 import sk.uniba.euromath.editor.xmlEditor.actions.lang.Messages;
33
34 /***
35 * Base action for generic xml modification actions.<br>
36 * Supposed to be subclassed. Naming convention for actions:
37 * ProccessSubject[Wizard]PlaceAction.
38 *
39 * @author Tomáš Studva 2.2.2006
40 */
41 public abstract class XMLAccessModifyAction extends WorkbenchPartAction {
42
43 /***
44 * Current selection on document.
45 */
46 protected DOMIntervalSet selection = new DOMIntervalSet();
47
48 /***
49 * Constructor.
50 *
51 * @param part
52 * The workbench part associated with this action, not
53 * null
54 */
55 public XMLAccessModifyAction(IWorkbenchPart part) {
56 super(part);
57 if (part == null)
58 throw new IllegalArgumentException();
59 }
60
61 /***
62 * Constructor.
63 *
64 * @param part
65 * The workbench part associated with this action
66 * @param style
67 * the style for this action
68 */
69 public XMLAccessModifyAction(IWorkbenchPart part, int style) {
70 super(part, style);
71 if (part == null)
72 throw new IllegalArgumentException();
73 }
74
75 /***
76 * When overriding, call to this method as first statement is needed.
77 */
78 @Override
79 protected boolean calculateEnabled() {
80 if (getSelectionProvider() != null) {
81 this.selection = getSelectionProvider()
82 .getDOMSelection();
83 } else {
84
85
86 this.selection = new DOMIntervalSet();
87 }
88 processSelection();
89 return getXMLAccess() != null;
90 }
91
92 /***
93 * Refreshes the properties of this action, which depends on selection.
94 * Before method execution, selection is current to editor state.
95 */
96 protected void processSelection() {
97 }
98
99 @Override
100 public void setWorkbenchPart(IWorkbenchPart part) {
101 if (part == null)
102 throw new IllegalArgumentException();
103 super.setWorkbenchPart(part);
104 }
105
106 /***
107 * If action run method should run whole in command(and undo/redo is
108 * getUndoManager().undo/redo), override this method instead of run.
109 */
110 protected void runAsCommand() {
111
112 }
113
114 /***
115 * Default implementation is run action in command.
116 */
117 @Override
118 public void run() {
119 execute(new Command() {
120 public void execute() {
121 runAsCommand();
122 };
123
124 @Override
125 public void undo() {
126 getXMLAccess().getModifier().startModify();
127 getXMLAccess().getUndoManager().undo();
128 try {
129 getXMLAccess().getModifier()
130 .endModify();
131 } catch (ExportException e) {
132 handleExportException(e);
133 }
134 }
135
136 @Override
137 public void redo() {
138 getXMLAccess().getModifier().startModify();
139 getXMLAccess().getUndoManager().redo();
140 try {
141 getXMLAccess().getModifier()
142 .endModify();
143 } catch (ExportException e) {
144 handleExportException(e);
145 }
146 }
147 });
148 }
149
150 /***
151 * Returns shell where the action executes. Use as parent for dialogs,
152 * wizards etc.
153 *
154 * @return parent shell instance.
155 */
156 protected final Shell getShell() {
157 return getWorkbenchPart().getSite().getShell();
158 }
159
160 /***
161 * Returns caret provider.
162 *
163 * @return caret provider
164 */
165 protected CaretManager getCaretManager() {
166 return (CaretManager) (getWorkbenchPart()
167 .getAdapter(CaretManager.class));
168 }
169
170 /***
171 * Returns pointer to place in DOM tree where is caret.
172 *
173 * @return pointer in DOM
174 */
175 protected DomPointer getPointer() {
176 if (getCaretManager() == null)
177 return null;
178 return getCaretManager().getDOMPointer();
179 }
180
181 /***
182 * Returns DOM selection provider.
183 *
184 * @return IDOMSelectionProvider
185 */
186 protected IDOMSelectionProvider getSelectionProvider() {
187 return (IDOMSelectionProvider) (getWorkbenchPart()
188 .getAdapter(IDOMSelectionProvider.class));
189 }
190
191 /***
192 * @return Returns the xmlSelection.
193 */
194 protected DOMIntervalSet getSelection() {
195 return this.selection;
196 }
197
198 /***
199 * Getter.
200 *
201 * @return Returns the modify helper instance.
202 */
203 protected final DocumentModifyHelper getModifyHelper() {
204 return getXMLAccess().getDocumentModifyHelper();
205 }
206
207 /***
208 * Returns instance of the document.
209 *
210 * @return instance of document, can be <code>null</code>.
211 */
212 protected XMLAccess getXMLAccess() {
213 assert (getWorkbenchPart() != null);
214 return (XMLAccess) getWorkbenchPart().getAdapter(
215 XMLAccess.class);
216 }
217
218 /***
219 * Getter.
220 *
221 * @return IDManager instance.
222 */
223 protected IDManager getIdManager() {
224 return getXMLAccess().getIDManager();
225 }
226
227 /***
228 * Error logging method.
229 *
230 * @param e
231 * exception to log
232 * @param msg
233 * description of the error
234 */
235 protected final void logError(Throwable e, String msg) {
236 EuroMath.log(IStatus.ERROR, 0, msg, e);
237 }
238
239 /***
240 * Error logging method.
241 *
242 * @param e
243 * exception to log
244 */
245 protected final void logError(Throwable e) {
246 EuroMath.log(IStatus.ERROR, 0, getId(), e);
247 }
248
249 /***
250 * Handles the exception that occured during the document modification:
251 * prints an error message and logs it.
252 *
253 * @param ex
254 * exception to handle.
255 */
256 protected final void handleExportException(ExportException e) {
257 EuroMath
258 .log(
259 IStatus.ERROR,
260 0,
261 Messages
262 .getString("XMLAccessModifyAction.ExportExceptionLogMessage"), e);
263 MessageDialog
264 .openError(
265 getWorkbenchPart().getSite()
266 .getShell(),
267 ErrorMessages
268 .getString("Error"),
269 Messages
270 .getString("XMLAccessModifyAction.ExportExceptionUserMessage")
271 + e
272 .getLocalizedMessage());
273 }
274
275 /***
276 * Handles the exception that occured when asking for node for invalid
277 * id in IDManager: prints an error message and logs it.
278 *
279 * @param ex
280 * exception to handle.
281 */
282 protected final void handleDocumentException(DocumentException e) {
283 EuroMath.log(IStatus.ERROR, 0, ErrorMessages
284 .getString("IdAccessErrorMessage"), e);
285 MessageDialog
286 .openError(
287 getWorkbenchPart().getSite()
288 .getShell(),
289 ErrorMessages
290 .getString("Error"),
291 Messages
292 .getString("XMLAccessModifyAction.DocumentExceptionUserMessage")
293 + e
294 .getLocalizedMessage());
295 }
296
297 @Override
298 public void dispose() {
299 super.dispose();
300 }
301 }