001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.pde.internal.ui.views.plugins;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.jface.action.Action;
018: import org.eclipse.jface.action.IMenuManager;
019: import org.eclipse.jface.action.Separator;
020: import org.eclipse.jface.operation.IRunnableWithProgress;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.core.PDECore;
025: import org.eclipse.pde.internal.core.SearchablePluginsManager;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.actions.ActionContext;
030: import org.eclipse.ui.actions.ActionGroup;
031:
032: public class JavaSearchActionGroup extends ActionGroup {
033:
034: class JavaSearchAction extends Action {
035:
036: private boolean add;
037:
038: public JavaSearchAction(boolean add) {
039: this .add = add;
040: if (add)
041: setText(PDEUIMessages.PluginsView_addToJavaSearch);
042: else
043: setText(PDEUIMessages.PluginsView_removeFromJavaSearch);
044: }
045:
046: public void run() {
047: handleJavaSearch(add);
048: }
049: }
050:
051: public void fillContextMenu(IMenuManager menu) {
052: ActionContext context = getContext();
053: ISelection selection = context.getSelection();
054: if (!selection.isEmpty()
055: && selection instanceof IStructuredSelection) {
056: IStructuredSelection sSelection = (IStructuredSelection) selection;
057:
058: boolean addSeparator = false;
059:
060: if (canDoJavaSearchOperation(sSelection, true)) {
061: menu.add(new JavaSearchAction(true));
062: addSeparator = true;
063: }
064: if (canDoJavaSearchOperation(sSelection, false)) {
065: menu.add(new JavaSearchAction(false));
066: addSeparator = true;
067: }
068: if (addSeparator) {
069: menu.add(new Separator());
070: }
071: }
072: }
073:
074: private boolean canDoJavaSearchOperation(
075: IStructuredSelection selection, boolean add) {
076: int nhits = 0;
077: IPluginModelBase model = null;
078: SearchablePluginsManager manager = PDECore.getDefault()
079: .getSearchablePluginsManager();
080: for (Iterator iter = selection.iterator(); iter.hasNext();) {
081: model = getModel(iter.next());
082: if (model == null)
083: return false;
084:
085: if (model.getUnderlyingResource() == null) {
086: if (add == !manager.isInJavaSearch(model
087: .getPluginBase().getId()))
088: nhits++;
089: }
090: }
091: return nhits > 0;
092: }
093:
094: private IPluginModelBase getModel(Object object) {
095: IPluginModelBase model = null;
096: if (object instanceof IAdaptable) {
097: model = (IPluginModelBase) ((IAdaptable) object)
098: .getAdapter(IPluginModelBase.class);
099: } else if (object instanceof IPluginModelBase) {
100: model = (IPluginModelBase) object;
101: }
102: return model;
103: }
104:
105: private void handleJavaSearch(final boolean add) {
106: IStructuredSelection selection = (IStructuredSelection) getContext()
107: .getSelection();
108: if (selection.size() == 0)
109: return;
110:
111: ArrayList result = new ArrayList();
112: SearchablePluginsManager manager = PDECore.getDefault()
113: .getSearchablePluginsManager();
114: for (Iterator iter = selection.iterator(); iter.hasNext();) {
115: IPluginModelBase model = getModel(iter.next());
116: if (model != null
117: && model.getUnderlyingResource() == null
118: && manager.isInJavaSearch(model.getPluginBase()
119: .getId()) != add) {
120: result.add(model);
121: }
122: }
123: if (result.size() == 0)
124: return;
125: final IPluginModelBase[] array = (IPluginModelBase[]) result
126: .toArray(new IPluginModelBase[result.size()]);
127:
128: IRunnableWithProgress op = new JavaSearchOperation(array, add);
129: try {
130: PlatformUI.getWorkbench().getProgressService()
131: .busyCursorWhile(op);
132: } catch (InterruptedException e) {
133: } catch (InvocationTargetException e) {
134: PDEPlugin.logException(e);
135: }
136: }
137:
138: }
|