KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > SimpleCSMasterTreeSection


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.simple;
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringWriter JavaDoc;
17
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.action.Separator;
21 import org.eclipse.jface.action.ToolBarManager;
22 import org.eclipse.jface.fieldassist.ControlDecoration;
23 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
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.simple.ISimpleCS;
30 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
31 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
32 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
33 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
34 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
35 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject;
36 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
37 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject;
38 import org.eclipse.pde.internal.ui.PDEPlugin;
39 import org.eclipse.pde.internal.ui.PDEUIMessages;
40 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
41 import org.eclipse.pde.internal.ui.editor.TreeSection;
42 import org.eclipse.pde.internal.ui.editor.actions.CollapseAction;
43 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSDetails;
44 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster;
45 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions.SimpleCSAddStepAction;
46 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions.SimpleCSAddSubStepAction;
47 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions.SimpleCSRemoveRunObjectAction;
48 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions.SimpleCSRemoveStepAction;
49 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions.SimpleCSRemoveSubStepAction;
50 import org.eclipse.pde.internal.ui.parts.TreePart;
51 import org.eclipse.swt.SWT;
52 import org.eclipse.swt.events.DisposeEvent;
53 import org.eclipse.swt.events.DisposeListener;
54 import org.eclipse.swt.graphics.Cursor;
55 import org.eclipse.swt.widgets.Button;
56 import org.eclipse.swt.widgets.Composite;
57 import org.eclipse.swt.widgets.Display;
58 import org.eclipse.swt.widgets.ToolBar;
59 import org.eclipse.ui.IEditorInput;
60 import org.eclipse.ui.actions.ActionFactory;
61 import org.eclipse.ui.cheatsheets.OpenCheatSheetAction;
62 import org.eclipse.ui.forms.IDetailsPage;
63 import org.eclipse.ui.forms.widgets.FormToolkit;
64 import org.eclipse.ui.forms.widgets.Section;
65
66 /**
67  * SimpleCSElementSection
68  *
69  */

