KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > NewExampleAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.dialogs.IDialogSettings;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.StructuredSelection;
18 import org.eclipse.jface.wizard.WizardDialog;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.ISharedImages;
21 import org.eclipse.ui.IWorkbench;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.internal.dialogs.NewWizard;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
27 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
28 import org.eclipse.ui.internal.registry.WizardsRegistryReader;
29
30 /**
31  * Standard action for launching the create project selection
32  * wizard.
33  * <p>
34  * This class may be instantiated; it is not intended to be subclassed.
35  * </p>
36  */

37 public class NewExampleAction extends Action {
38
39     /**
40      * The wizard dialog width
41      */

42     private static final int SIZING_WIZARD_WIDTH = 500;
43
44     /**
45      * The wizard dialog height
46      */

47     private static final int SIZING_WIZARD_HEIGHT = 500;
48
49     /**
50      * The workbench window this action will run in
51      */

52     private IWorkbenchWindow window;
53
54     /**
55      * This default constructor allows the the action to be called from the welcome page.
56      */

57     public NewExampleAction() {
58         this(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
59     }
60
61     /**
62      * Creates a new action for launching the new project
63      * selection wizard.
64      *
65      * @param window the workbench window to query the current
66      * selection and shell for opening the wizard.
67      */

68     public NewExampleAction(IWorkbenchWindow window) {
69         super(IDEWorkbenchMessages.NewExampleAction_text);
70         if (window == null) {
71             throw new IllegalArgumentException JavaDoc();
72         }
73         this.window = window;
74         ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
75         setImageDescriptor(images
76                 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
77         setDisabledImageDescriptor(images
78                 .getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED));
79         setToolTipText(IDEWorkbenchMessages.NewExampleAction_toolTip);
80         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
81                 org.eclipse.ui.internal.IWorkbenchHelpContextIds.NEW_ACTION);
82     }
83
84     /* (non-Javadoc)
85      * Method declared on IAction.
86      */

87     public void run() {
88         // Create wizard selection wizard.
89
IWorkbench workbench = PlatformUI.getWorkbench();
90         NewWizard wizard = new NewWizard();
91         wizard
92                 .setCategoryId(WizardsRegistryReader.FULL_EXAMPLES_WIZARD_CATEGORY);
93
94         ISelection selection = window.getSelectionService().getSelection();
95         IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
96         if (selection instanceof IStructuredSelection) {
97             selectionToPass = (IStructuredSelection) selection;
98         }
99         wizard.init(workbench, selectionToPass);
100         IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault()
101                 .getDialogSettings();
102         IDialogSettings wizardSettings = workbenchSettings
103                 .getSection("NewWizardAction"); //$NON-NLS-1$
104
if (wizardSettings == null) {
105             wizardSettings = workbenchSettings.addNewSection("NewWizardAction"); //$NON-NLS-1$
106
}
107         wizard.setDialogSettings(wizardSettings);
108         wizard.setForcePreviousAndNextButtons(true);
109
110         // Create wizard dialog.
111
Shell parent = window.getShell();
112         WizardDialog dialog = new WizardDialog(parent, wizard);
113         dialog.create();
114         wizard.setWindowTitle(IDEWorkbenchMessages.NewExample_title);
115         dialog.getShell().setSize(
116                 Math.max(SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x),
117                 SIZING_WIZARD_HEIGHT);
118         PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(),
119                 IIDEHelpContextIds.NEW_PROJECT_WIZARD);
120
121         // Open wizard.
122
dialog.open();
123     }
124 }
125
Popular Tags