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.plugin.views.outline.actions;
13  
14  import java.util.List;
15  
16  import org.eclipse.gef.ui.actions.ActionRegistry;
17  import org.eclipse.jface.action.Action;
18  import org.eclipse.jface.action.IAction;
19  import org.eclipse.jface.action.IMenuListener;
20  import org.eclipse.jface.action.IMenuManager;
21  import org.eclipse.jface.action.IToolBarManager;
22  import org.eclipse.jface.action.MenuManager;
23  import org.eclipse.jface.action.Separator;
24  import org.eclipse.swt.widgets.Menu;
25  import org.eclipse.ui.IWorkbenchActionConstants;
26  import org.eclipse.ui.IWorkbenchPart;
27  import org.w3c.dom.Node;
28  
29  import sk.baka.ikslibs.DOMUtils;
30  import sk.baka.ikslibs.ptr.DomPointer;
31  import sk.baka.ikslibs.ptr.DomPointerFactory;
32  import sk.uniba.euromath.editor.actions.AbstractActionContributor;
33  import sk.uniba.euromath.editor.actions.IMultiViewAction;
34  import sk.uniba.euromath.editor.textEditor.actions.DeleteMixedAction;
35  import sk.uniba.euromath.editor.xmlEditor.actions.ActionConsts;
36  import sk.uniba.euromath.editor.xmlEditor.actions.AddAttributeWizardAction;
37  import sk.uniba.euromath.editor.xmlEditor.actions.DeleteElementsOnlyAction;
38  import sk.uniba.euromath.editor.xmlEditor.actions.ModifyNodeAction;
39  import sk.uniba.euromath.editor.xmlEditor.actions.RedoAction;
40  import sk.uniba.euromath.editor.xmlEditor.actions.UndoAction;
41  import sk.uniba.euromath.plugin.views.outline.XMLAccessContentProvider;
42  import sk.uniba.euromath.plugin.views.outline.XMLOutlinePage;
43  import sk.uniba.euromath.plugin.views.outline.actions.lang.Messages;
44  
45  /***
46   * @author Tomáš Studva 16.10.2005
47   */
48  public class XMLOutlineActionContributor extends AbstractActionContributor {
49  
50          protected IWorkbenchPart workbenchPart;
51  
52          protected XMLOutlinePage xmlOutlinePage;
53  
54          protected ActionRegistry actionRegistry;
55  
56          private OutlineContentSwitcher attributeSwitcher;
57  
58          private OutlineContentSwitcher cdataSwitcher;
59  
60          private OutlineContentSwitcher commentSwitcher;
61  
62          private OutlineContentSwitcher prociSwitcher;
63  
64          private OutlineContentSwitcher textNodeSwitcher;
65  
66          private OutlineSourceSwitcher sourceSwitcher;
67  
68          /***
69           * Constructor.
70           * 
71           * @param wokbenchPart
72           */
73          public XMLOutlineActionContributor(IWorkbenchPart wokbenchPart,
74                          XMLOutlinePage page) {
75                  super(page);
76                  setWorkbechPart(wokbenchPart);
77                  setXMLOutlinePage(page);
78                  setActionRegistry(new ActionRegistry());
79                  createContextMenu();
80  
81                  setAttributeSwitcher(new OutlineContentSwitcher(
82                                  getContentProvider(),
83                                  Messages
84                                                  .getString("XMLOutlineActionContributor.0"), Node.ATTRIBUTE_NODE, true)); //$NON-NLS-1$
85                  setCdataSwitcher(new OutlineContentSwitcher(
86                                  getContentProvider(),
87                                  Messages
88                                                  .getString("XMLOutlineActionContributor.1"), Node.CDATA_SECTION_NODE, true)); //$NON-NLS-1$
89                  setCommentSwitcher(new OutlineContentSwitcher(
90                                  getContentProvider(),
91                                  Messages
92                                                  .getString("XMLOutlineActionContributor.2"), Node.COMMENT_NODE, true)); //$NON-NLS-1$
93                  setProciSwitcher(new OutlineContentSwitcher(
94                                  getContentProvider(),
95                                  Messages
96                                                  .getString("XMLOutlineActionContributor.3"), //$NON-NLS-1$
97                                  Node.PROCESSING_INSTRUCTION_NODE, true));
98                  setTextNodeSwitcher(new OutlineContentSwitcher(
99                                  getContentProvider(),
100                                 Messages
101                                                 .getString("XMLOutlineActionContributor.4"), Node.TEXT_NODE, true)); //$NON-NLS-1$
102                 // sourceSwitcher = new
103                 // OutlineSourceSwitcher(getXMLOutlinePage());
104                 fillLocalMenu(getXMLOutlinePage().getSite().getActionBars()
105                                 .getMenuManager());
106                 fillLocalToolBar(getXMLOutlinePage().getSite().getActionBars()
107                                 .getToolBarManager());
108         }
109 
110         @Override
111         protected void configureFactory() {
112                 // undo, redo
113                 getActionFactory().registerAction(UndoAction.id,
114                                 UndoAction.class);
115                 getActionFactory().registerAction(RedoAction.id,
116                                 RedoAction.class);
117 
118                 // deleting actions
119                 getActionFactory().registerAction(DeleteElementsOnlyAction.id,
120                                 DeleteElementsOnlyAction.class);
121 
122                 // attribute actions
123                 getActionFactory().registerAction(AddAttributeWizardAction.id,
124                                 AddAttributeWizardAction.class);
125                 getActionFactory().registerAction(ModifyNodeAction.id,
126                                 ModifyNodeAction.class);
127 
128                 // element actions
129                 getActionFactory().registerAction(DecloseElementAction.id,
130                                 DecloseElementAction.class);
131                 getActionFactory().registerAction(EncloseElementAction.id,
132                                 EncloseElementAction.class);
133 
134         }
135 
136         private void createContextMenu() {
137                 MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
138                 menuMgr.setRemoveAllWhenShown(true);
139                 menuMgr.addMenuListener(new IMenuListener() {
140                         public void menuAboutToShow(IMenuManager manager) {
141                                 populateContextMenu(manager);
142                         }
143                 });
144                 Menu menu = menuMgr.createContextMenu(getXMLOutlinePage()
145                                 .getControl());
146                 getXMLOutlinePage().getControl().setMenu(menu);
147                 getXMLOutlinePage().getSite().registerContextMenu(
148                                 "menu1", menuMgr, //$NON-NLS-1$
149                                 null);
150         }
151 
152         protected void populateContextMenu(IMenuManager manager) {
153                 final List<Node> selectedNodes = getXMLOutlinePage()
154                                 .getDOMSelection().getContents();
155                 List<IMultiViewAction> actions;
156 
157                 manager.add(new Separator("Undo actions")); //$NON-NLS-1$
158                 actions = checkOrCreateActions(ActionConsts.UNDO_ACTIONS);
159                 for (IAction action : actions) {
160                         manager.add(action);
161                 }
162 
163                 manager.add(new Separator("Node actions")); //$NON-NLS-1$
164                 // Insert
165                 // ->insert
166                 MenuManager insertSubMenu = new MenuManager("Insert"); //$NON-NLS-1$
167                 manager.add(insertSubMenu);
168                 // ->insert -> beforeSubMenu
169                 MenuManager insertBeforeSubMenu = new MenuManager("Before"); //$NON-NLS-1$
170                 insertBeforeSubMenu.setRemoveAllWhenShown(true);
171                 insertBeforeSubMenu.addMenuListener(new IMenuListener() {
172                         public void menuAboutToShow(IMenuManager manager) {
173                                 DOMUtils.sortByDocumentOrder(selectedNodes);
174                                 DomPointer p = DomPointerFactory
175                                                 .create(selectedNodes.get(0));
176                                 populateInsertSubMenu(p, manager);
177                         }
178                 });
179                 insertSubMenu.add(insertBeforeSubMenu);
180                 // ->insert -> afterSubMenu
181                 MenuManager insertAfterSubMenu = new MenuManager("After"); //$NON-NLS-1$
182                 insertAfterSubMenu.setRemoveAllWhenShown(true);
183                 insertAfterSubMenu.addMenuListener(new IMenuListener() {
184                         public void menuAboutToShow(IMenuManager manager) {
185                                 DOMUtils.sortByDocumentOrder(selectedNodes);
186                                 Node n = selectedNodes.get(
187                                                 selectedNodes.size() - 1)
188                                                 .getNextSibling();
189                                 if (n.getParentNode() == null)
190                                         return;
191                                 DomPointer p = DomPointerFactory.create(n
192                                                 .getParentNode(), n);
193                                 populateInsertSubMenu(p, manager);
194                         }
195                 });
196                 insertSubMenu.add(insertAfterSubMenu);
197                 // ->insert -> AsFirstChildSubMenu
198                 if (selectedNodes.size() == 1
199                                 && selectedNodes.get(0).getNodeType() == Node.ELEMENT_NODE) {
200                         MenuManager insertAsFirstChildSubMenu = new MenuManager(
201                                         "As first child"); //$NON-NLS-1$
202                         insertAsFirstChildSubMenu.setRemoveAllWhenShown(true);
203                         insertAsFirstChildSubMenu
204                                         .addMenuListener(new IMenuListener() {
205                                                 public void menuAboutToShow(
206                                                                 IMenuManager manager) {
207                                                         Node n = selectedNodes
208                                                                         .get(0);
209                                                         // TODO
210                                                         if (n.getParentNode() == null)
211                                                                 return;
212                                                         DomPointer p = DomPointerFactory
213                                                                         .create(
214                                                                                         n
215                                                                                                         .getParentNode(),
216                                                                                         n);
217                                                         populateInsertSubMenu(
218                                                                         p,
219                                                                         manager);
220                                                 }
221                                         });
222                         insertSubMenu.add(insertAsFirstChildSubMenu);
223                 }
224                 // Delete
225                 // ->delete
226                 manager.add(checkOrCreateAction(DeleteMixedAction.id));
227 
228                 manager.add(new Separator(
229                                 IWorkbenchActionConstants.MB_ADDITIONS));
230                 manager.updateAll(true);
231         }
232 
233         protected void populateInsertSubMenu(DomPointer p, IMenuManager manager) {
234               //TODO
235                 //  manager.add(checkOrCreateAction(InsertEntityAction));
236         }
237 
238         /***
239          * Fills menu shown after click on arrow in right upper corner.
240          */
241         public void fillLocalMenu(IMenuManager manager) {
242                 manager.add(getAttributeSwitcher());
243                 manager.add(getCdataSwitcher());
244                 manager.add(getCommentSwitcher());
245                 manager.add(getProciSwitcher());
246                 manager.add(getTextNodeSwitcher());
247                 // manager.add(sourceSwitcher);
248         }
249 
250         // IMPLEMENT: when images for atomic xml action will be chosen, local
251         // toolbar
252         // should be filled with them...
253         public void fillLocalToolBar(IToolBarManager manager) {
254                 // do nothing
255         }
256 
257         /*
258          * (non-Javadoc)
259          * 
260          * @see sk.uniba.euromath.editor.xmlEditor.actions.AbstractActionContributor#getWorkbenchPart()
261          */
262         @Override
263         public IWorkbenchPart getWorkbenchPart() {
264                 return this.workbenchPart;
265         }
266 
267         /***
268          * @param outlinePage
269          *                The outlinePage to set.
270          */
271         public void setXMLOutlinePage(XMLOutlinePage xmlOutlinePage) {
272                 this.xmlOutlinePage = xmlOutlinePage;
273         }
274 
275         /*
276          * (non-Javadoc)
277          * 
278          * @see sk.uniba.euromath.editor.xmlEditor.actions.AbstractActionContributor#getActionRegistry()
279          */
280         @Override
281         public ActionRegistry getActionRegistry() {
282                 return this.actionRegistry;
283         }
284 
285         /***
286          * @param actionRegistry
287          *                The actionRegistry to set.
288          */
289         public void setActionRegistry(ActionRegistry actionRegistry) {
290                 this.actionRegistry = actionRegistry;
291         }
292 
293         public XMLAccessContentProvider getContentProvider() {
294                 return getXMLOutlinePage().getContentProvider();
295         }
296 
297         /***
298          * @param workbechPart
299          *                The workbechPart to set.
300          */
301         public void setWorkbechPart(IWorkbenchPart workbenchPart) {
302                 this.workbenchPart = workbenchPart;
303         }
304 
305         private class OutlineContentSwitcher extends Action {
306                 private XMLAccessContentProvider provider;
307 
308                 private short nodeType;
309 
310                 public OutlineContentSwitcher(
311                                 XMLAccessContentProvider provider, String text,
312                                 short nodeType, boolean on) {
313                         super(text, IAction.AS_CHECK_BOX);
314                         setChecked(on);
315                         this.provider = provider;
316                         this.nodeType = nodeType;
317                 }
318 
319                 /*
320                  * (non-Javadoc)
321                  * 
322                  * @see org.eclipse.jface.action.IAction#run()
323                  */
324                 @Override
325                 public void run() {
326                         getProvider().setShowType(nodeType, isChecked());
327                         getXMLOutlinePage().getViewer().refresh();
328                 }
329 
330                 /***
331                  * @return Returns the provider.
332                  */
333                 protected XMLAccessContentProvider getProvider() {
334                         return this.provider;
335                 }
336 
337                 /***
338                  * @param provider
339                  *                The provider to set.
340                  */
341                 protected void setProvider(XMLAccessContentProvider provider) {
342                         this.provider = provider;
343                 }
344         }
345 
346         private class OutlineSourceSwitcher extends Action {
347                 private static final String SHOW_TRN = "Show transformed document"; //$NON-NLS-1$
348 
349                 private static final String SHOW_ORI = "Show original document"; //$NON-NLS-1$
350 
351                 private boolean showsTransformed;
352 
353                 private XMLOutlinePage page;
354 
355                 public OutlineSourceSwitcher(XMLOutlinePage page) {
356                         super(SHOW_TRN);
357                         this.page = page;
358                         this.showsTransformed = false;
359                 }
360 
361                 /*
362                  * (non-Javadoc)
363                  * 
364                  * @see org.eclipse.jface.action.IAction#run()
365                  */
366                 @Override
367                 public void run() {
368                         setShowsTransformed(!isShowsTransformed());
369                         if (isShowsTransformed()) {
370                                 setText(SHOW_ORI);
371                         } else {
372                                 setText(SHOW_TRN);
373                         }
374                         getPage().setShowTransformed(isShowsTransformed());
375                 }
376 
377                 /***
378                  * @return Returns the showsTransformed.
379                  */
380                 protected boolean isShowsTransformed() {
381                         return this.showsTransformed;
382                 }
383 
384                 /***
385                  * @param showsTransformed
386                  *                The showsTransformed to set.
387                  */
388                 protected void setShowsTransformed(boolean showsTransformed) {
389                         this.showsTransformed = showsTransformed;
390                 }
391 
392                 /***
393                  * @return Returns the page.
394                  */
395                 protected XMLOutlinePage getPage() {
396                         return this.page;
397                 }
398 
399                 /***
400                  * @param page
401                  *                The page to set.
402                  */
403                 protected void setPage(XMLOutlinePage page) {
404                         this.page = page;
405                 }
406         }
407 
408         /***
409          * @return Returns the attributeSwitcher.
410          */
411         protected OutlineContentSwitcher getAttributeSwitcher() {
412                 return this.attributeSwitcher;
413         }
414 
415         /***
416          * @param attributeSwitcher
417          *                The attributeSwitcher to set.
418          */
419         protected void setAttributeSwitcher(
420                         OutlineContentSwitcher attributeSwitcher) {
421                 this.attributeSwitcher = attributeSwitcher;
422         }
423 
424         /***
425          * @return Returns the cdataSwitcher.
426          */
427         protected OutlineContentSwitcher getCdataSwitcher() {
428                 return this.cdataSwitcher;
429         }
430 
431         /***
432          * @param cdataSwitcher
433          *                The cdataSwitcher to set.
434          */
435         protected void setCdataSwitcher(OutlineContentSwitcher cdataSwitcher) {
436                 this.cdataSwitcher = cdataSwitcher;
437         }
438 
439         /***
440          * @return Returns the commentSwitcher.
441          */
442         protected OutlineContentSwitcher getCommentSwitcher() {
443                 return this.commentSwitcher;
444         }
445 
446         /***
447          * @param commentSwitcher
448          *                The commentSwitcher to set.
449          */
450         protected void setCommentSwitcher(OutlineContentSwitcher commentSwitcher) {
451                 this.commentSwitcher = commentSwitcher;
452         }
453 
454         /***
455          * @return Returns the prociSwitcher.
456          */
457         protected OutlineContentSwitcher getProciSwitcher() {
458                 return this.prociSwitcher;
459         }
460 
461         /***
462          * @param prociSwitcher
463          *                The prociSwitcher to set.
464          */
465         protected void setProciSwitcher(OutlineContentSwitcher prociSwitcher) {
466                 this.prociSwitcher = prociSwitcher;
467         }
468 
469         /***
470          * @return Returns the sourceSwitcher.
471          */
472         protected OutlineSourceSwitcher getSourceSwitcher() {
473                 return this.sourceSwitcher;
474         }
475 
476         /***
477          * @param sourceSwitcher
478          *                The sourceSwitcher to set.
479          */
480         protected void setSourceSwitcher(OutlineSourceSwitcher sourceSwitcher) {
481                 this.sourceSwitcher = sourceSwitcher;
482         }
483 
484         /***
485          * @return Returns the textNodeSwitcher.
486          */
487         protected OutlineContentSwitcher getTextNodeSwitcher() {
488                 return this.textNodeSwitcher;
489         }
490 
491         /***
492          * @param textNodeSwitcher
493          *                The textNodeSwitcher to set.
494          */
495         protected void setTextNodeSwitcher(
496                         OutlineContentSwitcher textNodeSwitcher) {
497                 this.textNodeSwitcher = textNodeSwitcher;
498         }
499 
500         /***
501          * @return Returns the xmlOutlinePage.
502          */
503         protected XMLOutlinePage getXMLOutlinePage() {
504                 return this.xmlOutlinePage;
505         }
506 
507         /***
508          * @param workbenchPart
509          *                The workbenchPart to set.
510          */
511         protected void setWorkbenchPart(IWorkbenchPart workbenchPart) {
512                 this.workbenchPart = workbenchPart;
513         }
514 
515 }