70 public class SimpleCSMasterTreeSection extends TreeSection implements
71         ICSMaster {
72
73     private static final int F_BUTTON_ADD_STEP = 0;
74
75     private static final int F_BUTTON_ADD_SUBSTEP = 1;
76     
77     private static final int F_BUTTON_REMOVE = 2;
78     
79     private static final int F_BUTTON_UP = 3;
80     
81     private static final int F_BUTTON_DOWN = 4;
82
83     private static final int F_BUTTON_PREVIEW = 5;
84     
85     private static final int F_UP_FLAG = -1;
86
87     private static final int F_DOWN_FLAG = 1;
88     
89     private TreeViewer fTreeViewer;
90     
91     private ISimpleCSModel fModel;
92     
93     private SimpleCSAddStepAction fAddStepAction;
94     
95     private SimpleCSRemoveStepAction fRemoveStepAction;
96
97     private SimpleCSRemoveSubStepAction fRemoveSubStepAction;
98     
99     private SimpleCSAddSubStepAction fAddSubStepAction;
100     
101     private SimpleCSRemoveRunObjectAction fRemoveRunObjectAction;
102     
103     private CollapseAction fCollapseAction;
104     
105     private ControlDecoration fSubStepInfoDecoration;
106     
107     /**
108      * @param formPage
109      * @param parent
110      * @param style
111      * @param buttonLabels
112      */

113     public SimpleCSMasterTreeSection(PDEFormPage formPage, Composite parent) {
114         super(formPage, parent, Section.DESCRIPTION, new String JavaDoc[] {
115                 PDEUIMessages.SimpleCSElementSection_0,
116                 PDEUIMessages.SimpleCSElementSection_6,
117                 PDEUIMessages.SimpleCSElementSection_7,
118                 PDEUIMessages.SimpleCSElementSection_1,
119                 PDEUIMessages.SimpleCSElementSection_2,
120                 PDEUIMessages.SimpleCSElementSection_3});
121         getSection().setText(PDEUIMessages.SimpleCSElementSection_4);
122         getSection().setDescription(PDEUIMessages.SimpleCSElementSection_5);
123
124         // Create actions
125
fAddStepAction = new SimpleCSAddStepAction();
126         fRemoveStepAction = new SimpleCSRemoveStepAction();
127         fRemoveSubStepAction = new SimpleCSRemoveSubStepAction();
128         fAddSubStepAction = new SimpleCSAddSubStepAction();
129         fRemoveRunObjectAction = new SimpleCSRemoveRunObjectAction();
130         fCollapseAction = null;
131     }
132
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
136      */

137     protected void createClient(Section section, FormToolkit toolkit) {
138         // Get the model
139
fModel = (ISimpleCSModel)getPage().getModel();
140
141         Composite container = createClientContainer(section, 2, toolkit);
142         createTree(container, toolkit);
143         toolkit.paintBordersFor(container);
144         section.setClient(container);
145         initializeTreeViewer();
146         createSectionToolbar(section, toolkit);
147     }
148
149     /**
150      * @param section
151      * @param toolkit
152      */

153     private void createSectionToolbar(Section section, FormToolkit toolkit) {
154         
155         ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
156         ToolBar toolbar = toolBarManager.createControl(section);
157         final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
158         toolbar.setCursor(handCursor);
159         // Cursor needs to be explicitly disposed
160
toolbar.addDisposeListener(new DisposeListener() {
161             public void widgetDisposed(DisposeEvent e) {
162                 if ((handCursor != null) &&
163                         (handCursor.isDisposed() == false)) {
164                     handCursor.dispose();
165                 }
166             }
167         });
168         // Add collapse action to the tool bar
169
fCollapseAction = new CollapseAction(fTreeViewer,
170                 PDEUIMessages.ExtensionsPage_collapseAll,
171                 1,
172                 fModel.getSimpleCS());
173         toolBarManager.add(fCollapseAction);
174
175         toolBarManager.update(true);
176
177         section.setTextClient(toolbar);
178     }
179     
180     /**
181      *
182      */

183     private void initializeTreeViewer() {
184         
185         if (fModel == null) {
186             return;
187         }
188         fTreeViewer.setInput(fModel);
189
190         getTreePart().setButtonEnabled(F_BUTTON_ADD_STEP, fModel.isEditable());
191         // Set to false because initial node selected is the root cheatsheet node
192
getTreePart().setButtonEnabled(F_BUTTON_ADD_SUBSTEP, false);
193         // Set to false because initial node selected is the root cheatsheet node
194
getTreePart().setButtonEnabled(F_BUTTON_REMOVE, false);
195         // Set to false because initial node selected is the root cheatsheet node
196
getTreePart().setButtonEnabled(F_BUTTON_UP, false);
197         // Set to false because initial node selected is the root cheatsheet node
198
getTreePart().setButtonEnabled(F_BUTTON_DOWN, false);
199         getTreePart().setButtonEnabled(F_BUTTON_PREVIEW, true);
200         
201         ISimpleCS cheatsheet = fModel.getSimpleCS();
202         // Select the cheatsheet node in the tree
203
fTreeViewer.setSelection(new StructuredSelection(cheatsheet), true);
204         fTreeViewer.expandToLevel(2);
205     }
206
207     /**
208      * @param container
209      * @param toolkit
210      */

211     private void createTree(Composite container, FormToolkit toolkit) {
212         TreePart treePart = getTreePart();
213         createViewerPartControl(container, SWT.SINGLE, 2, toolkit);
214         fTreeViewer = treePart.getTreeViewer();
215         fTreeViewer.setContentProvider(new SimpleCSContentProvider());
216         fTreeViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
217         PDEPlugin.getDefault().getLabelProvider().connect(this);
218         createTreeListeners();
219         createSubStepInfoDecoration();
220     }
221
222     /**
223      *
224      */

225     private void createSubStepInfoDecoration() {
226         //
227
Button button = getStructuredViewerPart().getButton(F_BUTTON_ADD_SUBSTEP);
228         int bits = SWT.TOP | SWT.RIGHT;
229         fSubStepInfoDecoration = new ControlDecoration(button, bits);
230         fSubStepInfoDecoration.setMarginWidth(0);
231         updateSubStepInfoDecoration(false, false, false);
232         fSubStepInfoDecoration.setImage(
233             FieldDecorationRegistry.getDefault().getFieldDecoration(
234                 FieldDecorationRegistry.DEC_INFORMATION).getImage());
235     }
236
237     /**
238      * @param show
239      * @param itemHasNoExecutable
240      * @param itemIsNotOptional
241      */

242     private void updateSubStepInfoDecoration(boolean show,
243             boolean itemHasNoExecutable, boolean itemIsNotOptional) {
244         //
245
if (show) {
246             fSubStepInfoDecoration.show();
247             if (itemHasNoExecutable == false) {
248                 fSubStepInfoDecoration.setDescriptionText(PDEUIMessages.SimpleCSMasterTreeSection_msgButtonDisabledCommand);
249             } else if (itemIsNotOptional == false){
250                 fSubStepInfoDecoration.setDescriptionText(PDEUIMessages.SimpleCSMasterTreeSection_msgButtonDisabledOptional);
251             }
252         } else {
253             fSubStepInfoDecoration.hide();
254         }
255         fSubStepInfoDecoration.setShowHover(show);
256     }
257         
258     
259     /**
260      *
261      */

262     private void createTreeListeners() {
263         // Create listener for the outline view 'link with editor' toggle
264
// button
265
fTreeViewer.addPostSelectionChangedListener(
266                 getPage().getPDEEditor().new PDEFormEditorChangeListener());
267     }
268
269     /**
270      * @return
271      */

272     public ISelection getSelection() {
273         return fTreeViewer.getSelection();
274     }
275     
276     /* (non-Javadoc)
277      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#buttonSelected(int)
278      */

279     protected void buttonSelected(int index) {
280         switch (index) {
281         case F_BUTTON_ADD_STEP:
282             handleAddStepAction();
283             break;
284         case F_BUTTON_ADD_SUBSTEP:
285             handleAddSubStepAction();
286             break;
287         case F_BUTTON_REMOVE:
288             handleDeleteAction();
289             break;
290         case F_BUTTON_UP:
291             handleMoveStepAction(F_UP_FLAG);
292             break;
293         case F_BUTTON_DOWN:
294             handleMoveStepAction(F_DOWN_FLAG);
295             break;
296         case F_BUTTON_PREVIEW:
297             handlePreviewAction();
298             break;
299         }
300     }
301
302     /* (non-Javadoc)
303      * @see org.eclipse.pde.internal.ui.editor.TreeSection#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
304      */

305     protected void selectionChanged(IStructuredSelection selection) {
306         updateButtons();
307     }
308
309     /**
310      *
311      */

312     public void updateButtons() {
313         if (!fModel.isEditable()) {
314             return;
315         }
316         ISimpleCSObject csObject = getCurrentSelection();
317         
318         boolean canAddItem = false;
319         boolean canAddSubItem = false;
320         boolean canRemove = false;
321         boolean canMoveUp = false;
322         boolean canMoveDown = false;
323
324         boolean itemHasNoExecutable = false;
325         boolean itemIsNotOptional = false;
326         boolean showDecoration = false;
327         
328         if (csObject != null) {
329             
330             if (csObject.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET) {
331                 // Add item to end of cheat sheet child items
332
canAddItem = true;
333             } else if (csObject.getType() == ISimpleCSConstants.TYPE_INTRO) {
334                 // Add item as the first cheat sheet child item
335
// which is right after the introduction node
336
canAddItem = true;
337             } else if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
338                 ISimpleCSItem item = (ISimpleCSItem)csObject;
339                 if (item.getSimpleCS().isFirstItem(item) == false) {
340                     canMoveUp = true;
341                 }
342                 if (item.getSimpleCS().isLastItem(item) == false) {
343                     canMoveDown = true;
344                 }
345                 
346                 // Preserve cheat sheet validity
347
// Semantic Rule: Cannot have a cheat sheet with no items
348
if (item.getSimpleCS().getItemCount() > 1) {
349                     canRemove = true;
350                 }
351                 
352                 // Preserve cheat sheet validity
353
// Semantic Rule: Cannot have a subitem and any of the following
354
// together: perform-when, command, action
355
// Preserve cheat sheet validity
356
// Semantic Rule: Cannot add subitems to an item that is
357
// optional
358
itemHasNoExecutable = (item.getExecutable() == null);
359                 itemIsNotOptional = (item.getSkip() == false);
360                 if (itemHasNoExecutable && itemIsNotOptional) {
361                     canAddSubItem = true;
362                 }
363                 showDecoration = (canAddSubItem == false);
364                 // Add item right after this item
365
canAddItem = true;
366                 
367             } else if (csObject.getType() == ISimpleCSConstants.TYPE_SUBITEM) {
368                 ISimpleCSSubItem subitem = (ISimpleCSSubItem)csObject;
369                 ISimpleCSObject parent = subitem.getParent();
370                 if (parent.getType() == ISimpleCSConstants.TYPE_ITEM) {
371                     ISimpleCSItem item = (ISimpleCSItem)parent;
372                     if (item.isFirstSubItem(subitem) == false) {
373                         canMoveUp = true;
374                     }
375                     if (item.isLastSubItem(subitem) == false) {
376                         canMoveDown = true;
377                     }
378                     // Preserve cheat sheet validity
379
// Semantic Rule: Cannot have a subitem and any of the following
380
// together: perform-when, command, action
381
// Preserve cheat sheet validity
382
// Semantic Rule: Cannot add subitems to an item that is
383
// optional
384
itemHasNoExecutable = (item.getExecutable() == null);
385                     itemIsNotOptional = (item.getSkip() == false);
386                     if (itemHasNoExecutable && itemIsNotOptional) {
387                         canAddSubItem = true;
388                     }
389                     showDecoration = (canAddSubItem == false);
390                 }
391                 canRemove = true;
392                 
393             } else if ((csObject.getType() == ISimpleCSConstants.TYPE_REPEATED_SUBITEM) ||
394                         (csObject.getType() == ISimpleCSConstants.TYPE_CONDITIONAL_SUBITEM) ||
395                         (csObject.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN) ||
396                         (csObject.getType() == ISimpleCSConstants.TYPE_ACTION) ||
397                         (csObject.getType() == ISimpleCSConstants.TYPE_COMMAND)) {
398                 // Specifically for perform-when, repeated-subitem,
399
// conditional-subitem edge cases
400
// Action and command supported; but, will never be applicable
401
canRemove = true;
402             }
403
404             updateSubStepInfoDecoration(showDecoration, itemHasNoExecutable,
405                     itemIsNotOptional);
406         }
407
408         getTreePart().setButtonEnabled(F_BUTTON_ADD_STEP, canAddItem);
409         getTreePart().setButtonEnabled(F_BUTTON_ADD_SUBSTEP, canAddSubItem);
410         getTreePart().setButtonEnabled(F_BUTTON_REMOVE, canRemove);
411         getTreePart().setButtonEnabled(F_BUTTON_UP, canMoveUp);
412         getTreePart().setButtonEnabled(F_BUTTON_DOWN, canMoveDown);
413     }
414     
415     /**
416      *
417      */

