]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/newsourcepage/GenerateBuildPathActionGroup.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / newsourcepage / GenerateBuildPathActionGroup.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13import java.util.ArrayList;
14import java.util.Iterator;
15import java.util.List;
16
17import org.eclipse.jface.action.Action;
18import org.eclipse.jface.action.IAction;
19import org.eclipse.jface.action.IMenuListener;
20import org.eclipse.jface.action.IMenuManager;
21import org.eclipse.jface.action.MenuManager;
22import org.eclipse.jface.action.Separator;
23import org.eclipse.jface.viewers.ISelection;
24import org.eclipse.jface.viewers.ISelectionChangedListener;
25import org.eclipse.jface.viewers.ISelectionProvider;
26import org.eclipse.jface.viewers.IStructuredSelection;
27import org.eclipse.jface.viewers.SelectionChangedEvent;
28
29import org.eclipse.ui.IActionBars;
30import org.eclipse.ui.IViewPart;
31import org.eclipse.ui.IWorkbenchPart;
32import org.eclipse.ui.IWorkbenchSite;
33import org.eclipse.ui.IWorkingSet;
34import org.eclipse.ui.PlatformUI;
35import org.eclipse.ui.actions.ActionGroup;
36import org.eclipse.ui.part.Page;
37
38import org.eclipse.ui.texteditor.IUpdate;
39
40import org.eclipse.jdt.ui.IContextMenuConstants;
41
42import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
43import org.eclipse.jdt.internal.ui.JavaPluginImages;
44import org.eclipse.jdt.internal.ui.actions.ActionMessages;
45import org.eclipse.jdt.internal.ui.actions.JarImportWizardAction;
46import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
47
48/**
49 * Action group that adds the source and generate actions to a part's context
50 * menu and installs handlers for the corresponding global menu actions.
51 *
52 * <p>
53 * This class may be instantiated; it is not intended to be subclassed.
54 * </p>
55 *
56 * @since 3.1
57 */
58public class GenerateBuildPathActionGroup extends ActionGroup {
59 /**
60 * Pop-up menu: id of the source sub menu (value <code>org.eclipse.jdt.ui.buildpath.menu</code>).
61 *
62 * @since 3.1
63 */
64 public static final String MENU_ID= "org.eclipse.jdt.ui.buildpath.menu"; //$NON-NLS-1$
65
66 /**
67 * Pop-up menu: id of the build path (add /remove) group of the build path sub menu (value
68 * <code>buildpathGroup</code>).
69 *
70 * @since 3.1
71 */
72 public static final String GROUP_BUILDPATH= "buildpathGroup"; //$NON-NLS-1$
73
74 /**
75 * Pop-up menu: id of the filter (include / exclude) group of the build path sub menu (value
76 * <code>filterGroup</code>).
77 *
78 * @since 3.1
79 */
80 public static final String GROUP_FILTER= "filterGroup"; //$NON-NLS-1$
81
82 /**
83 * Pop-up menu: id of the customize (filters / output folder) group of the build path sub menu (value
84 * <code>customizeGroup</code>).
85 *
86 * @since 3.1
87 */
88 public static final String GROUP_CUSTOMIZE= "customizeGroup"; //$NON-NLS-1$
89
90 private static class NoActionAvailable extends Action {
91 public NoActionAvailable() {
92 setEnabled(false);
93 setText(NewWizardMessages.GenerateBuildPathActionGroup_no_action_available);
94 }
95 }
96 private Action fNoActionAvailable= new NoActionAvailable();
97
98 private class UpdateJarFileAction extends JarImportWizardAction implements IUpdate {
99
100 public UpdateJarFileAction() {
101 setText(ActionMessages.GenerateBuildPathActionGroup_update_jar_text);
102 setDescription(ActionMessages.GenerateBuildPathActionGroup_update_jar_description);
103 setToolTipText(ActionMessages.GenerateBuildPathActionGroup_update_jar_tooltip);
104 setImageDescriptor(JavaPluginImages.DESC_OBJS_JAR);
105 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 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, fSelectionProvider.getSelection());
116 }
117 }
118
119 private final IWorkbenchSite fSite;
120 private final ISelectionProvider fSelectionProvider;
121 private final List<Action> fActions;
122
123 private String fGroupName= IContextMenuConstants.GROUP_REORGANIZE;
124
125
126 /**
127 * Creates a new <code>GenerateActionGroup</code>. The group
128 * requires that the selection provided by the page's selection provider
129 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
130 *
131 * @param page the page that owns this action group
132 */
133 public GenerateBuildPathActionGroup(Page page) {
134 this(page.getSite(), page.getSite().getSelectionProvider());
135 }
136
137 /**
138 * Creates a new <code>GenerateActionGroup</code>. The group
139 * requires that the selection provided by the part's selection provider
140 * is of type <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
141 *
142 * @param part the view part that owns this action group
143 */
144 public GenerateBuildPathActionGroup(IViewPart part) {
145 this(part.getSite(), part.getSite().getSelectionProvider());
146 }
147 /**
148 * Creates a new <code>GenerateActionGroup</code>. The group requires
149 * that the selection provided by the given selection provider is of type
150 * {@link IStructuredSelection}.
151 *
152 * @param site the site that will own the action group.
153 * @param selectionProvider the selection provider used instead of the
154 * page selection provider.
155 *
156 * @since 3.4
157 */
158 public GenerateBuildPathActionGroup(IWorkbenchSite site, ISelectionProvider selectionProvider) {
159 fSite= site;
160 fSelectionProvider= selectionProvider;
161 fActions= new ArrayList<Action>();
162
163 final CreateLinkedSourceFolderAction addLinkedSourceFolderAction= new CreateLinkedSourceFolderAction(site);
164 fActions.add(addLinkedSourceFolderAction);
165
166 final CreateSourceFolderAction addSourceFolderAction= new CreateSourceFolderAction(site);
167 fActions.add(addSourceFolderAction);
168
169 final AddFolderToBuildpathAction addFolder= new AddFolderToBuildpathAction(site);
170 fActions.add(addFolder);
171
172 final AddSelectedLibraryToBuildpathAction addSelectedLibrary= new AddSelectedLibraryToBuildpathAction(site);
173 fActions.add(addSelectedLibrary);
174
175 final RemoveFromBuildpathAction remove= new RemoveFromBuildpathAction(site);
176 fActions.add(remove);
177
178 final AddArchiveToBuildpathAction addArchive= new AddArchiveToBuildpathAction(site);
179 fActions.add(addArchive);
180
181 final AddLibraryToBuildpathAction addLibrary= new AddLibraryToBuildpathAction(site);
182 fActions.add(addLibrary);
183
184 final UpdateJarFileAction updateAction= new UpdateJarFileAction();
185 fActions.add(updateAction);
186
187 final ExcludeFromBuildpathAction exclude= new ExcludeFromBuildpathAction(site);
188 fActions.add(exclude);
189
190 final IncludeToBuildpathAction include= new IncludeToBuildpathAction(site);
191 fActions.add(include);
192
193 final EditFilterAction editFilterAction= new EditFilterAction(site);
194 fActions.add(editFilterAction);
195
196 final EditOutputFolderAction editOutput= new EditOutputFolderAction(site);
197 fActions.add(editOutput);
198
199 final ConfigureBuildPathAction configure= new ConfigureBuildPathAction(site);
200 fActions.add(configure);
201
202 for (Iterator<Action> iter= fActions.iterator(); iter.hasNext();) {
203 Action action= iter.next();
204 if (action instanceof ISelectionChangedListener) {
205 ISelectionChangedListener listener= (ISelectionChangedListener)action;
206 selectionProvider.addSelectionChangedListener(listener);
207 listener.selectionChanged(new SelectionChangedEvent(selectionProvider, selectionProvider.getSelection()));
208 }
209 }
210
211 }
212
213 /* (non-Javadoc)
214 * Method declared in ActionGroup
215 */
216 @Override
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 @Override
226 public void fillContextMenu(IMenuManager menu) {
227 super.fillContextMenu(menu);
228 if (!canOperateOnSelection())
229 return;
230 String menuText= ActionMessages.BuildPath_label;
231 IMenuManager subMenu= new MenuManager(menuText, MENU_ID);
232 subMenu.addMenuListener(new IMenuListener() {
233 public void menuAboutToShow(IMenuManager manager) {
234 fillViewSubMenu(manager);
235 }
236 });
237 subMenu.setRemoveAllWhenShown(true);
238 subMenu.add(new ConfigureBuildPathAction(fSite));
239 menu.appendToGroup(fGroupName, subMenu);
240 }
241
242 private void fillViewSubMenu(IMenuManager source) {
243 int added= 0;
244 int i=0;
245 for (Iterator<Action> iter= fActions.iterator(); iter.hasNext();) {
246 Action action= iter.next();
247 if (action instanceof IUpdate)
248 ((IUpdate) action).update();
249
250 if (i == 2)
251 source.add(new Separator(GROUP_BUILDPATH));
252 else if (i == 8)
253 source.add(new Separator(GROUP_FILTER));
254 else if (i == 10)
255 source.add(new Separator(GROUP_CUSTOMIZE));
256 added+= addAction(source, action);
257 i++;
258 }
259
260 if (added == 0) {
261 source.add(fNoActionAvailable);
262 }
263 }
264
265 /**
266 * @param actionBar the action bars to set the handler for
267 */
268 private void setGlobalActionHandlers(IActionBars actionBar) {
269 // TODO implement
270 }
271
272 private int addAction(IMenuManager menu, IAction action) {
273 if (action != null && action.isEnabled()) {
274 menu.add(action);
275 return 1;
276 }
277 return 0;
278 }
279
280 private boolean canOperateOnSelection() {
281 ISelection sel= fSelectionProvider.getSelection();
282 if (!(sel instanceof IStructuredSelection))
283 return false;
284 IStructuredSelection selection= (IStructuredSelection)sel;
285 if (selection.isEmpty())
286 return false;
287 for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
288 Object element= iter.next();
289 if (element instanceof IWorkingSet)
290 return false;
291 }
292 return true;
293 }
294
295 /**
296 * {@inheritDoc}
297 */
298 @Override
299 public void dispose() {
300 if (fActions != null) {
301 for (Iterator<Action> iter= fActions.iterator(); iter.hasNext();) {
302 Action action= iter.next();
303 if (action instanceof ISelectionChangedListener)
304 fSelectionProvider.removeSelectionChangedListener((ISelectionChangedListener) action);
305 }
306 fActions.clear();
307 }
308 super.dispose();
309 }
310}