001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.jdt.core.IJavaProject;
018: import org.eclipse.jdt.core.JavaCore;
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.dialogs.MessageDialog;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.jface.wizard.WizardDialog;
024: import org.eclipse.pde.core.plugin.IPluginModelBase;
025: import org.eclipse.pde.core.plugin.PluginRegistry;
026: import org.eclipse.pde.internal.core.WorkspaceModelManager;
027: import org.eclipse.pde.internal.ui.PDEPlugin;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.swt.custom.BusyIndicator;
030: import org.eclipse.ui.IViewActionDelegate;
031: import org.eclipse.ui.IViewPart;
032:
033: public class UpdateClasspathAction implements IViewActionDelegate {
034: private ISelection fSelection;
035:
036: /*
037: * @see IActionDelegate#run(IAction)
038: */
039: public void run(IAction action) {
040: IPluginModelBase[] fUnupdated = getModelsToUpdate();
041: if (fUnupdated.length == 0) {
042: MessageDialog.openInformation(PDEPlugin
043: .getActiveWorkbenchShell(),
044: PDEUIMessages.UpdateClasspathAction_find,
045: PDEUIMessages.UpdateClasspathAction_none);
046: return;
047: }
048: if (fSelection instanceof IStructuredSelection) {
049: Object[] elems = ((IStructuredSelection) fSelection)
050: .toArray();
051: ArrayList models = new ArrayList(elems.length);
052: for (int i = 0; i < elems.length; i++) {
053: Object elem = elems[i];
054: IProject project = null;
055:
056: if (elem instanceof IFile) {
057: IFile file = (IFile) elem;
058: project = file.getProject();
059: } else if (elem instanceof IProject) {
060: project = (IProject) elem;
061: } else if (elem instanceof IJavaProject) {
062: project = ((IJavaProject) elem).getProject();
063: }
064: try {
065: if (project != null
066: && WorkspaceModelManager
067: .isPluginProject(project)
068: && project.hasNature(JavaCore.NATURE_ID)) {
069: IPluginModelBase model = PluginRegistry
070: .findModel(project);
071: if (model != null) {
072: models.add(model);
073: }
074: }
075: } catch (CoreException e) {
076: PDEPlugin.log(e);
077: }
078: }
079:
080: final IPluginModelBase[] modelArray = (IPluginModelBase[]) models
081: .toArray(new IPluginModelBase[models.size()]);
082:
083: UpdateBuildpathWizard wizard = new UpdateBuildpathWizard(
084: fUnupdated, modelArray);
085: final WizardDialog dialog = new WizardDialog(PDEPlugin
086: .getActiveWorkbenchShell(), wizard);
087: BusyIndicator.showWhile(PDEPlugin.getActiveWorkbenchShell()
088: .getDisplay(), new Runnable() {
089: public void run() {
090: dialog.open();
091: }
092: });
093: }
094: }
095:
096: /*
097: * @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
098: */
099: public void init(IViewPart view) {
100: }
101:
102: /*
103: * @see IActionDelegate#selectionChanged(IAction, ISelection)
104: */
105: public void selectionChanged(IAction action, ISelection selection) {
106: fSelection = selection;
107: }
108:
109: private IPluginModelBase[] getModelsToUpdate() {
110: IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
111: ArrayList modelArray = new ArrayList();
112: try {
113: for (int i = 0; i < models.length; i++) {
114: if (models[i].getUnderlyingResource().getProject()
115: .hasNature(JavaCore.NATURE_ID))
116: modelArray.add(models[i]);
117: }
118: } catch (CoreException e) {
119: PDEPlugin.logException(e);
120: }
121: return (IPluginModelBase[]) modelArray
122: .toArray(new IPluginModelBase[modelArray.size()]);
123: }
124:
125: }
|