]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/AbstractOpenWizardAction.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / AbstractOpenWizardAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.ui.actions;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.core.runtime.CoreException;
16
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.layout.PixelConverter;
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.wizards.NewElementWizard;
40 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
41
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  * @noextend This class is not intended to be subclassed by clients.
52  */
53 public abstract class AbstractOpenWizardAction extends Action {
54
55         private Shell fShell;
56         private IStructuredSelection fSelection;
57         private IJavaElement fCreatedElement;
58
59         /**
60          * Creates the action.
61          */
62         protected AbstractOpenWizardAction() {
63                 fShell= null;
64                 fSelection= null;
65                 fCreatedElement= null;
66         }
67
68         /* (non-Javadoc)
69          * @see org.eclipse.jface.action.Action#run()
70          */
71         @Override
72         public void run() {
73                 Shell shell= getShell();
74                 if (!doCreateProjectFirstOnEmptyWorkspace(shell)) {
75                         return;
76                 }
77                 try {
78                         INewWizard wizard= createWizard();
79                         wizard.init(PlatformUI.getWorkbench(), getSelection());
80
81                         WizardDialog dialog= new WizardDialog(shell, wizard);
82                         PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
83                         dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
84                         dialog.create();
85                         int res= dialog.open();
86                         if (res == Window.OK && wizard instanceof NewElementWizard) {
87                                 fCreatedElement= ((NewElementWizard)wizard).getCreatedElement();
88                         }
89
90                         notifyResult(res == Window.OK);
91                 } catch (CoreException e) {
92                         String title= NewWizardMessages.AbstractOpenWizardAction_createerror_title;
93                         String message= NewWizardMessages.AbstractOpenWizardAction_createerror_message;
94                         ExceptionHandler.handle(e, shell, title, message);
95                 }
96         }
97
98         /**
99          * Creates and configures the wizard. This method should only be called once.
100          * @return returns the created wizard.
101          * @throws CoreException exception is thrown when the creation was not successful.
102          */
103         abstract protected INewWizard createWizard() throws CoreException;
104
105         /**
106          * Returns the configured selection. If no selection has been configured using {@link #setSelection(IStructuredSelection)},
107          * the currently selected element of the active workbench is returned.
108          * @return the configured selection
109          */
110         protected IStructuredSelection getSelection() {
111                 if (fSelection == null) {
112                         return evaluateCurrentSelection();
113                 }
114                 return fSelection;
115         }
116
117         private IStructuredSelection evaluateCurrentSelection() {
118                 IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
119                 if (window != null) {
120                         ISelection selection= window.getSelectionService().getSelection();
121                         if (selection instanceof IStructuredSelection) {
122                                 return (IStructuredSelection) selection;
123                         }
124                 }
125                 return StructuredSelection.EMPTY;
126         }
127
128         /**
129          * Configures the selection to be used as initial selection of the wizard.
130          * @param selection the selection to be set or <code>null</code> to use the selection of the active workbench window
131          */
132         public void setSelection(IStructuredSelection selection) {
133                 fSelection= selection;
134         }
135
136         /**
137          * Returns the configured shell. If no shell has been configured using {@link #setShell(Shell)},
138          *      the shell of the currently active workbench is returned.
139          * @return the configured shell
140          */
141         protected Shell getShell() {
142                 if (fShell == null) {
143                         return JavaPlugin.getActiveWorkbenchShell();
144                 }
145                 return fShell;
146         }
147
148         /**
149          * Configures the shell to be used as parent shell by the wizard.
150          * @param shell the shell to be set or <code>null</code> to use the shell of the active workbench window
151          */
152         public void setShell(Shell shell) {
153                 fShell= shell;
154         }
155
156         /**
157          * Opens the new project dialog if the workspace is empty. This method is called on {@link #run()}.
158          * @param shell the shell to use
159          * @return returns <code>true</code> when a project has been created, or <code>false</code> when the
160          * new project has been canceled.
161          */
162         protected boolean doCreateProjectFirstOnEmptyWorkspace(Shell shell) {
163                 IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
164                 if (workspaceRoot.getProjects().length == 0) {
165                         String title= NewWizardMessages.AbstractOpenWizardAction_noproject_title;
166                         String message= NewWizardMessages.AbstractOpenWizardAction_noproject_message;
167                         if (MessageDialog.openQuestion(shell, title, message)) {
168                                 new NewProjectAction().run();
169                                 return workspaceRoot.getProjects().length != 0;
170                         }
171                         return false;
172                 }
173                 return true;
174         }
175
176         /**
177          * Returns the created element or <code>null</code> if the wizard has not run or was canceled.
178          * @return the created element or <code>null</code>
179          */
180         public IJavaElement getCreatedElement() {
181                 return fCreatedElement;
182         }
183
184
185 }