001: /*******************************************************************************
002: * Copyright (c) 2005, 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.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.IMenuManager;
017: import org.eclipse.jface.action.MenuManager;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.pde.core.plugin.IPluginBase;
021: import org.eclipse.pde.core.plugin.IPluginExtension;
022: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.ui.PDEUIMessages;
025: import org.eclipse.pde.internal.ui.wizards.imports.PluginImportOperation;
026: import org.eclipse.pde.internal.ui.wizards.imports.PluginImportWizard;
027: import org.eclipse.swt.widgets.Display;
028: import org.eclipse.ui.actions.ActionContext;
029: import org.eclipse.ui.actions.ActionGroup;
030:
031: public class ImportActionGroup extends ActionGroup {
032:
033: class ImportAction extends Action {
034: IStructuredSelection fSel;
035: int fImportType;
036:
037: ImportAction(int importType, IStructuredSelection selection) {
038: fSel = selection;
039: fImportType = importType;
040: switch (fImportType) {
041: case PluginImportOperation.IMPORT_BINARY:
042: setText(PDEUIMessages.PluginsView_asBinaryProject);
043: break;
044: case PluginImportOperation.IMPORT_BINARY_WITH_LINKS:
045: setText(PDEUIMessages.ImportActionGroup_binaryWithLinkedContent);
046: break;
047: case PluginImportOperation.IMPORT_WITH_SOURCE:
048: setText(PDEUIMessages.PluginsView_asSourceProject);
049: break;
050: }
051: }
052:
053: public void run() {
054: handleImport(fImportType, fSel);
055: }
056: }
057:
058: public void fillContextMenu(IMenuManager menu) {
059: ActionContext context = getContext();
060: ISelection selection = context.getSelection();
061: if (!selection.isEmpty()
062: && selection instanceof IStructuredSelection) {
063: IStructuredSelection sSelection = (IStructuredSelection) selection;
064: String menuName = null;
065: if (sSelection.getFirstElement() instanceof IPluginExtension
066: || sSelection.getFirstElement() instanceof IPluginExtensionPoint)
067: menuName = PDEUIMessages.ImportActionGroup_importContributingPlugin;
068: else
069: menuName = PDEUIMessages.PluginsView_import;
070: MenuManager importMenu = new MenuManager(menuName);
071: importMenu.add(new ImportAction(
072: PluginImportOperation.IMPORT_BINARY, sSelection));
073: importMenu.add(new ImportAction(
074: PluginImportOperation.IMPORT_BINARY_WITH_LINKS,
075: sSelection));
076: importMenu.add(new ImportAction(
077: PluginImportOperation.IMPORT_WITH_SOURCE,
078: sSelection));
079: menu.add(importMenu);
080: }
081: }
082:
083: private void handleImport(int importType,
084: IStructuredSelection selection) {
085: ArrayList externalModels = new ArrayList();
086: for (Iterator iter = selection.iterator(); iter.hasNext();) {
087: IPluginModelBase model = getModel(iter.next());
088: if (model != null && model.getUnderlyingResource() == null)
089: externalModels.add(model);
090: }
091: Display display = Display.getCurrent();
092: if (display == null)
093: display = Display.getDefault();
094: IPluginModelBase[] models = (IPluginModelBase[]) externalModels
095: .toArray(new IPluginModelBase[externalModels.size()]);
096:
097: PluginImportWizard.doImportOperation(display.getActiveShell(),
098: importType, models, false);
099: }
100:
101: public static boolean canImport(IStructuredSelection selection) {
102: for (Iterator iter = selection.iterator(); iter.hasNext();) {
103: IPluginModelBase model = getModel(iter.next());
104: if (model != null && model.getUnderlyingResource() == null)
105: return true;
106: }
107: return false;
108: }
109:
110: private static IPluginModelBase getModel(Object next) {
111: IPluginModelBase model = null;
112: if (next instanceof IPluginModelBase)
113: model = (IPluginModelBase) next;
114: else if (next instanceof IPluginBase)
115: model = ((IPluginBase) next).getPluginModel();
116: else if (next instanceof IPluginExtension)
117: model = ((IPluginExtension) next).getPluginModel();
118: else if (next instanceof IPluginExtensionPoint)
119: model = ((IPluginExtensionPoint) next).getPluginModel();
120: return model;
121: }
122: }
|