KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > actions > BaseMenuCreator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.actions;
5
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.jdt.core.IJavaElement;
8 import org.eclipse.jface.action.ActionContributionItem;
9 import org.eclipse.jface.action.IAction;
10 import org.eclipse.jface.action.IMenuCreator;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.swt.events.MenuAdapter;
13 import org.eclipse.swt.events.MenuEvent;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Menu;
16 import org.eclipse.swt.widgets.MenuItem;
17 import org.eclipse.ui.IEditorActionDelegate;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IObjectActionDelegate;
20 import org.eclipse.ui.IViewActionDelegate;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
25 import org.eclipse.ui.texteditor.ITextEditor;
26 import org.terracotta.dso.ConfigurationHelper;
27 import org.terracotta.dso.TcPlugin;
28
29 /**
30  * Hackey way to dynamically create popup action submenus.
31  */

32
33 public abstract class BaseMenuCreator
34   implements IObjectActionDelegate,
35              IEditorActionDelegate,
36              IViewActionDelegate,
37              IMenuCreator,
38              IWorkbenchWindowPulldownDelegate2
39 {
40   protected IJavaElement m_element;
41   protected IAction m_delegateAction;
42   protected ISelection m_selection;
43   protected IEditorPart m_editorPart;
44
45   public BaseMenuCreator() {/**/}
46
47   public void init(IWorkbenchWindow window) {/**/}
48   public void run(IAction action) {/**/}
49   public void dispose() {/**/}
50   public void init(IViewPart view) {/**/}
51   public void setActivePart(IAction action, IWorkbenchPart targetPart) {/**/}
52
53   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
54     m_editorPart = targetEditor;
55   }
56   
57   protected IJavaElement getElement(ISelection selection) {
58     IJavaElement element;
59     
60     if((element = getJavaElement(selection)) != null &&
61        hasTerracottaNature(element))
62     {
63       return element;
64     }
65     
66     return null;
67   }
68   
69   public void selectionChanged(IAction action, ISelection selection) {
70     if(m_delegateAction == null) {
71       m_delegateAction = action;
72       m_delegateAction.setMenuCreator(this);
73     }
74     m_selection = selection;
75   }
76
77   protected abstract IJavaElement getJavaElement(ISelection selection);
78   
79   protected void buildMenu(Menu menu) {
80     menu.addMenuListener(new MenuAdapter() {
81       public void menuShown(MenuEvent e) {
82         Menu m = (Menu)e.widget;
83         MenuItem[] items = m.getItems();
84         
85         for(int i = 0; i < items.length; i++) {
86           items[i].dispose();
87         }
88         
89         fillMenu(m);
90       }
91     });
92   }
93
94   protected ISelection getSelection() {
95     if(m_editorPart != null) {
96       if(m_editorPart instanceof ITextEditor) {
97         return ((ITextEditor)m_editorPart).getSelectionProvider().getSelection();
98       }
99     }
100     
101     return m_selection;
102   }
103   
104   public Menu getMenu(Control parent) {
105     Menu menu = null;
106     
107     m_selection = getSelection();
108     if((m_element = getElement(m_selection)) != null) {
109       buildMenu(menu = new Menu(parent));
110     }
111
112     return menu;
113   }
114
115   public Menu getMenu(Menu parent) {
116     Menu menu = null;
117     
118     m_selection = getSelection();
119     if((m_element = getElement(m_selection)) != null) {
120       buildMenu(menu = new Menu(parent));
121     }
122
123     return menu;
124   }
125
126   protected abstract void fillMenu(Menu menu);
127   
128   protected ConfigurationHelper getConfigHelper() {
129     ConfigurationHelper helper = null;
130     IProject project = getProject();
131     
132     if(project != null) {
133       helper = TcPlugin.getDefault().getConfigurationHelper(project);
134     }
135     
136     return helper;
137   }
138   
139   protected IProject getProject() {
140     return (m_element != null) ? m_element.getJavaProject().getProject() : null;
141   }
142   
143   protected void addMenuAction(Menu menu, IAction action) {
144     ActionContributionItem item = new ActionContributionItem(action);
145     item.fill(menu, -1);
146   }
147   
148   protected boolean hasTerracottaNature(IJavaElement element) {
149     return TcPlugin.getDefault().hasTerracottaNature(element);
150   }
151 }
152
Popular Tags