KickJava   Java API By Example, From Geeks To Geeks.

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


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.IProgressMonitor;
19 import org.eclipse.core.runtime.OperationCanceledException;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21 import org.eclipse.core.runtime.jobs.ISchedulingRule;
22 import org.eclipse.core.runtime.jobs.Job;
23
24 import org.eclipse.core.resources.IWorkspaceRunnable;
25 import org.eclipse.core.resources.ResourcesPlugin;
26
27 import org.eclipse.swt.widgets.Shell;
28
29 import org.eclipse.jface.operation.IRunnableContext;
30 import org.eclipse.jface.operation.IRunnableWithProgress;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.wizard.WizardDialog;
34
35 import org.eclipse.ui.IWorkbenchSite;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.part.ISetSelectionTarget;
38
39 import org.eclipse.jdt.core.IClasspathEntry;
40 import org.eclipse.jdt.core.IJavaProject;
41 import org.eclipse.jdt.core.JavaModelException;
42
43 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
44 import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
45
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.JavaPluginImages;
48 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
49 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
50 import org.eclipse.jdt.internal.ui.util.PixelConverter;
51 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
52 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ClasspathContainerWizard;
54
55 //SelectedElements: IJavaProject && size == 1
56
public class AddLibraryToBuildpathAction extends BuildpathModifierAction {
57
58     public AddLibraryToBuildpathAction(IWorkbenchSite site) {
59         this(site, null, PlatformUI.getWorkbench().getProgressService());
60     }
61     
62     public AddLibraryToBuildpathAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
63         this(null, selectionTarget, context);
64     }
65
66     private AddLibraryToBuildpathAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
67         super(site, selectionTarget, BuildpathModifierAction.ADD_LIB_TO_BP);
68
69         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddLibCP_label);
70         setImageDescriptor(JavaPluginImages.DESC_OBJS_LIBRARY);
71         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddLibCP_tooltip);
72     }
73     
74     /**
75      * {@inheritDoc}
76      */

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

84     public void run() {
85         final IJavaProject project= (IJavaProject)getSelectedElements().get(0);
86
87         Shell shell= getShell();
88         if (shell == null) {
89             shell= JavaPlugin.getActiveWorkbenchShell();
90         }
91
92         IClasspathEntry[] classpath;
93         try {
94             classpath= project.getRawClasspath();
95         } catch (JavaModelException e1) {
96             showExceptionDialog(e1, NewWizardMessages.AddLibraryToBuildpathAction_ErrorTitle);
97             return;
98         }
99
100         ClasspathContainerWizard wizard= new ClasspathContainerWizard((IClasspathEntry) null, project, classpath) {
101
102             /**
103              * {@inheritDoc}
104              */

105             public boolean performFinish() {
106                 if (super.performFinish()) {
107                     IWorkspaceRunnable op= new IWorkspaceRunnable() {
108                         public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
109                             try {
110                                 finishPage(monitor);
111                             } catch (InterruptedException JavaDoc e) {
112                                 throw new OperationCanceledException(e.getMessage());
113                             }
114                         }
115                     };
116                     try {
117                         ISchedulingRule rule= null;
118                         Job job= Job.getJobManager().currentJob();
119                         if (job != null)
120                             rule= job.getRule();
121                         IRunnableWithProgress runnable= null;
122                         if (rule != null)
123                             runnable= new WorkbenchRunnableAdapter(op, rule, true);
124                         else
125                             runnable= new WorkbenchRunnableAdapter(op, ResourcesPlugin.getWorkspace().getRoot());
126                         getContainer().run(false, true, runnable);
127                     } catch (InvocationTargetException JavaDoc e) {
128                         JavaPlugin.log(e);
129                         return false;
130                     } catch (InterruptedException JavaDoc e) {
131                         return false;
132                     }
133                     return true;
134                 }
135                 return false;
136             }
137
138             private void finishPage(IProgressMonitor pm) throws InterruptedException JavaDoc {
139                 IClasspathEntry[] selected= getNewEntries();
140                 if (selected != null) {
141                     try {
142                         pm.beginTask(NewWizardMessages.ClasspathModifier_Monitor_AddToBuildpath, 4);
143
144                         List JavaDoc addedEntries= new ArrayList JavaDoc();
145                         for (int i= 0; i < selected.length; i++) {
146                             addedEntries.add(new CPListElement(project, IClasspathEntry.CPE_CONTAINER, selected[i].getPath(), null));
147                         }
148
149                         pm.worked(1);
150                         if (pm.isCanceled())
151                             throw new InterruptedException JavaDoc();
152
153                         List JavaDoc existingEntries= ClasspathModifier.getExistingEntries(project);
154                         ClasspathModifier.setNewEntry(existingEntries, addedEntries, project, new SubProgressMonitor(pm, 1));
155                         if (pm.isCanceled())
156                             throw new InterruptedException JavaDoc();
157
158                         ClasspathModifier.commitClassPath(existingEntries, project, new SubProgressMonitor(pm, 1));
159                         
160                         BuildpathDelta delta= new BuildpathDelta(getToolTipText());
161                         delta.setNewEntries((CPListElement[])existingEntries.toArray(new CPListElement[existingEntries.size()]));
162                         informListeners(delta);
163
164                         List JavaDoc result= new ArrayList JavaDoc(addedEntries.size());
165                         for (int i= 0; i < addedEntries.size(); i++) {
166                             result.add(new ClassPathContainer(project, selected[i]));
167                         }
168                         selectAndReveal(new StructuredSelection(result));
169
170                         pm.worked(1);
171                     } catch (CoreException e) {
172                         showExceptionDialog(e, NewWizardMessages.AddLibraryToBuildpathAction_ErrorTitle);
173                     } finally {
174                         pm.done();
175                     }
176                 }
177             }
178         };
179         wizard.setNeedsProgressMonitor(true);
180
181         WizardDialog dialog= new WizardDialog(shell, wizard);
182         PixelConverter converter= new PixelConverter(shell);
183         dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
184         dialog.create();
185         dialog.open();
186     }
187
188     protected boolean canHandle(IStructuredSelection selection) {
189         if (selection.size() != 1)
190             return false;
191         
192         if (!(selection.getFirstElement() instanceof IJavaProject))
193             return false;
194             
195         return true;
196     }
197
198 }
199
Popular Tags