001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.Path;
020:
021: import org.eclipse.core.resources.IMarker;
022:
023: import org.eclipse.swt.graphics.Image;
024: import org.eclipse.swt.widgets.Shell;
025:
026: import org.eclipse.jface.operation.IRunnableContext;
027: import org.eclipse.jface.operation.IRunnableWithProgress;
028:
029: import org.eclipse.ui.IMarkerResolution;
030: import org.eclipse.ui.IMarkerResolution2;
031: import org.eclipse.ui.IMarkerResolutionGenerator;
032: import org.eclipse.ui.IMarkerResolutionGenerator2;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.dialogs.PreferencesUtil;
035:
036: import org.eclipse.jdt.core.CorrectionEngine;
037: import org.eclipse.jdt.core.IClasspathEntry;
038: import org.eclipse.jdt.core.IJavaModelMarker;
039: import org.eclipse.jdt.core.IJavaModelStatusConstants;
040: import org.eclipse.jdt.core.IJavaProject;
041: import org.eclipse.jdt.core.JavaCore;
042: import org.eclipse.jdt.core.JavaModelException;
043:
044: import org.eclipse.jdt.internal.corext.util.Messages;
045:
046: import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
047:
048: import org.eclipse.jdt.internal.ui.JavaPlugin;
049: import org.eclipse.jdt.internal.ui.JavaPluginImages;
050: import org.eclipse.jdt.internal.ui.preferences.BuildPathsPropertyPage;
051: import org.eclipse.jdt.internal.ui.preferences.UserLibraryPreferencePage;
052: import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
053: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
054: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
055:
056: public class UserLibraryMarkerResolutionGenerator implements
057: IMarkerResolutionGenerator, IMarkerResolutionGenerator2 {
058:
059: private final static IMarkerResolution[] NO_RESOLUTION = new IMarkerResolution[0];
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.ui.IMarkerResolutionGenerator2#hasResolutions(org.eclipse.core.resources.IMarker)
063: */
064: public boolean hasResolutions(IMarker marker) {
065: int id = marker.getAttribute(IJavaModelMarker.ID, -1);
066: if (id == IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND
067: || id == IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND
068: || id == IJavaModelStatusConstants.INVALID_CP_CONTAINER_ENTRY
069: || id == IJavaModelStatusConstants.DEPRECATED_VARIABLE
070: || id == IJavaModelStatusConstants.INVALID_CLASSPATH) {
071: return true;
072: }
073: return false;
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.ui.IMarkerResolutionGenerator#getResolutions(org.eclipse.core.resources.IMarker)
078: */
079: public IMarkerResolution[] getResolutions(IMarker marker) {
080: final Shell shell = JavaPlugin.getActiveWorkbenchShell();
081: if (!hasResolutions(marker) || shell == null) {
082: return NO_RESOLUTION;
083: }
084:
085: ArrayList resolutions = new ArrayList();
086:
087: final IJavaProject project = getJavaProject(marker);
088:
089: int id = marker.getAttribute(IJavaModelMarker.ID, -1);
090: if (id == IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND) {
091: String[] arguments = CorrectionEngine
092: .getProblemArguments(marker);
093: final IPath path = new Path(arguments[0]);
094:
095: if (path.segment(0).equals(
096: JavaCore.USER_LIBRARY_CONTAINER_ID)) {
097: String label = NewWizardMessages.UserLibraryMarkerResolutionGenerator_changetouserlib_label;
098: Image image = JavaPluginImages
099: .get(JavaPluginImages.IMG_CORRECTION_RENAME);
100: resolutions.add(new UserLibraryMarkerResolution(label,
101: image) {
102: public void run(IMarker m) {
103: changeToExistingLibrary(shell, path, false,
104: project);
105: }
106: });
107: if (path.segmentCount() == 2) {
108: String label2 = Messages
109: .format(
110: NewWizardMessages.UserLibraryMarkerResolutionGenerator_createuserlib_label,
111: path.segment(1));
112: Image image2 = JavaPluginImages
113: .get(JavaPluginImages.IMG_CORRECTION_ADD);
114: resolutions.add(new UserLibraryMarkerResolution(
115: label2, image2) {
116: public void run(IMarker m) {
117: createUserLibrary(shell, path, project);
118: }
119: });
120: }
121: }
122: String label = NewWizardMessages.UserLibraryMarkerResolutionGenerator_changetoother;
123: Image image = JavaPluginImages
124: .get(JavaPluginImages.IMG_CORRECTION_RENAME);
125: resolutions.add(new UserLibraryMarkerResolution(label,
126: image) {
127: public void run(IMarker m) {
128: changeToExistingLibrary(shell, path, true, project);
129: }
130: });
131: }
132:
133: if (project != null) {
134: resolutions.add(new OpenBuildPathMarkerResolution(project));
135: }
136:
137: return (IMarkerResolution[]) resolutions
138: .toArray(new IMarkerResolution[resolutions.size()]);
139: }
140:
141: protected void changeToExistingLibrary(Shell shell, IPath path,
142: boolean isNew, final IJavaProject project) {
143: try {
144: IClasspathEntry[] entries = project.getRawClasspath();
145: int idx = indexOfClasspath(entries, path);
146: if (idx == -1) {
147: return;
148: }
149: IClasspathEntry[] res;
150: if (isNew) {
151: res = BuildPathDialogAccess.chooseContainerEntries(
152: shell, project, entries);
153: if (res == null) {
154: return;
155: }
156: } else {
157: IClasspathEntry resEntry = BuildPathDialogAccess
158: .configureContainerEntry(shell, entries[idx],
159: project, entries);
160: if (resEntry == null) {
161: return;
162: }
163: res = new IClasspathEntry[] { resEntry };
164: }
165: final IClasspathEntry[] newEntries = new IClasspathEntry[entries.length
166: - 1 + res.length];
167: System.arraycopy(entries, 0, newEntries, 0, idx);
168: System.arraycopy(res, 0, newEntries, idx, res.length);
169: System.arraycopy(entries, idx + 1, newEntries, idx
170: + res.length, entries.length - idx - 1);
171:
172: IRunnableContext context = JavaPlugin
173: .getActiveWorkbenchWindow();
174: if (context == null) {
175: context = PlatformUI.getWorkbench()
176: .getProgressService();
177: }
178: context.run(true, true, new IRunnableWithProgress() {
179: public void run(IProgressMonitor monitor)
180: throws InvocationTargetException,
181: InterruptedException {
182: try {
183: project.setRawClasspath(newEntries, project
184: .getOutputLocation(), monitor);
185: } catch (CoreException e) {
186: throw new InvocationTargetException(e);
187: }
188: }
189: });
190: } catch (JavaModelException e) {
191: String title = NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title;
192: String message = NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_creationfailed_message;
193: ExceptionHandler.handle(e, shell, title, message);
194: } catch (InvocationTargetException e) {
195: String title = NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_title;
196: String message = NewWizardMessages.UserLibraryMarkerResolutionGenerator_error_applyingfailed_message;
197: ExceptionHandler.handle(e, shell, title, message);
198: } catch (InterruptedException e) {
199: // user cancelled
200: }
201: }
202:
203: private int indexOfClasspath(IClasspathEntry[] entries, IPath path) {
204: for (int i = 0; i < entries.length; i++) {
205: IClasspathEntry curr = entries[i];
206: if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER
207: && curr.getPath().equals(path)) {
208: return i;
209: }
210: }
211: return -1;
212: }
213:
214: protected void createUserLibrary(final Shell shell,
215: IPath unboundPath, IJavaProject project) {
216: String name = unboundPath.segment(1);
217: String id = UserLibraryPreferencePage.ID;
218: HashMap data = new HashMap(3);
219: data
220: .put(UserLibraryPreferencePage.DATA_LIBRARY_TO_SELECT,
221: name);
222: data
223: .put(UserLibraryPreferencePage.DATA_DO_CREATE,
224: Boolean.TRUE);
225: PreferencesUtil.createPreferenceDialogOn(shell, id,
226: new String[] { id }, data).open();
227: }
228:
229: private IJavaProject getJavaProject(IMarker marker) {
230: return JavaCore.create(marker.getResource().getProject());
231: }
232:
233: /**
234: * Library quick fix base class
235: */
236: private static abstract class UserLibraryMarkerResolution implements
237: IMarkerResolution, IMarkerResolution2 {
238:
239: private String fLabel;
240: private Image fImage;
241:
242: public UserLibraryMarkerResolution(String label, Image image) {
243: fLabel = label;
244: fImage = image;
245: }
246:
247: /* (non-Javadoc)
248: * @see org.eclipse.ui.IMarkerResolution#getLabel()
249: */
250: public String getLabel() {
251: return fLabel;
252: }
253:
254: /* (non-Javadoc)
255: * @see org.eclipse.ui.IMarkerResolution2#getDescription()
256: */
257: public String getDescription() {
258: return null;
259: }
260:
261: /* (non-Javadoc)
262: * @see org.eclipse.ui.IMarkerResolution2#getImage()
263: */
264: public Image getImage() {
265: return fImage;
266: }
267: }
268:
269: private static class OpenBuildPathMarkerResolution implements
270: IMarkerResolution2 {
271: private IJavaProject fProject;
272:
273: public OpenBuildPathMarkerResolution(IJavaProject project) {
274: fProject = project;
275: }
276:
277: public String getDescription() {
278: return Messages
279: .format(
280: CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_description,
281: fProject.getElementName());
282: }
283:
284: public Image getImage() {
285: return JavaPluginImages
286: .get(JavaPluginImages.IMG_OBJS_ACCESSRULES_ATTRIB);
287: }
288:
289: public String getLabel() {
290: return CorrectionMessages.ReorgCorrectionsSubProcessor_configure_buildpath_label;
291: }
292:
293: public void run(IMarker marker) {
294: PreferencesUtil.createPropertyDialogOn(
295: JavaPlugin.getActiveWorkbenchShell(), fProject,
296: BuildPathsPropertyPage.PROP_ID, null, null).open();
297: }
298: }
299:
300: }
|