KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > ModifyWorkingSetDelegate


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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  * Benjamin Muskalla - Bug 169023 [WorkingSets] "Add to working set"
11  * drop down should include a "new working set" option
12  *******************************************************************************/

13 package org.eclipse.ui.internal.actions;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.core.runtime.IConfigurationElement;
24 import org.eclipse.core.runtime.IExecutableExtension;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.ActionContributionItem;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.action.IContributionItem;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.Separator;
31 import org.eclipse.jface.util.IPropertyChangeListener;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.jface.wizard.WizardDialog;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.widgets.Event;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.ui.IActionDelegate2;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.IWorkingSet;
43 import org.eclipse.ui.IWorkingSetManager;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.actions.QuickMenuCreator;
46 import org.eclipse.ui.dialogs.IWorkingSetNewWizard;
47 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
48 import org.eclipse.ui.internal.WorkbenchMessages;
49 import org.eclipse.ui.internal.WorkbenchPlugin;
50
51 /**
52  * @since 3.3
53  *
54  */

55 public class ModifyWorkingSetDelegate extends
56         AbstractWorkingSetPulldownDelegate implements IExecutableExtension, IActionDelegate2 {
57
58     public class NewWorkingSetAction extends Action {
59
60         /**
61          * Create a new instance of this action.
62          */

63         public NewWorkingSetAction() {
64             super(WorkbenchMessages.NewWorkingSet);
65         }
66
67         public void run() {
68             IWorkingSetManager manager = WorkbenchPlugin.getDefault()
69             .getWorkingSetManager();
70             IWorkingSetNewWizard wizard = manager.createWorkingSetNewWizard(null);
71             // the wizard can never be null since we have at least a resource
72
// working set
73
// creation page
74
WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench()
75                                 .getDisplay().getActiveShell(), wizard);
76         
77             dialog.create();
78             PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
79                     IWorkbenchHelpContextIds.WORKING_SET_NEW_WIZARD);
80             if (dialog.open() == Window.OK) {
81                 IWorkingSet workingSet = wizard.getSelection();
82                 if(workingSet != null) {
83                     manager.addWorkingSet(workingSet);
84                 }
85             }
86         }
87
88     }
89
90     private class ModifyAction extends Action {
91
92         private IWorkingSet set;
93
94         private IAdaptable [] selectedElements;
95
96         /**
97          * @param set
98          * @param selectedElements
99          * @param add
100          */

101         private ModifyAction(IWorkingSet set, IAdaptable [] selectedElements) {
102             super(set.getLabel(), IAction.AS_CHECK_BOX);
103             this.set = set;
104             this.selectedElements = selectedElements;
105             setImageDescriptor(set.getImageDescriptor());
106         }
107
108         public void run() {
109
110             Collection JavaDoc oldElements = Arrays.asList(set.getElements());
111             Set JavaDoc newElements = new HashSet JavaDoc(oldElements.size()
112                     + selectedElements.length);
113             newElements.addAll(oldElements);
114             List JavaDoc selectedAsList = Arrays.asList(selectedElements);
115             if (add) {
116                 newElements.addAll(selectedAsList);
117             } else {
118                 newElements.removeAll(selectedAsList);
119             }
120             set.setElements((IAdaptable[]) newElements
121                     .toArray(new IAdaptable[newElements.size()]));
122         }
123     }
124     
125     private QuickMenuCreator contextMenuCreator = new QuickMenuCreator() {
126         protected void fillMenu(IMenuManager menu) {
127             ModifyWorkingSetDelegate.this.fillMenu(menu);
128         }
129     };
130
131     private boolean add = true;
132
133     private IPropertyChangeListener listener = new IPropertyChangeListener() {
134
135         public void propertyChange(PropertyChangeEvent event) {
136             refreshEnablement();
137         }
138
139         /**
140          *
141          */

142         private void refreshEnablement() {
143             selectionChanged(actionProxy, getSelection());
144         }
145     };
146
147     private IAction actionProxy;
148
149     /**
150      *
151      */

