/******************************************************************************* * Copyright (c) 2000, 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.ui; import java.util.ArrayList; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.actions.PartEventAction; import org.eclipse.ui.help.WorkbenchHelp; import org.eclipse.ui.internal.AboutInfo; import org.eclipse.ui.internal.FeatureSelectionDialog; import org.eclipse.ui.internal.IHelpContextIds; import org.eclipse.ui.internal.Workbench; import org.eclipse.ui.internal.WorkbenchMessages; import org.eclipse.ui.internal.WorkbenchPage; import org.eclipse.ui.internal.WorkbenchPlugin; import org.eclipse.ui.internal.dialogs.WelcomeEditorInput; /** * Launch the quick start action. */ public class QuickStartAction extends PartEventAction { private static final String EDITOR_ID = "org.eclipse.ui.internal.dialogs.WelcomeEditor"; //$NON-NLS-1$ private IWorkbenchWindow window; /** * Create an instance of this class. *
* This consructor added to support calling the action from the welcome page *
*/ public QuickStartAction() { this(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); } /** * Create an instance of this class */ public QuickStartAction(IWorkbenchWindow window) { super(WorkbenchMessages.getString("QuickStart.text")); //$NON-NLS-1$ setToolTipText(WorkbenchMessages.getString("QuickStart.toolTip")); //$NON-NLS-1$ WorkbenchHelp.setHelp(this, IHelpContextIds.QUICK_START_ACTION); setActionDefinitionId("org.eclipse.ui.help.quickStartAction"); //$NON-NLS-1$ this.window = window; } /** * The user has invoked this action * to prompt for a feature and open that feature's welcome page */ public void run() { if (window == null) { return; } AboutInfo featureInfo = promptForFeatureInfo(); if (featureInfo == null) { return; } openWelcomePage(featureInfo); } /** * Open the welcome page using the specified feature information. * No action is taken if either the feature cannot be found * or the feature does not have a welcome page associated with it. * * @param featureId the feature's unique identifier */ public void openWelcomePage(String featureId) { if (window == null) { return; } AboutInfo featureInfo = getFeatureInfo(featureId); if (featureInfo == null) { return; } openWelcomePage(featureInfo); } /** * Open a dialog prompting the user to select a feature * * @return the feature information * ornull
if the user canceled the dialog
*/
private AboutInfo promptForFeatureInfo() {
// Build a list of features with welcome pages
AboutInfo[] features =
((Workbench) window.getWorkbench())
.getConfigurationInfo()
.getFeaturesInfo();
ArrayList welcomeFeatures = new ArrayList();
for (int i = 0; i < features.length; i++) {
if (features[i].getWelcomePageURL() != null) {
welcomeFeatures.add(features[i]);
}
}
features =
(AboutInfo[]) welcomeFeatures.toArray(
new AboutInfo[welcomeFeatures.size()]);
Shell shell = window.getShell();
// If no features with welcome pages, then notify the user and abort
if (welcomeFeatures.size() == 0) {
MessageDialog.openInformation(shell, WorkbenchMessages.getString("QuickStartMessageDialog.title"), //$NON-NLS-1$
WorkbenchMessages.getString("QuickStartMessageDialog.message")); //$NON-NLS-1$
return null;
}
// Open a feature selection dialog
AboutInfo primaryFeature =
((Workbench) window.getWorkbench())
.getConfigurationInfo()
.getAboutInfo();
FeatureSelectionDialog d = new FeatureSelectionDialog(
shell,
features,
primaryFeature,
"WelcomePageSelectionDialog.title", //$NON-NLS-1$
"WelcomePageSelectionDialog.message", //$NON-NLS-1$
IHelpContextIds.WELCOME_PAGE_SELECTION_DIALOG);
if (d.open() != Dialog.OK || d.getResult().length != 1) {
return null;
}
return (AboutInfo) d.getResult()[0];
}
/**
* Return the feature information for the specified feature
*
* @param featureId the feature's unique identifier
* @return the feature information
* or null
if either
* 1) the feature does not exist, or
* 2) the feature does not have a welcome page
*/
private AboutInfo getFeatureInfo(String featureId) {
AboutInfo[] features =
((Workbench) window.getWorkbench())
.getConfigurationInfo()
.getFeaturesInfo();
for (int i = 0; i < features.length; i++) {
if (features[i].getFeatureId().equals(featureId)
&& features[i].getWelcomePageURL() != null) {
return features[i];
}
}
return null;
}
/**
* Open the welcome page using the specified feature information.
*
* @param feature the feature information (not null
)
*/
private void openWelcomePage(AboutInfo featureInfo) {
IWorkbenchPage page = null;
// See if the feature wants a specific perspective
String perspectiveId = featureInfo.getWelcomePerspective();
if (perspectiveId == null) {
// Just use the current perspective unless one is not open
// in which case use the default
page = window.getActivePage();
if (page == null || page.getPerspective() == null) {
perspectiveId =
WorkbenchPlugin
.getDefault()
.getPerspectiveRegistry()
.getDefaultPerspective();
}
}
if (perspectiveId != null) {
try {
page =
(WorkbenchPage) window.getWorkbench().showPerspective(
perspectiveId,
window);
}
catch (WorkbenchException e) {
return;
}
}
page.setEditorAreaVisible(true);
// create input
WelcomeEditorInput input = new WelcomeEditorInput(featureInfo);
// see if we already have a welcome editor
IEditorPart editor = page.findEditor(input);
if (editor != null) {
page.activate(editor);
return;
}
try {
page.openEditor(input, EDITOR_ID);
}
catch (PartInitException e) {
IStatus status = new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 1, WorkbenchMessages.getString("QuickStartAction.openEditorException"), e); //$NON-NLS-1$
ErrorDialog.openError(window.getShell(), WorkbenchMessages.getString("Workbench.openEditorErrorDialogTitle"), //$NON-NLS-1$
WorkbenchMessages.getString("Workbench.openEditorErrorDialogMessage"), //$NON-NLS-1$
status);
}
}
}