001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.action.Action;
017: import org.eclipse.jface.action.IAction;
018: import org.eclipse.jface.action.IMenuListener;
019: import org.eclipse.jface.action.IMenuManager;
020: import org.eclipse.jface.action.MenuManager;
021: import org.eclipse.jface.action.Separator;
022: import org.eclipse.jface.viewers.ISelection;
023: import org.eclipse.jface.viewers.ISelectionChangedListener;
024: import org.eclipse.jface.viewers.ISelectionProvider;
025: import org.eclipse.jface.viewers.IStructuredSelection;
026:
027: import org.eclipse.ui.IActionBars;
028: import org.eclipse.ui.IViewPart;
029: import org.eclipse.ui.IWorkbenchPart;
030: import org.eclipse.ui.IWorkbenchSite;
031: import org.eclipse.ui.IWorkingSet;
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.actions.ActionGroup;
034: import org.eclipse.ui.part.Page;
035: import org.eclipse.ui.texteditor.IUpdate;
036:
037: import org.eclipse.jdt.ui.IContextMenuConstants;
038:
039: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
040: import org.eclipse.jdt.internal.ui.JavaPluginImages;
041: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
042: import org.eclipse.jdt.internal.ui.actions.JarImportWizardAction;
043: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
044:
045: /**
046: * Action group that adds the source and generate actions to a part's context
047: * menu and installs handlers for the corresponding global menu actions.
048: *
049: * <p>
050: * This class may be instantiated; it is not intended to be subclassed.
051: * </p>
052: *
053: * @since 3.1
054: */
055: public class GenerateBuildPathActionGroup extends ActionGroup {
056: /**
057: * Pop-up menu: id of the source sub menu (value <code>org.eclipse.jdt.ui.buildpath.menu</code>).
058: *
059: * @since 3.1
060: */
061: public static final String MENU_ID = "org.eclipse.jdt.ui.buildpath.menu"; //$NON-NLS-1$
062:
063: /**
064: * Pop-up menu: id of the build path (add /remove) group of the build path sub menu (value
065: * <code>buildpathGroup</code>).
066: *
067: * @since 3.1
068: */
069: public static final String GROUP_BUILDPATH = "buildpathGroup"; //$NON-NLS-1$
070:
071: /**
072: * Pop-up menu: id of the filter (include / exclude) group of the build path sub menu (value
073: * <code>filterGroup</code>).
074: *
075: * @since 3.1
076: */
077: public static final String GROUP_FILTER = "filterGroup"; //$NON-NLS-1$
078:
079: /**
080: * Pop-up menu: id of the customize (filters / output folder) group of the build path sub menu (value
081: * <code>customizeGroup</code>).
082: *
083: * @since 3.1
084: */
085: public static final String GROUP_CUSTOMIZE = "customizeGroup"; //$NON-NLS-1$
086:
087: private static class NoActionAvailable extends Action {
088: public NoActionAvailable() {
089: setEnabled(false);
090: setText(NewWizardMessages.GenerateBuildPathActionGroup_no_action_available);
091: }
092: }
093:
094: private Action fNoActionAvailable = new NoActionAvailable();
095:
096: private class UpdateJarFileAction extends JarImportWizardAction
097: implements IUpdate {
098:
099: public UpdateJarFileAction() {
100: setText(ActionMessages.GenerateBuildPathActionGroup_update_jar_text);
101: setDescription(ActionMessages.GenerateBuildPathActionGroup_update_jar_description);
102: setToolTipText(ActionMessages.GenerateBuildPathActionGroup_update_jar_tooltip);
103: setImageDescriptor(JavaPluginImages.DESC_OBJS_JAR);
104: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
105: IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
106: }
107:
108: /**
109: * {@inheritDoc}
110: */
111: public void update() {
112: final IWorkbenchPart part = fSite.getPage().getActivePart();
113: if (part != null)
114: setActivePart(this , part);
115: selectionChanged(this , fSite.getSelectionProvider()
116: .getSelection());
117: }
118: }
119:
120: private IWorkbenchSite fSite;
121: private List/*<Action>*/fActions;
122:
123: private String fGroupName = IContextMenuConstants.GROUP_REORGANIZE;
124:
125: /**
126: * Creates a new <code>GenerateActionGroup</code>. The group
127: * requires that the selection provided by the page's selection provider
128: * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
129: *
130: * @param page the page that owns this action group
131: */
132: public GenerateBuildPathActionGroup(Page page) {
133: this (page.getSite());
134: }
135:
136: /**
137: * Creates a new <code>GenerateActionGroup</code>. The group
138: * requires that the selection provided by the part's selection provider
139: * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
140: *
141: * @param part the view part that owns this action group
142: */
143: public GenerateBuildPathActionGroup(IViewPart part) {
144: this (part.getSite());
145: }
146:
147: private GenerateBuildPathActionGroup(IWorkbenchSite site) {
148: fSite = site;
149: fActions = new ArrayList();
150:
151: final CreateLinkedSourceFolderAction addLinkedSourceFolderAction = new CreateLinkedSourceFolderAction(
152: site);
153: fActions.add(addLinkedSourceFolderAction);
154:
155: final CreateSourceFolderAction addSourceFolderAction = new CreateSourceFolderAction(
156: site);
157: fActions.add(addSourceFolderAction);
158:
159: final AddFolderToBuildpathAction addFolder = new AddFolderToBuildpathAction(
160: site);
161: fActions.add(addFolder);
162:
163: final AddSelectedLibraryToBuildpathAction addSelectedLibrary = new AddSelectedLibraryToBuildpathAction(
164: site);
165: fActions.add(addSelectedLibrary);
166:
167: final RemoveFromBuildpathAction remove = new RemoveFromBuildpathAction(
168: site);
169: fActions.add(remove);
170:
171: final AddArchiveToBuildpathAction addArchive = new AddArchiveToBuildpathAction(
172: site);
173: fActions.add(addArchive);
174:
175: final AddLibraryToBuildpathAction addLibrary = new AddLibraryToBuildpathAction(
176: site);
177: fActions.add(addLibrary);
178:
179: final UpdateJarFileAction updateAction = new UpdateJarFileAction();
180: fActions.add(updateAction);
181:
182: final ExcludeFromBuildpathAction exclude = new ExcludeFromBuildpathAction(
183: site);
184: fActions.add(exclude);
185:
186: final IncludeToBuildpathAction include = new IncludeToBuildpathAction(
187: site);
188: fActions.add(include);
189:
190: final EditFilterAction editFilterAction = new EditFilterAction(
191: site);
192: fActions.add(editFilterAction);
193:
194: final EditOutputFolderAction editOutput = new EditOutputFolderAction(
195: site);
196: fActions.add(editOutput);
197:
198: final ConfigureBuildPathAction configure = new ConfigureBuildPathAction(
199: site);
200: fActions.add(configure);
201:
202: final ISelectionProvider provider = fSite
203: .getSelectionProvider();
204: for (Iterator iter = fActions.iterator(); iter.hasNext();) {
205: Action action = (Action) iter.next();
206: if (action instanceof ISelectionChangedListener) {
207: provider
208: .addSelectionChangedListener((ISelectionChangedListener) action);
209: }
210: }
211:
212: }
213:
214: /* (non-Javadoc)
215: * Method declared in ActionGroup
216: */
217: public void fillActionBars(IActionBars actionBar) {
218: super .fillActionBars(actionBar);
219: setGlobalActionHandlers(actionBar);
220: }
221:
222: /* (non-Javadoc)
223: * Method declared in ActionGroup
224: */
225: public void fillContextMenu(IMenuManager menu) {
226: super .fillContextMenu(menu);
227: if (!canOperateOnSelection())
228: return;
229: String menuText = ActionMessages.BuildPath_label;
230: IMenuManager subMenu = new MenuManager(menuText, MENU_ID);
231: subMenu.addMenuListener(new IMenuListener() {
232: public void menuAboutToShow(IMenuManager manager) {
233: fillViewSubMenu(manager);
234: }
235: });
236: subMenu.setRemoveAllWhenShown(true);
237: subMenu.add(new ConfigureBuildPathAction(fSite));
238: menu.appendToGroup(fGroupName, subMenu);
239: }
240:
241: private void fillViewSubMenu(IMenuManager source) {
242: int added = 0;
243: int i = 0;
244: for (Iterator iter = fActions.iterator(); iter.hasNext();) {
245: Action action = (Action) iter.next();
246: if (action instanceof IUpdate)
247: ((IUpdate) action).update();
248:
249: if (i == 2)
250: source.add(new Separator(GROUP_BUILDPATH));
251: else if (i == 8)
252: source.add(new Separator(GROUP_FILTER));
253: else if (i == 10)
254: source.add(new Separator(GROUP_CUSTOMIZE));
255: added += addAction(source, action);
256: i++;
257: }
258:
259: if (added == 0) {
260: source.add(fNoActionAvailable);
261: }
262: }
263:
264: private void setGlobalActionHandlers(IActionBars actionBar) {
265: // TODO implement
266: }
267:
268: private int addAction(IMenuManager menu, IAction action) {
269: if (action != null && action.isEnabled()) {
270: menu.add(action);
271: return 1;
272: }
273: return 0;
274: }
275:
276: private boolean canOperateOnSelection() {
277: ISelection sel = fSite.getSelectionProvider().getSelection();
278: if (!(sel instanceof IStructuredSelection))
279: return false;
280: IStructuredSelection selection = (IStructuredSelection) sel;
281: for (Iterator iter = selection.iterator(); iter.hasNext();) {
282: Object element = iter.next();
283: if (element instanceof IWorkingSet)
284: return false;
285: }
286: return true;
287: }
288:
289: /**
290: * {@inheritDoc}
291: */
292: public void dispose() {
293: if (fActions != null) {
294: final ISelectionProvider provider = fSite
295: .getSelectionProvider();
296: for (Iterator iter = fActions.iterator(); iter.hasNext();) {
297: Action action = (Action) iter.next();
298: if (action instanceof ISelectionChangedListener)
299: provider
300: .removeSelectionChangedListener((ISelectionChangedListener) action);
301: }
302: }
303: fActions = null;
304: super.dispose();
305: }
306: }
|