KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > comp > CompCSMasterTreeSection


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp;
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.net.URL JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.action.Separator;
23 import org.eclipse.jface.action.ToolBarManager;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.viewers.TreeViewer;
28 import org.eclipse.pde.core.IModelChangedEvent;
29 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS;
30 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSConstants;
31 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel;
32 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject;
33 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTask;
34 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskGroup;
35 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSTaskObject;
36 import org.eclipse.pde.internal.ui.PDEPlugin;
37 import org.eclipse.pde.internal.ui.PDEUIMessages;
38 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
39 import org.eclipse.pde.internal.ui.editor.TreeSection;
40 import org.eclipse.pde.internal.ui.editor.actions.CollapseAction;
41 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSDetails;
42 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
43 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.actions.CompCSAddGroupAction;
44 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.actions.CompCSAddTaskAction;
45 import org.eclipse.pde.internal.ui.editor.cheatsheet.comp.actions.CompCSRemoveTaskObjectAction;
46 import org.eclipse.pde.internal.ui.parts.TreePart;
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.events.DisposeEvent;
49 import org.eclipse.swt.events.DisposeListener;
50 import org.eclipse.swt.graphics.Cursor;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Display;
53 import org.eclipse.swt.widgets.ToolBar;
54 import org.eclipse.ui.IEditorInput;
55 import org.eclipse.ui.IFileEditorInput;
56 import org.eclipse.ui.IStorageEditorInput;
57 import org.eclipse.ui.actions.ActionFactory;
58 import org.eclipse.ui.cheatsheets.OpenCheatSheetAction;
59 import org.eclipse.ui.forms.IDetailsPage;
60 import org.eclipse.ui.forms.widgets.FormToolkit;
61 import org.eclipse.ui.forms.widgets.Section;
62
63 /**
64  * CompCSMasterTreeSection
65  *
66  */

