1 11 package org.eclipse.ui.actions; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 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 44 public class QuickStartAction extends Action implements 45 ActionFactory.IWorkbenchAction { 46 47 private static final String EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; 49 53 private IWorkbenchWindow workbenchWindow; 54 55 62 public QuickStartAction() { 63 this(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); 64 } 65 66 70 public QuickStartAction(IWorkbenchWindow window) { 71 super(IDEWorkbenchMessages.QuickStart_text); 72 if (window == null) { 73 throw new IllegalArgumentException (); 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"); } 81 82 86 public void run() { 87 if (workbenchWindow == null) { 88 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 109 private AboutInfo promptForFeature() throws WorkbenchException { 110 ArrayList welcomeFeatures = new ArrayList (); 112 113 URL 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 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 157 public boolean openWelcomePage(String 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 172 private AboutInfo findFeature(String 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 190 private boolean openWelcomePage(AboutInfo feature) { 191 IWorkbenchPage page = null; 192 193 String perspectiveId = feature.getWelcomePerspectiveId(); 195 196 if (perspectiveId == null) { 197 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()); return false; 215 } 216 } 217 218 if (page == null) { 219 return false; 220 } 221 222 page.setEditorAreaVisible(true); 223 224 WelcomeEditorInput input = new WelcomeEditorInput(feature); 226 227 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); 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 259 public void dispose() { 260 if (workbenchWindow == null) { 261 return; 263 } 264 workbenchWindow = null; 265 } 266 267 } 268 | Popular Tags |