KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > cheatsheets > OpenCheatSheetAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.cheatsheets;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.help.ui.internal.views.HelpTray;
16 import org.eclipse.help.ui.internal.views.IHelpPartPage;
17 import org.eclipse.help.ui.internal.views.ReusableHelpPart;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.dialogs.TrayDialog;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
24 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
25 import org.eclipse.ui.internal.cheatsheets.state.DefaultStateManager;
26 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetHelpPart;
27 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetView;
28 import org.eclipse.ui.internal.cheatsheets.views.ViewUtilities;
29
30 /**
31  * Action for opening a cheat sheet. The cheat sheet can be specified
32  * either by a cheat sheet id or by a URL of a cheat sheet content file.
33  * <p>
34  * This class is not intended to be subclassed by clients.
35  * </p>
36  *
37  * @since 3.0
38  */

39 public final class OpenCheatSheetAction extends Action {
40     private String JavaDoc id;
41     private String JavaDoc name;
42     private URL JavaDoc url;
43     private String JavaDoc xml;
44     private String JavaDoc basePath;
45
46     /**
47      * Creates an action that opens the cheat sheet with the given id.
48      * The cheat sheet content file is located via the
49      * <code>org.eclipse.ui.cheatsheets.cheatSheetContent</code>
50      * extension point.
51      *
52      * @param id the cheat sheet id
53      * @exception IllegalArgumentException if <code>id</code>
54      * is <code>null</code>
55      */

56     public OpenCheatSheetAction(String JavaDoc id) {
57         if (id == null) {
58             throw new IllegalArgumentException JavaDoc();
59         }
60         this.id = id;
61     }
62     
63     /**
64      * Creates an action that opens the cheat sheet with the
65      * given cheat sheet content file.
66      *
67      * @param id the id to give this cheat sheet
68      * @param name the name to give this cheat sheet
69      * @param url URL of the cheat sheet content file
70      * @exception IllegalArgumentException if the parameters
71      * are <code>null</code>
72      */

73     public OpenCheatSheetAction(String JavaDoc id, String JavaDoc name, URL JavaDoc url) {
74         if (id == null || name == null || url == null) {
75             throw new IllegalArgumentException JavaDoc();
76         }
77         this.id = id;
78         this.name = name;
79         this.url = url;
80     }
81     
82     /**
83      * Creates an action that opens a cheat sheet using
84      * XML passed in as a string.
85      *
86      * @param id the id to give this cheat sheet
87      * @param name the name to give this cheat sheet
88      * @param xml the cheatsheet content in xml format
89      * @param baseURL is a URL which is only required if the cheat sheet is
90      * a composite cheat sheet which has tasks which use path parameters in which
91      * case the paths will be relative to baseURL. May be <code>null</code>
92      * if this is not a composite cheat sheet
93      * @exception IllegalArgumentException if the parameters
94      * are <code>null</code>
95      * @since 3.3
96      */

97     public OpenCheatSheetAction(String JavaDoc id, String JavaDoc name, String JavaDoc xml, URL JavaDoc baseURL) {
98         if (id == null || name == null || xml == null) {
99             throw new IllegalArgumentException JavaDoc();
100         }
101         this.id = id;
102         this.name = name;
103         this.xml = xml;
104         if (baseURL !=null) {
105             basePath = baseURL.toExternalForm();
106         }
107     }
108
109
110     /* (non-javadoc)
111      * This action will try to launch the cheat sheet view and populate
112      * it with the content specified either in the URL or the content
113      * file specified in the cheatsheetContent extension point
114      * for the cheat sheet with the id passed to this action.
115      * @see IAction#run()
116      */

117     public void run() {
118         Shell shell = Display.getDefault().getActiveShell();
119         Object JavaDoc data = shell.getData();
120         // are we in a dialog that can show a cheat sheet?
121
if (!shell.isFocusControl() && data instanceof TrayDialog) {
122             TrayDialog dialog = (TrayDialog)data;
123             HelpTray tray = (HelpTray)dialog.getTray();
124             if (tray == null) {
125                 tray = new HelpTray();
126                 dialog.openTray(tray);
127             }
128             ReusableHelpPart helpPart = tray.getHelpPart();
129             IHelpPartPage page = helpPart.createPage(CheatSheetHelpPart.ID, null, null);
130             page.setVerticalSpacing(0);
131             page.setHorizontalMargin(0);
132             CheatSheetElement contentElement = CheatSheetRegistryReader.getInstance().findCheatSheet(id);
133             helpPart.addPart(CheatSheetHelpPart.ID, new CheatSheetHelpPart(helpPart.getForm().getForm().getBody(), helpPart.getForm().getToolkit(), page.getToolBarManager(), contentElement, new DefaultStateManager()));
134             page.addPart(CheatSheetHelpPart.ID, true);
135             helpPart.addPage(page);
136             helpPart.showPage(CheatSheetHelpPart.ID);
137         }
138         else {
139             CheatSheetView view = ViewUtilities.showCheatSheetView();
140             if (view == null) {
141                 return;
142             }
143             // Depending on which constructor was used open the cheat sheet view from a
144
// URL, an XML string or based on the id
145
if(url != null) {
146                 view.setInput(id, name, url);
147             } else if (xml != null) {
148                 view.setInputFromXml(id, name, xml, basePath);
149             } else {
150                 view.setInput(id);
151             }
152             IWorkbenchPage page = view.getSite().getWorkbenchWindow().getActivePage();
153             page.bringToTop(view);
154         }
155     }
156 }
157
Popular Tags