KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.pde.core.plugin.IPluginBase;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.internal.ui.wizards.imports.PluginImportOperation;
27 import org.eclipse.pde.internal.ui.wizards.imports.PluginImportWizard;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.ui.actions.ActionContext;
30 import org.eclipse.ui.actions.ActionGroup;
31
32 public class ImportActionGroup extends ActionGroup {
33
34     class ImportAction extends Action {
35         IStructuredSelection fSel;
36         int fImportType;
37         ImportAction(int importType, IStructuredSelection selection) {
38             fSel = selection;
39             fImportType = importType;
40             switch (fImportType) {
41             case PluginImportOperation.IMPORT_BINARY:
42                 setText(PDEUIMessages.PluginsView_asBinaryProject);
43                 break;
44             case PluginImportOperation.IMPORT_BINARY_WITH_LINKS:
45                 setText(PDEUIMessages.ImportActionGroup_binaryWithLinkedContent);
46                 break;
47             case PluginImportOperation.IMPORT_WITH_SOURCE:
48                 setText(PDEUIMessages.PluginsView_asSourceProject);
49                 break;
50             }
51         }
52         public void run() {
53             handleImport(fImportType, fSel);
54         }
55     }
56
57     public void fillContextMenu(IMenuManager menu) {
58         ActionContext context = getContext();
59         ISelection selection = context.getSelection();
60         if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
61             IStructuredSelection sSelection = (IStructuredSelection) selection;
62             String JavaDoc menuName = null;
63             if (sSelection.getFirstElement() instanceof IPluginExtension ||
64                     sSelection.getFirstElement() instanceof IPluginExtensionPoint)
65                 menuName = PDEUIMessages.ImportActionGroup_importContributingPlugin;
66             else
67                 menuName = PDEUIMessages.PluginsView_import;
68             MenuManager importMenu = new MenuManager(menuName);
69             importMenu.add(new ImportAction(PluginImportOperation.IMPORT_BINARY, sSelection));
70             importMenu.add(new ImportAction(PluginImportOperation.IMPORT_BINARY_WITH_LINKS, sSelection));
71             importMenu.add(new ImportAction(PluginImportOperation.IMPORT_WITH_SOURCE, sSelection));
72             menu.add(importMenu);
73         }
74     }
75
76     private void handleImport(int importType, IStructuredSelection selection) {
77         ArrayList JavaDoc externalModels = new ArrayList JavaDoc();
78         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
79             IPluginModelBase model = getModel(iter.next());
80             if (model != null && model.getUnderlyingResource() == null)
81                 externalModels.add(model);
82         }
83         Display display = Display.getCurrent();
84         if (display == null)
85             display = Display.getDefault();
86         IPluginModelBase[] models =
87             (IPluginModelBase[]) externalModels.toArray(
88                     new IPluginModelBase[externalModels.size()]);
89
90         PluginImportWizard.doImportOperation(display.getActiveShell(), importType, models, false);
91     }
92
93     public static boolean canImport(IStructuredSelection selection) {
94         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
95             IPluginModelBase model = getModel(iter.next());
96             if (model != null && model.getUnderlyingResource() == null)
97                 return true;
98         }
99         return false;
100     }
101     
102     private static IPluginModelBase getModel(Object JavaDoc next) {
103         IPluginModelBase model = null;
104         if (next instanceof IPluginModelBase)
105             model = (IPluginModelBase) next;
106         else if (next instanceof IPluginBase)
107             model = ((IPluginBase)next).getPluginModel();
108         else if (next instanceof IPluginExtension)
109             model = ((IPluginExtension)next).getPluginModel();
110         else if (next instanceof IPluginExtensionPoint)
111             model = ((IPluginExtensionPoint)next).getPluginModel();
112         return model;
113     }
114 }
115
Popular Tags