KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > ConfigureContainerAction


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.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17
18 import org.eclipse.jdt.core.IClasspathEntry;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.JavaModelException;
21
22 import org.eclipse.swt.widgets.Shell;
23
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.operation.IRunnableContext;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29
30 import org.eclipse.ui.IObjectActionDelegate;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.PlatformUI;
33
34 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
36
37 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
38
39 /**
40  * Action to open a dialog to configure classpath containers. Added as a <code>objectContribution</code>
41  * to {@link ClassPathContainer}.
42  */

43 public class ConfigureContainerAction implements IObjectActionDelegate {
44
45     private ISelection fCurrentSelection;
46     private IWorkbenchPart fPart;
47
48     /*
49      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
50      */

51     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
52         fPart= targetPart;
53     }
54
55     /*
56      * @see IActionDelegate#run(IAction)
57      */

58     public void run(IAction action) {
59         if (fCurrentSelection instanceof IStructuredSelection) {
60             ClassPathContainer container= (ClassPathContainer) ((IStructuredSelection) fCurrentSelection).getFirstElement();
61             openWizard(container.getClasspathEntry(), container.getLabel(), container.getJavaProject());
62         }
63     }
64     
65     private void openWizard(IClasspathEntry entry, String JavaDoc label, final IJavaProject project) {
66         Shell shell= fPart.getSite().getShell();
67         try {
68             IClasspathEntry[] entries= project.getRawClasspath();
69             
70             IClasspathEntry result= BuildPathDialogAccess.configureContainerEntry(shell, entry, project, entries);
71             if (result == null || result.equals(entry)) {
72                 return; // user cancelled or no changes
73
}
74
75             int idx= indexInClasspath(entries, entry);
76             if (idx == -1) {
77                 return;
78             }
79             
80             final IClasspathEntry[] newEntries= new IClasspathEntry[entries.length];
81             System.arraycopy(entries, 0, newEntries, 0, entries.length);
82             newEntries[idx]= result;
83             
84             IRunnableContext context= fPart.getSite().getWorkbenchWindow();
85             if (context == null) {
86                 context= PlatformUI.getWorkbench().getProgressService();
87             }
88             context.run(true, true, new IRunnableWithProgress() {
89                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
90                     try {
91                         project.setRawClasspath(newEntries, project.getOutputLocation(), monitor);
92                     } catch (CoreException e) {
93                         throw new InvocationTargetException JavaDoc(e);
94                     }
95                 }
96             });
97         } catch (JavaModelException e) {
98             String JavaDoc title= ActionMessages.ConfigureContainerAction_error_title;
99             String JavaDoc message= ActionMessages.ConfigureContainerAction_error_creationfailed_message;
100             ExceptionHandler.handle(e, shell, title, message);
101         } catch (InvocationTargetException JavaDoc e) {
102             String JavaDoc title= ActionMessages.ConfigureContainerAction_error_title;
103             String JavaDoc message= ActionMessages.ConfigureContainerAction_error_applyingfailed_message;
104             ExceptionHandler.handle(e, shell, title, message);
105         } catch (InterruptedException JavaDoc e) {
106             // user cancelled
107
}
108     }
109     
110     protected static int indexInClasspath(IClasspathEntry[] entries, IClasspathEntry entry) {
111         for (int i= 0; i < entries.length; i++) {
112             if (entries[i] == entry) {
113                 return i;
114             }
115         }
116         return -1;
117     }
118
119     /*
120      * @see IActionDelegate#selectionChanged(IAction, ISelection)
121      */

122     public void selectionChanged(IAction action, ISelection selection) {
123         fCurrentSelection= selection;
124     }
125
126 }
127
Popular Tags