001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.actions;
005:
006: import org.eclipse.core.resources.IProject;
007: import org.eclipse.jdt.core.IJavaElement;
008: import org.eclipse.jface.action.ActionContributionItem;
009: import org.eclipse.jface.action.IAction;
010: import org.eclipse.jface.action.IMenuCreator;
011: import org.eclipse.jface.viewers.ISelection;
012: import org.eclipse.swt.events.MenuAdapter;
013: import org.eclipse.swt.events.MenuEvent;
014: import org.eclipse.swt.widgets.Control;
015: import org.eclipse.swt.widgets.Menu;
016: import org.eclipse.swt.widgets.MenuItem;
017: import org.eclipse.ui.IEditorActionDelegate;
018: import org.eclipse.ui.IEditorPart;
019: import org.eclipse.ui.IObjectActionDelegate;
020: import org.eclipse.ui.IViewActionDelegate;
021: import org.eclipse.ui.IViewPart;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
025: import org.eclipse.ui.texteditor.ITextEditor;
026: import org.terracotta.dso.ConfigurationHelper;
027: import org.terracotta.dso.TcPlugin;
028:
029: /**
030: * Hackey way to dynamically create popup action submenus.
031: */
032:
033: public abstract class BaseMenuCreator implements IObjectActionDelegate,
034: IEditorActionDelegate, IViewActionDelegate, IMenuCreator,
035: IWorkbenchWindowPulldownDelegate2 {
036: protected IJavaElement m_element;
037: protected IAction m_delegateAction;
038: protected ISelection m_selection;
039: protected IEditorPart m_editorPart;
040:
041: public BaseMenuCreator() {/**/
042: }
043:
044: public void init(IWorkbenchWindow window) {/**/
045: }
046:
047: public void run(IAction action) {/**/
048: }
049:
050: public void dispose() {/**/
051: }
052:
053: public void init(IViewPart view) {/**/
054: }
055:
056: public void setActivePart(IAction action, IWorkbenchPart targetPart) {/**/
057: }
058:
059: public void setActiveEditor(IAction action, IEditorPart targetEditor) {
060: m_editorPart = targetEditor;
061: }
062:
063: protected IJavaElement getElement(ISelection selection) {
064: IJavaElement element;
065:
066: if ((element = getJavaElement(selection)) != null
067: && hasTerracottaNature(element)) {
068: return element;
069: }
070:
071: return null;
072: }
073:
074: public void selectionChanged(IAction action, ISelection selection) {
075: if (m_delegateAction == null) {
076: m_delegateAction = action;
077: m_delegateAction.setMenuCreator(this );
078: }
079: m_selection = selection;
080: }
081:
082: public void setJavaElement(IJavaElement javaElement) {
083: m_element = javaElement;
084: }
085:
086: protected abstract IJavaElement getJavaElement(ISelection selection);
087:
088: protected void buildMenu(Menu menu) {
089: menu.addMenuListener(new MenuAdapter() {
090: public void menuShown(MenuEvent e) {
091: Menu m = (Menu) e.widget;
092: MenuItem[] items = m.getItems();
093:
094: for (int i = 0; i < items.length; i++) {
095: items[i].dispose();
096: }
097:
098: fillMenu(m);
099: }
100: });
101: }
102:
103: protected ISelection getSelection() {
104: if (m_editorPart != null) {
105: if (m_editorPart instanceof ITextEditor) {
106: return ((ITextEditor) m_editorPart)
107: .getSelectionProvider().getSelection();
108: }
109: }
110:
111: return m_selection;
112: }
113:
114: public Menu getMenu(Control parent) {
115: Menu menu = null;
116:
117: m_selection = getSelection();
118: if ((m_element = getElement(m_selection)) != null) {
119: buildMenu(menu = new Menu(parent));
120: }
121:
122: return menu;
123: }
124:
125: public Menu getMenu(Menu parent) {
126: Menu menu = null;
127:
128: m_selection = getSelection();
129: if ((m_element = getElement(m_selection)) != null) {
130: buildMenu(menu = new Menu(parent));
131: }
132:
133: return menu;
134: }
135:
136: protected abstract void fillMenu(Menu menu);
137:
138: protected ConfigurationHelper getConfigHelper() {
139: ConfigurationHelper helper = null;
140: IProject project = getProject();
141:
142: if (project != null) {
143: helper = TcPlugin.getDefault().getConfigurationHelper(
144: project);
145: }
146:
147: return helper;
148: }
149:
150: protected IProject getProject() {
151: return (m_element != null) ? m_element.getJavaProject()
152: .getProject() : null;
153: }
154:
155: protected void addMenuAction(Menu menu, IAction action) {
156: ActionContributionItem item = new ActionContributionItem(action);
157: item.fill(menu, -1);
158: }
159:
160: protected boolean hasTerracottaNature(IJavaElement element) {
161: return TcPlugin.getDefault().hasTerracottaNature(element);
162: }
163: }
|