67 public class CompCSMasterTreeSection extends TreeSection implements
68         ICSMaster {
69
70     private static final int F_BUTTON_ADD_TASK = 0;
71
72     private static final int F_BUTTON_ADD_GROUP = 1;
73     
74     private static final int F_BUTTON_REMOVE = 2;
75     
76     private static final int F_BUTTON_UP = 3;
77     
78     private static final int F_BUTTON_DOWN = 4;
79
80     private static final int F_BUTTON_PREVIEW = 5;
81     
82     private static final int F_UP_FLAG = -1;
83
84     private static final int F_DOWN_FLAG = 1;
85     
86     private TreeViewer fTreeViewer;
87     
88     private ICompCSModel fModel;
89     
90     private CollapseAction fCollapseAction;
91     
92     private CompCSRemoveTaskObjectAction fRemoveTaskObjectAction;
93     
94     private CompCSAddGroupAction fAddGroupAction;
95     
96     private CompCSAddTaskAction fAddTaskAction;
97     
98     private CompCSGroupValidator fGroupValidator;
99     
100     /**
101      * @param formPage
102      * @param parent
103      * @param style
104      * @param buttonLabels
105      */

106     public CompCSMasterTreeSection(PDEFormPage formPage, Composite parent) {
107         super(formPage, parent, Section.DESCRIPTION, new String JavaDoc[] {
108                 PDEUIMessages.CompCSMasterTreeSection_addTask,
109                 PDEUIMessages.CompCSMasterTreeSection_addGroup,
110                 PDEUIMessages.SimpleCSElementSection_7,
111                 PDEUIMessages.SimpleCSElementSection_1,
112                 PDEUIMessages.SimpleCSElementSection_2,
113                 PDEUIMessages.SimpleCSElementSection_3});
114
115         // Create actions
116
fAddGroupAction = new CompCSAddGroupAction();
117         fAddTaskAction = new CompCSAddTaskAction();
118         fRemoveTaskObjectAction = new CompCSRemoveTaskObjectAction();
119         fCollapseAction = null;
120     }
121
122     /* (non-Javadoc)
123      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
124      */

125     protected void createClient(Section section, FormToolkit toolkit) {
126         // Get the model
127
fModel = (ICompCSModel)getPage().getModel();
128         
129         Composite container = createClientContainer(section, 2, toolkit);
130         createTree(container, toolkit);
131         toolkit.paintBordersFor(container);
132         section.setText(PDEUIMessages.SimpleCSElementSection_4);
133         section.setDescription(PDEUIMessages.SimpleCSElementSection_5);
134         section.setClient(container);
135         initializeTreeViewer();
136         createSectionToolbar(section, toolkit);
137     }
138
139     /**
140      * @param container
141      * @param toolkit
142      */

143     private void createTree(Composite container, FormToolkit toolkit) {
144         TreePart treePart = getTreePart();
145         createViewerPartControl(container, SWT.SINGLE, 2, toolkit);
146         fTreeViewer = treePart.getTreeViewer();
147         fTreeViewer.setContentProvider(new CompCSContentProvider());
148         fTreeViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
149         PDEPlugin.getDefault().getLabelProvider().connect(this);
150         createTreeListeners();
151         // TODO: MP: LOW: CompCS: Implement drag and drop move feature
152
}
153
154     /**
155      *
156      */

157     private void createTreeListeners() {
158         // Create listener for the outline view 'link with editor' toggle
159
// button
160
fTreeViewer.addPostSelectionChangedListener(
161                 getPage().getPDEEditor().new PDEFormEditorChangeListener());
162     }
163
164     /**
165      * @return
166      */

167     public ISelection getSelection() {
168         return fTreeViewer.getSelection();
169     }
170     
171     /**
172      *
173      */

174     private void initializeTreeViewer() {
175
176         if (fModel == null) {
177             return;
178         }
179         fTreeViewer.setInput(fModel);
180         ICompCS cheatsheet = fModel.getCompCS();
181
182         // Create the group validator and register all existing groups to be
183
// validated within the workspace model
184
fGroupValidator = new CompCSGroupValidator(cheatsheet,
185                 getManagedForm().getForm().getForm(),
186                 PDEUIMessages.SimpleCSElementSection_4);
187         
188         // If the cheat sheet already has a task object, then the object has
189
// to be deleted first before a new task or group can be added to
190
// the root cheatsheet node
191
boolean addFlag = false;
192         if (cheatsheet.getFieldTaskObject() == null) {
193             addFlag = fModel.isEditable();
194         }
195         getTreePart().setButtonEnabled(F_BUTTON_ADD_TASK, addFlag);
196         getTreePart().setButtonEnabled(F_BUTTON_ADD_GROUP, addFlag);
197
198         // Set to false because initial node selected is the root cheatsheet node
199
getTreePart().setButtonEnabled(F_BUTTON_REMOVE, false);
200         // Set to false because initial node selected is the root cheatsheet node
201
getTreePart().setButtonEnabled(F_BUTTON_UP, false);
202         // Set to false because initial node selected is the root cheatsheet node
203
getTreePart().setButtonEnabled(F_BUTTON_DOWN, false);
204
205         // Validate initial file content
206
// TODO: MP: LOW: CompCS: The error message does not show up in the form on load for some reason
207
// TODO: MP: LOW: CompCS: Implement error image overlay on icon ILightWeightLabelDecorator
208
// TODO: MP: LOW: CompCS: The error message dissapears on up / down movement
209
updatePreviewButton(fGroupValidator.validate());
210         
211         // Select the cheatsheet node in the tree
212
fTreeViewer.setSelection(new StructuredSelection(cheatsheet), true);
213         fTreeViewer.expandToLevel(2);
214     }
215
216     /**
217      * @param section
218      * @param toolkit
219      */

220     private void createSectionToolbar(Section section, FormToolkit toolkit) {
221         
222         ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
223         ToolBar toolbar = toolBarManager.createControl(section);
224         final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
225         toolbar.setCursor(handCursor);
226         // Cursor needs to be explicitly disposed
227
toolbar.addDisposeListener(new DisposeListener() {
228             public void widgetDisposed(DisposeEvent e) {
229                 if ((handCursor != null) &&
230                         (handCursor.isDisposed() == false)) {
231                     handCursor.dispose();
232                 }
233             }
234         });
235         // Add collapse action to the tool bar
236
fCollapseAction = new CollapseAction(fTreeViewer,
237                 PDEUIMessages.ExtensionsPage_collapseAll,
238                 1,
239                 fModel.getCompCS());
240         toolBarManager.add(fCollapseAction);
241
242         toolBarManager.update(true);
243
244         section.setTextClient(toolbar);
245     }
246
247     /* (non-Javadoc)
248      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#buttonSelected(int)
249      */

250     protected void buttonSelected(int index) {
251         switch (index) {
252         case F_BUTTON_ADD_TASK:
253             handleAddTaskAction();
254             break;
255         case F_BUTTON_ADD_GROUP:
256             handleAddGroupAction();
257             break;
258         case F_BUTTON_REMOVE:
259             handleDeleteAction();
260             break;
261         case F_BUTTON_UP:
262             handleMoveTaskObjectAction(F_UP_FLAG);
263             break;
264         case F_BUTTON_DOWN:
265             handleMoveTaskObjectAction(F_DOWN_FLAG);
266             break;
267         case F_BUTTON_PREVIEW:
268             handlePreviewAction();
269             break;
270         }
271     }
272
273     /* (non-Javadoc)
274      * @see org.eclipse.pde.internal.ui.editor.TreeSection#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
275      */

276     protected void selectionChanged(IStructuredSelection selection) {
277         updateButtons();
278     }
279     
280     /* (non-Javadoc)
281      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details.ISimpleCSMaster#updateButtons()
282      */

283     public void updateButtons() {
284         if (!fModel.isEditable()) {
285             return;
286         }
287         Object JavaDoc object = ((IStructuredSelection) fTreeViewer.getSelection()).getFirstElement();
288         ICompCSObject csObject = (ICompCSObject)object;
289         boolean canAddTask = false;
290         boolean canAddGroup = false;
291         boolean canRemove = false;
292         boolean canMoveUp = false;
293         boolean canMoveDown = false;
294
295         if (csObject != null) {
296             ICompCSObject parent = csObject.getParent();
297             if ((csObject.getType() == ICompCSConstants.TYPE_TASK) ||
298                     (csObject.getType() == ICompCSConstants.TYPE_TASKGROUP)) {
299                 
300                 if ((parent.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) &&
301                         (csObject.getType() == ICompCSConstants.TYPE_TASKGROUP)) {
302                     canAddTask = true;
303                     canAddGroup = true;
304                 } else if (parent.getType() == ICompCSConstants.TYPE_TASKGROUP) {
305                     ICompCSTaskGroup taskGroup = (ICompCSTaskGroup)parent;
306                     ICompCSTaskObject taskObject = (ICompCSTaskObject)csObject;
307                     if (taskGroup.isFirstFieldTaskObject(taskObject) == false) {
308                         canMoveUp = true;
309                     }
310                     if (taskGroup.isLastFieldTaskObject(taskObject) == false) {
311                         canMoveDown = true;
312                     }
313                     canRemove = canRemoveTaskObject(taskGroup);
314                     canAddTask = true;
315                     canAddGroup = true;
316                 }
317             }
318         }
319
320         getTreePart().setButtonEnabled(F_BUTTON_ADD_TASK, canAddTask);
321         getTreePart().setButtonEnabled(F_BUTTON_ADD_GROUP, canAddGroup);
322         getTreePart().setButtonEnabled(F_BUTTON_REMOVE, canRemove);
323         getTreePart().setButtonEnabled(F_BUTTON_UP, canMoveUp);
324         getTreePart().setButtonEnabled(F_BUTTON_DOWN, canMoveDown);
325     }
326
327     /**
328      *
329      */

330     private void handleAddTaskAction() {
331         
332         ISelection sel = fTreeViewer.getSelection();
333         Object JavaDoc object = ((IStructuredSelection) sel).getFirstElement();
334         if (object == null) {
335             return;
336         }
337         if (object instanceof ICompCSTaskGroup) {
338             fAddTaskAction.setParentObject((ICompCSObject)object);
339             fAddTaskAction.run();
340         } else if (object instanceof ICompCSTask) {
341             fAddTaskAction.setParentObject(((ICompCSObject)object).getParent());
342             fAddTaskAction.run();
343         }
344     }
345
346     /**
347      * @param flag
348      */

349     private void updatePreviewButton(boolean flag) {
350         getTreePart().setButtonEnabled(F_BUTTON_PREVIEW, flag);
351     }
352     
353     /**
354      *
355      */

356     private void handleAddGroupAction() {
357         
358         ISelection sel = fTreeViewer.getSelection();
359         Object JavaDoc object = ((IStructuredSelection) sel).getFirstElement();
360         if (object == null) {
361             return;
362         }
363         if (object instanceof ICompCSTaskGroup) {
364             fAddGroupAction.setParentObject((ICompCSObject)object);
365             fAddGroupAction.run();
366         } else if (object instanceof ICompCSTask) {
367             fAddGroupAction.setParentObject(((ICompCSObject)object).getParent());
368             fAddGroupAction.run();
369         }
370     }
371
372     /**
373      *
374      */

375     private void handleMoveTaskObjectAction(int positionFlag) {
376     
377         ISelection sel = fTreeViewer.getSelection();
378         Object JavaDoc object = ((IStructuredSelection) sel).getFirstElement();
379         if (object == null) {
380             return;
381         } else if (object instanceof ICompCSTaskObject) {
382             ICompCSTaskObject taskObject = (ICompCSTaskObject)object;
383             ICompCSTaskGroup parent = null;
384             // Determine the parents type
385
if (taskObject.getParent().getType() == ICompCSConstants.TYPE_TASKGROUP) {
386                 parent = (ICompCSTaskGroup)taskObject.getParent();
387             } else {
388                 return;
389             }
390             // Move the task object up or down one position
391
parent.moveFieldTaskObject(taskObject, positionFlag);
392         }
393     }
394
395     /**
396      *
397      */

398     private void handlePreviewAction() {
399         // Get the editor input
400
// Could be IFileEditorInput (File in workpspace - e.g. Package Explorer View)
401
// Could be IStorageEditorInput (File not in workpsace - e.g. CVS Repositories View)
402
IEditorInput input = getPage().getEditorInput();
403         URL JavaDoc url = null;
404         try {
405             if (input instanceof IFileEditorInput) {
406                 IFileEditorInput fileInput = (IFileEditorInput)input;
407                 url = fileInput.getFile().getLocationURI().toURL();
408             } else if (input instanceof IStorageEditorInput) {
409                 // Note: This URL does not exist on the local file system
410
// As a result any tasks this composite cheat sheet has that
411
// specify a pathes to simple cheat sheets will not resolve
412
// Cheat sheet view will log an error loading simple cheat
413
// sheets
414
IStorageEditorInput storageInput = (IStorageEditorInput)input;
415                 url = storageInput.getStorage().getFullPath().toFile().toURL();
416             } else {
417                 // No base URL. Pathes will definitely not resolve here
418
url = null;
419             }
420         
421             // Write the current model into a String as raw XML
422
StringWriter JavaDoc swriter = new StringWriter JavaDoc();
423             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
424             fModel.getCompCS().write("", writer); //$NON-NLS-1$
425
writer.flush();
426             swriter.close();
427             // Launch in the cheat sheet view
428
OpenCheatSheetAction openAction = new OpenCheatSheetAction(
429                     input.getName(),
430                     input.getName(),
431                     swriter.toString(),
432                     url);
433             openAction.run();
434         } catch (IOException JavaDoc e) {
435             PDEPlugin.logException(e);
436         } catch (CoreException e) {
437             PDEPlugin.logException(e);
438         }
439     }
440
441     /* (non-Javadoc)
442      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
443      */

444     public void modelChanged(IModelChangedEvent event) {
445         // No need to call super, world changed event handled here
446
if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
447             handleModelEventWorldChanged(event);
448         } else if (event.getChangeType() == IModelChangedEvent.INSERT) {
449             handleModelInsertType(event);
450         } else if (event.getChangeType() == IModelChangedEvent.REMOVE) {
451             handleModelRemoveType(event);
452         } else if (event.getChangeType() == IModelChangedEvent.CHANGE) {
453             handleModelChangeType(event);
454         }
455         
456         // Validate registered groups regardless of change type
457
// Validation is not required for task and composite cheat sheet
458
// change types (performance savings available); but, is required for
459
// everything else
460
updatePreviewButton(fGroupValidator.validate());
461     }
462
463     /**
464      * @param event
465      */

