KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > BuildpathModifierAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IStatus;
19
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionChangedListener;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StructuredSelection;
31
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.IWorkbenchPartReference;
35 import org.eclipse.ui.IWorkbenchSite;
36 import org.eclipse.ui.part.ISetSelectionTarget;
37
38 import org.eclipse.ui.forms.widgets.FormText;
39
40 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
41 import org.eclipse.jdt.internal.corext.buildpath.IBuildpathModifierListener;
42
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44
45 public abstract class BuildpathModifierAction extends Action implements ISelectionChangedListener {
46     
47     public static final int ADD_SEL_SF_TO_BP= 0;
48     public static final int REMOVE_FROM_BP= 1;
49     public static final int EXCLUDE= 2;
50     public static final int UNEXCLUDE= 3;
51     public static final int EDIT_FILTERS= 4;
52     public static final int CREATE_LINK= 5;
53     public static final int RESET_ALL= 6;
54     public static final int EDIT_OUTPUT= 7;
55     public static final int CREATE_OUTPUT= 8;
56     public static final int RESET= 9;
57     public static final int INCLUDE= 10;
58     public static final int UNINCLUDE= 11;
59     public static final int CREATE_FOLDER= 12;
60     public static final int ADD_JAR_TO_BP= 13;
61     public static final int ADD_LIB_TO_BP= 14;
62     public static final int ADD_SEL_LIB_TO_BP= 15;
63     public static final int CONFIGURE_BUILD_PATH= 16;
64     public static final int RESET_ALL_OUTPUT_FOLDERS= 17;
65     public static final int DROP_DOWN_ACTION= 18;
66
67     private final IWorkbenchSite fSite;
68     private final List JavaDoc fSelectedElements;
69     private final ISetSelectionTarget fSelectionTarget;
70     private final List JavaDoc fListeners;
71
72     public BuildpathModifierAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, int id) {
73         this(site, selectionTarget, id, IAction.AS_PUSH_BUTTON);
74     }
75     
76     public BuildpathModifierAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, int id, int style) {
77         super("", style); //$NON-NLS-1$
78

79         fSite= site;
80         fSelectionTarget= selectionTarget;
81         fSelectedElements= new ArrayList JavaDoc();
82         fListeners= new ArrayList JavaDoc();
83         
84         setId(Integer.toString(id));
85     }
86
87     /**
88      * A detailed description usable for a {@link FormText}
89      * depending on the current selection, or <code>null</code>
90      * if <code>!enabled()</code>
91      *
92      * @return A detailed description or null if <code>!enabled()</code>
93      */

94     public abstract String JavaDoc getDetailedDescription();
95     
96     /**
97      * {@inheritDoc}
98      */

99     public void selectionChanged(final SelectionChangedEvent event) {
100         final ISelection selection = event.getSelection();
101         if (selection instanceof IStructuredSelection) {
102             setEnabled(canHandle((IStructuredSelection) selection));
103             fSelectedElements.clear();
104             fSelectedElements.addAll(((IStructuredSelection)selection).toList());
105         } else {
106             setEnabled(canHandle(StructuredSelection.EMPTY));
107             fSelectedElements.clear();
108         }
109     }
110
111     protected abstract boolean canHandle(IStructuredSelection elements);
112     
113     protected List JavaDoc getSelectedElements() {
114         return fSelectedElements;
115     }
116     
117     public void addBuildpathModifierListener(IBuildpathModifierListener listener) {
118         fListeners.add(listener);
119     }
120     
121     public void removeBuildpathModifierListener(IBuildpathModifierListener listener) {
122         fListeners.remove(listener);
123     }
124
125     protected void informListeners(BuildpathDelta delta) {
126         for (Iterator JavaDoc iterator= fListeners.iterator(); iterator.hasNext();) {
127             ((IBuildpathModifierListener)iterator.next()).buildpathChanged(delta);
128         }
129     }
130     
131     protected Shell getShell() {
132         if (fSite == null)
133             return JavaPlugin.getActiveWorkbenchShell();
134         
135         return fSite.getShell() != null ? fSite.getShell() : JavaPlugin.getActiveWorkbenchShell();
136     }
137     
138     protected void showExceptionDialog(CoreException exception, String JavaDoc title) {
139         showError(exception, getShell(), title, exception.getMessage());
140     }
141     
142     protected void showError(CoreException e, Shell shell, String JavaDoc title, String JavaDoc message) {
143         IStatus status= e.getStatus();
144         if (status != null) {
145             ErrorDialog.openError(shell, message, title, status);
146         } else {
147             MessageDialog.openError(shell, title, message);
148         }
149     }
150     
151     protected void selectAndReveal(final ISelection selection) {
152         if (fSelectionTarget != null)
153             fSelectionTarget.selectReveal(selection);
154         
155         if (fSite == null)
156             return;
157             
158         // validate the input
159
IWorkbenchPage page= fSite.getPage();
160         if (page == null)
161             return;
162
163         // get all the view and editor parts
164
List JavaDoc parts= new ArrayList JavaDoc();
165         IWorkbenchPartReference refs[]= page.getViewReferences();
166         for (int i= 0; i < refs.length; i++) {
167             IWorkbenchPart part= refs[i].getPart(false);
168             if (part != null)
169                 parts.add(part);
170         }
171         refs= page.getEditorReferences();
172         for (int i= 0; i < refs.length; i++) {
173             if (refs[i].getPart(false) != null)
174                 parts.add(refs[i].getPart(false));
175         }
176
177         Iterator JavaDoc itr= parts.iterator();
178         while (itr.hasNext()) {
179             IWorkbenchPart part= (IWorkbenchPart) itr.next();
180
181             // get the part's ISetSelectionTarget implementation
182
ISetSelectionTarget target= null;
183             if (part instanceof ISetSelectionTarget)
184                 target= (ISetSelectionTarget) part;
185             else
186                 target= (ISetSelectionTarget) part.getAdapter(ISetSelectionTarget.class);
187
188             if (target != null) {
189                 // select and reveal resource
190
final ISetSelectionTarget finalTarget= target;
191                 page.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
192                     public void run() {
193                         finalTarget.selectReveal(selection);
194                     }
195                 });
196             }
197         }
198     }
199 }
Popular Tags