418     private void handleAddStepAction() {
419         // Get the current selection
420
ISimpleCSObject csObject = getCurrentSelection();
421         // If nothing is selected add to the root cheat sheet node
422
if (csObject == null) {
423             fAddStepAction.setDataObject(fModel.getSimpleCS());
424         } else {
425             fAddStepAction.setDataObject(csObject);
426         }
427         // Execute the action
428
fAddStepAction.run();
429     }
430     
431     /**
432      * @return
433      */

434     private ISimpleCSObject getCurrentSelection() {
435         ISelection selection = fTreeViewer.getSelection();
436         Object JavaDoc object = ((IStructuredSelection) selection).getFirstElement();
437         return (ISimpleCSObject)object;
438     }
439
440     /**
441      *
442      */

443     private void handleAddSubStepAction() {
444         // Get the current selection
445
ISimpleCSObject csObject = getCurrentSelection();
446         // Ensure the selection is defined
447
if (csObject == null) {
448             return;
449         }
450         // Set the selection object to operate on
451
fAddSubStepAction.setDataObject(csObject);
452         // Execute the action
453
fAddSubStepAction.run();
454     }
455     
456     /**
457      *
458      */

459     private void handleMoveStepAction(int positionFlag) {
460         ISimpleCSObject object = getCurrentSelection();
461         if (object != null) {
462             if (object instanceof ISimpleCSItem) {
463                 ISimpleCSItem item = (ISimpleCSItem)object;
464                 item.getSimpleCS().moveItem(item, positionFlag);
465             } else if (object instanceof ISimpleCSSubItem) {
466                 ISimpleCSSubItem subitem = (ISimpleCSSubItem)object;
467                 // Get the current index of the subitem
468
ISimpleCSObject parent = subitem.getParent();
469                 if (parent.getType() == ISimpleCSConstants.TYPE_ITEM) {
470                     ISimpleCSItem item = (ISimpleCSItem)parent;
471                     item.moveSubItem(subitem, positionFlag);
472                 }
473             }
474         }
475     }
476     
477     /**
478      *
479      */

