KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > AbstractOpenWizardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.jface.wizard.Wizard;
24 import org.eclipse.jface.wizard.WizardDialog;
25
26 import org.eclipse.core.resources.IWorkspace;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.CoreException;
29
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
33 import org.eclipse.ui.IWorkbenchWizard;
34 import org.eclipse.ui.actions.NewProjectAction;
35
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IPackageFragmentRoot;
38 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41 import org.eclipse.jdt.internal.ui.util.PixelConverter;
42
43 public abstract class AbstractOpenWizardAction extends Action implements IWorkbenchWindowActionDelegate {
44
45     private Class JavaDoc[] fActivatedOnTypes;
46     private boolean fAcceptEmptySelection;
47     private boolean fNoChecking;
48     
49     /**
50      * Creates a AbstractOpenWizardAction.
51      * @param label The label of the action
52      * @param acceptEmptySelection Specifies if the action allows an empty selection
53      */

54     public AbstractOpenWizardAction(String JavaDoc label, boolean acceptEmptySelection) {
55         this(label, null, acceptEmptySelection);
56     }
57
58     /**
59      * Creates a AbstractOpenWizardAction.
60      * @param label The label of the action
61      * @param activatedOnTypes The action is only enabled when all objects in the selection
62      * are of the given types. <code>null</code> will allow all types.
63      * @param acceptEmptySelection Specifies if the action allows an empty selection
64      */

65     public AbstractOpenWizardAction(String JavaDoc label, Class JavaDoc[] activatedOnTypes, boolean acceptEmptySelection) {
66         super(label);
67         fActivatedOnTypes= activatedOnTypes;
68         fAcceptEmptySelection= acceptEmptySelection;
69         fNoChecking= false;
70     }
71
72     /**
73      * Creates a AbstractOpenWizardAction with no restrictions on types, and does allow
74      * an empty selection.
75      */

76     protected AbstractOpenWizardAction() {
77         fActivatedOnTypes= null;
78         fAcceptEmptySelection= true;
79         fNoChecking= true;
80     }
81     
82     protected IWorkbench getWorkbench() {
83         return JavaPlugin.getDefault().getWorkbench();
84     }
85     
86     private boolean isOfAcceptedType(Object JavaDoc obj) {
87         if (fActivatedOnTypes != null) {
88             for (int i= 0; i < fActivatedOnTypes.length; i++) {
89                 if (fActivatedOnTypes[i].isInstance(obj)) {
90                     return true;
91                 }
92             }
93             return false;
94         }
95         return true;
96     }
97     
98     
99     private boolean isEnabled(IStructuredSelection selection) {
100         Iterator JavaDoc iter= selection.iterator();
101         while (iter.hasNext()) {
102             Object JavaDoc obj= iter.next();
103             if (!isOfAcceptedType(obj) || !shouldAcceptElement(obj)) {
104                 return false;
105             }
106         }
107         return true;
108     }
109     
110     /**
111      * Can be overridden to add more checks.
112      * obj is guaranteed to be instance of one of the accepted types
113      */

114     protected boolean shouldAcceptElement(Object JavaDoc obj) {
115         return true;
116     }
117         
118     /**
119      * Creates the specific wizard.
120      * (to be implemented by a subclass)
121      */

122     abstract protected Wizard createWizard() throws CoreException;
123
124
125     protected IStructuredSelection getCurrentSelection() {
126         IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
127         if (window != null) {
128             ISelection selection= window.getSelectionService().getSelection();
129             if (selection instanceof IStructuredSelection) {
130                 return (IStructuredSelection) selection;
131             }
132             
133         }
134         return null;
135     }
136
137     /**
138      * The user has invoked this action.
139      */

140     public void run() {
141         if (!fNoChecking && !canActionBeAdded()) {
142             return;
143         }
144         if (!checkWorkspaceNotEmpty()) {
145             return;
146         }
147         Shell shell= JavaPlugin.getActiveWorkbenchShell();
148         try {
149             Wizard wizard= createWizard();
150             if (wizard instanceof IWorkbenchWizard) {
151                 ((IWorkbenchWizard)wizard).init(getWorkbench(), getCurrentSelection());
152             }
153             
154             WizardDialog dialog= new WizardDialog(shell, wizard);
155             PixelConverter converter= new PixelConverter(JavaPlugin.getActiveWorkbenchShell());
156             
157             dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
158             dialog.create();
159             int res= dialog.open();
160             
161             notifyResult(res == Window.OK);
162         } catch (CoreException e) {
163             String JavaDoc title= NewWizardMessages.AbstractOpenWizardAction_createerror_title;
164             String JavaDoc message= NewWizardMessages.AbstractOpenWizardAction_createerror_message;
165             ExceptionHandler.handle(e, shell, title, message);
166         }
167     }
168     
169     /**
170      * Tests if the action can be run on the current selection.
171      */

172     public boolean canActionBeAdded() {
173         IStructuredSelection selection= getCurrentSelection();
174         if (selection == null || selection.isEmpty()) {
175             return fAcceptEmptySelection;
176         }
177         return isEnabled(selection);
178     }
179
180     /*
181      * @see IActionDelegate#run(IAction)
182      */

183     public void run(IAction action) {
184         run();
185     }
186
187     /*
188      * @see IWorkbenchWindowActionDelegate#dispose()
189      */

190     public void dispose() {
191     }
192
193     /*
194      * @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
195      */

196     public void init(IWorkbenchWindow window) {
197     }
198
199     /*
200      * @see IActionDelegate#selectionChanged(IAction, ISelection)
201      */

202     public void selectionChanged(IAction action, ISelection selection) {
203         // selection taken from selectionprovider
204
}
205     
206     protected boolean checkWorkspaceNotEmpty() {
207         IWorkspace workspace= ResourcesPlugin.getWorkspace();
208         if (workspace.getRoot().getProjects().length == 0) {
209             Shell shell= JavaPlugin.getActiveWorkbenchShell();
210             String JavaDoc title= NewWizardMessages.AbstractOpenWizardAction_noproject_title;
211             String JavaDoc message= NewWizardMessages.AbstractOpenWizardAction_noproject_message;
212             if (MessageDialog.openQuestion(shell, title, message)) {
213                 IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
214                 (new NewProjectAction(window)).run();
215                 return workspace.getRoot().getProjects().length != 0;
216             }
217             return false;
218         }
219         return true;
220     }
221     
222     protected static boolean isOnBuildPath(Object JavaDoc obj) {
223         if (obj instanceof IJavaElement) {
224             IJavaElement elem= (IJavaElement)obj;
225             return elem.getJavaProject().isOnClasspath(elem);
226         }
227         return false;
228     }
229     
230     protected static boolean isInArchive(Object JavaDoc obj) {
231         if (obj instanceof IJavaElement) {
232             IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot((IJavaElement)obj);
233             return (root != null) && root.isArchive();
234         }
235         return false;
236     }
237 }
238
Popular Tags