KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > tools > MigrationAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.wizards.tools;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.jdt.core.IJavaProject;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.PDECore;
26 import org.eclipse.pde.internal.core.PluginModelManager;
27 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.swt.custom.BusyIndicator;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.ui.IObjectActionDelegate;
32 import org.eclipse.ui.IWorkbenchPart;
33
34 public class MigrationAction implements IObjectActionDelegate {
35
36     private ISelection fSelection;
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
42      * org.eclipse.ui.IWorkbenchPart)
43      */

44     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
45     }
46
47     /*
48      * (non-Javadoc)
49      *
50      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
51      */

52     public void run(IAction action) {
53         IPluginModelBase[] modelsToMigrate = getModelsToMigrate();
54         if (modelsToMigrate.length == 0) {
55             MessageDialog
56                     .openInformation(
57                             this.getDisplay().getActiveShell(),
58                             PDEUIMessages.MigrationAction_find, PDEUIMessages.MigrationAction_none);//
59
return;
60         }
61
62         if (fSelection instanceof IStructuredSelection) {
63             Object JavaDoc[] elems = ((IStructuredSelection) fSelection).toArray();
64             ArrayList JavaDoc models = new ArrayList JavaDoc(elems.length);
65
66             PluginModelManager manager = PDECore.getDefault().getModelManager();
67             for (int i = 0; i < elems.length; i++) {
68                 Object JavaDoc elem = elems[i];
69                 IProject project = null;
70
71                 if (elem instanceof IFile) {
72                     IFile file = (IFile) elem;
73                     project = file.getProject();
74                 } else if (elem instanceof IProject) {
75                     project = (IProject) elem;
76                 } else if (elem instanceof IJavaProject) {
77                     project = ((IJavaProject) elem).getProject();
78                 }
79                 if (project != null) {
80                     IPluginModelBase model = manager.findModel(project);
81                     if (model != null) {
82                         models.add(model);
83                     }
84                 }
85             }
86
87             final IPluginModelBase[] modelArray = (IPluginModelBase[]) models
88                     .toArray(new IPluginModelBase[models.size()]);
89
90             MigratePluginWizard wizard = new MigratePluginWizard(
91                     modelsToMigrate, modelArray);
92             final Display display = getDisplay();
93             final WizardDialog dialog = new WizardDialog(display
94                     .getActiveShell(), wizard);
95             BusyIndicator.showWhile(display, new Runnable JavaDoc() {
96                 public void run() {
97                     dialog.open();
98                 }
99             });
100         }
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
107      * org.eclipse.jface.viewers.ISelection)
108      */

109     public void selectionChanged(IAction action, ISelection selection) {
110         fSelection = selection;
111     }
112
113     private Display getDisplay() {
114         Display display = Display.getCurrent();
115         if (display == null) {
116             display = Display.getDefault();
117         }
118         return display;
119     }
120
121     private IPluginModelBase[] getModelsToMigrate() {
122         Vector JavaDoc result = new Vector JavaDoc();
123         IPluginModelBase[] models = PDECore.getDefault()
124                 .getModelManager().getWorkspaceModels();
125         for (int i = 0; i < models.length; i++) {
126             if (!models[i].getUnderlyingResource().isLinked()
127                     && models[i].isLoaded()
128                     && !(models[i] instanceof IBundlePluginModelBase)
129                     && models[i].getPluginBase().getSchemaVersion() == null)
130                 result.add(models[i]);
131         }
132         return (IPluginModelBase[]) result.toArray(new IPluginModelBase[result
133                 .size()]);
134     }
135
136 }
137
Popular Tags