466     private void handleModelEventWorldChanged(IModelChangedEvent event) {
467         
468         Object JavaDoc[] objects = event.getChangedObjects();
469         ICompCSObject object = (ICompCSObject) objects[0];
470         if (object == null) {
471             // Ignore
472
return;
473         } else if (object.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) {
474             // Get the form page
475
CompCSPage page = (CompCSPage)getPage();
476             // Remember the currently selected page
477
IDetailsPage previousDetailsPage =
478                 page.getBlock().getDetailsPart().getCurrentPage();
479             // Replace the current dirty model with the model reloaded from
480
// file
481
fModel = ((ICompCS)object).getModel();
482             // Reset the treeviewer using the new model as input
483
// TODO: MP: CompCS: This is redundant and should be deleted
484
fTreeViewer.setInput(fModel);
485             // Re-initialize the tree viewer. Makes a details page selection
486
initializeTreeViewer();
487             // Get the current details page selection
488
IDetailsPage currentDetailsPage =
489                 page.getBlock().getDetailsPart().getCurrentPage();
490             // If the selected page before the revert is the same as the
491
// selected page after the revert, then its fields will need to
492
// be updated
493
// TODO: MP: REVERT: LOW: Revisit to see if updating details page is necessary - especially after making static
494
if (currentDetailsPage.equals(previousDetailsPage) &&
495                     currentDetailsPage instanceof ICSDetails) {
496                 ((ICSDetails)currentDetailsPage).updateFields();
497             }
498         }
499         
500     }
501     
502     /**
503      * @param event
504      */

