KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > GenerateBuildPathActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.IMenuListener;
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.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.ISelectionProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27
28 import org.eclipse.ui.IActionBars;
29 import org.eclipse.ui.IViewPart;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchSite;
32 import org.eclipse.ui.IWorkingSet;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.actions.ActionGroup;
35 import org.eclipse.ui.part.Page;
36 import org.eclipse.ui.texteditor.IUpdate;
37
38 import org.eclipse.jdt.ui.IContextMenuConstants;
39
40 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
41 import org.eclipse.jdt.internal.ui.JavaPluginImages;
42 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
43 import org.eclipse.jdt.internal.ui.actions.JarImportWizardAction;
44 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
45
46 /**
47  * Action group that adds the source and generate actions to a part's context
48  * menu and installs handlers for the corresponding global menu actions.
49  *
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @since 3.1
55  */

56 public class GenerateBuildPathActionGroup extends ActionGroup {
57     /**
58      * Pop-up menu: id of the source sub menu (value <code>org.eclipse.jdt.ui.buildpath.menu</code>).
59      *
60      * @since 3.1
61      */

62     public static final String JavaDoc MENU_ID= "org.eclipse.jdt.ui.buildpath.menu"; //$NON-NLS-1$
63

64     /**
65      * Pop-up menu: id of the build path (add /remove) group of the build path sub menu (value
66      * <code>buildpathGroup</code>).
67      *
68      * @since 3.1
69      */

70     public static final String JavaDoc GROUP_BUILDPATH= "buildpathGroup"; //$NON-NLS-1$
71

72     /**
73      * Pop-up menu: id of the filter (include / exclude) group of the build path sub menu (value
74      * <code>filterGroup</code>).
75      *
76      * @since 3.1
77      */

78     public static final String JavaDoc GROUP_FILTER= "filterGroup"; //$NON-NLS-1$
79

80     /**
81      * Pop-up menu: id of the customize (filters / output folder) group of the build path sub menu (value
82      * <code>customizeGroup</code>).
83      *
84      * @since 3.1
85      */

86     public static final String JavaDoc GROUP_CUSTOMIZE= "customizeGroup"; //$NON-NLS-1$
87

88     private static class NoActionAvailable extends Action {
89         public NoActionAvailable() {
90             setEnabled(false);
91             setText(NewWizardMessages.GenerateBuildPathActionGroup_no_action_available);
92         }
93     }
94     private Action fNoActionAvailable= new NoActionAvailable();
95             
96     private class UpdateJarFileAction extends JarImportWizardAction implements IUpdate {
97
98         public UpdateJarFileAction() {
99             setText(ActionMessages.GenerateBuildPathActionGroup_update_jar_text);
100             setDescription(ActionMessages.GenerateBuildPathActionGroup_update_jar_description);
101             setToolTipText(ActionMessages.GenerateBuildPathActionGroup_update_jar_tooltip);
102             setImageDescriptor(JavaPluginImages.DESC_OBJS_JAR);
103             PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);
104         }
105
106         /**
107          * {@inheritDoc}
108          */

109         public void update() {
110             final IWorkbenchPart part= fSite.getPage().getActivePart();
111             if (part != null)
112                 setActivePart(this, part);
113             selectionChanged(this, fSite.getSelectionProvider().getSelection());
114         }
115     }
116
117     private IWorkbenchSite fSite;
118     private List JavaDoc/*<Action>*/ fActions;
119
120     private String JavaDoc fGroupName= IContextMenuConstants.GROUP_REORGANIZE;
121         
122     /**
123      * Creates a new <code>GenerateActionGroup</code>. The group
124      * requires that the selection provided by the page's selection provider
125      * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
126      *
127      * @param page the page that owns this action group
128      */

129     public GenerateBuildPathActionGroup(Page page) {
130         this(page.getSite());
131     }
132     
133     /**
134      * Creates a new <code>GenerateActionGroup</code>. The group
135      * requires that the selection provided by the part's selection provider
136      * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
137      *
138      * @param part the view part that owns this action group
139      */

