KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.IProduct;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.WorkbenchException;
31 import org.eclipse.ui.internal.ProductProperties;
32 import org.eclipse.ui.internal.ide.AboutInfo;
33 import org.eclipse.ui.internal.ide.FeatureSelectionDialog;
34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
35 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
36 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
37 import org.eclipse.ui.internal.ide.dialogs.WelcomeEditorInput;
38
39 /**
40  * The quick start (Welcome...) action.
41  *
42  * @deprecated the IDE now uses the new intro mechanism
43  */

44 public class QuickStartAction extends Action implements
45         ActionFactory.IWorkbenchAction {
46
47     private static final String JavaDoc EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; //$NON-NLS-1$
48

49     /**
50      * The workbench window; or <code>null</code> if this
51      * action has been <code>dispose</code>d.
52      */

53     private IWorkbenchWindow workbenchWindow;
54
55     /**
56      * Create an instance of this class.
57      * <p>
58      * This consructor added to support calling the action from the welcome
59      * page.
60      * </p>
61      */

62     public QuickStartAction() {
63         this(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
64     }
65
66     /**
67      * Creates an instance of this action, for use in the given window.
68      * @param window the window
69      */

70     public QuickStartAction(IWorkbenchWindow window) {
71         super(IDEWorkbenchMessages.QuickStart_text);
72         if (window == null) {
73             throw new IllegalArgumentException JavaDoc();
74         }
75         this.workbenchWindow = window;
76         setToolTipText(IDEWorkbenchMessages.QuickStart_toolTip);
77         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
78                 IIDEHelpContextIds.QUICK_START_ACTION);
79         setActionDefinitionId("org.eclipse.ui.help.quickStartAction"); //$NON-NLS-1$
80
}
81
82     /**
83      * The user has invoked this action. Prompts for a feature with a welcome page,
84      * then opens the corresponding welcome page.
85      */

86     public void run() {
87         if (workbenchWindow == null) {
88             // action has been disposed
89
return;
90         }
91         try {
92             AboutInfo feature = promptForFeature();
93             if (feature != null) {
94                 openWelcomePage(feature);
95             }
96         } catch (WorkbenchException e) {
97             ErrorDialog.openError(workbenchWindow.getShell(),
98                     IDEWorkbenchMessages.QuickStartAction_errorDialogTitle,
99                     IDEWorkbenchMessages.QuickStartAction_infoReadError,
100                     e.getStatus());
101         }
102     }
103
104     /**
105      * Prompts the user for a feature that has a welcome page.
106      *
107      * @return the chosen feature, or <code>null</code> if none was chosen
108      */

109     private AboutInfo promptForFeature() throws WorkbenchException {
110         // Ask the user to select a feature
111
ArrayList JavaDoc welcomeFeatures = new ArrayList JavaDoc();
112
113         URL JavaDoc productUrl = null;
114         IProduct product = Platform.getProduct();
115         if (product != null) {
116             productUrl = ProductProperties.getWelcomePageUrl(product);
117             welcomeFeatures.add(new AboutInfo(product));
118         }
119
120         AboutInfo[] features = IDEWorkbenchPlugin.getDefault()
121                 .getFeatureInfos();
122         for (int i = 0; i < features.length; i++) {
123             URL JavaDoc url = features[i].getWelcomePageURL();
124             if (url != null && !url.equals(productUrl)) {
125                 welcomeFeatures.add(features[i]);
126             }
127         }
128
129         Shell shell = workbenchWindow.getShell();
130
131         if (welcomeFeatures.size() == 0) {
132             MessageDialog.openInformation(shell, IDEWorkbenchMessages.QuickStartMessageDialog_title,
133                     IDEWorkbenchMessages.QuickStartMessageDialog_message);
134             return null;
135         }
136
137         features = new AboutInfo[welcomeFeatures.size()];
138         welcomeFeatures.toArray(features);
139
140         FeatureSelectionDialog d = new FeatureSelectionDialog(shell, features,
141                 product == null ? null : product.getId(), IDEWorkbenchMessages.WelcomePageSelectionDialog_title,
142                 IDEWorkbenchMessages.WelcomePageSelectionDialog_message,
143                 IIDEHelpContextIds.WELCOME_PAGE_SELECTION_DIALOG);
144         if (d.open() != Window.OK || d.getResult().length != 1) {
145             return null;
146         }
147         return (AboutInfo) d.getResult()[0];
148     }
149
150     /**
151      * Opens the welcome page for the given feature.
152      *
153      * @param featureId the about info for the feature
154      * @return <code>true</code> if successful, <code>false</code> otherwise
155      * @throws WorkbenchException
156      */

157     public boolean openWelcomePage(String JavaDoc featureId) throws WorkbenchException {
158         AboutInfo feature = findFeature(featureId);
159         if (feature == null || feature.getWelcomePageURL() == null) {
160             return false;
161         }
162         return openWelcomePage(feature);
163     }
164
165     /**
166      * Returns the about info for the feature with the given id, or <code>null</code>
167      * if there is no such feature.
168      *
169      * @return the about info for the feature with the given id, or <code>null</code>
170      * if there is no such feature.
171      */

172     private AboutInfo findFeature(String JavaDoc featureId) throws WorkbenchException {
173         AboutInfo[] features = IDEWorkbenchPlugin.getDefault()
174                 .getFeatureInfos();
175         for (int i = 0; i < features.length; i++) {
176             AboutInfo info = features[i];
177             if (info.getFeatureId().equals(featureId)) {
178                 return info;
179             }
180         }
181         return null;
182     }
183
184     /**
185      * Opens the welcome page for a feature.
186      *
187      * @param feature the about info for the feature
188      * @return <code>true</code> if successful, <code>false</code> otherwise
189      */

190     private boolean openWelcomePage(AboutInfo feature) {
191         IWorkbenchPage page = null;
192
193         // See if the feature wants a specific perspective
194
String JavaDoc perspectiveId = feature.getWelcomePerspectiveId();
195
196         if (perspectiveId == null) {
197             // Just use the current perspective unless one is not open
198
// in which case use the default
199
page = workbenchWindow.getActivePage();
200
201             if (page == null || page.getPerspective() == null) {
202                 perspectiveId = PlatformUI.getWorkbench()
203                         .getPerspectiveRegistry().getDefaultPerspective();
204             }
205         }
206
207         if (perspectiveId != null) {
208             try {
209                 page = PlatformUI.getWorkbench().showPerspective(perspectiveId,
210                         workbenchWindow);
211             } catch (WorkbenchException e) {
212                 IDEWorkbenchPlugin
213                         .log("Error opening perspective: " + perspectiveId, e.getStatus()); //$NON-NLS-1$
214
return false;
215             }
216         }
217
218         if (page == null) {
219             return false;
220         }
221         
222         page.setEditorAreaVisible(true);
223
224         // create input
225
WelcomeEditorInput input = new WelcomeEditorInput(feature);
226
227         // see if we already have a welcome editorz
228
IEditorPart editor = page.findEditor(input);
229         if (editor != null) {
230             page.activate(editor);
231             return true;
232         }
233
234         try {
235             page.openEditor(input, EDITOR_ID);
236         } catch (PartInitException e) {
237             IDEWorkbenchPlugin
238                     .log("Error opening welcome editor for feature: " + feature.getFeatureId(), e); //$NON-NLS-1$
239
IStatus status = new Status(
240                     IStatus.ERROR,
241                     IDEWorkbenchPlugin.IDE_WORKBENCH,
242                     1,
243                     IDEWorkbenchMessages.QuickStartAction_openEditorException, e);
244             ErrorDialog
245                     .openError(
246                             workbenchWindow.getShell(),
247                             IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle,
248                             IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage,
249                             status);
250             return false;
251         }
252         return true;
253     }
254
255     /* (non-Javadoc)
256      * Method declared on ActionFactory.IWorkbenchAction.
257      * @since 3.0
258      */

259     public void dispose() {
260         if (workbenchWindow == null) {
261             // action has already been disposed
262
return;
263         }
264         workbenchWindow = null;
265     }
266
267 }
268
Popular Tags