]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/AddToClasspathAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / AddToClasspathAction.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.ui.actions;
12
13import java.lang.reflect.InvocationTargetException;
14import java.util.ArrayList;
15import java.util.Iterator;
16
17import org.eclipse.core.runtime.CoreException;
18import org.eclipse.core.runtime.IAdaptable;
19import org.eclipse.core.runtime.IPath;
20import org.eclipse.core.runtime.IProgressMonitor;
21import org.eclipse.core.runtime.OperationCanceledException;
22import org.eclipse.core.runtime.SubProgressMonitor;
23
24import org.eclipse.core.resources.IFile;
25import org.eclipse.core.resources.IResource;
26import org.eclipse.core.resources.IWorkspaceRunnable;
27
28import org.eclipse.jface.viewers.IStructuredSelection;
29
30import org.eclipse.ui.IWorkbenchSite;
31import org.eclipse.ui.PlatformUI;
32
33import org.eclipse.jdt.core.IClasspathEntry;
34import org.eclipse.jdt.core.IJavaProject;
35import org.eclipse.jdt.core.JavaCore;
36import org.eclipse.jdt.core.JavaModelException;
37
38import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
39
40import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
41import org.eclipse.jdt.internal.ui.JavaPlugin;
42import org.eclipse.jdt.internal.ui.actions.ActionMessages;
43import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
44import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
46import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
47
48/**
49 * Action to add a JAR to the classpath of its parent project.
50 * Action is applicable to selections containing archives (JAR or zip)
51 *
52 * <p>
53 * This class may be instantiated; it is not intended to be subclassed.
54 * </p>
55 *
56 * @since 2.1
57 *
58 * @noextend This class is not intended to be subclassed by clients.
59 */
60public class AddToClasspathAction extends SelectionDispatchAction {
61
62 /**
63 * Creates a new <code>AddToClasspathAction</code>. The action requires that
64 * the selection provided by the site's selection provider is of type <code>
65 * org.eclipse.jface.viewers.IStructuredSelection</code>.
66 *
67 * @param site the site providing context information for this action
68 */
69 public AddToClasspathAction(IWorkbenchSite site) {
70 super(site);
71 setText(ActionMessages.AddToClasspathAction_label);
72 setToolTipText(ActionMessages.AddToClasspathAction_toolTip);
73 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_TO_CLASSPATH_ACTION);
74 }
75
76 /* (non-Javadoc)
77 * Method declared in SelectionDispatchAction
78 */
79 @Override
80 public void selectionChanged(IStructuredSelection selection) {
81 try {
82 setEnabled(checkEnabled(selection));
83 } catch (JavaModelException e) {
84 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
85 if (JavaModelUtil.isExceptionToBeLogged(e))
86 JavaPlugin.log(e);
87 setEnabled(false);
88 }
89 }
90
91 private static boolean checkEnabled(IStructuredSelection selection) throws JavaModelException {
92 if (selection.isEmpty())
93 return false;
94 for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
95 if (! canBeAddedToBuildPath(iter.next()))
96 return false;
97 }
98 return true;
99 }
100
101 private static boolean canBeAddedToBuildPath(Object element) throws JavaModelException{
102 return (element instanceof IAdaptable) && getCandidate((IAdaptable) element) != null;
103 }
104
105 private static IFile getCandidate(IAdaptable element) throws JavaModelException {
106 IResource resource= (IResource)element.getAdapter(IResource.class);
107 if (! (resource instanceof IFile) || ! ArchiveFileFilter.isArchivePath(resource.getFullPath(), true))
108 return null;
109
110 IJavaProject project= JavaCore.create(resource.getProject());
111 if (project != null && project.exists() && (project.findPackageFragmentRoot(resource.getFullPath()) == null))
112 return (IFile) resource;
113 return null;
114 }
115
116 /* (non-Javadoc)
117 * Method declared in SelectionDispatchAction
118 */
119 @Override
120 public void run(IStructuredSelection selection) {
121 try {
122 final IFile[] files= getJARFiles(selection);
123
124 IWorkspaceRunnable operation= new IWorkspaceRunnable() {
125 public void run(IProgressMonitor monitor) throws CoreException {
126 monitor.beginTask(ActionMessages.AddToClasspathAction_progressMessage, files.length);
127 for (int i= 0; i < files.length; i++) {
128 monitor.subTask(BasicElementLabels.getPathLabel(files[i].getFullPath(), false));
129 IJavaProject project= JavaCore.create(files[i].getProject());
130 addToClassPath(project, files[i].getFullPath(), new SubProgressMonitor(monitor, 1));
131 }
132 }
133
134 private void addToClassPath(IJavaProject project, IPath jarPath, IProgressMonitor monitor) throws JavaModelException {
135 if (monitor.isCanceled())
136 throw new OperationCanceledException();
137 IClasspathEntry[] entries= project.getRawClasspath();
138 IClasspathEntry[] newEntries= new IClasspathEntry[entries.length + 1];
139 System.arraycopy(entries, 0, newEntries, 0, entries.length);
140 newEntries[entries.length]= JavaCore.newLibraryEntry(jarPath, null, null, false);
141 project.setRawClasspath(newEntries, monitor);
142 }
143 };
144
145 PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new WorkbenchRunnableAdapter(operation));
146 } catch (InvocationTargetException e) {
147 ExceptionHandler.handle(e, getShell(),
148 ActionMessages.AddToClasspathAction_error_title,
149 ActionMessages.AddToClasspathAction_error_message);
150 } catch (JavaModelException e) {
151 ExceptionHandler.handle(e, getShell(),
152 ActionMessages.AddToClasspathAction_error_title,
153 ActionMessages.AddToClasspathAction_error_message);
154 } catch (InterruptedException e) {
155 // canceled
156 }
157
158 }
159
160 private static IFile[] getJARFiles(IStructuredSelection selection) throws JavaModelException {
161 ArrayList<IFile> list= new ArrayList<IFile>();
162 for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
163 Object element= iter.next();
164 if (element instanceof IAdaptable) {
165 IFile file= getCandidate((IAdaptable) element);
166 if (file != null) {
167 list.add(file);
168 }
169 }
170 }
171 return list.toArray(new IFile[list.size()]);
172 }
173}
174