480     private void handlePreviewAction() {
481         // Get the editor input
482
// Could be IFileEditorInput (File in workpspace - e.g. Package Explorer View)
483
// Could be IStorageEditorInput (File not in workpsace - e.g. CVS Repositories View)
484
IEditorInput input = getPage().getEditorInput();
485         try {
486             // Write the current model into a String as raw XML
487
StringWriter JavaDoc swriter = new StringWriter JavaDoc();
488             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
489             fModel.getSimpleCS().write("", writer); //$NON-NLS-1$
490
writer.flush();
491             swriter.close();
492             // Launch in the cheat sheet view
493
// Note: Having a null URL is valid for simple cheat sheets
494
OpenCheatSheetAction openAction = new OpenCheatSheetAction(
495                     input.getName(),
496                     input.getName(),
497                     swriter.toString(),
498                     null);
499             openAction.run();
500         } catch (IOException JavaDoc e) {
501             PDEPlugin.logException(e);
502         }
503     }
504     
505     /* (non-Javadoc)
506      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
507      */

508     public void modelChanged(IModelChangedEvent event) {
509         // No need to call super, world changed event handled here
510

511         if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
512             handleModelEventWorldChanged(event);
513         } else if (event.getChangeType() == IModelChangedEvent.INSERT) {
514             handleModelInsertType(event);
515         } else if (event.getChangeType() == IModelChangedEvent.REMOVE) {
516             handleModelRemoveType(event);
517         } else if (event.getChangeType() == IModelChangedEvent.CHANGE) {
518             handleModelChangeType(event);
519         }
520     }
521
522     /**
523      * @param event
524      */

