001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.actions;
011:
012: import org.eclipse.core.runtime.CoreException;
013:
014: import org.eclipse.core.resources.IWorkspaceRoot;
015: import org.eclipse.core.resources.ResourcesPlugin;
016:
017: import org.eclipse.swt.widgets.Shell;
018:
019: import org.eclipse.jface.action.Action;
020: import org.eclipse.jface.dialogs.MessageDialog;
021: import org.eclipse.jface.resource.JFaceResources;
022: import org.eclipse.jface.viewers.ISelection;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.jface.window.Window;
026: import org.eclipse.jface.wizard.WizardDialog;
027:
028: import org.eclipse.ui.INewWizard;
029: import org.eclipse.ui.IWorkbenchWindow;
030: import org.eclipse.ui.PlatformUI;
031: import org.eclipse.ui.actions.NewProjectAction;
032:
033: import org.eclipse.jdt.core.IJavaElement;
034:
035: import org.eclipse.jdt.internal.ui.JavaPlugin;
036: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
037: import org.eclipse.jdt.internal.ui.util.PixelConverter;
038: import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
039: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
040:
041: /**
042: * <p>Abstract base classed used for the open wizard actions.</p>
043: *
044: * <p>
045: * Note: This class is for internal use only. Clients should not use this class.
046: * </p>
047: * @since 3.2
048: */
049: public abstract class AbstractOpenWizardAction extends Action {
050:
051: private Shell fShell;
052: private IStructuredSelection fSelection;
053: private IJavaElement fCreatedElement;
054:
055: /**
056: * Creates the action.
057: */
058: protected AbstractOpenWizardAction() {
059: fShell = null;
060: fSelection = null;
061: fCreatedElement = null;
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.jface.action.Action#run()
066: */
067: public void run() {
068: Shell shell = getShell();
069: if (!doCreateProjectFirstOnEmptyWorkspace(shell)) {
070: return;
071: }
072: try {
073: INewWizard wizard = createWizard();
074: wizard.init(PlatformUI.getWorkbench(), getSelection());
075:
076: WizardDialog dialog = new WizardDialog(shell, wizard);
077: PixelConverter converter = new PixelConverter(
078: JFaceResources.getDialogFont());
079: dialog.setMinimumPageSize(converter
080: .convertWidthInCharsToPixels(70), converter
081: .convertHeightInCharsToPixels(20));
082: dialog.create();
083: int res = dialog.open();
084: if (res == Window.OK && wizard instanceof NewElementWizard) {
085: fCreatedElement = ((NewElementWizard) wizard)
086: .getCreatedElement();
087: }
088:
089: notifyResult(res == Window.OK);
090: } catch (CoreException e) {
091: String title = NewWizardMessages.AbstractOpenWizardAction_createerror_title;
092: String message = NewWizardMessages.AbstractOpenWizardAction_createerror_message;
093: ExceptionHandler.handle(e, shell, title, message);
094: }
095: }
096:
097: /**
098: * Creates and configures the wizard. This method should only be called once.
099: * @return returns the created wizard.
100: * @throws CoreException exception is thrown when the creation was not successful.
101: */
102: abstract protected INewWizard createWizard() throws CoreException;
103:
104: /**
105: * Returns the configured selection. If no selection has been configured using {@link #setSelection(IStructuredSelection)},
106: * the currently selected element of the active workbench is returned.
107: * @return the configured selection
108: */
109: protected IStructuredSelection getSelection() {
110: if (fSelection == null) {
111: return evaluateCurrentSelection();
112: }
113: return fSelection;
114: }
115:
116: private IStructuredSelection evaluateCurrentSelection() {
117: IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow();
118: if (window != null) {
119: ISelection selection = window.getSelectionService()
120: .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()
164: .getRoot();
165: if (workspaceRoot.getProjects().length == 0) {
166: String title = NewWizardMessages.AbstractOpenWizardAction_noproject_title;
167: String message = NewWizardMessages.AbstractOpenWizardAction_noproject_message;
168: if (MessageDialog.openQuestion(shell, title, message)) {
169: new NewProjectAction().run();
170: return workspaceRoot.getProjects().length != 0;
171: }
172: return false;
173: }
174: return true;
175: }
176:
177: /**
178: * Returns the created element or <code>null</code> if the wizard has not run or was canceled.
179: * @return the created element or <code>null</code>
180: */
181: public IJavaElement getCreatedElement() {
182: return fCreatedElement;
183: }
184:
185: }
|