1 11 12 package org.eclipse.ui.internal; 13 14 import java.io.File ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.OutputStreamWriter ; 18 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.MultiStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jface.dialogs.ErrorDialog; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.ui.IMemento; 27 import org.eclipse.ui.IPersistableElement; 28 import org.eclipse.ui.IPerspectiveDescriptor; 29 import org.eclipse.ui.IViewPart; 30 import org.eclipse.ui.IViewReference; 31 import org.eclipse.ui.IWorkbench; 32 import org.eclipse.ui.IWorkbenchPage; 33 import org.eclipse.ui.IWorkbenchPreferenceConstants; 34 import org.eclipse.ui.IWorkbenchWindow; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.XMLMemento; 37 import org.eclipse.ui.internal.intro.IIntroConstants; 38 import org.eclipse.ui.internal.preferences.WorkbenchSettingsTransfer; 39 import org.eclipse.ui.internal.presentations.PresentationFactoryUtil; 40 import org.eclipse.ui.internal.util.PrefUtil; 41 import org.eclipse.ui.internal.util.Util; 42 import org.eclipse.ui.presentations.AbstractPresentationFactory; 43 import org.eclipse.ui.presentations.IStackPresentationSite; 44 45 52 public class WorkbenchLayoutSettingsTransfer extends WorkbenchSettingsTransfer { 53 54 58 61 public WorkbenchLayoutSettingsTransfer() { 62 super(); 63 } 64 65 70 public XMLMemento recordSharableWorkbenchState() { 71 XMLMemento memento = XMLMemento 72 .createWriteRoot(IWorkbenchConstants.TAG_WORKBENCH); 73 IStatus status = saveSettings(memento); 74 if (status.getSeverity() != IStatus.OK) { 75 ErrorDialog.openError(null, 78 WorkbenchMessages.Workbench_problemsSaving, 79 WorkbenchMessages.Workbench_problemsSavingMsg, status); 80 } 81 return memento; 82 } 83 84 90 private IStatus saveSettings(XMLMemento memento) { 91 MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, 92 WorkbenchMessages.Workbench_problemsSaving, null); 93 94 memento.putString(IWorkbenchConstants.TAG_VERSION, 96 Workbench.VERSION_STRING[1]); 97 98 IWorkbenchWindow[] windows = PlatformUI.getWorkbench() 100 .getWorkbenchWindows(); 101 for (int nX = 0; nX < windows.length; nX++) { 102 WorkbenchWindow window = (WorkbenchWindow) windows[nX]; 103 IMemento childMem = memento 104 .createChild(IWorkbenchConstants.TAG_WINDOW); 105 result.merge(saveState(window, childMem)); 106 } 107 return result; 108 } 109 110 117 private IStatus saveState(WorkbenchWindow window, IMemento memento) { 118 119 MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK, 120 WorkbenchMessages.WorkbenchWindow_problemsSavingWindow, null); 121 122 IWorkbenchPage activePage = window.getActivePage(); 123 if (activePage != null 124 && activePage.findView(IIntroConstants.INTRO_VIEW_ID) != null) { 125 IMemento introMem = memento 126 .createChild(IWorkbenchConstants.TAG_INTRO); 127 boolean isStandby = getWorkbench() 128 .getIntroManager() 129 .isIntroStandby(getWorkbench().getIntroManager().getIntro()); 130 introMem.putString(IWorkbenchConstants.TAG_STANDBY, String 131 .valueOf(isStandby)); 132 } 133 134 IWorkbenchPage[] pages = window.getPages(); 136 for (int i = 0; i < pages.length; i++) { 137 IWorkbenchPage page = pages[i]; 138 139 IMemento pageMem = memento 141 .createChild(IWorkbenchConstants.TAG_PAGE); 142 pageMem.putString(IWorkbenchConstants.TAG_LABEL, page.getLabel()); 143 result.add(saveState((WorkbenchPage) page, pageMem)); 144 145 if (page == window.getActivePage()) { 146 pageMem.putString(IWorkbenchConstants.TAG_FOCUS, "true"); } 148 149 IAdaptable input = page.getInput(); 151 if (input != null) { 152 IPersistableElement persistable = (IPersistableElement) Util 153 .getAdapter(input, IPersistableElement.class); 154 if (persistable == null) { 155 WorkbenchPlugin 156 .log("Unable to save page input: " + input 158 + ", because it does not adapt to IPersistableElement"); 160 } else { 161 IMemento inputMem = pageMem 163 .createChild(IWorkbenchConstants.TAG_INPUT); 164 inputMem.putString(IWorkbenchConstants.TAG_FACTORY_ID, 165 persistable.getFactoryId()); 166 persistable.saveState(inputMem); 167 } 168 } 169 } 170 171 return result; 172 } 173 174 181 private IStatus saveState(WorkbenchPage page, IMemento memento) { 182 183 MultiStatus result = new MultiStatus( 184 PlatformUI.PLUGIN_ID, 185 IStatus.OK, 186 NLS 187 .bind( 188 WorkbenchMessages.WorkbenchPage_unableToSavePerspective, 189 page.getLabel()), null); 190 191 saveEditorState( memento); 192 193 IMemento viewMem = memento.createChild(IWorkbenchConstants.TAG_VIEWS); 194 195 IViewReference[] refs = page.getViewReferences(); 196 197 for (int i = 0; i < refs.length; i++) { 198 IViewReference viewReference = refs[i]; 199 String tagId = ViewFactory.getKey(viewReference); 200 if (tagId != null) { 201 IMemento childMem = viewMem 202 .createChild(IWorkbenchConstants.TAG_VIEW); 203 childMem.putString(IWorkbenchConstants.TAG_ID, tagId); 204 String name = viewReference.getPartName(); 205 if (name != null) { 206 childMem.putString(IWorkbenchConstants.TAG_PART_NAME, name); 207 } 208 } 209 } 210 211 IMemento perspectiveMemento = memento 213 .createChild(IWorkbenchConstants.TAG_PERSPECTIVES); 214 if (page.getPerspective() != null) { 215 perspectiveMemento.putString( 216 IWorkbenchConstants.TAG_ACTIVE_PERSPECTIVE, page 217 .getPerspective().getId()); 218 } 219 if (page.getActivePart() != null) { 220 if (page.getActivePart() instanceof IViewPart) { 221 IViewReference ref = (IViewReference) page.getReference(page 222 .getActivePart()); 223 if (ref != null) { 224 perspectiveMemento.putString( 225 IWorkbenchConstants.TAG_ACTIVE_PART, ViewFactory 226 .getKey(ref)); 227 } 228 } else { 229 perspectiveMemento.putString( 230 IWorkbenchConstants.TAG_ACTIVE_PART, page 231 .getActivePart().getSite().getId()); 232 } 233 } 234 235 IPerspectiveDescriptor[] perspectives = page.getOpenPerspectives(); 237 238 for (int i = 0; i < perspectives.length; i++) { 239 IPerspectiveDescriptor perspectiveDescriptor = perspectives[i]; 240 IMemento gChildMem = perspectiveMemento 241 .createChild(IWorkbenchConstants.TAG_PERSPECTIVE); 242 Perspective perspective = page 243 .findPerspective(perspectiveDescriptor); 244 perspective.saveState(gChildMem); 245 } 246 247 return result; 248 249 } 250 251 256 private void saveEditorState(IMemento memento) { 257 258 IMemento editorsMemento = memento 259 .createChild(IWorkbenchConstants.TAG_EDITORS); 260 IMemento editorArea = editorsMemento 261 .createChild(IWorkbenchConstants.TAG_AREA); 262 editorArea.putString(IWorkbenchConstants.TAG_ACTIVE_WORKBOOK, 263 EditorSashContainer.DEFAULT_WORKBOOK_ID); 264 IMemento info = editorArea.createChild(IWorkbenchConstants.TAG_INFO); 265 info.putString(IWorkbenchConstants.TAG_PART, 266 EditorSashContainer.DEFAULT_WORKBOOK_ID); 267 IMemento folder = info.createChild(IWorkbenchConstants.TAG_FOLDER); 268 folder.putInteger(IWorkbenchConstants.TAG_APPEARANCE, 269 PresentationFactoryUtil.ROLE_EDITOR); 270 folder.putInteger(IWorkbenchConstants.TAG_EXPANDED, 271 IStackPresentationSite.STATE_RESTORED); 272 IMemento presentation = folder 273 .createChild(IWorkbenchConstants.TAG_PRESENTATION); 274 presentation.putString(IWorkbenchConstants.TAG_ID, 275 getCurrentPresentationClassName()); 276 277 } 278 279 284 private String getCurrentPresentationClassName() { 285 286 String currentPresentationFactoryId = PrefUtil.getAPIPreferenceStore() 288 .getString( 289 IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID); 290 292 AbstractPresentationFactory factory = WorkbenchPlugin.getDefault() 293 .getPresentationFactory(currentPresentationFactoryId); 294 295 if (factory == null) 296 factory = WorkbenchPlugin.getDefault().getPresentationFactory( 297 IWorkbenchConstants.DEFAULT_PRESENTATION_ID); 298 return factory.getClass().getName(); 299 300 } 301 302 307 private IWorkbench getWorkbench() { 308 return PlatformUI.getWorkbench(); 309 } 310 311 316 public IStatus transferSettings(IPath newWorkspaceRoot) { 317 try { 318 File workspaceFile = createFileAndDirectories(newWorkspaceRoot); 319 320 if (workspaceFile == null) 321 return new Status( 322 IStatus.ERROR, 323 WorkbenchPlugin.PI_WORKBENCH, 324 WorkbenchMessages.WorkbenchSettings_CouldNotCreateDirectories); 325 326 FileOutputStream stream = new FileOutputStream (workspaceFile); 327 OutputStreamWriter writer = new OutputStreamWriter (stream, "utf-8"); XMLMemento memento = XMLMemento 329 .createWriteRoot(IWorkbenchConstants.TAG_WORKBENCH); 330 IStatus status = saveSettings(memento); 331 if (status.getSeverity() != IStatus.OK) 332 return status; 333 334 memento.save(writer); 335 writer.close(); 336 337 } catch (IOException e) { 338 return new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 339 WorkbenchMessages.Workbench_problemsSavingMsg, e); 340 341 } 342 343 return Status.OK_STATUS; 344 } 345 346 354 private File createFileAndDirectories(IPath newWorkspaceRoot) { 355 IPath newWorkspaceLocation = getNewWorkbenchStateLocation( 356 newWorkspaceRoot).append( 357 Workbench.DEFAULT_WORKBENCH_STATE_FILENAME); 358 File workspaceFile = new File (newWorkspaceLocation.toOSString()); 359 360 File parent = workspaceFile.getParentFile(); 361 if (!parent.exists()) { 362 if (!parent.mkdirs()) 363 return null; 364 } 365 366 return workspaceFile; 367 } 368 369 374 public String getName() { 375 return WorkbenchMessages.WorkbenchLayoutSettings_Name; 376 } 377 378 } 379 | Popular Tags |