001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 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.cheatsheets;
011:
012: import java.net.URL;
013:
014: import org.eclipse.help.ui.internal.views.HelpTray;
015: import org.eclipse.help.ui.internal.views.IHelpPartPage;
016: import org.eclipse.help.ui.internal.views.ReusableHelpPart;
017: import org.eclipse.jface.action.Action;
018: import org.eclipse.jface.dialogs.TrayDialog;
019: import org.eclipse.swt.widgets.Display;
020: import org.eclipse.swt.widgets.Shell;
021: import org.eclipse.ui.IWorkbenchPage;
022: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
023: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
024: import org.eclipse.ui.internal.cheatsheets.state.DefaultStateManager;
025: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetHelpPart;
026: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetView;
027: import org.eclipse.ui.internal.cheatsheets.views.ViewUtilities;
028:
029: /**
030: * Action for opening a cheat sheet. The cheat sheet can be specified
031: * either by a cheat sheet id or by a URL of a cheat sheet content file.
032: * <p>
033: * This class is not intended to be subclassed by clients.
034: * </p>
035: *
036: * @since 3.0
037: */
038: public final class OpenCheatSheetAction extends Action {
039: private String id;
040: private String name;
041: private URL url;
042: private String xml;
043: private String basePath;
044:
045: /**
046: * Creates an action that opens the cheat sheet with the given id.
047: * The cheat sheet content file is located via the
048: * <code>org.eclipse.ui.cheatsheets.cheatSheetContent</code>
049: * extension point.
050: *
051: * @param id the cheat sheet id
052: * @exception IllegalArgumentException if <code>id</code>
053: * is <code>null</code>
054: */
055: public OpenCheatSheetAction(String id) {
056: if (id == null) {
057: throw new IllegalArgumentException();
058: }
059: this .id = id;
060: }
061:
062: /**
063: * Creates an action that opens the cheat sheet with the
064: * given cheat sheet content file.
065: *
066: * @param id the id to give this cheat sheet
067: * @param name the name to give this cheat sheet
068: * @param url URL of the cheat sheet content file
069: * @exception IllegalArgumentException if the parameters
070: * are <code>null</code>
071: */
072: public OpenCheatSheetAction(String id, String name, URL url) {
073: if (id == null || name == null || url == null) {
074: throw new IllegalArgumentException();
075: }
076: this .id = id;
077: this .name = name;
078: this .url = url;
079: }
080:
081: /**
082: * Creates an action that opens a cheat sheet using
083: * XML passed in as a string.
084: *
085: * @param id the id to give this cheat sheet
086: * @param name the name to give this cheat sheet
087: * @param xml the cheatsheet content in xml format
088: * @param baseURL is a URL which is only required if the cheat sheet is
089: * a composite cheat sheet which has tasks which use path parameters in which
090: * case the paths will be relative to baseURL. May be <code>null</code>
091: * if this is not a composite cheat sheet
092: * @exception IllegalArgumentException if the parameters
093: * are <code>null</code>
094: * @since 3.3
095: */
096: public OpenCheatSheetAction(String id, String name, String xml,
097: URL baseURL) {
098: if (id == null || name == null || xml == null) {
099: throw new IllegalArgumentException();
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: /* (non-javadoc)
110: * This action will try to launch the cheat sheet view and populate
111: * it with the content specified either in the URL or the content
112: * file specified in the cheatsheetContent extension point
113: * for the cheat sheet with the id passed to this action.
114: * @see IAction#run()
115: */
116: public void run() {
117: Shell shell = Display.getDefault().getActiveShell();
118: Object data = shell.getData();
119: // are we in a dialog that can show a cheat sheet?
120: if (!shell.isFocusControl() && data instanceof TrayDialog) {
121: TrayDialog dialog = (TrayDialog) data;
122: HelpTray tray = (HelpTray) dialog.getTray();
123: if (tray == null) {
124: tray = new HelpTray();
125: dialog.openTray(tray);
126: }
127: ReusableHelpPart helpPart = tray.getHelpPart();
128: IHelpPartPage page = helpPart.createPage(
129: CheatSheetHelpPart.ID, null, null);
130: page.setVerticalSpacing(0);
131: page.setHorizontalMargin(0);
132: CheatSheetElement contentElement = CheatSheetRegistryReader
133: .getInstance().findCheatSheet(id);
134: helpPart.addPart(CheatSheetHelpPart.ID,
135: new CheatSheetHelpPart(helpPart.getForm().getForm()
136: .getBody(),
137: helpPart.getForm().getToolkit(), page
138: .getToolBarManager(),
139: contentElement, new DefaultStateManager()));
140: page.addPart(CheatSheetHelpPart.ID, true);
141: helpPart.addPage(page);
142: helpPart.showPage(CheatSheetHelpPart.ID);
143: } else {
144: CheatSheetView view = ViewUtilities.showCheatSheetView();
145: if (view == null) {
146: return;
147: }
148: // Depending on which constructor was used open the cheat sheet view from a
149: // URL, an XML string or based on the id
150: if (url != null) {
151: view.setInput(id, name, url);
152: } else if (xml != null) {
153: view.setInputFromXml(id, name, xml, basePath);
154: } else {
155: view.setInput(id);
156: }
157: IWorkbenchPage page = view.getSite().getWorkbenchWindow()
158: .getActivePage();
159: page.bringToTop(view);
160: }
161: }
162: }
|