KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > TipsAndTricksAction


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.internal.ide;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IProduct;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.window.Window;
22 import org.eclipse.swt.custom.BusyIndicator;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.actions.ActionFactory;
26 import org.eclipse.ui.actions.PartEventAction;
27
28 /**
29  * Launch the tips and tricks action.
30  */

31 public class TipsAndTricksAction extends PartEventAction implements
32         ActionFactory.IWorkbenchAction {
33
34     /**
35      * The workbench window this action is registered with.
36      */

37     private IWorkbenchWindow workbenchWindow;
38
39     /**
40      * Create an instance of this class.
41      *
42      * @param window the window
43      */

44     public TipsAndTricksAction(IWorkbenchWindow window) {
45         super(IDEWorkbenchMessages.TipsAndTricks_text);
46         if (window == null) {
47             throw new IllegalArgumentException JavaDoc();
48         }
49         this.workbenchWindow = window;
50         setToolTipText(IDEWorkbenchMessages.TipsAndTricks_toolTip);
51         window.getWorkbench().getHelpSystem().setHelp(this,
52                 IIDEHelpContextIds.TIPS_AND_TRICKS_ACTION);
53         setActionDefinitionId("org.eclipse.ui.help.tipsAndTricksAction"); //$NON-NLS-1$
54
workbenchWindow.getPartService().addPartListener(this);
55     }
56
57     /**
58      * The user has invoked this action
59      */

60     public void run() {
61         if (workbenchWindow == null) {
62             // action has been disposed
63
return;
64         }
65         // Ask the user to select a feature
66
AboutInfo[] featureInfos = IDEWorkbenchPlugin.getDefault()
67                 .getFeatureInfos();
68         ArrayList JavaDoc tipsAndTricksFeatures = new ArrayList JavaDoc(featureInfos.length);
69         for (int i = 0; i < featureInfos.length; i++) {
70             if (featureInfos[i].getTipsAndTricksHref() != null) {
71                 tipsAndTricksFeatures.add(featureInfos[i]);
72             }
73         }
74
75         Shell shell = workbenchWindow.getShell();
76
77         if (tipsAndTricksFeatures.size() == 0) {
78             MessageDialog.openInformation(shell, IDEWorkbenchMessages.TipsAndTricksMessageDialog_title,
79                     IDEWorkbenchMessages.TipsAndTricksMessageDialog_message);
80             return;
81         }
82
83         AboutInfo[] features = new AboutInfo[tipsAndTricksFeatures.size()];
84         tipsAndTricksFeatures.toArray(features);
85
86         IProduct product = Platform.getProduct();
87         FeatureSelectionDialog d = new FeatureSelectionDialog(shell, features,
88                 product == null ? null : product.getId(), IDEWorkbenchMessages.TipsAndTricksPageSelectionDialog_title,
89                 IDEWorkbenchMessages.TipsAndTricksPageSelectionDialog_message,
90                 IIDEHelpContextIds.TIPS_AND_TRICKS_PAGE_SELECTION_DIALOG);
91
92         if (d.open() != Window.OK || d.getResult().length != 1) {
93             return;
94         }
95
96         AboutInfo feature = (AboutInfo) d.getResult()[0];
97
98         /**
99          * Open the tips and trick help topic
100          */

101         if (feature != null) {
102             final String JavaDoc href = feature.getTipsAndTricksHref();
103             if (href != null) {
104                 BusyIndicator.showWhile(shell.getDisplay(), new Runnable JavaDoc() {
105                     public void run() {
106                         workbenchWindow.getWorkbench().getHelpSystem()
107                                 .displayHelpResource(href);
108                     }
109                 });
110             } else {
111                 IStatus status = new Status(
112                         IStatus.ERROR,
113                         IDEWorkbenchPlugin.IDE_WORKBENCH,
114                         1,
115                         IDEWorkbenchMessages.TipsAndTricksErrorDialog_noHref, null);
116                 ErrorDialog.openError(shell, IDEWorkbenchMessages.TipsAndTricksErrorDialog_title,
117                         IDEWorkbenchMessages.TipsAndTricksErrorDialog_noHref,
118                         status);
119             }
120         } else {
121             IStatus status = new Status(IStatus.ERROR,
122                     IDEWorkbenchPlugin.IDE_WORKBENCH, 1, IDEWorkbenchMessages.TipsAndTricksErrorDialog_noHref, null);
123             ErrorDialog.openError(shell, IDEWorkbenchMessages.TipsAndTricksErrorDialog_title,
124                     IDEWorkbenchMessages.TipsAndTricksErrorDialog_noFeatures,
125                     status);
126         }
127     }
128
129     /* (non-Javadoc)
130      * Method declared on ActionFactory.IWorkbenchAction.
131      */

132     public void dispose() {
133         if (workbenchWindow == null) {
134             // action has already been disposed
135
return;
136         }
137         workbenchWindow.getPartService().removePartListener(this);
138         workbenchWindow = null;
139     }
140 }
141
Popular Tags