KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.IJavaProject;
19 import org.eclipse.jdt.core.JavaCore;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.pde.core.plugin.IPluginModelBase;
26 import org.eclipse.pde.core.plugin.PluginRegistry;
27 import org.eclipse.pde.internal.core.WorkspaceModelManager;
28 import org.eclipse.pde.internal.ui.PDEPlugin;
29 import org.eclipse.pde.internal.ui.PDEUIMessages;
30 import org.eclipse.swt.custom.BusyIndicator;
31 import org.eclipse.ui.IViewActionDelegate;
32 import org.eclipse.ui.IViewPart;
33
34 public class UpdateClasspathAction implements IViewActionDelegate {
35     private ISelection fSelection;
36
37     /*
38      * @see IActionDelegate#run(IAction)
39      */

40     public void run(IAction action) {
41         IPluginModelBase[] fUnupdated = getModelsToUpdate();
42         if (fUnupdated.length == 0) {
43             MessageDialog.openInformation(
44                     PDEPlugin.getActiveWorkbenchShell(),
45                     PDEUIMessages.UpdateClasspathAction_find,
46                     PDEUIMessages.UpdateClasspathAction_none);
47             return;
48         }
49         if (fSelection instanceof IStructuredSelection) {
50             Object JavaDoc[] elems = ((IStructuredSelection) fSelection).toArray();
51             ArrayList JavaDoc models = new ArrayList JavaDoc(elems.length);
52             for (int i = 0; i < elems.length; i++) {
53                 Object JavaDoc elem = elems[i];
54                 IProject project = null;
55
56                 if (elem instanceof IFile) {
57                     IFile file = (IFile) elem;
58                     project = file.getProject();
59                 } else if (elem instanceof IProject) {
60                     project = (IProject) elem;
61                 } else if (elem instanceof IJavaProject) {
62                     project = ((IJavaProject) elem).getProject();
63                 }
64                 try {
65                     if (project != null
66                             && WorkspaceModelManager.isPluginProject(project)
67                             && project.hasNature(JavaCore.NATURE_ID)) {
68                         IPluginModelBase model = PluginRegistry.findModel(project);
69                         if (model != null) {
70                             models.add(model);
71                         }
72                     }
73                 } catch (CoreException e) {
74                     PDEPlugin.log(e);
75                 }
76             }
77
78             final IPluginModelBase[] modelArray = (IPluginModelBase[]) models
79                     .toArray(new IPluginModelBase[models.size()]);
80
81             UpdateBuildpathWizard wizard = new UpdateBuildpathWizard(fUnupdated, modelArray);
82             final WizardDialog dialog = new WizardDialog(PDEPlugin
83                     .getActiveWorkbenchShell(), wizard);
84             BusyIndicator.showWhile(PDEPlugin.getActiveWorkbenchShell()
85                     .getDisplay(), new Runnable JavaDoc() {
86                 public void run() {
87                     dialog.open();
88                 }
89             });
90         }
91     }
92
93     /*
94      * @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
95      */

96     public void init(IViewPart view) {
97     }
98
99     /*
100      * @see IActionDelegate#selectionChanged(IAction, ISelection)
101      */

102     public void selectionChanged(IAction action, ISelection selection) {
103         fSelection = selection;
104     }
105     
106     private IPluginModelBase[] getModelsToUpdate(){
107         IPluginModelBase[] models = PluginRegistry.getWorkspaceModels();
108         ArrayList JavaDoc modelArray = new ArrayList JavaDoc();
109         try {
110             for (int i = 0; i < models.length; i++) {
111                 if (models[i].getUnderlyingResource().getProject().hasNature(JavaCore.NATURE_ID))
112                     modelArray.add(models[i]);
113             }
114         } catch (CoreException e) {
115             PDEPlugin.logException(e);
116         }
117         return (IPluginModelBase[])modelArray.toArray(new IPluginModelBase[modelArray.size()]);
118     }
119     
120 }
121
Popular Tags