KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > views > CheatSheetView


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.internal.cheatsheets.views;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IMemento;
22 import org.eclipse.ui.IViewSite;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.actions.ActionFactory;
26 import org.eclipse.ui.internal.cheatsheets.CheatSheetStopWatch;
27 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
28 import org.eclipse.ui.internal.cheatsheets.Messages;
29 import org.eclipse.ui.internal.cheatsheets.actions.CheatSheetMenu;
30 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetElement;
31 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
32 import org.eclipse.ui.part.ViewPart;
33
34 public class CheatSheetView extends ViewPart {
35     
36     private boolean actionBarContributed = false;
37     private CheatSheetExpandRestoreAction expandRestoreAction;
38     private Action copyAction;
39     private CheatSheetViewer viewer;
40     private IMemento memento;
41     private static final String JavaDoc CHEAT_SHEET_VIEW_HELP_ID = "org.eclipse.ui.cheatsheets.cheatSheetView"; //$NON-NLS-1$
42

43     private void contributeToActionBars() {
44         IActionBars bars = getViewSite().getActionBars();
45         IMenuManager menuManager = bars.getMenuManager();
46         IToolBarManager tbmanager = bars.getToolBarManager();
47     
48         expandRestoreAction = new CheatSheetExpandRestoreAction(Messages.COLLAPSE_ALL_BUT_CURRENT_TOOLTIP, false, viewer);
49         
50         copyAction = new Action("copy") { //$NON-NLS-1$
51
public void run() {
52                 viewer.copy();
53             }
54         };
55         copyAction.setEnabled(false);
56         tbmanager.add(expandRestoreAction);
57         bars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
58
59         viewer.setExpandRestoreAction(expandRestoreAction);
60         viewer.setCopyAction(copyAction);
61     
62         CheatSheetMenu cheatsheetMenuMenuItem = new CheatSheetMenu();
63         menuManager.add(cheatsheetMenuMenuItem);
64
65         cheatsheetMenuMenuItem.setMenuContributor(viewer);
66     }
67
68
69     /**
70      * Creates the SWT controls for this workbench part.
71      * <p>
72      * Clients should not call this method (the workbench calls this method at
73      * appropriate times).
74      * </p>
75      * <p>
76      * For implementors this is a multi-step process:
77      * <ol>
78      * <li>Create one or more controls within the parent.</li>
79      * <li>Set the parent layout as needed.</li>
80      * <li>Register any global actions with the <code>IActionService</code>.</li>
81      * <li>Register any popup menus with the <code>IActionService</code>.</li>
82      * <li>Register a selection provider with the <code>ISelectionService</code>
83      * (optional). </li>
84      * </ol>
85      * </p>
86      *
87      * @param parent the parent control
88      */

89     public void createPartControl(Composite parent) {
90         CheatSheetStopWatch.startStopWatch("CheatSheetView.createPartControl"); //$NON-NLS-1$
91

92         viewer = new CheatSheetViewer(false);
93         viewer.createPartControl(parent);
94         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, CHEAT_SHEET_VIEW_HELP_ID);
95     
96         if (!actionBarContributed) {
97             contributeToActionBars();
98             actionBarContributed = true;
99         }
100         CheatSheetStopWatch.printLapTime("CheatSheetView.createPartControl", "Time in CheatSheetView.createPartControl() before restoreState: "); //$NON-NLS-1$ //$NON-NLS-2$
101
if (memento != null) {
102             restoreState(memento);
103         }
104
105         CheatSheetStopWatch.printTotalTime("CheatSheetView.createPartControl", "Time in CheatSheetView.createPartControl(): "); //$NON-NLS-1$ //$NON-NLS-2$
106
}
107
108     /**
109      * @see org.eclipse.ui.IWorkbenchPart#dispose()
110      */

111     public void dispose() {
112         super.dispose();
113     }
114     
115     public CheatSheetElement getContent() {
116         if(viewer != null) {
117             return viewer.getContent();
118         }
119         return null;
120     }
121
122     public String JavaDoc getCheatSheetID() {
123         if(viewer != null) {
124             return viewer.getCheatSheetID();
125         }
126         return null;
127     }
128
129     /*
130      * Returns the CheatSheetViewer contained in this view.
131      */

