KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WorkbenchLayoutSettingsTransfer


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.ui.internal;
13
14 import java.io.File JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStreamWriter JavaDoc;
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 /**
46  * The WorkbenchSettings handles the recording and restoring of workbench
47  * settings.
48  *
49  * @since 3.3
50  *
51  */

52 public class WorkbenchLayoutSettingsTransfer extends WorkbenchSettingsTransfer {
53
54     // private static final String WORKBENCH_LAYOUT_PATH =
55
// ".metadata/.plugins/org.eclipse.ui.workbench/workbench.xml";
56
// //$NON-NLS-1$
57

58     /**
59      * Create a new instance of the receiver.
60      */

61     public WorkbenchLayoutSettingsTransfer() {
62         super();
63     }
64
65     /**
66      * Record the sharable workbench state in a document.
67      *
68      * @return {@link XMLMemento}
69      */

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             // don't use newWindow as parent because it has not yet been opened
76
// (bug 76724)
77
ErrorDialog.openError(null,
78                     WorkbenchMessages.Workbench_problemsSaving,
79                     WorkbenchMessages.Workbench_problemsSavingMsg, status);
80         }
81         return memento;
82     }
83
84     /**
85      * Save the settings to the memento.
86      *
87      * @param memento
88      * @return IStatus
89      */

90     private IStatus saveSettings(XMLMemento memento) {
91         MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID, IStatus.OK,
92                 WorkbenchMessages.Workbench_problemsSaving, null);
93
94         // Save the version number.
95
memento.putString(IWorkbenchConstants.TAG_VERSION,
96                 Workbench.VERSION_STRING[1]);
97
98         // Save the workbench windows.
99
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     /**
111      * Save the workbench window state.
112      *
113      * @param window
114      * @param memento
115      * @return IStatus
116      */

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         // Save each page.
135
IWorkbenchPage[] pages = window.getPages();
136         for (int i = 0; i < pages.length; i++) {
137             IWorkbenchPage page = pages[i];
138
139             // Save perspective.
140
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"); //$NON-NLS-1$
147
}
148
149             // Get the input.
150
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: " //$NON-NLS-1$
157
+ input
158                                     + ", because it does not adapt to IPersistableElement"); //$NON-NLS-1$
159

160                 } else {
161                     // Save input.
162
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     /**
175      * Save the state of the workbench page.
176      *
177      * @param page
178      * @param pageMem
179      * @return IStatus
180      */

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 JavaDoc 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 JavaDoc name = viewReference.getPartName();
205                 if (name != null) {
206                     childMem.putString(IWorkbenchConstants.TAG_PART_NAME, name);
207                 }
208             }
209         }
210
211         // Create persp block.
212
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         // Save each perspective in opened order
236
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     /**
252      * Save the editor state. Set it to be the defaults.
253      *
254      * @param memento
255      */

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     /**
280      * Get the name of the current presentation class name.
281      *
282      * @return String
283      */

284     private String JavaDoc getCurrentPresentationClassName() {
285
286         // update the current selection (used to look for changes on apply)
287
String JavaDoc currentPresentationFactoryId = PrefUtil.getAPIPreferenceStore()
288                 .getString(
289                         IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID);
290         // Workbench.getInstance().getPresentationId();
291

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     /**
303      * Return the workbench we are using.
304      *
305      * @return IWorkbench
306      */

307     private IWorkbench getWorkbench() {
308         return PlatformUI.getWorkbench();
309     }
310
311     /*
312      * (non-Javadoc)
313      *
314      * @see org.eclipse.ui.preferences.SettingsTransfer#transferSettings(org.eclipse.core.runtime.IPath)
315      */

316     public IStatus transferSettings(IPath newWorkspaceRoot) {
317         try {
318             File JavaDoc 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 JavaDoc stream = new FileOutputStream JavaDoc(workspaceFile);
327             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(stream, "utf-8"); //$NON-NLS-1$
328
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 JavaDoc 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     /**
347      * Create the parent directories for the workbench layout file and then
348      * return the File.
349      *
350      * @param newWorkspaceRoot
351      * @return File the new layout file. Return <code>null</code> if the file
352      * cannot be created.
353      */

354     private File JavaDoc createFileAndDirectories(IPath newWorkspaceRoot) {
355         IPath newWorkspaceLocation = getNewWorkbenchStateLocation(
356                 newWorkspaceRoot).append(
357                 Workbench.DEFAULT_WORKBENCH_STATE_FILENAME);
358         File JavaDoc workspaceFile = new File JavaDoc(newWorkspaceLocation.toOSString());
359
360         File JavaDoc parent = workspaceFile.getParentFile();
361         if (!parent.exists()) {
362             if (!parent.mkdirs())
363                 return null;
364         }
365
366         return workspaceFile;
367     }
368
369     /*
370      * (non-Javadoc)
371      *
372      * @see org.eclipse.ui.preferences.SettingsTransfer#getName()
373      */

374     public String JavaDoc getName() {
375         return WorkbenchMessages.WorkbenchLayoutSettings_Name;
376     }
377
378 }
379
Popular Tags