152     public ModifyWorkingSetDelegate() {
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
159      */

160     public void run(IAction action) {
161         contextMenuCreator.createMenu();
162     }
163      
164     /* (non-Javadoc)
165      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
166      */

167     public void runWithEvent(IAction action, Event event) {
168         if (event.type == SWT.KeyDown || event.type == SWT.KeyUp)
169             run(action);
170     }
171
172     /*
173      * (non-Javadoc)
174      *
175      * @see org.eclipse.ui.internal.actions.AbstractWorkingSetPulldownDelegate#init(org.eclipse.ui.IWorkbenchWindow)
176      */

177     public void init(IWorkbenchWindow window) {
178         super.init(window);
179         getWindow().getWorkbench().getWorkingSetManager()
180                 .addPropertyChangeListener(listener);
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.eclipse.ui.internal.actions.AbstractWorkingSetPulldownDelegate#dispose()
187      */

188     public void dispose() {
189         getWindow().getWorkbench().getWorkingSetManager()
190                 .removePropertyChangeListener(listener);
191         super.dispose();
192         contextMenuCreator.dispose();
193     }
194     
195     public void fillMenu(Menu menu) {
196         List JavaDoc menuItems = getItems();
197         for (int i = 0; i < menuItems.size(); i++) {
198             Object JavaDoc object = menuItems.get(i);
199             if (object instanceof IAction) {
200                 ActionContributionItem item = new ActionContributionItem((IAction) object);
201                 item.fill(menu, -1);
202             } else {
203                 IContributionItem item = (IContributionItem) object;
204                 item.fill(menu, -1);
205             }
206         }
207         // create working set action only for add menu
208
if(add) {
209             IContributionItem item = null;
210             if (menu.getItemCount() > 0) {
211                 item = new Separator();
212                 item.fill(menu, -1);
213             }
214             
215             item = new ActionContributionItem(new NewWorkingSetAction());
216             item.fill(menu, -1);
217         }
218     }
219     /**
220      * Return the list of items to show in the submenu.
221      *
222      * @return the items to show in the submenu
223      */

224     private List JavaDoc getItems() {
225         List JavaDoc menuItems = new ArrayList JavaDoc();
226         ISelection selection = getSelection();
227         if (!(selection instanceof IStructuredSelection)) {
228             if(!add) {
229                 IAction emptyAction = new Action(WorkbenchMessages.NoApplicableWorkingSets) {};
230                 emptyAction.setEnabled(false);
231                 menuItems.add(emptyAction);
232             }
233             return menuItems;
234         }
235         
236         IWorkingSet[][] typedSets = splitSets();
237         Object JavaDoc [] selectedElements = ((IStructuredSelection)selection).toArray();
238         
239         for (int i = 0; i < typedSets.length; i++) {
240             // add a seperator only if the last item is not a seperator
241
if (menuItems.size() > 0
242                     && !(menuItems.get(menuItems.size() - 1) instanceof Separator)) {
243                 Separator item = new Separator();
244                 menuItems.add(item);
245             }
246             IWorkingSet[] sets = typedSets[i];
247             for (int j = 0; j < sets.length; j++) {
248                 IWorkingSet set = sets[j];
249
250                 Set JavaDoc existingElements = new HashSet JavaDoc();
251                 existingElements.addAll(Arrays
252                         .asList(set.getElements()));
253
254                 boolean visible = false;
255                 IAdaptable [] adaptables = new IAdaptable[selectedElements.length];
256                 System.arraycopy(selectedElements, 0, adaptables, 0, selectedElements.length);
257                 adaptables = set.adaptElements(adaptables);
258                 if (adaptables.length > 0 && add) {
259                     for (int k = 0; k < adaptables.length; k++) {
260                         if (!existingElements.contains(adaptables[k])) {
261                             // show if any element is not present in
262
// addition
263
visible = true;
264                             break;
265                         }
266                     }
267                 }
268                 else if (adaptables.length > 0) {
269                     for (int k = 0; k < adaptables.length; k++) {
270                         if (existingElements.contains(adaptables[k]))
271                             visible = true; // show if any element
272
// ispresent in removal
273
break;
274                     }
275                 }
276                 
277                 if (visible) {
278                     ModifyAction action = new ModifyAction(set,
279                             adaptables);
280                     menuItems.add(action);
281                 }
282             }
283         }
284         if (menuItems.isEmpty() && !add) {
285             IAction emptyAction = new Action(
286                     WorkbenchMessages.NoApplicableWorkingSets) {
287             };
288             emptyAction.setEnabled(false);
289             menuItems.add(emptyAction);
290         }
291         return menuItems;
292     }
293
294     private void fillMenu(IMenuManager menu) {
295         List JavaDoc menuItems = getItems();
296         for (int i = 0; i < menuItems.size(); i++) {
297             Object JavaDoc object = menuItems.get(i);
298             if (object instanceof IAction) {
299                 menu.add((IAction) object);
300             } else {
301                 IContributionItem item = (IContributionItem) object;
302                 menu.add(item);
303             }
304         }
305     }
306
307     /*
308      * (non-Javadoc)
309      *
310      * @see org.eclipse.ui.internal.actions.AbstractWorkingSetPulldownDelegate#selectionChanged(org.eclipse.jface.action.IAction,
311      * org.eclipse.jface.viewers.ISelection)
312      */

313     public void selectionChanged(IAction actionProxy, ISelection selection) {
314         super.selectionChanged(actionProxy, selection);
315         if (selection instanceof IStructuredSelection) {
316             Object JavaDoc[] selectedElements = ((IStructuredSelection) getSelection())
317                     .toArray();
318             // ensure every item is of type IAdaptable and is NOT an IWorkingSet (minimal fix for 157799)
319
boolean minimallyOkay = true;
320             for (int i = 0; i < selectedElements.length; i++) {
321                 Object JavaDoc object = selectedElements[i];
322                 if (!(object instanceof IAdaptable) || object instanceof IWorkingSet) {
323                     minimallyOkay = false;
324                     break;
325                 }
326             }
327             actionProxy.setEnabled(minimallyOkay);
328             
329         }
330         else
331             actionProxy.setEnabled(false);
332     }
333
334     /*
335      * (non-Javadoc)
336      *
337      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
338      * java.lang.String, java.lang.Object)
339      */

340     public void setInitializationData(IConfigurationElement config,
341             String JavaDoc propertyName, Object JavaDoc data) {
342         if (data instanceof String JavaDoc) {
343             add = Boolean.valueOf((String JavaDoc) data).booleanValue();
344         }
345     }
346
347     /* (non-Javadoc)
348      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
349      */

350     public void init(IAction action) {
351         this.actionProxy = action;
352     }
353 }
354
Popular Tags