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.actions;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016:
017: import org.eclipse.jdt.core.IClasspathEntry;
018: import org.eclipse.jdt.core.IJavaProject;
019: import org.eclipse.jdt.core.JavaModelException;
020:
021: import org.eclipse.swt.widgets.Shell;
022:
023: import org.eclipse.jface.action.IAction;
024: import org.eclipse.jface.operation.IRunnableContext;
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.jface.viewers.ISelection;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028:
029: import org.eclipse.ui.IObjectActionDelegate;
030: import org.eclipse.ui.IWorkbenchPart;
031: import org.eclipse.ui.PlatformUI;
032:
033: import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
034: import org.eclipse.jdt.internal.ui.preferences.ClasspathContainerPreferencePage;
035: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
036:
037: import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
038:
039: /**
040: * Action to open a dialog to configure classpath containers. Added as a <code>objectContribution</code>
041: * to {@link ClassPathContainer}.
042: *
043: * @deprecated DO NOT USE this class will be removed soon, it is replaced by {@link ClasspathContainerPreferencePage}
044: */
045: public class ConfigureContainerAction implements IObjectActionDelegate {
046:
047: private ISelection fCurrentSelection;
048: private IWorkbenchPart fPart;
049:
050: /*
051: * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
052: */
053: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
054: fPart = targetPart;
055: }
056:
057: /*
058: * @see IActionDelegate#run(IAction)
059: */
060: public void run(IAction action) {
061: if (fCurrentSelection instanceof IStructuredSelection) {
062: ClassPathContainer container = (ClassPathContainer) ((IStructuredSelection) fCurrentSelection)
063: .getFirstElement();
064: openWizard(container.getClasspathEntry(), container
065: .getLabel(), container.getJavaProject());
066: }
067: }
068:
069: private void openWizard(IClasspathEntry entry, String label,
070: final IJavaProject project) {
071: Shell shell = fPart.getSite().getShell();
072: try {
073: IClasspathEntry[] entries = project.getRawClasspath();
074:
075: IClasspathEntry result = BuildPathDialogAccess
076: .configureContainerEntry(shell, entry, project,
077: entries);
078: if (result == null || result.equals(entry)) {
079: return; // user cancelled or no changes
080: }
081:
082: int idx = indexInClasspath(entries, entry);
083: if (idx == -1) {
084: return;
085: }
086:
087: final IClasspathEntry[] newEntries = new IClasspathEntry[entries.length];
088: System.arraycopy(entries, 0, newEntries, 0, entries.length);
089: newEntries[idx] = result;
090:
091: IRunnableContext context = fPart.getSite()
092: .getWorkbenchWindow();
093: if (context == null) {
094: context = PlatformUI.getWorkbench()
095: .getProgressService();
096: }
097: context.run(true, true, new IRunnableWithProgress() {
098: public void run(IProgressMonitor monitor)
099: throws InvocationTargetException,
100: InterruptedException {
101: try {
102: project.setRawClasspath(newEntries, project
103: .getOutputLocation(), monitor);
104: } catch (CoreException e) {
105: throw new InvocationTargetException(e);
106: }
107: }
108: });
109: } catch (JavaModelException e) {
110: String title = ActionMessages.ConfigureContainerAction_error_title;
111: String message = ActionMessages.ConfigureContainerAction_error_creationfailed_message;
112: ExceptionHandler.handle(e, shell, title, message);
113: } catch (InvocationTargetException e) {
114: String title = ActionMessages.ConfigureContainerAction_error_title;
115: String message = ActionMessages.ConfigureContainerAction_error_applyingfailed_message;
116: ExceptionHandler.handle(e, shell, title, message);
117: } catch (InterruptedException e) {
118: // user cancelled
119: }
120: }
121:
122: protected static int indexInClasspath(IClasspathEntry[] entries,
123: IClasspathEntry entry) {
124: for (int i = 0; i < entries.length; i++) {
125: if (entries[i] == entry) {
126: return i;
127: }
128: }
129: return -1;
130: }
131:
132: /*
133: * @see IActionDelegate#selectionChanged(IAction, ISelection)
134: */
135: public void selectionChanged(IAction action, ISelection selection) {
136: fCurrentSelection = selection;
137: }
138:
139: }
|