132     public CheatSheetViewer getCheatSheetViewer() {
133         return viewer;
134     }
135     
136     /* (non-Javadoc)
137      * Initializes this view with the given view site. A memento is passed to
138      * the view which contains a snapshot of the views state from a previous
139      * session. Where possible, the view should try to recreate that state
140      * within the part controls.
141      * <p>
142      * This implementation will ignore the memento and initialize the view in
143      * a fresh state. Subclasses may override the implementation to perform any
144      * state restoration as needed.
145      */

146     public void init(IViewSite site, IMemento memento) throws PartInitException {
147         init(site);
148         this.memento = memento;
149     }
150
151     /**
152      * Restore the view state
153      */

154     private void restoreState(IMemento memento) {
155         IMemento contentMemento = memento.getChild(ICheatSheetResource.MEMENTO);
156         if (contentMemento != null) {
157             String JavaDoc id = contentMemento.getString(ICheatSheetResource.MEMENTO_ID);
158             String JavaDoc name = contentMemento.getString(ICheatSheetResource.MEMENTO_NAME);
159             
160             // Using an if/else if here because at a point in time there was a different
161
// attribute used. As a result an if/else could cause setInput(null) to be
162
// invoked but this would throw an IllegalArgumentException.
163
if(name != null) {
164                 try {
165                 URL JavaDoc fileURL = new URL JavaDoc(contentMemento.getString(ICheatSheetResource.MEMENTO_URL));
166                 setInput(id, name, fileURL);
167                 } catch (MalformedURLException JavaDoc mue) {
168                 }
169             } else if (id != null) {
170                 setInput(id);
171             }
172
173         }
174     }
175
176     /* (non-Javadoc)
177      * Method declared on IViewPart.
178      */

179     public void saveState(IMemento memento) {
180         if(viewer != null) {
181             CheatSheetElement element = viewer.getContent();
182
183             if(element == null) {
184                 // Currently no cheat sheet is being displayed so just return
185
return;
186             }
187
188             IMemento contentMemento = memento.createChild(ICheatSheetResource.MEMENTO);
189
190             CheatSheetElement tempElement = CheatSheetRegistryReader.getInstance().findCheatSheet(element.getID());
191             if(tempElement != null) {
192                 contentMemento.putString(ICheatSheetResource.MEMENTO_ID, element.getID());
193             } else {
194                 contentMemento.putString(ICheatSheetResource.MEMENTO_ID, element.getID());
195                 contentMemento.putString(ICheatSheetResource.MEMENTO_NAME, element.getLabel(null));
196                 contentMemento.putString(ICheatSheetResource.MEMENTO_URL, element.getHref());
197             }
198
199             // Make sure the current cheat sheet is saved
200
viewer.saveCurrentSheet();
201         }
202     }
203
204     /**
205      * Passing the focus request to the viewer's control.
206      */

207     public void setFocus() {
208         if(viewer != null) {
209             viewer.setFocus();
210         }
211     }
212     
213     public void setInput(String JavaDoc id) {
214         CheatSheetStopWatch.startStopWatch("CheatSheetView.setInput"); //$NON-NLS-1$
215

216         if(viewer != null) {
217             viewer.setInput(id);
218         }
219
220         CheatSheetStopWatch.printTotalTime("CheatSheetView.setInput", "Time in CheatSheetView.setInput(String id): "); //$NON-NLS-1$ //$NON-NLS-2$
221
}
222
223     public void setInput(String JavaDoc id, String JavaDoc name, URL JavaDoc url) {
224         if(viewer != null) {
225             viewer.setInput(id, name, url);
226         }
227     }
228
229     public void setInputFromXml(String JavaDoc id, String JavaDoc name, String JavaDoc xml, String JavaDoc basePath) {
230         if(viewer != null) {
231             viewer.setInputFromXml(id, name, xml, basePath);
232         }
233     }
234 }
235
Popular Tags