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.wizards.buildpaths.newsourcepage;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IStatus;
018:
019: import org.eclipse.swt.widgets.Shell;
020:
021: import org.eclipse.jface.action.Action;
022: import org.eclipse.jface.action.IAction;
023: import org.eclipse.jface.dialogs.ErrorDialog;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.jface.viewers.ISelection;
026: import org.eclipse.jface.viewers.ISelectionChangedListener;
027: import org.eclipse.jface.viewers.IStructuredSelection;
028: import org.eclipse.jface.viewers.SelectionChangedEvent;
029: import org.eclipse.jface.viewers.StructuredSelection;
030:
031: import org.eclipse.ui.IWorkbenchPage;
032: import org.eclipse.ui.IWorkbenchPart;
033: import org.eclipse.ui.IWorkbenchPartReference;
034: import org.eclipse.ui.IWorkbenchSite;
035: import org.eclipse.ui.part.ISetSelectionTarget;
036:
037: import org.eclipse.ui.forms.widgets.FormText;
038:
039: import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
040: import org.eclipse.jdt.internal.corext.buildpath.IBuildpathModifierListener;
041:
042: import org.eclipse.jdt.internal.ui.JavaPlugin;
043:
044: public abstract class BuildpathModifierAction extends Action implements
045: ISelectionChangedListener {
046:
047: public static final int ADD_SEL_SF_TO_BP = 0;
048: public static final int REMOVE_FROM_BP = 1;
049: public static final int EXCLUDE = 2;
050: public static final int UNEXCLUDE = 3;
051: public static final int EDIT_FILTERS = 4;
052: public static final int CREATE_LINK = 5;
053: public static final int RESET_ALL = 6;
054: public static final int EDIT_OUTPUT = 7;
055: public static final int CREATE_OUTPUT = 8;
056: public static final int RESET = 9;
057: public static final int INCLUDE = 10;
058: public static final int UNINCLUDE = 11;
059: public static final int CREATE_FOLDER = 12;
060: public static final int ADD_JAR_TO_BP = 13;
061: public static final int ADD_LIB_TO_BP = 14;
062: public static final int ADD_SEL_LIB_TO_BP = 15;
063: public static final int CONFIGURE_BUILD_PATH = 16;
064: public static final int RESET_ALL_OUTPUT_FOLDERS = 17;
065: public static final int DROP_DOWN_ACTION = 18;
066:
067: private final IWorkbenchSite fSite;
068: private final List fSelectedElements;
069: private final ISetSelectionTarget fSelectionTarget;
070: private final List fListeners;
071:
072: public BuildpathModifierAction(IWorkbenchSite site,
073: ISetSelectionTarget selectionTarget, int id) {
074: this (site, selectionTarget, id, IAction.AS_PUSH_BUTTON);
075: }
076:
077: public BuildpathModifierAction(IWorkbenchSite site,
078: ISetSelectionTarget selectionTarget, int id, int style) {
079: super ("", style); //$NON-NLS-1$
080:
081: fSite = site;
082: fSelectionTarget = selectionTarget;
083: fSelectedElements = new ArrayList();
084: fListeners = new ArrayList();
085:
086: setId(Integer.toString(id));
087: }
088:
089: /**
090: * A detailed description usable for a {@link FormText}
091: * depending on the current selection, or <code>null</code>
092: * if <code>!enabled()</code>
093: *
094: * @return A detailed description or null if <code>!enabled()</code>
095: */
096: public abstract String getDetailedDescription();
097:
098: /**
099: * {@inheritDoc}
100: */
101: public void selectionChanged(final SelectionChangedEvent event) {
102: final ISelection selection = event.getSelection();
103: if (selection instanceof IStructuredSelection) {
104: setEnabled(canHandle((IStructuredSelection) selection));
105: fSelectedElements.clear();
106: fSelectedElements.addAll(((IStructuredSelection) selection)
107: .toList());
108: } else {
109: setEnabled(canHandle(StructuredSelection.EMPTY));
110: fSelectedElements.clear();
111: }
112: }
113:
114: protected abstract boolean canHandle(IStructuredSelection elements);
115:
116: protected List getSelectedElements() {
117: return fSelectedElements;
118: }
119:
120: public void addBuildpathModifierListener(
121: IBuildpathModifierListener listener) {
122: fListeners.add(listener);
123: }
124:
125: public void removeBuildpathModifierListener(
126: IBuildpathModifierListener listener) {
127: fListeners.remove(listener);
128: }
129:
130: protected void informListeners(BuildpathDelta delta) {
131: for (Iterator iterator = fListeners.iterator(); iterator
132: .hasNext();) {
133: ((IBuildpathModifierListener) iterator.next())
134: .buildpathChanged(delta);
135: }
136: }
137:
138: protected Shell getShell() {
139: if (fSite == null)
140: return JavaPlugin.getActiveWorkbenchShell();
141:
142: return fSite.getShell() != null ? fSite.getShell() : JavaPlugin
143: .getActiveWorkbenchShell();
144: }
145:
146: protected void showExceptionDialog(CoreException exception,
147: String title) {
148: showError(exception, getShell(), title, exception.getMessage());
149: }
150:
151: protected void showError(CoreException e, Shell shell,
152: String title, String message) {
153: IStatus status = e.getStatus();
154: if (status != null) {
155: ErrorDialog.openError(shell, message, title, status);
156: } else {
157: MessageDialog.openError(shell, title, message);
158: }
159: }
160:
161: protected void selectAndReveal(final ISelection selection) {
162: if (fSelectionTarget != null)
163: fSelectionTarget.selectReveal(selection);
164:
165: if (fSite == null)
166: return;
167:
168: // validate the input
169: IWorkbenchPage page = fSite.getPage();
170: if (page == null)
171: return;
172:
173: // get all the view and editor parts
174: List parts = new ArrayList();
175: IWorkbenchPartReference refs[] = page.getViewReferences();
176: for (int i = 0; i < refs.length; i++) {
177: IWorkbenchPart part = refs[i].getPart(false);
178: if (part != null)
179: parts.add(part);
180: }
181: refs = page.getEditorReferences();
182: for (int i = 0; i < refs.length; i++) {
183: if (refs[i].getPart(false) != null)
184: parts.add(refs[i].getPart(false));
185: }
186:
187: Iterator itr = parts.iterator();
188: while (itr.hasNext()) {
189: IWorkbenchPart part = (IWorkbenchPart) itr.next();
190:
191: // get the part's ISetSelectionTarget implementation
192: ISetSelectionTarget target = null;
193: if (part instanceof ISetSelectionTarget)
194: target = (ISetSelectionTarget) part;
195: else
196: target = (ISetSelectionTarget) part
197: .getAdapter(ISetSelectionTarget.class);
198:
199: if (target != null) {
200: // select and reveal resource
201: final ISetSelectionTarget finalTarget = target;
202: page.getWorkbenchWindow().getShell().getDisplay()
203: .asyncExec(new Runnable() {
204: public void run() {
205: finalTarget.selectReveal(selection);
206: }
207: });
208: }
209: }
210: }
211: }
|