525     private void handleModelEventWorldChanged(IModelChangedEvent event) {
526         
527         Object JavaDoc[] objects = event.getChangedObjects();
528         ISimpleCSObject object = (ISimpleCSObject) objects[0];
529         if (object == null) {
530             // Ignore
531
return;
532         } else if (object.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET) {
533             // Get the form page
534
SimpleCSPage page = (SimpleCSPage)getPage();
535             // Remember the currently selected page
536
IDetailsPage previousDetailsPage =
537                 page.getBlock().getDetailsPart().getCurrentPage();
538             // Replace the current dirty model with the model reloaded from
539
// file
540
fModel = ((ISimpleCS)object).getModel();
541             // Reset the treeviewer using the new model as input
542
fTreeViewer.setInput(fModel);
543             // Re-initialize the tree viewer. Makes a details page selection
544
initializeTreeViewer();
545             // Get the current details page selection
546
IDetailsPage currentDetailsPage =
547                 page.getBlock().getDetailsPart().getCurrentPage();
548             // If the selected page before the revert is the same as the
549
// selected page after the revert, then its fields will need to
550
// be updated
551
// TODO: MP: REVERT: LOW: Revisit to see if updating details page is necessary - especially after making static
552
if (currentDetailsPage.equals(previousDetailsPage) &&
553                     currentDetailsPage instanceof ICSDetails) {
554                 ((ICSDetails)currentDetailsPage).updateFields();
555             }
556         }
557         
558     }
559     
560     /**
561      * @param event
562      */

563     private void handleModelInsertType(IModelChangedEvent event) {
564         // Insert event
565
Object JavaDoc[] objects = event.getChangedObjects();
566         ISimpleCSObject object = (ISimpleCSObject) objects[0];
567         if (object == null) {
568             // Ignore
569
} else if ((object.getType() == ISimpleCSConstants.TYPE_ITEM)
570                 || (object.getType() == ISimpleCSConstants.TYPE_SUBITEM)) {
571             // Refresh the parent element in the tree viewer
572
fTreeViewer.refresh(object.getParent());
573             // Select the new item in the tree
574
fTreeViewer.setSelection(new StructuredSelection(object), true);
575         }
576     }
577
578     /**
579      * @param event
580      */

