001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.wizards.tools;
011:
012: import java.util.ArrayList;
013: import java.util.Vector;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.jdt.core.IJavaProject;
018: import org.eclipse.jface.action.IAction;
019: import org.eclipse.jface.dialogs.MessageDialog;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.jface.wizard.WizardDialog;
023: import org.eclipse.pde.internal.core.natures.PDE;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.PDEUIMessages;
026: import org.eclipse.swt.custom.BusyIndicator;
027: import org.eclipse.swt.widgets.Display;
028: import org.eclipse.ui.IObjectActionDelegate;
029: import org.eclipse.ui.IWorkbenchPart;
030:
031: public class ConvertProjectsAction implements IObjectActionDelegate {
032:
033: private ISelection fSelection;
034:
035: /*
036: * (non-Javadoc)
037: *
038: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
039: * org.eclipse.ui.IWorkbenchPart)
040: */
041: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
042: }
043:
044: /*
045: * (non-Javadoc)
046: *
047: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
048: */
049: public void run(IAction action) {
050: IProject[] unconverted = getUnconvertedProjects();
051: if (unconverted.length == 0) {
052: MessageDialog.openInformation(this .getDisplay()
053: .getActiveShell(),
054: PDEUIMessages.ConvertProjectsAction_find,
055: PDEUIMessages.ConvertProjectsAction_none); //
056: return;
057: }
058:
059: if (fSelection instanceof IStructuredSelection) {
060: Object[] elems = ((IStructuredSelection) fSelection)
061: .toArray();
062: Vector initialSelection = new Vector(elems.length);
063:
064: for (int i = 0; i < elems.length; i++) {
065: Object elem = elems[i];
066: IProject project = null;
067:
068: if (elem instanceof IFile) {
069: IFile file = (IFile) elem;
070: project = file.getProject();
071: } else if (elem instanceof IProject) {
072: project = (IProject) elem;
073: } else if (elem instanceof IJavaProject) {
074: project = ((IJavaProject) elem).getProject();
075: }
076: if (project != null)
077: initialSelection.add(project);
078: }
079:
080: ConvertedProjectWizard wizard = new ConvertedProjectWizard(
081: unconverted, initialSelection);
082:
083: final Display display = getDisplay();
084: final WizardDialog dialog = new WizardDialog(display
085: .getActiveShell(), wizard);
086: BusyIndicator.showWhile(display, new Runnable() {
087: public void run() {
088: dialog.open();
089: }
090: });
091: }
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
098: * org.eclipse.jface.viewers.ISelection)
099: */
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 unconverted = new ArrayList();
113: IProject[] projects = PDEPlugin.getWorkspace().getRoot()
114: .getProjects();
115: for (int i = 0; i < projects.length; i++) {
116: if (projects[i].isOpen()
117: && !PDE.hasPluginNature(projects[i])
118: && !PDE.hasFeatureNature(projects[i])
119: && !PDE.hasUpdateSiteNature(projects[i])
120: && projects[i].getName().indexOf('%') == -1
121: && projects[i].getLocation().toString()
122: .indexOf('%') == -1)
123: unconverted.add(projects[i]);
124: }
125: return (IProject[]) unconverted
126: .toArray(new IProject[unconverted.size()]);
127: }
128: }
|