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.IAdaptable;
15 import org.eclipse.core.runtime.IStatus;
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 import org.w3c.dom.DOMException;
21
22 import sk.baka.ikslibs.ids.IDManager;
23 import sk.baka.ikslibs.interval.DOMIntervalSet;
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.actions.IMultiViewAction;
30 import sk.uniba.euromath.editor.lang.ErrorMessages;
31 import sk.uniba.euromath.editor.selections.IDOMSelectionProvider;
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 * ProccessActor[Wizard]PlaceAction.
38 *
39 * @author Tomáš Studva 2.2.2006
40 */
41 public abstract class XMLAccessModifyAction extends WorkbenchPartAction
42 implements IMultiViewAction, IAdaptable {
43
44 /***
45 * Document instance.
46 */
47 private XMLAccess xmlAccess;
48
49 /***
50 * Current selection on document.
51 */
52 protected DOMIntervalSet selection = null;
53
54 /***
55 * Adaptable provider
56 */
57 protected IAdaptable adaptableProvider;
58
59 /***
60 * Creates a <code>XMLAccessModifyAction</code> and associates it with the
61 * given editor.
62 *
63 * @param part
64 * The workbench part associated with this action
65 */
66 public XMLAccessModifyAction(IWorkbenchPart part) {
67 super(part);
68 }
69
70 /***
71 * Creates a <code>XMLAccessModifyAction</code> and associates it with the
72 * given editor.
73 *
74 * @param part
75 * The workbench part associated with this action
76 * @param style
77 * the style for this action
78 */
79 public XMLAccessModifyAction(IWorkbenchPart part, int style) {
80 super(part, style);
81 }
82
83 /***
84 * Returns DOM selection provider.
85 *
86 * @return IDOMSelectionProvider
87 */
88 protected IDOMSelectionProvider getSelectionProvider() {
89 if (getWorkbenchPart() != null)
90 return (IDOMSelectionProvider) (getWorkbenchPart()
91 .getAdapter(IDOMSelectionProvider.class));
92 return null;
93 }
94
95
96
97
98
99
100 @Override
101 protected boolean calculateEnabled() {
102 return (getXMLAccess() != null);
103 }
104
105 /***
106 * Clears the selection.
107 */
108 protected void clear() {
109 this.selection.clear();
110 }
111
112 /***
113 * @return Returns the xmlSelection.
114 */
115 protected DOMIntervalSet getSelection() {
116 return this.selection;
117 }
118
119 /***
120 * Returns the controller instance.
121 *
122 * @return Returns the controller.
123 */
124 protected final DocumentModifyHelper getModifyHelper() {
125 return getXMLAccess().getDocumentModifyHelper();
126 }
127
128 /***
129 * Returns instance of the document.
130 *
131 * @return instance of document, never <code>null</code>.
132 */
133 protected XMLAccess getXMLAccess() {
134 assert (getWorkbenchPart() != null);
135 if (this.xmlAccess == null) {
136 this.xmlAccess = (XMLAccess) getWorkbenchPart().getAdapter(
137 XMLAccess.class);
138 }
139 return this.xmlAccess;
140 }
141
142 /***
143 * Method call is delagated to AdaptableProvider if is not null, if is null
144 * method is delegated to WorkbenchPart.
145 *
146 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
147 */
148 public Object getAdapter(Class adapter) {
149 if ((getAdaptableProvider() != null)
150 && (getAdaptableProvider().getAdapter(adapter) != null))
151 return getAdaptableProvider().getAdapter(adapter);
152
153 return getWorkbenchPart().getAdapter(adapter);
154 }
155
156 /***
157 * Sets adaptable provider. If adaptable provider is not set, workbench part
158 * is used as IAdaptable.
159 *
160 * @param a
161 * provider to set
162 */
163 public void setAdaptableProvider(IAdaptable a) {
164 this.adaptableProvider = a;
165 }
166
167 /***
168 * Returns IdManager.
169 *
170 * @return IdManager.
171 */
172 protected IDManager getIdManager() {
173 return getXMLAccess().getIdManager();
174 }
175
176 /***
177 * Makes xmlSelection valid to current selection.
178 */
179 protected void processSelection() {
180 }
181
182
183
184
185
186
187 @Override
188 protected void refresh() {
189 processSelection();
190 setEnabled(calculateEnabled());
191 }
192
193
194
195
196
197
198 @Override
199 public void setWorkbenchPart(IWorkbenchPart part) {
200 super.setWorkbenchPart(part);
201 }
202
203 /***
204 * Setter for xml access.
205 *
206 * @param access
207 * XMLAccess to set
208 */
209 public void setXMLAccess(XMLAccess access) {
210 this.xmlAccess = access;
211 }
212
213 /***
214 * Error logging method.
215 *
216 * @param e
217 * exception to log
218 * @param msg
219 * description of the error
220 */
221 protected final void logError(Throwable e, String msg) {
222 EuroMath.log(IStatus.ERROR, 0, msg, e);
223 }
224
225 /***
226 * Error logging method.
227 *
228 * @param e
229 * exception to log
230 */
231 protected final void logError(Throwable e) {
232 EuroMath.log(IStatus.ERROR, 0, getId(), e);
233 }
234
235 /***
236 * Handles the exception that occured during the document modification:
237 * prints an error message and logs it.
238 *
239 * @param ex
240 * exception to handle.
241 */
242 protected final void handleExportException(ExportException ex) {
243 EuroMath
244 .log(
245 IStatus.ERROR,
246 0,
247 Messages
248 .getString("XMLAccessModifyAction.ExportExceptionLogMessage"), ex);
249 MessageDialog
250 .openError(
251 getWorkbenchPart().getSite().getShell(),
252 ErrorMessages.getString("Error"),
253 Messages
254 .getString("XMLAccessModifyAction.ExportExceptionUserMessage")
255 + ex.getLocalizedMessage());
256 }
257
258 /***
259 * Handles the exception that occured when asking for node for invalid id in
260 * IDManager: prints an error message and logs it.
261 *
262 * @param ex
263 * exception to handle.
264 */
265 protected final void handleDocumentException(DocumentException ex) {
266 EuroMath.log(IStatus.ERROR, 0, ErrorMessages
267 .getString("IdAccessErrorMessage"), ex);
268 MessageDialog
269 .openError(
270 getWorkbenchPart().getSite().getShell(),
271 ErrorMessages.getString("Error"),
272 Messages
273 .getString("XMLAccessModifyAction.DocumentExceptionUserMessage")
274 + ex.getLocalizedMessage());
275 }
276
277 /***
278 * TODO
279 * @param ex
280 * exception to handle.
281 */
282 protected final void handleDOMException(DOMException ex) {
283 EuroMath.log(IStatus.ERROR, 0, ErrorMessages
284 .getString("IdAccessErrorMessage"), ex);
285 MessageDialog
286 .openError(
287 getWorkbenchPart().getSite().getShell(),
288 ErrorMessages.getString("Error"),
289 Messages
290 .getString("XMLAccessModifyAction.DocumentExceptionUserMessage")
291 + ex.getLocalizedMessage());
292 }
293
294 /***
295 * Returns shell where the action executes. Use as parent for dialogs,
296 * wizards etc.
297 *
298 * @return parent shell instance.
299 */
300 protected final Shell getShell() {
301 return getWorkbenchPart().getSite().getShell();
302 }
303
304 @Override
305 public void update() {
306 if (getSelectionProvider() != null)
307 this.selection = new DOMIntervalSet(getSelectionProvider()
308 .getDOMSelection());
309 else
310 logError(new IllegalStateException("Selection provider missing."));
311 super.update();
312 }
313
314 /***
315 * @return Returns the adaptableProvider.
316 */
317 protected IAdaptable getAdaptableProvider() {
318 return this.adaptableProvider;
319 }
320 }