505     private void handleModelInsertType(IModelChangedEvent event) {
506         // Insert event
507
Object JavaDoc[] objects = event.getChangedObjects();
508         ICompCSObject object = (ICompCSObject) objects[0];
509         if (object == null) {
510             // Ignore
511
} else if (object.getType() == ICompCSConstants.TYPE_TASK) {
512             handleTaskObjectInsert(object);
513         } else if (object.getType() == ICompCSConstants.TYPE_TASKGROUP) {
514             handleTaskObjectInsert(object);
515             // Register the group for validation
516
fGroupValidator.addGroup((ICompCSTaskGroup) object);
517         }
518     }
519
520     /**
521      * @param object
522      */

523     private void handleTaskObjectInsert(ICompCSObject object) {
524         // Refresh the parent element in the tree viewer
525
// TODO: MP: CompCS: LOW: Can we get away with an update instead of a refresh here?
526
fTreeViewer.refresh(object.getParent());
527         // Select the new task / group in the tree
528
fTreeViewer.setSelection(new StructuredSelection(object), true);
529     }
530     
531     /**
532      * @param event
533      */

534     private void handleModelRemoveType(IModelChangedEvent event) {
535         // Remove event
536
Object JavaDoc[] objects = event.getChangedObjects();
537         ICompCSObject object = (ICompCSObject) objects[0];
538         if (object == null) {
539             // Ignore
540
} else if (object.getType() == ICompCSConstants.TYPE_TASK) {
541             handleTaskObjectRemove(object);
542         } else if (object.getType() == ICompCSConstants.TYPE_TASKGROUP) {
543             handleTaskObjectRemove(object);
544             // Unregister the group from validation
545
fGroupValidator.removeGroup((ICompCSTaskGroup) object);
546         }
547     }
548
549     /**
550      * @param object
551      */

