1 11 package org.eclipse.ui.internal.cheatsheets.views; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 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 CHEAT_SHEET_VIEW_HELP_ID = "org.eclipse.ui.cheatsheets.cheatSheetView"; 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") { 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 89 public void createPartControl(Composite parent) { 90 CheatSheetStopWatch.startStopWatch("CheatSheetView.createPartControl"); 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: "); if (memento != null) { 102 restoreState(memento); 103 } 104 105 CheatSheetStopWatch.printTotalTime("CheatSheetView.createPartControl", "Time in CheatSheetView.createPartControl(): "); } 107 108 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 getCheatSheetID() { 123 if(viewer != null) { 124 return viewer.getCheatSheetID(); 125 } 126 return null; 127 } 128 129 132 public CheatSheetViewer getCheatSheetViewer() { 133 return viewer; 134 } 135 136 146 public void init(IViewSite site, IMemento memento) throws PartInitException { 147 init(site); 148 this.memento = memento; 149 } 150 151 154 private void restoreState(IMemento memento) { 155 IMemento contentMemento = memento.getChild(ICheatSheetResource.MEMENTO); 156 if (contentMemento != null) { 157 String id = contentMemento.getString(ICheatSheetResource.MEMENTO_ID); 158 String name = contentMemento.getString(ICheatSheetResource.MEMENTO_NAME); 159 160 if(name != null) { 164 try { 165 URL fileURL = new URL (contentMemento.getString(ICheatSheetResource.MEMENTO_URL)); 166 setInput(id, name, fileURL); 167 } catch (MalformedURLException mue) { 168 } 169 } else if (id != null) { 170 setInput(id); 171 } 172 173 } 174 } 175 176 179 public void saveState(IMemento memento) { 180 if(viewer != null) { 181 CheatSheetElement element = viewer.getContent(); 182 183 if(element == null) { 184 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 viewer.saveCurrentSheet(); 201 } 202 } 203 204 207 public void setFocus() { 208 if(viewer != null) { 209 viewer.setFocus(); 210 } 211 } 212 213 public void setInput(String id) { 214 CheatSheetStopWatch.startStopWatch("CheatSheetView.setInput"); 216 if(viewer != null) { 217 viewer.setInput(id); 218 } 219 220 CheatSheetStopWatch.printTotalTime("CheatSheetView.setInput", "Time in CheatSheetView.setInput(String id): "); } 222 223 public void setInput(String id, String name, URL url) { 224 if(viewer != null) { 225 viewer.setInput(id, name, url); 226 } 227 } 228 229 public void setInputFromXml(String id, String name, String xml, String basePath) { 230 if(viewer != null) { 231 viewer.setInputFromXml(id, name, xml, basePath); 232 } 233 } 234 } 235 | Popular Tags |