581     private void handleModelRemoveType(IModelChangedEvent event) {
582         // Remove event
583
Object JavaDoc[] objects = event.getChangedObjects();
584         ISimpleCSObject object = (ISimpleCSObject) objects[0];
585         if (object == null) {
586             // Ignore
587
} else if (object.getType() == ISimpleCSConstants.TYPE_ITEM) {
588             // Remove the item
589
fTreeViewer.remove(object);
590             // Select the appropriate object
591
ISimpleCSObject csObject = fRemoveStepAction.getObjectToSelect();
592             if (csObject == null) {
593                 csObject = object.getParent();
594             }
595             fTreeViewer.setSelection(new StructuredSelection(csObject), true);
596         } else if (object.getType() == ISimpleCSConstants.TYPE_SUBITEM) {
597             // Remove the subitem
598
fTreeViewer.remove(object);
599             // Select the appropriate object
600
ISimpleCSObject csObject = fRemoveSubStepAction.getObjectToSelect();
601             if (csObject == null) {
602                 csObject = object.getParent();
603             }
604             fTreeViewer.setSelection(new StructuredSelection(csObject), true);
605         } else if ((object.getType() == ISimpleCSConstants.TYPE_CONDITIONAL_SUBITEM)
606                 || (object.getType() == ISimpleCSConstants.TYPE_REPEATED_SUBITEM)
607                 || (object.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN)) {
608             // Remove the object
609
fTreeViewer.remove(object);
610             // Select the parent in the tree
611
fTreeViewer.setSelection(
612                     new StructuredSelection(object.getParent()), true);
613         }
614     }
615     
616     /**
617      * @param event
618      */

619     private void handleModelChangeType(IModelChangedEvent event) {
620         // Change event
621
Object JavaDoc[] objects = event.getChangedObjects();
622         ISimpleCSObject object = (ISimpleCSObject)objects[0];
623         if (object == null) {
624             // Ignore
625
} else if ((object.getType() == ISimpleCSConstants.TYPE_ITEM)
626                 || (object.getType() == ISimpleCSConstants.TYPE_SUBITEM)
627                 || (object.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET)) {
628             // Refresh the element in the tree viewer
629
fTreeViewer.update(object, null);
630         }
631     }
632     
633     /* (non-Javadoc)
634      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster#fireSelection()
635      */

636     public void fireSelection() {
637         fTreeViewer.setSelection(fTreeViewer.getSelection());
638     }
639     
640     /* (non-Javadoc)
641      * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#fillContextMenu(org.eclipse.jface.action.IMenuManager)
642      */

643     protected void fillContextMenu(IMenuManager manager) {
644         // Get the current selection
645
ISimpleCSObject csObject = getCurrentSelection();
646         // Create the "New" submenu
647
MenuManager submenu = new MenuManager(PDEUIMessages.Menus_new_label);
648         // Add the "New" submenu to the main context menu
649
manager.add(submenu);
650         if ((csObject == null) ||
651                 (csObject.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET)) {
652             // Add to the "New" submenu
653
// Add step action
654
fAddStepAction.setDataObject(fModel.getSimpleCS());
655             fAddStepAction.setEnabled(fModel.isEditable());
656             submenu.add(fAddStepAction);
657         } else if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
658             ISimpleCSItem item = (ISimpleCSItem)csObject;
659             // Add to the "New" submenu
660
// Add sub-step action
661
fAddSubStepAction.setDataObject(csObject);
662             // Preserve cheat sheet validity
663
// Semantic Rule: Cannot have a subitem and any of the following
664
// together: perform-when, command, action
665
// Preserve cheat sheet validity
666
// Semantic Rule: Cannot add subitems to an item that is
667
// optional
668
if ((item.getExecutable() == null) &&
669                     (item.getSkip() == false)) {
670                 fAddSubStepAction.setEnabled(fModel.isEditable());
671             } else {
672                 fAddSubStepAction.setEnabled(false);
673             }
674             submenu.add(fAddSubStepAction);
675             // Add to the main context menu
676
// Add a separator to the main context menu
677
manager.add(new Separator());
678             // Delete step action
679
fRemoveStepAction.setItem((ISimpleCSItem)csObject);
680             // Preserve cheat sheet validity
681
// Semantic Rule: Cannot have a cheat sheet with no items
682
if (item.getSimpleCS().getItemCount() > 1) {
683                 fRemoveStepAction.setEnabled(fModel.isEditable());
684             } else {
685                 fRemoveStepAction.setEnabled(false);
686             }
687             manager.add(fRemoveStepAction);
688         } else if ((csObject.getType() == ISimpleCSConstants.TYPE_SUBITEM) ||
689                     (csObject.getType() == ISimpleCSConstants.TYPE_REPEATED_SUBITEM) ||
690                     (csObject.getType() == ISimpleCSConstants.TYPE_CONDITIONAL_SUBITEM)) {
691             // Add to the main context menu
692
// Add a separator to the main context menu
693
manager.add(new Separator());
694             // Delete sub-step action
695
fRemoveSubStepAction.setSubItem((ISimpleCSSubItemObject)csObject);
696             fRemoveSubStepAction.setEnabled(fModel.isEditable());
697             manager.add(fRemoveSubStepAction);
698         } else if ((csObject.getType() == ISimpleCSConstants.TYPE_PERFORM_WHEN) ||
699                     (csObject.getType() == ISimpleCSConstants.TYPE_ACTION) ||
700                     (csObject.getType() == ISimpleCSConstants.TYPE_COMMAND)) {
701             // Specifically for perform-when edge case
702
// Action and command supported; but, will never be applicable
703
// Add to the main context menu
704
// Add a separator to the main context menu
705
manager.add(new Separator());
706             // Delete run object action
707
fRemoveRunObjectAction.setRunObject((ISimpleCSRunContainerObject)csObject);
708             fRemoveRunObjectAction.setEnabled(fModel.isEditable());
709             manager.add(fRemoveRunObjectAction);
710         }
711         // Add normal edit operations
712
// TODO: MP: LOW: SimpleCS: Enable context menu edit operations
713
//getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager);
714
//manager.add(new Separator());
715
}
716     
717     /* (non-Javadoc)
718      * @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String)
719      */

