KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > views > plugins > JavaSearchActionGroup


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  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.views.plugins;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.Separator;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.core.SearchablePluginsManager;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.ActionContext;
31 import org.eclipse.ui.actions.ActionGroup;
32
33 public class JavaSearchActionGroup extends ActionGroup {
34
35     class JavaSearchAction extends Action {
36
37         private boolean add;
38
39         public JavaSearchAction(boolean add) {
40             this.add = add;
41             if(add)
42                 setText(PDEUIMessages.PluginsView_addToJavaSearch);
43             else
44                 setText(PDEUIMessages.PluginsView_removeFromJavaSearch);
45         }
46
47         public void run() {
48             handleJavaSearch(add);
49         }
50     }
51
52     public void fillContextMenu(IMenuManager menu) {
53         ActionContext context = getContext();
54         ISelection selection = context.getSelection();
55         if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
56             IStructuredSelection sSelection = (IStructuredSelection) selection;
57
58             boolean addSeparator = false;
59
60             if (canDoJavaSearchOperation(sSelection, true)) {
61                 menu.add(new JavaSearchAction(true));
62                 addSeparator = true;
63             }
64             if (canDoJavaSearchOperation(sSelection, false)) {
65                 menu.add(new JavaSearchAction(false));
66                 addSeparator = true;
67             }
68             if (addSeparator) {
69                 menu.add(new Separator());
70             }
71         }
72     }
73
74     private boolean canDoJavaSearchOperation(IStructuredSelection selection, boolean add) {
75         int nhits = 0;
76         IPluginModelBase model = null;
77         SearchablePluginsManager manager = PDECore.getDefault().getSearchablePluginsManager();
78         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
79             model = getModel(iter.next());
80             if (model == null)
81                 return false;
82             
83             if (model.getUnderlyingResource() == null) {
84                 if (add == !manager.isInJavaSearch(model.getPluginBase().getId()))
85                     nhits++;
86             }
87         }
88         return nhits > 0;
89     }
90     
91     private IPluginModelBase getModel(Object JavaDoc object) {
92         IPluginModelBase model = null;
93         if (object instanceof IAdaptable) {
94             model = (IPluginModelBase) ((IAdaptable) object).getAdapter(IPluginModelBase.class);
95         } else if (object instanceof IPluginModelBase) {
96             model = (IPluginModelBase) object;
97         }
98         return model;
99     }
100
101     private void handleJavaSearch(final boolean add) {
102         IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
103         if (selection.size() == 0)
104             return;
105
106         ArrayList JavaDoc result = new ArrayList JavaDoc();
107         SearchablePluginsManager manager = PDECore.getDefault().getSearchablePluginsManager();
108         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
109             IPluginModelBase model = getModel(iter.next());
110                 if (model != null
111                       && model.getUnderlyingResource() == null
112                       && manager.isInJavaSearch(model.getPluginBase().getId()) != add) {
113                     result.add(model);
114                 }
115             }
116         if (result.size() == 0)
117             return;
118         final IPluginModelBase[] array =
119             (IPluginModelBase[]) result.toArray(new IPluginModelBase[result.size()]);
120
121         IRunnableWithProgress op = new JavaSearchOperation(array, add);
122         try {
123             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
124         } catch (InterruptedException JavaDoc e) {
125         } catch (InvocationTargetException JavaDoc e) {
126             PDEPlugin.logException(e);
127         }
128     }
129
130 }
131
Popular Tags