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.jdt.internal.ui.wizards.buildpaths.newsourcepage;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.core.runtime.OperationCanceledException;
019: import org.eclipse.core.runtime.SubProgressMonitor;
020: import org.eclipse.core.runtime.jobs.ISchedulingRule;
021: import org.eclipse.core.runtime.jobs.Job;
022:
023: import org.eclipse.core.resources.IWorkspaceRunnable;
024: import org.eclipse.core.resources.ResourcesPlugin;
025:
026: import org.eclipse.swt.widgets.Shell;
027:
028: import org.eclipse.jface.operation.IRunnableContext;
029: import org.eclipse.jface.operation.IRunnableWithProgress;
030: import org.eclipse.jface.viewers.IStructuredSelection;
031: import org.eclipse.jface.viewers.StructuredSelection;
032: import org.eclipse.jface.wizard.WizardDialog;
033:
034: import org.eclipse.ui.IWorkbenchSite;
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.part.ISetSelectionTarget;
037:
038: import org.eclipse.jdt.core.IClasspathEntry;
039: import org.eclipse.jdt.core.IJavaProject;
040: import org.eclipse.jdt.core.JavaModelException;
041:
042: import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
043: import org.eclipse.jdt.internal.corext.buildpath.ClasspathModifier;
044:
045: import org.eclipse.jdt.internal.ui.JavaPlugin;
046: import org.eclipse.jdt.internal.ui.JavaPluginImages;
047: import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
048: import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
049: import org.eclipse.jdt.internal.ui.util.PixelConverter;
050: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
051: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
052: import org.eclipse.jdt.internal.ui.wizards.buildpaths.ClasspathContainerWizard;
053:
054: //SelectedElements: IJavaProject && size == 1
055: public class AddLibraryToBuildpathAction extends
056: BuildpathModifierAction {
057:
058: public AddLibraryToBuildpathAction(IWorkbenchSite site) {
059: this (site, null, PlatformUI.getWorkbench().getProgressService());
060: }
061:
062: public AddLibraryToBuildpathAction(IRunnableContext context,
063: ISetSelectionTarget selectionTarget) {
064: this (null, selectionTarget, context);
065: }
066:
067: private AddLibraryToBuildpathAction(IWorkbenchSite site,
068: ISetSelectionTarget selectionTarget,
069: IRunnableContext context) {
070: super (site, selectionTarget,
071: BuildpathModifierAction.ADD_LIB_TO_BP);
072:
073: setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddLibCP_label);
074: setImageDescriptor(JavaPluginImages.DESC_OBJS_LIBRARY);
075: setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddLibCP_tooltip);
076: }
077:
078: /**
079: * {@inheritDoc}
080: */
081: public String getDetailedDescription() {
082: return NewWizardMessages.PackageExplorerActionGroup_FormText_Default_toBuildpath_library;
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: public void run() {
089: final IJavaProject project = (IJavaProject) getSelectedElements()
090: .get(0);
091:
092: Shell shell = getShell();
093: if (shell == null) {
094: shell = JavaPlugin.getActiveWorkbenchShell();
095: }
096:
097: IClasspathEntry[] classpath;
098: try {
099: classpath = project.getRawClasspath();
100: } catch (JavaModelException e1) {
101: showExceptionDialog(
102: e1,
103: NewWizardMessages.AddLibraryToBuildpathAction_ErrorTitle);
104: return;
105: }
106:
107: ClasspathContainerWizard wizard = new ClasspathContainerWizard(
108: (IClasspathEntry) null, project, classpath) {
109:
110: /**
111: * {@inheritDoc}
112: */
113: public boolean performFinish() {
114: if (super .performFinish()) {
115: IWorkspaceRunnable op = new IWorkspaceRunnable() {
116: public void run(IProgressMonitor monitor)
117: throws CoreException,
118: OperationCanceledException {
119: try {
120: finishPage(monitor);
121: } catch (InterruptedException e) {
122: throw new OperationCanceledException(e
123: .getMessage());
124: }
125: }
126: };
127: try {
128: ISchedulingRule rule = null;
129: Job job = Job.getJobManager().currentJob();
130: if (job != null)
131: rule = job.getRule();
132: IRunnableWithProgress runnable = null;
133: if (rule != null)
134: runnable = new WorkbenchRunnableAdapter(op,
135: rule, true);
136: else
137: runnable = new WorkbenchRunnableAdapter(op,
138: ResourcesPlugin.getWorkspace()
139: .getRoot());
140: getContainer().run(false, true, runnable);
141: } catch (InvocationTargetException e) {
142: JavaPlugin.log(e);
143: return false;
144: } catch (InterruptedException e) {
145: return false;
146: }
147: return true;
148: }
149: return false;
150: }
151:
152: private void finishPage(IProgressMonitor pm)
153: throws InterruptedException {
154: IClasspathEntry[] selected = getNewEntries();
155: if (selected != null) {
156: try {
157: pm
158: .beginTask(
159: NewWizardMessages.ClasspathModifier_Monitor_AddToBuildpath,
160: 4);
161:
162: List addedEntries = new ArrayList();
163: for (int i = 0; i < selected.length; i++) {
164: addedEntries.add(new CPListElement(project,
165: IClasspathEntry.CPE_CONTAINER,
166: selected[i].getPath(), null));
167: }
168:
169: pm.worked(1);
170: if (pm.isCanceled())
171: throw new InterruptedException();
172:
173: List existingEntries = ClasspathModifier
174: .getExistingEntries(project);
175: ClasspathModifier.setNewEntry(existingEntries,
176: addedEntries, project,
177: new SubProgressMonitor(pm, 1));
178: if (pm.isCanceled())
179: throw new InterruptedException();
180:
181: ClasspathModifier.commitClassPath(
182: existingEntries, project,
183: new SubProgressMonitor(pm, 1));
184:
185: BuildpathDelta delta = new BuildpathDelta(
186: getToolTipText());
187: delta
188: .setNewEntries((CPListElement[]) existingEntries
189: .toArray(new CPListElement[existingEntries
190: .size()]));
191: informListeners(delta);
192:
193: List result = new ArrayList(addedEntries.size());
194: for (int i = 0; i < addedEntries.size(); i++) {
195: result.add(new ClassPathContainer(project,
196: selected[i]));
197: }
198: selectAndReveal(new StructuredSelection(result));
199:
200: pm.worked(1);
201: } catch (CoreException e) {
202: showExceptionDialog(
203: e,
204: NewWizardMessages.AddLibraryToBuildpathAction_ErrorTitle);
205: } finally {
206: pm.done();
207: }
208: }
209: }
210: };
211: wizard.setNeedsProgressMonitor(true);
212:
213: WizardDialog dialog = new WizardDialog(shell, wizard);
214: PixelConverter converter = new PixelConverter(shell);
215: dialog.setMinimumPageSize(converter
216: .convertWidthInCharsToPixels(70), converter
217: .convertHeightInCharsToPixels(20));
218: dialog.create();
219: dialog.open();
220: }
221:
222: protected boolean canHandle(IStructuredSelection selection) {
223: if (selection.size() != 1)
224: return false;
225:
226: if (!(selection.getFirstElement() instanceof IJavaProject))
227: return false;
228:
229: return true;
230: }
231:
232: }
|