001: /*******************************************************************************
002: * Copyright (c) 2003, 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.internal.cheatsheets.views;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014:
015: import org.eclipse.jface.action.Action;
016: import org.eclipse.jface.action.IMenuManager;
017: import org.eclipse.jface.action.IToolBarManager;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.ui.IActionBars;
020: import org.eclipse.ui.IMemento;
021: import org.eclipse.ui.IViewSite;
022: import org.eclipse.ui.PartInitException;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.actions.ActionFactory;
025: import org.eclipse.ui.internal.cheatsheets.CheatSheetStopWatch;
026: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
027: import org.eclipse.ui.internal.cheatsheets.Messages;
028: import org.eclipse.ui.internal.cheatsheets.actions.CheatSheetMenu;
029: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
030: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
031: import org.eclipse.ui.part.ViewPart;
032:
033: public class CheatSheetView extends ViewPart {
034:
035: private boolean actionBarContributed = false;
036: private CheatSheetExpandRestoreAction expandRestoreAction;
037: private Action copyAction;
038: private CheatSheetViewer viewer;
039: private IMemento memento;
040: private static final String CHEAT_SHEET_VIEW_HELP_ID = "org.eclipse.ui.cheatsheets.cheatSheetView"; //$NON-NLS-1$
041:
042: private void contributeToActionBars() {
043: IActionBars bars = getViewSite().getActionBars();
044: IMenuManager menuManager = bars.getMenuManager();
045: IToolBarManager tbmanager = bars.getToolBarManager();
046:
047: expandRestoreAction = new CheatSheetExpandRestoreAction(
048: Messages.COLLAPSE_ALL_BUT_CURRENT_TOOLTIP, false,
049: viewer);
050:
051: copyAction = new Action("copy") { //$NON-NLS-1$
052: public void run() {
053: viewer.copy();
054: }
055: };
056: copyAction.setEnabled(false);
057: tbmanager.add(expandRestoreAction);
058: bars.setGlobalActionHandler(ActionFactory.COPY.getId(),
059: copyAction);
060:
061: viewer.setExpandRestoreAction(expandRestoreAction);
062: viewer.setCopyAction(copyAction);
063:
064: CheatSheetMenu cheatsheetMenuMenuItem = new CheatSheetMenu();
065: menuManager.add(cheatsheetMenuMenuItem);
066:
067: cheatsheetMenuMenuItem.setMenuContributor(viewer);
068: }
069:
070: /**
071: * Creates the SWT controls for this workbench part.
072: * <p>
073: * Clients should not call this method (the workbench calls this method at
074: * appropriate times).
075: * </p>
076: * <p>
077: * For implementors this is a multi-step process:
078: * <ol>
079: * <li>Create one or more controls within the parent.</li>
080: * <li>Set the parent layout as needed.</li>
081: * <li>Register any global actions with the <code>IActionService</code>.</li>
082: * <li>Register any popup menus with the <code>IActionService</code>.</li>
083: * <li>Register a selection provider with the <code>ISelectionService</code>
084: * (optional). </li>
085: * </ol>
086: * </p>
087: *
088: * @param parent the parent control
089: */
090: public void createPartControl(Composite parent) {
091: CheatSheetStopWatch
092: .startStopWatch("CheatSheetView.createPartControl"); //$NON-NLS-1$
093:
094: viewer = new CheatSheetViewer(false);
095: viewer.createPartControl(parent);
096: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
097: CHEAT_SHEET_VIEW_HELP_ID);
098:
099: if (!actionBarContributed) {
100: contributeToActionBars();
101: actionBarContributed = true;
102: }
103: CheatSheetStopWatch
104: .printLapTime(
105: "CheatSheetView.createPartControl", "Time in CheatSheetView.createPartControl() before restoreState: "); //$NON-NLS-1$ //$NON-NLS-2$
106: if (memento != null) {
107: restoreState(memento);
108: }
109:
110: CheatSheetStopWatch
111: .printTotalTime(
112: "CheatSheetView.createPartControl", "Time in CheatSheetView.createPartControl(): "); //$NON-NLS-1$ //$NON-NLS-2$
113: }
114:
115: /**
116: * @see org.eclipse.ui.IWorkbenchPart#dispose()
117: */
118: public void dispose() {
119: super .dispose();
120: }
121:
122: public CheatSheetElement getContent() {
123: if (viewer != null) {
124: return viewer.getContent();
125: }
126: return null;
127: }
128:
129: public String getCheatSheetID() {
130: if (viewer != null) {
131: return viewer.getCheatSheetID();
132: }
133: return null;
134: }
135:
136: /*
137: * Returns the CheatSheetViewer contained in this view.
138: */
139: public CheatSheetViewer getCheatSheetViewer() {
140: return viewer;
141: }
142:
143: /* (non-Javadoc)
144: * Initializes this view with the given view site. A memento is passed to
145: * the view which contains a snapshot of the views state from a previous
146: * session. Where possible, the view should try to recreate that state
147: * within the part controls.
148: * <p>
149: * This implementation will ignore the memento and initialize the view in
150: * a fresh state. Subclasses may override the implementation to perform any
151: * state restoration as needed.
152: */
153: public void init(IViewSite site, IMemento memento)
154: throws PartInitException {
155: init(site);
156: this .memento = memento;
157: }
158:
159: /**
160: * Restore the view state
161: */
162: private void restoreState(IMemento memento) {
163: IMemento contentMemento = memento
164: .getChild(ICheatSheetResource.MEMENTO);
165: if (contentMemento != null) {
166: String id = contentMemento
167: .getString(ICheatSheetResource.MEMENTO_ID);
168: String name = contentMemento
169: .getString(ICheatSheetResource.MEMENTO_NAME);
170:
171: // Using an if/else if here because at a point in time there was a different
172: // attribute used. As a result an if/else could cause setInput(null) to be
173: // invoked but this would throw an IllegalArgumentException.
174: if (name != null) {
175: try {
176: URL fileURL = new URL(contentMemento
177: .getString(ICheatSheetResource.MEMENTO_URL));
178: setInput(id, name, fileURL);
179: } catch (MalformedURLException mue) {
180: }
181: } else if (id != null) {
182: setInput(id);
183: }
184:
185: }
186: }
187:
188: /* (non-Javadoc)
189: * Method declared on IViewPart.
190: */
191: public void saveState(IMemento memento) {
192: if (viewer != null) {
193: CheatSheetElement element = viewer.getContent();
194:
195: if (element == null) {
196: // Currently no cheat sheet is being displayed so just return
197: return;
198: }
199:
200: IMemento contentMemento = memento
201: .createChild(ICheatSheetResource.MEMENTO);
202:
203: CheatSheetElement tempElement = CheatSheetRegistryReader
204: .getInstance().findCheatSheet(element.getID());
205: if (tempElement != null) {
206: contentMemento
207: .putString(ICheatSheetResource.MEMENTO_ID,
208: element.getID());
209: } else {
210: contentMemento
211: .putString(ICheatSheetResource.MEMENTO_ID,
212: element.getID());
213: contentMemento.putString(
214: ICheatSheetResource.MEMENTO_NAME, element
215: .getLabel(null));
216: contentMemento.putString(
217: ICheatSheetResource.MEMENTO_URL, element
218: .getHref());
219: }
220:
221: // Make sure the current cheat sheet is saved
222: viewer.saveCurrentSheet();
223: }
224: }
225:
226: /**
227: * Passing the focus request to the viewer's control.
228: */
229: public void setFocus() {
230: if (viewer != null) {
231: viewer.setFocus();
232: }
233: }
234:
235: public void setInput(String id) {
236: CheatSheetStopWatch.startStopWatch("CheatSheetView.setInput"); //$NON-NLS-1$
237:
238: if (viewer != null) {
239: viewer.setInput(id);
240: }
241:
242: CheatSheetStopWatch
243: .printTotalTime(
244: "CheatSheetView.setInput", "Time in CheatSheetView.setInput(String id): "); //$NON-NLS-1$ //$NON-NLS-2$
245: }
246:
247: public void setInput(String id, String name, URL url) {
248: if (viewer != null) {
249: viewer.setInput(id, name, url);
250: }
251: }
252:
253: public void setInputFromXml(String id, String name, String xml,
254: String basePath) {
255: if (viewer != null) {
256: viewer.setInputFromXml(id, name, xml, basePath);
257: }
258: }
259: }
|