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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
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;
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;
35 import org.eclipse.jdt.core.IJavaElement;
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;
44 * <p>Abstract base classed used for the open wizard actions.</p>
47 * Note: This class is for internal use only. Clients should not use this class.
51 * @noextend This class is not intended to be subclassed by clients.
53 public abstract class AbstractOpenWizardAction extends Action {
56 private IStructuredSelection fSelection;
57 private IJavaElement fCreatedElement;
62 protected AbstractOpenWizardAction() {
65 fCreatedElement= null;
69 * @see org.eclipse.jface.action.Action#run()
73 Shell shell= getShell();
74 if (!doCreateProjectFirstOnEmptyWorkspace(shell)) {
78 INewWizard wizard= createWizard();
79 wizard.init(PlatformUI.getWorkbench(), getSelection());
81 WizardDialog dialog= new WizardDialog(shell, wizard);
82 PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
83 dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
85 int res= dialog.open();
86 if (res == Window.OK && wizard instanceof NewElementWizard) {
87 fCreatedElement= ((NewElementWizard)wizard).getCreatedElement();
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);
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.
103 abstract protected INewWizard createWizard() throws CoreException;
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
110 protected IStructuredSelection getSelection() {
111 if (fSelection == null) {
112 return evaluateCurrentSelection();
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;
125 return StructuredSelection.EMPTY;
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
132 public void setSelection(IStructuredSelection selection) {
133 fSelection= selection;
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
141 protected Shell getShell() {
142 if (fShell == null) {
143 return JavaPlugin.getActiveWorkbenchShell();
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
152 public void setShell(Shell shell) {
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.
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;
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>
180 public IJavaElement getCreatedElement() {
181 return fCreatedElement;