KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > ImportActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.view;
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.jface.action.Action;
18 import org.eclipse.jface.action.IMenuManager;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.pde.core.plugin.IPluginBase;
24 import org.eclipse.pde.core.plugin.IPluginExtension;
25 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
26 import org.eclipse.pde.core.plugin.IPluginModelBase;
27 import org.eclipse.pde.internal.core.ModelEntry;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.pde.internal.ui.wizards.imports.PluginImportOperation;
31 import org.eclipse.pde.internal.ui.wizards.imports.PluginImportWizard;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.actions.ActionContext;
35 import org.eclipse.ui.actions.ActionGroup;
36
37 public class ImportActionGroup extends ActionGroup {
38
39     class ImportAction extends Action {
40         IStructuredSelection fSel;
41         int fImportType;
42         ImportAction(int importType, IStructuredSelection selection) {
43             fSel = selection;
44             fImportType = importType;
45             switch (fImportType) {
46             case PluginImportOperation.IMPORT_BINARY:
47                 setText(PDEUIMessages.PluginsView_asBinaryProject);
48                 break;
49             case PluginImportOperation.IMPORT_BINARY_WITH_LINKS:
50                 setText(PDEUIMessages.ImportActionGroup_binaryWithLinkedContent);
51                 break;
52             case PluginImportOperation.IMPORT_WITH_SOURCE:
53                 setText(PDEUIMessages.PluginsView_asSourceProject);
54                 break;
55             }
56         }
57         public void run() {
58             handleImport(fImportType, fSel);
59         }
60     }
61     
62     public void fillContextMenu(IMenuManager menu) {
63         ActionContext context = getContext();
64         ISelection selection = context.getSelection();
65         if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
66             IStructuredSelection sSelection = (IStructuredSelection) selection;
67             String JavaDoc menuName = null;
68             if (sSelection.getFirstElement() instanceof IPluginExtension ||
69                     sSelection.getFirstElement() instanceof IPluginExtensionPoint)
70                 menuName = PDEUIMessages.ImportActionGroup_importContributingPlugin;
71             else
72                 menuName = PDEUIMessages.PluginsView_import;
73             MenuManager importMenu = new MenuManager(menuName);
74             importMenu.add(new ImportAction(PluginImportOperation.IMPORT_BINARY, sSelection));
75             importMenu.add(new ImportAction(PluginImportOperation.IMPORT_BINARY_WITH_LINKS, sSelection));
76             importMenu.add(new ImportAction(PluginImportOperation.IMPORT_WITH_SOURCE, sSelection));
77             menu.add(importMenu);
78         }
79     }
80     
81     private void handleImport(int importType, IStructuredSelection selection) {
82         ArrayList JavaDoc externalModels = new ArrayList JavaDoc();
83         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
84             Object JavaDoc next = iter.next();
85             IPluginModelBase model = null;
86             if (next instanceof ModelEntry)
87                 model = ((ModelEntry) next).getActiveModel();
88             else if (next instanceof IPluginBase)
89                 model = ((IPluginBase)next).getPluginModel();
90             else if (next instanceof IPluginExtension)
91                 model = ((IPluginExtension)next).getPluginModel();
92             else if (next instanceof IPluginExtensionPoint)
93                 model = ((IPluginExtensionPoint)next).getPluginModel();
94             
95             if (model != null && model.getUnderlyingResource() == null)
96                 externalModels.add(model);
97         }
98         Display display = Display.getCurrent();
99         if (display == null)
100             display = Display.getDefault();
101         IPluginModelBase[] models =
102             (IPluginModelBase[]) externalModels.toArray(
103                 new IPluginModelBase[externalModels.size()]);
104         try {
105             IRunnableWithProgress op =
106                 PluginImportWizard.getImportOperation(display.getActiveShell(), importType, models, false);
107             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(op);
108         } catch (InterruptedException JavaDoc e) {
109         } catch (InvocationTargetException JavaDoc e) {
110             PDEPlugin.logException(e);
111         } catch (Exception JavaDoc e) {
112         }
113     }
114     
115     public static boolean canImport(IStructuredSelection selection) {
116         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
117             Object JavaDoc obj = iter.next();
118             if (obj instanceof ModelEntry) {
119                 ModelEntry entry = (ModelEntry) obj;
120                 if (entry.getWorkspaceModel() == null)
121                     return true;
122             } else if (obj instanceof IPluginBase) {
123                 IPluginBase base = (IPluginBase)obj;
124                 if (base.getPluginModel().getUnderlyingResource() == null)
125                     return true;
126             } else if (obj instanceof IPluginExtension) {
127                 IPluginExtension ext = (IPluginExtension)obj;
128                 if (ext.getPluginModel().getUnderlyingResource() == null)
129                     return true;
130             } else if (obj instanceof IPluginExtensionPoint) {
131                 IPluginExtensionPoint exp = (IPluginExtensionPoint)obj;
132                 if (exp.getPluginModel().getUnderlyingResource() == null)
133                     return true;
134             }
135         }
136         return false;
137     }
138     
139 }
140
Popular Tags