552     private void handleTaskObjectRemove(ICompCSObject object) {
553         // Remove the item
554
fTreeViewer.remove(object);
555         // Select the appropriate object
556
ICompCSObject csObject = fRemoveTaskObjectAction.getObjectToSelect();
557         if (csObject == null) {
558             csObject = object.getParent();
559         }
560         fTreeViewer.setSelection(new StructuredSelection(csObject), true);
561     }
562
563     /**
564      * @param event
565      */

566     private void handleModelChangeType(IModelChangedEvent event) {
567         // Change event
568
Object JavaDoc[] objects = event.getChangedObjects();
569         ICompCSObject object = (ICompCSObject) objects[0];
570         if (object == null) {
571             // Ignore
572
} else if (object.getType() == ICompCSConstants.TYPE_TASK) {
573             // Update the element in the tree viewer
574
fTreeViewer.update(object, null);
575         } else if (object.getType() == ICompCSConstants.TYPE_TASKGROUP) {
576             // Refresh the element in the tree viewer
577
fTreeViewer.update(object, null);
578         } else if (object.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) {
579             // Refresh the element in the tree viewer
580
fTreeViewer.update(object, null);
581         }
582     }
583     
584     /*
585      * (non-Javadoc)
586      *
587      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster#fireSelection()
588      */

589     public void fireSelection() {
590         fTreeViewer.setSelection(fTreeViewer.getSelection());
591     }
592
593     /* (non-Javadoc)
594      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#fillContextMenu(org.eclipse.jface.action.IMenuManager)
595      */

