KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > AbstractOpenWizardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.ui.actions;
13
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.resources.ResourcesPlugin;
18
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.jface.action.Action;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.resource.JFaceResources;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jface.wizard.WizardDialog;
29
30 import org.eclipse.ui.INewWizard;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.actions.NewProjectAction;
34
35 import org.eclipse.jdt.core.IJavaElement;
36
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
39 import org.eclipse.jdt.internal.ui.util.PixelConverter;
40 import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
41 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
42
43 /**
44  * <p>Abstract base classed used for the open wizard actions.</p>
45  *
46  * <p>
47  * Note: This class is for internal use only. Clients should not use this class.
48  * </p>
49  * @since 3.2
50  */

51 public abstract class AbstractOpenWizardAction extends Action {
52     
53     private Shell fShell;
54     private IStructuredSelection fSelection;
55     private IJavaElement fCreatedElement;
56     
57     /**
58      * Creates the action.
59      */

60     protected AbstractOpenWizardAction() {
61         fShell= null;
62         fSelection= null;
63         fCreatedElement= null;
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.jface.action.Action#run()
68      */

69     public void run() {
70         Shell shell= getShell();
71         if (!doCreateProjectFirstOnEmptyWorkspace(shell)) {
72             return;
73         }
74         try {
75             INewWizard wizard= createWizard();
76             wizard.init(PlatformUI.getWorkbench(), getSelection());
77             
78             WizardDialog dialog= new WizardDialog(shell, wizard);
79             PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
80             dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
81             dialog.create();
82             int res= dialog.open();
83             if (res == Window.OK && wizard instanceof NewElementWizard) {
84                 fCreatedElement= ((NewElementWizard)wizard).getCreatedElement();
85             }
86             
87             notifyResult(res == Window.OK);
88         } catch (CoreException e) {
89             String JavaDoc title= NewWizardMessages.AbstractOpenWizardAction_createerror_title;
90             String JavaDoc message= NewWizardMessages.AbstractOpenWizardAction_createerror_message;
91             ExceptionHandler.handle(e, shell, title, message);
92         }
93     }
94     
95     /**
96      * Creates and configures the wizard. This method should only be called once.
97      * @return returns the created wizard.
98      * @throws CoreException exception is thrown when the creation was not successful.
99      */

100     abstract protected INewWizard createWizard() throws CoreException;
101     
102     /**
103      * Returns the configured selection. If no selection has been configured using {@link #setSelection(IStructuredSelection)},
104      * the currently selected element of the active workbench is returned.
105      * @return the configured selection
106      */

107     protected IStructuredSelection getSelection() {
108         if (fSelection == null) {
109             return evaluateCurrentSelection();
110         }
111         return fSelection;
112     }
113             
114     private IStructuredSelection evaluateCurrentSelection() {
115         IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
116         if (window != null) {
117             ISelection selection= window.getSelectionService().getSelection();
118             if (selection instanceof IStructuredSelection) {
119                 return (IStructuredSelection) selection;
120             }
121         }
122         return StructuredSelection.EMPTY;
123     }
124     
125     /**
126      * Configures the selection to be used as initial selection of the wizard.
127      * @param selection the selection to be set or <code>null</code> to use the selection of the active workbench window
128      */

129     public void setSelection(IStructuredSelection selection) {
130         fSelection= selection;
131     }
132     
133     /**
134      * Returns the configured shell. If no shell has been configured using {@link #setShell(Shell)},
135      * the shell of the currently active workbench is returned.
136      * @return the configured shell
137      */

138     protected Shell getShell() {
139         if (fShell == null) {
140             return JavaPlugin.getActiveWorkbenchShell();
141         }
142         return fShell;
143     }
144     
145     /**
146      * Configures the shell to be used as parent shell by the wizard.
147      * @param shell the shell to be set or <code>null</code> to use the shell of the active workbench window
148      */

149     public void setShell(Shell shell) {
150         fShell= shell;
151     }
152     
153     /**
154      * Opens the new project dialog if the workspace is empty. This method is called on {@link #run()}.
155      * @param shell the shell to use
156      * @return returns <code>true</code> when a project has been created, or <code>false</code> when the
157      * new project has been canceled.
158      */

159     protected boolean doCreateProjectFirstOnEmptyWorkspace(Shell shell) {
160         IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
161         if (workspaceRoot.getProjects().length == 0) {
162             String JavaDoc title= NewWizardMessages.AbstractOpenWizardAction_noproject_title;
163             String JavaDoc message= NewWizardMessages.AbstractOpenWizardAction_noproject_message;
164             if (MessageDialog.openQuestion(shell, title, message)) {
165                 new NewProjectAction().run();
166                 return workspaceRoot.getProjects().length != 0;
167             }
168             return false;
169         }
170         return true;
171     }
172     
173     /**
174      * Returns the created element or <code>null</code> if the wizard has not run or was canceled.
175      * @return the created element or <code>null</code>
176      */

177     public IJavaElement getCreatedElement() {
178         return fCreatedElement;
179     }
180     
181     
182 }
183
Popular Tags