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.ui.internal.actions;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.action.Action;
014: import org.eclipse.jface.dialogs.ErrorDialog;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.StructuredSelection;
018: import org.eclipse.jface.wizard.WizardDialog;
019: import org.eclipse.swt.graphics.Point;
020: import org.eclipse.swt.widgets.Shell;
021: import org.eclipse.ui.IEditorInput;
022: import org.eclipse.ui.IEditorPart;
023: import org.eclipse.ui.INewWizard;
024: import org.eclipse.ui.IPluginContribution;
025: import org.eclipse.ui.IWorkbenchPart;
026: import org.eclipse.ui.IWorkbenchWindow;
027: import org.eclipse.ui.actions.ActionFactory;
028: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
029: import org.eclipse.ui.internal.LegacyResourceSupport;
030: import org.eclipse.ui.internal.WorkbenchMessages;
031: import org.eclipse.ui.internal.util.Util;
032: import org.eclipse.ui.wizards.IWizardDescriptor;
033:
034: /**
035: * Opens a specific new wizard.
036: */
037: public class NewWizardShortcutAction extends Action implements
038: IPluginContribution {
039: private IWizardDescriptor wizardElement;
040:
041: /**
042: * The wizard dialog width
043: */
044: private static final int SIZING_WIZARD_WIDTH = 500;
045:
046: /**
047: * The wizard dialog height
048: */
049: private static final int SIZING_WIZARD_HEIGHT = 500;
050:
051: private IWorkbenchWindow window;
052:
053: /**
054: * Create an instance of this class.
055: *
056: * @param window the workbench window in which this action will appear
057: * @param wizardDesc a wizard element
058: */
059: public NewWizardShortcutAction(IWorkbenchWindow window,
060: IWizardDescriptor wizardDesc) {
061: super (wizardDesc.getLabel());
062: setToolTipText(wizardDesc.getDescription());
063: setImageDescriptor(wizardDesc.getImageDescriptor());
064: setId(ActionFactory.NEW.getId());
065: wizardElement = wizardDesc;
066: this .window = window;
067: }
068:
069: /**
070: * Get the wizard descriptor for this action.
071: *
072: * @return the wizard descriptor
073: */
074: public IWizardDescriptor getWizardDescriptor() {
075: return wizardElement;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.jface.action.IAction#run()
080: */
081: public void run() {
082: // create instance of target wizard
083:
084: INewWizard wizard;
085: try {
086: wizard = (INewWizard) wizardElement.createWizard();
087: } catch (CoreException e) {
088: ErrorDialog
089: .openError(
090: window.getShell(),
091: WorkbenchMessages.NewWizardShortcutAction_errorTitle,
092: WorkbenchMessages.NewWizardShortcutAction_errorMessage,
093: e.getStatus());
094: return;
095: }
096:
097: ISelection selection = window.getSelectionService()
098: .getSelection();
099: IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
100: if (selection instanceof IStructuredSelection) {
101: selectionToPass = wizardElement
102: .adaptedSelection((IStructuredSelection) selection);
103: } else {
104: // Build the selection from the IFile of the editor
105: IWorkbenchPart part = window.getPartService()
106: .getActivePart();
107: if (part instanceof IEditorPart) {
108: IEditorInput input = ((IEditorPart) part)
109: .getEditorInput();
110: Class fileClass = LegacyResourceSupport.getFileClass();
111: if (input != null && fileClass != null) {
112: Object file = Util.getAdapter(input, fileClass);
113: if (file != null) {
114: selectionToPass = new StructuredSelection(file);
115: }
116: }
117: }
118: }
119:
120: // even tho we MAY finish early without showing a dialog, prep the
121: // wizard with a dialog and such in case it's logic requires it
122: // - yes, it wastes a dialog but they are plentiful...
123: wizard.init(window.getWorkbench(), selectionToPass);
124:
125: Shell parent = window.getShell();
126: WizardDialog dialog = new WizardDialog(parent, wizard);
127: dialog.create();
128: Point defaultSize = dialog.getShell().getSize();
129: dialog.getShell().setSize(
130: Math.max(SIZING_WIZARD_WIDTH, defaultSize.x),
131: Math.max(SIZING_WIZARD_HEIGHT, defaultSize.y));
132: window.getWorkbench().getHelpSystem().setHelp(
133: dialog.getShell(),
134: IWorkbenchHelpContextIds.NEW_WIZARD_SHORTCUT);
135:
136: // if the wizard can finish early and doesn't have any pages, just finish it.
137: if (wizardElement.canFinishEarly() && !wizardElement.hasPages()) {
138: wizard.performFinish();
139: dialog.close();
140: } else {
141: dialog.open();
142: }
143: }
144:
145: /* (non-Javadoc)
146: * @see org.eclipse.ui.IPluginContribution#getLocalId()
147: */
148: public String getLocalId() {
149: IPluginContribution contribution = getPluginContribution();
150: if (contribution != null) {
151: return contribution.getLocalId();
152: }
153: return wizardElement.getId();
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.ui.IPluginContribution#getPluginId()
158: */
159: public String getPluginId() {
160: IPluginContribution contribution = getPluginContribution();
161: if (contribution != null) {
162: return contribution.getPluginId();
163: }
164: return null;
165: }
166:
167: /**
168: * Return the plugin contribution associated with the wizard.
169: *
170: * @return the contribution or <code>null</code>
171: * @since 3.1
172: */
173: private IPluginContribution getPluginContribution() {
174: return (IPluginContribution) Util.getAdapter(wizardElement,
175: IPluginContribution.class);
176: }
177: }
|