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 java.net.URL;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.runtime.IProduct;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.jface.action.Action;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.jface.window.Window;
023: import org.eclipse.swt.widgets.Shell;
024: import org.eclipse.ui.IEditorPart;
025: import org.eclipse.ui.IWorkbenchPage;
026: import org.eclipse.ui.IWorkbenchWindow;
027: import org.eclipse.ui.PartInitException;
028: import org.eclipse.ui.PlatformUI;
029: import org.eclipse.ui.WorkbenchException;
030: import org.eclipse.ui.internal.ProductProperties;
031: import org.eclipse.ui.internal.ide.AboutInfo;
032: import org.eclipse.ui.internal.ide.FeatureSelectionDialog;
033: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
034: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
035: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
036: import org.eclipse.ui.internal.ide.dialogs.WelcomeEditorInput;
037:
038: /**
039: * The quick start (Welcome...) action.
040: *
041: * @deprecated the IDE now uses the new intro mechanism
042: */
043: public class QuickStartAction extends Action implements
044: ActionFactory.IWorkbenchAction {
045:
046: private static final String EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; //$NON-NLS-1$
047:
048: /**
049: * The workbench window; or <code>null</code> if this
050: * action has been <code>dispose</code>d.
051: */
052: private IWorkbenchWindow workbenchWindow;
053:
054: /**
055: * Create an instance of this class.
056: * <p>
057: * This consructor added to support calling the action from the welcome
058: * page.
059: * </p>
060: */
061: public QuickStartAction() {
062: this (PlatformUI.getWorkbench().getActiveWorkbenchWindow());
063: }
064:
065: /**
066: * Creates an instance of this action, for use in the given window.
067: * @param window the window
068: */
069: public QuickStartAction(IWorkbenchWindow window) {
070: super (IDEWorkbenchMessages.QuickStart_text);
071: if (window == null) {
072: throw new IllegalArgumentException();
073: }
074: this .workbenchWindow = window;
075: setToolTipText(IDEWorkbenchMessages.QuickStart_toolTip);
076: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
077: IIDEHelpContextIds.QUICK_START_ACTION);
078: setActionDefinitionId("org.eclipse.ui.help.quickStartAction"); //$NON-NLS-1$
079: }
080:
081: /**
082: * The user has invoked this action. Prompts for a feature with a welcome page,
083: * then opens the corresponding welcome page.
084: */
085: public void run() {
086: if (workbenchWindow == null) {
087: // action has been disposed
088: return;
089: }
090: try {
091: AboutInfo feature = promptForFeature();
092: if (feature != null) {
093: openWelcomePage(feature);
094: }
095: } catch (WorkbenchException e) {
096: ErrorDialog
097: .openError(
098: workbenchWindow.getShell(),
099: IDEWorkbenchMessages.QuickStartAction_errorDialogTitle,
100: IDEWorkbenchMessages.QuickStartAction_infoReadError,
101: e.getStatus());
102: }
103: }
104:
105: /**
106: * Prompts the user for a feature that has a welcome page.
107: *
108: * @return the chosen feature, or <code>null</code> if none was chosen
109: */
110: private AboutInfo promptForFeature() throws WorkbenchException {
111: // Ask the user to select a feature
112: ArrayList welcomeFeatures = new ArrayList();
113:
114: URL productUrl = null;
115: IProduct product = Platform.getProduct();
116: if (product != null) {
117: productUrl = ProductProperties.getWelcomePageUrl(product);
118: welcomeFeatures.add(new AboutInfo(product));
119: }
120:
121: AboutInfo[] features = IDEWorkbenchPlugin.getDefault()
122: .getFeatureInfos();
123: for (int i = 0; i < features.length; i++) {
124: URL url = features[i].getWelcomePageURL();
125: if (url != null && !url.equals(productUrl)) {
126: welcomeFeatures.add(features[i]);
127: }
128: }
129:
130: Shell shell = workbenchWindow.getShell();
131:
132: if (welcomeFeatures.size() == 0) {
133: MessageDialog
134: .openInformation(
135: shell,
136: IDEWorkbenchMessages.QuickStartMessageDialog_title,
137: IDEWorkbenchMessages.QuickStartMessageDialog_message);
138: return null;
139: }
140:
141: features = new AboutInfo[welcomeFeatures.size()];
142: welcomeFeatures.toArray(features);
143:
144: FeatureSelectionDialog d = new FeatureSelectionDialog(
145: shell,
146: features,
147: product == null ? null : product.getId(),
148: IDEWorkbenchMessages.WelcomePageSelectionDialog_title,
149: IDEWorkbenchMessages.WelcomePageSelectionDialog_message,
150: IIDEHelpContextIds.WELCOME_PAGE_SELECTION_DIALOG);
151: if (d.open() != Window.OK || d.getResult().length != 1) {
152: return null;
153: }
154: return (AboutInfo) d.getResult()[0];
155: }
156:
157: /**
158: * Opens the welcome page for the given feature.
159: *
160: * @param featureId the about info for the feature
161: * @return <code>true</code> if successful, <code>false</code> otherwise
162: * @throws WorkbenchException
163: */
164: public boolean openWelcomePage(String featureId)
165: throws WorkbenchException {
166: AboutInfo feature = findFeature(featureId);
167: if (feature == null || feature.getWelcomePageURL() == null) {
168: return false;
169: }
170: return openWelcomePage(feature);
171: }
172:
173: /**
174: * Returns the about info for the feature with the given id, or <code>null</code>
175: * if there is no such feature.
176: *
177: * @return the about info for the feature with the given id, or <code>null</code>
178: * if there is no such feature.
179: */
180: private AboutInfo findFeature(String featureId)
181: throws WorkbenchException {
182: AboutInfo[] features = IDEWorkbenchPlugin.getDefault()
183: .getFeatureInfos();
184: for (int i = 0; i < features.length; i++) {
185: AboutInfo info = features[i];
186: if (info.getFeatureId().equals(featureId)) {
187: return info;
188: }
189: }
190: return null;
191: }
192:
193: /**
194: * Opens the welcome page for a feature.
195: *
196: * @param feature the about info for the feature
197: * @return <code>true</code> if successful, <code>false</code> otherwise
198: */
199: private boolean openWelcomePage(AboutInfo feature) {
200: IWorkbenchPage page = null;
201:
202: // See if the feature wants a specific perspective
203: String perspectiveId = feature.getWelcomePerspectiveId();
204:
205: if (perspectiveId == null) {
206: // Just use the current perspective unless one is not open
207: // in which case use the default
208: page = workbenchWindow.getActivePage();
209:
210: if (page == null || page.getPerspective() == null) {
211: perspectiveId = PlatformUI.getWorkbench()
212: .getPerspectiveRegistry()
213: .getDefaultPerspective();
214: }
215: }
216:
217: if (perspectiveId != null) {
218: try {
219: page = PlatformUI.getWorkbench().showPerspective(
220: perspectiveId, workbenchWindow);
221: } catch (WorkbenchException e) {
222: IDEWorkbenchPlugin
223: .log(
224: "Error opening perspective: " + perspectiveId, e.getStatus()); //$NON-NLS-1$
225: return false;
226: }
227: }
228:
229: if (page == null) {
230: return false;
231: }
232:
233: page.setEditorAreaVisible(true);
234:
235: // create input
236: WelcomeEditorInput input = new WelcomeEditorInput(feature);
237:
238: // see if we already have a welcome editorz
239: IEditorPart editor = page.findEditor(input);
240: if (editor != null) {
241: page.activate(editor);
242: return true;
243: }
244:
245: try {
246: page.openEditor(input, EDITOR_ID);
247: } catch (PartInitException e) {
248: IDEWorkbenchPlugin
249: .log(
250: "Error opening welcome editor for feature: " + feature.getFeatureId(), e); //$NON-NLS-1$
251: IStatus status = new Status(
252: IStatus.ERROR,
253: IDEWorkbenchPlugin.IDE_WORKBENCH,
254: 1,
255: IDEWorkbenchMessages.QuickStartAction_openEditorException,
256: e);
257: ErrorDialog
258: .openError(
259: workbenchWindow.getShell(),
260: IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle,
261: IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage,
262: status);
263: return false;
264: }
265: return true;
266: }
267:
268: /* (non-Javadoc)
269: * Method declared on ActionFactory.IWorkbenchAction.
270: * @since 3.0
271: */
272: public void dispose() {
273: if (workbenchWindow == null) {
274: // action has already been disposed
275: return;
276: }
277: workbenchWindow = null;
278: }
279:
280: }
|