001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ui.actions;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.dialogs.IDialogSettings;
014: import org.eclipse.jface.viewers.ISelection;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.StructuredSelection;
017: import org.eclipse.jface.wizard.WizardDialog;
018: import org.eclipse.swt.widgets.Shell;
019: import org.eclipse.ui.IEditorInput;
020: import org.eclipse.ui.IEditorPart;
021: import org.eclipse.ui.ISharedImages;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.IWorkbenchWindow;
024: import org.eclipse.ui.PlatformUI;
025: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
026: import org.eclipse.ui.internal.LegacyResourceSupport;
027: import org.eclipse.ui.internal.PerspectiveTracker;
028: import org.eclipse.ui.internal.WorkbenchMessages;
029: import org.eclipse.ui.internal.WorkbenchPlugin;
030: import org.eclipse.ui.internal.dialogs.NewWizard;
031: import org.eclipse.ui.internal.util.Util;
032:
033: /**
034: * Invoke the resource creation wizard selection Wizard.
035: * <p>
036: * This class may be instantiated; it is not intended to be subclassed.
037: * </p>
038: * <p>
039: * This method automatically registers listeners so that it can keep its
040: * enablement state up to date. Ordinarily, the window's references to these
041: * listeners will be dropped automatically when the window closes. However,
042: * if the client needs to get rid of an action while the window is still open,
043: * the client must call #dispose() to give the
044: * action an opportunity to deregister its listeners and to perform any other
045: * cleanup.
046: * </p>
047: */
048: public class NewWizardAction extends Action implements
049: ActionFactory.IWorkbenchAction {
050:
051: /**
052: * The wizard dialog width
053: */
054: private static final int SIZING_WIZARD_WIDTH = 500;
055:
056: /**
057: * The wizard dialog height
058: */
059: private static final int SIZING_WIZARD_HEIGHT = 500;
060:
061: /**
062: * The id of the category to show or <code>null</code> to
063: * show all the categories.
064: */
065: private String categoryId = null;
066:
067: /**
068: * The workbench window; or <code>null</code> if this
069: * action has been <code>dispose</code>d.
070: */
071: private IWorkbenchWindow workbenchWindow;
072:
073: /**
074: * Tracks perspective activation, to update this action's
075: * enabled state.
076: */
077: private PerspectiveTracker tracker;
078:
079: /**
080: * Create a new instance of this class.
081: * @param window
082: */
083: public NewWizardAction(IWorkbenchWindow window) {
084: super (WorkbenchMessages.NewWizardAction_text);
085: if (window == null) {
086: throw new IllegalArgumentException();
087: }
088: this .workbenchWindow = window;
089: tracker = new PerspectiveTracker(window, this );
090: // @issues should be IDE-specific images
091: ISharedImages images = PlatformUI.getWorkbench()
092: .getSharedImages();
093: setImageDescriptor(images
094: .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
095: setDisabledImageDescriptor(images
096: .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED));
097: setToolTipText(WorkbenchMessages.NewWizardAction_toolTip);
098: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
099: IWorkbenchHelpContextIds.NEW_ACTION);
100: }
101:
102: /**
103: * Create a new instance of this class
104: *
105: * @deprecated use the constructor <code>NewWizardAction(IWorkbenchWindow)</code>
106: */
107: public NewWizardAction() {
108: this (PlatformUI.getWorkbench().getActiveWorkbenchWindow());
109: }
110:
111: /**
112: * Returns the id of the category of wizards to show
113: * or <code>null</code> to show all categories.
114: * @return String
115: */
116: public String getCategoryId() {
117: return categoryId;
118: }
119:
120: /**
121: * Sets the id of the category of wizards to show
122: * or <code>null</code> to show all categories.
123: * @param id
124: */
125: public void setCategoryId(String id) {
126: categoryId = id;
127: }
128:
129: /* (non-Javadoc)
130: * Method declared on IAction.
131: */
132: public void run() {
133: if (workbenchWindow == null) {
134: // action has been disposed
135: return;
136: }
137: NewWizard wizard = new NewWizard();
138: wizard.setCategoryId(categoryId);
139:
140: ISelection selection = workbenchWindow.getSelectionService()
141: .getSelection();
142: IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
143: if (selection instanceof IStructuredSelection) {
144: selectionToPass = (IStructuredSelection) selection;
145: } else {
146: // @issue the following is resource-specific legacy code
147: // Build the selection from the IFile of the editor
148: Class resourceClass = LegacyResourceSupport
149: .getResourceClass();
150: if (resourceClass != null) {
151: IWorkbenchPart part = workbenchWindow.getPartService()
152: .getActivePart();
153: if (part instanceof IEditorPart) {
154: IEditorInput input = ((IEditorPart) part)
155: .getEditorInput();
156: Object resource = Util.getAdapter(input,
157: resourceClass);
158: if (resource != null) {
159: selectionToPass = new StructuredSelection(
160: resource);
161: }
162: }
163: }
164: }
165:
166: wizard.init(workbenchWindow.getWorkbench(), selectionToPass);
167: IDialogSettings workbenchSettings = WorkbenchPlugin
168: .getDefault().getDialogSettings();
169: IDialogSettings wizardSettings = workbenchSettings
170: .getSection("NewWizardAction"); //$NON-NLS-1$
171: if (wizardSettings == null) {
172: wizardSettings = workbenchSettings
173: .addNewSection("NewWizardAction"); //$NON-NLS-1$
174: }
175: wizard.setDialogSettings(wizardSettings);
176: wizard.setForcePreviousAndNextButtons(true);
177:
178: Shell parent = workbenchWindow.getShell();
179: WizardDialog dialog = new WizardDialog(parent, wizard);
180: dialog.create();
181: dialog.getShell().setSize(
182: Math.max(SIZING_WIZARD_WIDTH, dialog.getShell()
183: .getSize().x), SIZING_WIZARD_HEIGHT);
184: PlatformUI.getWorkbench().getHelpSystem().setHelp(
185: dialog.getShell(), IWorkbenchHelpContextIds.NEW_WIZARD);
186: dialog.open();
187: }
188:
189: /* (non-Javadoc)
190: * Method declared on ActionFactory.IWorkbenchAction.
191: * @since 3.0
192: */
193: public void dispose() {
194: if (workbenchWindow == null) {
195: // action has already been disposed
196: return;
197: }
198: tracker.dispose();
199: workbenchWindow = null;
200: }
201:
202: }
|