596     protected void fillContextMenu(IMenuManager manager) {
597         // Get the current selection
598
ISelection selection = fTreeViewer.getSelection();
599         Object JavaDoc object = ((IStructuredSelection) selection).getFirstElement();
600         // Do blind cast - has to be a composite CS object
601
// Could be null
602
ICompCSObject csObject = (ICompCSObject)object;
603         // Create the "New" sub-menu
604
MenuManager submenu = new MenuManager(PDEUIMessages.Menus_new_label);
605         // Add the "New" sub-menu to the main context menu
606
manager.add(submenu);
607         if ((csObject == null) ||
608                 (csObject.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET)) {
609             // NO-OP
610
} else if (csObject.getType() == ICompCSConstants.TYPE_TASK) {
611             // Remove task action
612
fillContextMenuRemoveAction(manager, (ICompCSTaskObject)csObject);
613         } else if (csObject.getType() == ICompCSConstants.TYPE_TASKGROUP) {
614             ICompCSTaskGroup group = (ICompCSTaskGroup)csObject;
615             // Add to the "New" sub-menu
616
// Add task action
617
fAddTaskAction.setParentObject(group);
618             fAddTaskAction.setEnabled(fModel.isEditable());
619             submenu.add(fAddTaskAction);
620             // Add to the "New" sub-menu
621
// Add group action
622
fAddGroupAction.setParentObject(group);
623             fAddGroupAction.setEnabled(fModel.isEditable());
624             submenu.add(fAddGroupAction);
625             // Remove task group action
626
fillContextMenuRemoveAction(manager, (ICompCSTaskObject)csObject);
627         }
628         // Add normal edit operations
629
// TODO: MP: LOW: SimpleCS: Enable context menu edit operations
630
//getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
631
//manager.add(new Separator());
632
}
633
634     /**
635      * @param manager
636      * @param csObject
637      */

638     private void fillContextMenuRemoveAction(IMenuManager manager,
639             ICompCSTaskObject taskObject) {
640         // Add to the main context menu
641
// Add a separator to the main context menu
642
manager.add(new Separator());
643         // Delete task object action
644
fRemoveTaskObjectAction.setTaskObject(taskObject);
645         manager.add(fRemoveTaskObjectAction);
646         ICompCSObject parent = taskObject.getParent();
647         if (canRemoveTaskObject(parent) == false) {
648             fRemoveTaskObjectAction.setEnabled(false);
649         } else {
650             fRemoveTaskObjectAction.setEnabled(fModel.isEditable());
651         }
652     }
653
654     /* (non-Javadoc)
655      * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
656      */

657     public boolean doGlobalAction(String JavaDoc actionId) {
658         if (actionId.equals(ActionFactory.DELETE.getId())) {
659             handleDeleteAction();
660             return true;
661         }
662         return false;
663     }
664
665     /**
666      * @param object
667      */

668     private void handleDeleteAction() {
669
670         ISelection sel = fTreeViewer.getSelection();
671         Object JavaDoc object = ((IStructuredSelection) sel).getFirstElement();
672         if (object != null) {
673             if (object instanceof ICompCSTaskObject) {
674                 ICompCSTaskObject taskObject = (ICompCSTaskObject)object;
675                 ICompCSObject parent = taskObject.getParent();
676                 if (canRemoveTaskObject(parent) == false) {
677                     // Preserve cheat sheet validity
678
// Semantic Rule: Cannot have a task group with no tasks
679
Display.getCurrent().beep();
680                 } else {
681                     fRemoveTaskObjectAction.setTaskObject(taskObject);
682                     fRemoveTaskObjectAction.run();
683                 }
684             } else if (object instanceof ICompCS) {
685                 // Preserve cheat sheet validity
686
// Semantic Rule: Cannot have a cheat sheet with no root
687
// cheatsheet node
688
// Produce audible beep
689
Display.getCurrent().beep();
690             }
691         }
692     }
693
694     /**
695      * @param parent
696      * @return
697      */

698     private boolean canRemoveTaskObject(ICompCSObject parent) {
699         if (parent.getType() == ICompCSConstants.TYPE_COMPOSITE_CHEATSHEET) {
700             // Preserve cheat sheet validity
701
// Semantic Rule: Cannot delete the task object directly under
702
// the root cheat sheet node
703
// Wizard by default creates a task group as the only allowed
704
// child of the root cheat sheet node. No good reason to
705
// substitute with a task instead. Specification supports its,
706
// but cheat sheet editor will not support it
707
return false;
708         }
709         return true;
710     }
711     
712     /* (non-Javadoc)
713      * @see org.eclipse.ui.forms.AbstractFormPart#setFormInput(java.lang.Object)
714      */

715     public boolean setFormInput(Object JavaDoc object) {
716         // This method allows the outline view to select items in the tree
717
// Invoked by
718
// org.eclipse.ui.forms.editor.IFormPage.selectReveal(Object object)
719
if (object instanceof ICompCSObject) {
720             // Select the item in the tree
721
fTreeViewer.setSelection(new StructuredSelection(object), true);
722             // Verify that something was actually selected
723
ISelection selection = fTreeViewer.getSelection();
724             if ((selection != null) &&
725                     (selection.isEmpty() == false)) {
726                 return true;
727             }
728         }
729         return false;
730     }
731     
732 }
733
Popular Tags