140     public GenerateBuildPathActionGroup(IViewPart part) {
141         this(part.getSite());
142     }
143     
144     private GenerateBuildPathActionGroup(IWorkbenchSite site) {
145         fSite= site;
146         fActions= new ArrayList JavaDoc();
147         
148         final CreateLinkedSourceFolderAction addLinkedSourceFolderAction= new CreateLinkedSourceFolderAction(site);
149         fActions.add(addLinkedSourceFolderAction);
150         
151         final CreateSourceFolderAction addSourceFolderAction= new CreateSourceFolderAction(site);
152         fActions.add(addSourceFolderAction);
153         
154         final AddFolderToBuildpathAction addFolder= new AddFolderToBuildpathAction(site);
155         fActions.add(addFolder);
156         
157         final AddSelectedLibraryToBuildpathAction addSelectedLibrary= new AddSelectedLibraryToBuildpathAction(site);
158         fActions.add(addSelectedLibrary);
159     
160         final RemoveFromBuildpathAction remove= new RemoveFromBuildpathAction(site);
161         fActions.add(remove);
162         
163         final AddArchiveToBuildpathAction addArchive= new AddArchiveToBuildpathAction(site);
164         fActions.add(addArchive);
165         
166         final AddLibraryToBuildpathAction addLibrary= new AddLibraryToBuildpathAction(site);
167         fActions.add(addLibrary);
168         
169         final UpdateJarFileAction updateAction= new UpdateJarFileAction();
170         fActions.add(updateAction);
171         
172         final ExcludeFromBuildpathAction exclude= new ExcludeFromBuildpathAction(site);
173         fActions.add(exclude);
174         
175         final IncludeToBuildpathAction include= new IncludeToBuildpathAction(site);
176         fActions.add(include);
177
178         final EditFilterAction editFilterAction= new EditFilterAction(site);
179         fActions.add(editFilterAction);
180             
181         final EditOutputFolderAction editOutput= new EditOutputFolderAction(site);
182         fActions.add(editOutput);
183         
184         final ConfigureBuildPathAction configure= new ConfigureBuildPathAction(site);
185         fActions.add(configure);
186         
187         final ISelectionProvider provider= fSite.getSelectionProvider();
188         for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext();) {
189             Action action= (Action)iter.next();
190             if (action instanceof ISelectionChangedListener) {
191                 provider.addSelectionChangedListener((ISelectionChangedListener)action);
192             }
193         }
194         
195     }
196             
197     /* (non-Javadoc)
198      * Method declared in ActionGroup
199      */

200     public void fillActionBars(IActionBars actionBar) {
201         super.fillActionBars(actionBar);
202         setGlobalActionHandlers(actionBar);
203     }
204     
205     /* (non-Javadoc)
206      * Method declared in ActionGroup
207      */

208     public void fillContextMenu(IMenuManager menu) {
209         super.fillContextMenu(menu);
210         if (!canOperateOnSelection())
211             return;
212         String JavaDoc menuText= ActionMessages.BuildPath_label;
213         IMenuManager subMenu= new MenuManager(menuText, MENU_ID);
214         subMenu.addMenuListener(new IMenuListener() {
215             public void menuAboutToShow(IMenuManager manager) {
216                 fillViewSubMenu(manager);
217             }
218         });
219         subMenu.setRemoveAllWhenShown(true);
220         subMenu.add(new ConfigureBuildPathAction(fSite));
221         menu.appendToGroup(fGroupName, subMenu);
222     }
223         
224     private void fillViewSubMenu(IMenuManager source) {
225         int added= 0;
226         int i=0;
227         for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext();) {
228             Action action= (Action)iter.next();
229             if (action instanceof IUpdate)
230                 ((IUpdate) action).update();
231             
232             if (i == 2)
233                 source.add(new Separator(GROUP_BUILDPATH));
234             else if (i == 8)
235                 source.add(new Separator(GROUP_FILTER));
236             else if (i == 10)
237                 source.add(new Separator(GROUP_CUSTOMIZE));
238             added+= addAction(source, action);
239             i++;
240         }
241
242         if (added == 0) {
243             source.add(fNoActionAvailable);
244         }
245     }
246         
247     private void setGlobalActionHandlers(IActionBars actionBar) {
248         // TODO implement
249
}
250     
251     private int addAction(IMenuManager menu, IAction action) {
252         if (action != null && action.isEnabled()) {
253             menu.add(action);
254             return 1;
255         }
256         return 0;
257     }
258     
259     private boolean canOperateOnSelection() {
260         ISelection sel= fSite.getSelectionProvider().getSelection();
261         if (!(sel instanceof IStructuredSelection))
262             return false;
263         IStructuredSelection selection= (IStructuredSelection)sel;
264         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
265             Object JavaDoc element= iter.next();
266             if (element instanceof IWorkingSet)
267                 return false;
268         }
269         return true;
270     }
271
272     /**
273      * {@inheritDoc}
274      */

275     public void dispose() {
276         if (fActions != null) {
277             final ISelectionProvider provider= fSite.getSelectionProvider();
278             for (Iterator JavaDoc iter= fActions.iterator(); iter.hasNext();) {
279                 Action action= (Action)iter.next();
280                 if (action instanceof ISelectionChangedListener)
281                     provider.removeSelectionChangedListener((ISelectionChangedListener) action);
282             }
283         }
284         fActions= null;
285         super.dispose();
286     }
287 }
288
Popular Tags