KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > AddToClasspathAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.OperationCanceledException;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26
27 import org.eclipse.jface.viewers.IStructuredSelection;
28
29 import org.eclipse.ui.IWorkbenchSite;
30 import org.eclipse.ui.PlatformUI;
31
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.JavaModelException;
36
37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
41 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
43 import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
44
45 /**
46  * Action to add a JAR to the classpath of its parent project.
47  * Action is applicable to selections containing archives (JAR or zip)
48  *
49  * <p>
50  * This class may be instantiated; it is not intended to be subclassed.
51  * </p>
52  *
53  * @since 2.1
54  */

55 public class AddToClasspathAction extends SelectionDispatchAction {
56
57     /**
58      * Creates a new <code>AddToClasspathAction</code>. The action requires that
59      * the selection provided by the site's selection provider is of type <code>
60      * org.eclipse.jface.viewers.IStructuredSelection</code>.
61      *
62      * @param site the site providing context information for this action
63      */

64     public AddToClasspathAction(IWorkbenchSite site) {
65         super(site);
66         setText(ActionMessages.AddToClasspathAction_label);
67         setToolTipText(ActionMessages.AddToClasspathAction_toolTip);
68         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_TO_CLASSPATH_ACTION);
69     }
70     
71     /* (non-Javadoc)
72      * Method declared in SelectionDispatchAction
73      */

74     public void selectionChanged(IStructuredSelection selection) {
75         try {
76             setEnabled(checkEnabled(selection));
77         } catch (JavaModelException e) {
78             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
79
if (JavaModelUtil.isExceptionToBeLogged(e))
80                 JavaPlugin.log(e);
81             setEnabled(false);
82         }
83     }
84     
85     private static boolean checkEnabled(IStructuredSelection selection) throws JavaModelException {
86         if (selection.isEmpty())
87             return false;
88         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
89             if (! canBeAddedToBuildPath(iter.next()))
90                 return false;
91         }
92         return true;
93     }
94
95     private static boolean canBeAddedToBuildPath(Object JavaDoc element) throws JavaModelException{
96         return (element instanceof IAdaptable) && getCandidate((IAdaptable) element) != null;
97     }
98
99     private static IFile getCandidate(IAdaptable element) throws JavaModelException {
100         IResource resource= (IResource)element.getAdapter(IResource.class);
101         if (! (resource instanceof IFile) || ! ArchiveFileFilter.isArchivePath(resource.getFullPath()))
102             return null;
103         
104         IJavaProject project= JavaCore.create(resource.getProject());
105         if (project != null && project.exists() && (project.findPackageFragmentRoot(resource.getFullPath()) == null))
106             return (IFile) resource;
107         return null;
108     }
109             
110     /* (non-Javadoc)
111      * Method declared in SelectionDispatchAction
112      */

113     public void run(IStructuredSelection selection) {
114         try {
115             final IFile[] files= getJARFiles(selection);
116             
117             IWorkspaceRunnable operation= new IWorkspaceRunnable() {
118                 public void run(IProgressMonitor monitor) throws CoreException {
119                     monitor.beginTask(ActionMessages.AddToClasspathAction_progressMessage, files.length);
120                     for (int i= 0; i < files.length; i++) {
121                         monitor.subTask(files[i].getFullPath().toString());
122                         IJavaProject project= JavaCore.create(files[i].getProject());
123                         addToClassPath(project, files[i].getFullPath(), new SubProgressMonitor(monitor, 1));
124                     }
125                 }
126                 
127                 private void addToClassPath(IJavaProject project, IPath jarPath, IProgressMonitor monitor) throws JavaModelException {
128                     if (monitor.isCanceled())
129                         throw new OperationCanceledException();
130                     IClasspathEntry[] entries= project.getRawClasspath();
131                     IClasspathEntry[] newEntries= new IClasspathEntry[entries.length + 1];
132                     System.arraycopy(entries, 0, newEntries, 0, entries.length);
133                     newEntries[entries.length]= JavaCore.newLibraryEntry(jarPath, null, null, false);
134                     project.setRawClasspath(newEntries, monitor);
135                 }
136             };
137             
138             PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new WorkbenchRunnableAdapter(operation));
139         } catch (InvocationTargetException JavaDoc e) {
140             ExceptionHandler.handle(e, getShell(),
141                 ActionMessages.AddToClasspathAction_error_title,
142                 ActionMessages.AddToClasspathAction_error_message);
143         } catch (JavaModelException e) {
144             ExceptionHandler.handle(e, getShell(),
145                     ActionMessages.AddToClasspathAction_error_title,
146                     ActionMessages.AddToClasspathAction_error_message);
147         } catch (InterruptedException JavaDoc e) {
148             // canceled
149
}
150
151     }
152     
153     private static IFile[] getJARFiles(IStructuredSelection selection) throws JavaModelException {
154         ArrayList JavaDoc list= new ArrayList JavaDoc();
155         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
156             Object JavaDoc element= iter.next();
157             if (element instanceof IAdaptable) {
158                 IFile file= getCandidate((IAdaptable) element);
159                 if (file != null) {
160                     list.add(file);
161                 }
162             }
163         }
164         return (IFile[]) list.toArray(new IFile[list.size()]);
165     }
166 }
167
168
Popular Tags