KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > AddArchiveToBuildpathAction


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.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.operation.IRunnableContext;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31
32 import org.eclipse.ui.IWorkbenchSite;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.part.ISetSelectionTarget;
35
36 import org.eclipse.jdt.core.IClasspathEntry;
37 import org.eclipse.jdt.core.IJavaElement;
38 import org.eclipse.jdt.core.IJavaProject;
39
40 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
41 import org.eclipse.jdt.internal.corext.buildpath.CPJavaProject;
42 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
43
44 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
45
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.JavaPluginImages;
48 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
49 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
50
51 //SelectedElements iff enabled: IJavaProject && size==1
52
public class AddArchiveToBuildpathAction extends BuildpathModifierAction {
53
54     private final IRunnableContext fContext;
55
56     public AddArchiveToBuildpathAction(IWorkbenchSite site) {
57         this(site, null, PlatformUI.getWorkbench().getProgressService());
58     }
59     
60     public AddArchiveToBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
61         this(null, selectionTarget, context);
62     }
63     
64     private AddArchiveToBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
65         super(site, selectionTarget, BuildpathModifierAction.ADD_LIB_TO_BP);
66         
67         fContext= context;
68
69         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddJarCP_label);
70         setImageDescriptor(JavaPluginImages.DESC_OBJS_EXTJAR);
71         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddJarCP_tooltip);
72     }
73     
74     /**
75      * {@inheritDoc}
76      */

77     public String JavaDoc getDetailedDescription() {
78         return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath_archives;
79     }
80
81     /**
82      * {@inheritDoc}
83      */

84     public void run() {
85
86         final Shell shell= getShell();
87         final IPath[] selected= BuildPathDialogAccess.chooseExternalJAREntries(shell);
88         if (selected == null)
89             return;
90         
91         try {
92             final IJavaProject javaProject= (IJavaProject)getSelectedElements().get(0);
93             IStatus status= ClasspathModifier.checkAddExternalJarsPrecondition(selected, CPJavaProject.createFromExisting(javaProject));
94             if (status.getSeverity() == IStatus.ERROR) {
95                 MessageDialog.openError(getShell(), NewWizardMessages.AddArchiveToBuildpathAction_InfoTitle, status.getMessage());
96                 return;
97             } else if (status.getSeverity() == IStatus.INFO) {
98                 MessageDialog.openInformation(getShell(), NewWizardMessages.AddArchiveToBuildpathAction_InfoTitle, status.getMessage());
99             } else if (status.getSeverity() == IStatus.WARNING) {
100                 MessageDialog.openWarning(getShell(), NewWizardMessages.AddArchiveToBuildpathAction_InfoTitle, status.getMessage());
101             }
102             
103             final IRunnableWithProgress runnable= new IRunnableWithProgress() {
104                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
105                     try {
106                         List JavaDoc result= addExternalJars(selected, javaProject, monitor);
107                         if (result.size() > 0)
108                             selectAndReveal(new StructuredSelection(result));
109                     } catch (CoreException e) {
110                         throw new InvocationTargetException JavaDoc(e);
111                     }
112                 }
113             };
114             fContext.run(false, false, runnable);
115         } catch (final InvocationTargetException JavaDoc e) {
116             if (e.getCause() instanceof CoreException) {
117                 showExceptionDialog((CoreException)e.getCause(), NewWizardMessages.AddArchiveToBuildpathAction_ErrorTitle);
118             } else {
119                 JavaPlugin.log(e);
120             }
121         } catch (CoreException e) {
122             showExceptionDialog(e, NewWizardMessages.AddArchiveToBuildpathAction_ErrorTitle);
123             JavaPlugin.log(e);
124         } catch (InterruptedException JavaDoc e) {
125         }
126     }
127     
128     private List JavaDoc addExternalJars(IPath[] jarPaths, IJavaProject project, IProgressMonitor monitor) throws CoreException {
129         if (monitor == null)
130             monitor= new NullProgressMonitor();
131         try {
132             monitor.beginTask(NewWizardMessages.ClasspathModifier_Monitor_AddToBuildpath, 4);
133
134             CPJavaProject cpProject= CPJavaProject.createFromExisting(project);
135             BuildpathDelta delta= ClasspathModifier.addExternalJars(jarPaths, cpProject);
136             ClasspathModifier.commitClassPath(cpProject, new SubProgressMonitor(monitor, 4));
137
138             informListeners(delta);
139
140             List JavaDoc addedEntries= delta.getAddedEntries();
141             List JavaDoc result= new ArrayList JavaDoc(addedEntries.size());
142             for (int i= 0; i < addedEntries.size(); i++) {
143                 IClasspathEntry entry= ((CPListElement) addedEntries.get(i)).getClasspathEntry();
144                 IJavaElement elem= project.findPackageFragmentRoot(entry.getPath());
145                 if (elem != null) {
146                     result.add(elem);
147                 }
148             }
149             monitor.worked(1);
150             return result;
151         } finally {
152             monitor.done();
153         }
154     }
155
156     protected boolean canHandle(IStructuredSelection selection) {
157         if (selection.size() != 1)
158             return false;
159
160         Object JavaDoc first= selection.getFirstElement();
161         if (!(first instanceof IJavaProject))
162             return false;
163
164         return true;
165     }
166 }
167
Popular Tags