720     public boolean doGlobalAction(String JavaDoc actionId) {
721         if (actionId.equals(ActionFactory.DELETE.getId())) {
722             handleDeleteAction();
723             return true;
724         }
725         return false;
726     }
727     
728     /**
729      * @param object
730      */

731     private void handleDeleteAction() {
732         ISimpleCSObject object = getCurrentSelection();
733         if (object != null) {
734             if (object instanceof ISimpleCSItem) {
735                 ISimpleCSItem item = (ISimpleCSItem)object;
736                 // Preserve cheat sheet validity
737
// Semantic Rule: Cannot have a cheat sheet with no items
738
if (item.getSimpleCS().getItemCount() > 1) {
739                     fRemoveStepAction.setItem(item);
740                     fRemoveStepAction.run();
741                 } else {
742                     // Produce audible beep
743
Display.getCurrent().beep();
744                 }
745             } else if (object instanceof ISimpleCSSubItemObject) {
746                 fRemoveSubStepAction.setSubItem((ISimpleCSSubItemObject)object);
747                 fRemoveSubStepAction.run();
748             } else if (object instanceof ISimpleCSRunContainerObject) {
749                 // Specifically for perform-when edge case
750
// Action and command supported; but, will never be applicable
751
fRemoveRunObjectAction.setRunObject((ISimpleCSRunContainerObject)object);
752                 fRemoveRunObjectAction.run();
753             } else if (object instanceof ISimpleCS) {
754                 // Preserve cheat sheet validity
755
// Semantic Rule: Cannot have a cheat sheet with no root
756
// cheatsheet node
757
// Produce audible beep
758
Display.getCurrent().beep();
759             } else if (object instanceof ISimpleCSIntro) {
760                 // Preserve cheat sheet validity
761
// Semantic Rule: Cannot have a cheat sheet with no
762
// introduction
763
// Produce audible beep
764
Display.getCurrent().beep();
765             }
766         }
767     }
768     
769     /* (non-Javadoc)
770      * @see org.eclipse.ui.forms.AbstractFormPart#setFormInput(java.lang.Object)
771      */

772     public boolean setFormInput(Object JavaDoc object) {
773         // This method allows the outline view to select items in the tree
774
// Invoked by
775
// org.eclipse.ui.forms.editor.IFormPage.selectReveal(Object object)
776
if (object instanceof ISimpleCSObject) {
777             // Select the item in the tree
778
fTreeViewer.setSelection(new StructuredSelection(object), true);
779             // Verify that something was actually selected
780
ISelection selection = fTreeViewer.getSelection();
781             if ((selection != null) &&
782                     (selection.isEmpty() == false)) {
783                 return true;
784             }
785         }
786         return false;
787     }
788
789 }
790
Popular Tags