KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.internal.core.natures.PDE;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.PDEUIMessages;
27 import org.eclipse.swt.custom.BusyIndicator;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.ui.IObjectActionDelegate;
30 import org.eclipse.ui.IWorkbenchPart;
31
32 public class ConvertProjectsAction implements IObjectActionDelegate {
33
34     private ISelection fSelection;
35
36     /*
37      * (non-Javadoc)
38      *
39      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
40      * org.eclipse.ui.IWorkbenchPart)
41      */

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

50     public void run(IAction action) {
51         IProject[] unconverted = getUnconvertedProjects();
52         if (unconverted.length == 0) {
53             MessageDialog
54                     .openInformation(
55                             this.getDisplay().getActiveShell(),
56                             PDEUIMessages.ConvertProjectsAction_find, PDEUIMessages.ConvertProjectsAction_none); //
57
return;
58         }
59
60         if (fSelection instanceof IStructuredSelection) {
61             Object JavaDoc[] elems = ((IStructuredSelection) fSelection).toArray();
62             Vector JavaDoc initialSelection = new Vector JavaDoc(elems.length);
63
64             for (int i = 0; i < elems.length; i++) {
65                 Object JavaDoc elem = elems[i];
66                 IProject project = null;
67
68                 if (elem instanceof IFile) {
69                     IFile file = (IFile) elem;
70                     project = file.getProject();
71                 } else if (elem instanceof IProject) {
72                     project = (IProject) elem;
73                 } else if (elem instanceof IJavaProject) {
74                     project = ((IJavaProject) elem).getProject();
75                 }
76                 if (project != null)
77                     initialSelection.add(project);
78             }
79
80             ConvertedProjectWizard wizard = new ConvertedProjectWizard(
81                     unconverted, initialSelection);
82
83             final Display display = getDisplay();
84             final WizardDialog dialog = new WizardDialog(display
85                     .getActiveShell(), wizard);
86             BusyIndicator.showWhile(display, new Runnable JavaDoc() {
87                 public void run() {
88                     dialog.open();
89                 }
90             });
91         }
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
98      * org.eclipse.jface.viewers.ISelection)
99      */

100     public void selectionChanged(IAction action, ISelection selection) {
101         fSelection = selection;
102     }
103
104     public Display getDisplay() {
105         Display display = Display.getCurrent();
106         if (display == null)
107             display = Display.getDefault();
108         return display;
109     }
110
111     private IProject[] getUnconvertedProjects() {
112         ArrayList JavaDoc unconverted = new ArrayList JavaDoc();
113         IProject[] projects = PDEPlugin.getWorkspace().getRoot().getProjects();
114         for (int i = 0; i < projects.length; i++) {
115             if (projects[i].isOpen() && !PDE.hasPluginNature(projects[i])
116                     && !PDE.hasFeatureNature(projects[i])
117                     && !PDE.hasUpdateSiteNature(projects[i])
118                     && projects[i].getName().indexOf('%') == -1
119                     && projects[i].getLocation().toString().indexOf('%') == -1)
120                 unconverted.add(projects[i]);
121         }
122         return (IProject[]) unconverted
123                 .toArray(new IProject[unconverted.size()]);
124     }
125 }
126
Popular Tags