KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.jface.dialogs.ErrorDialog;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.IWorkingSet;
25 import org.eclipse.ui.IWorkingSetManager;
26 import org.eclipse.ui.WorkbenchException;
27 import org.eclipse.ui.XMLMemento;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.BundleListener;
30
31 /**
32  * A working set manager stores working sets and provides property change
33  * notification when a working set is added or removed. Working sets are
34  * persisted whenever one is added or removed.
35  *
36  * @see IWorkingSetManager
37  * @since 2.0
38  */

39 public class WorkingSetManager extends AbstractWorkingSetManager implements
40         IWorkingSetManager, BundleListener {
41
42     // Working set persistence
43
public static final String JavaDoc WORKING_SET_STATE_FILENAME = "workingsets.xml"; //$NON-NLS-1$
44

45     public WorkingSetManager(BundleContext context) {
46         super(context);
47     }
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.eclipse.ui.IWorkingSetManager
53      */

54     public void addRecentWorkingSet(IWorkingSet workingSet) {
55         internalAddRecentWorkingSet(workingSet);
56         saveState();
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.eclipse.ui.IWorkingSetManager
63      */

64     public void addWorkingSet(IWorkingSet workingSet) {
65         super.addWorkingSet(workingSet);
66         saveState();
67     }
68
69     /**
70      * Returns the file used as the persistence store, or <code>null</code> if
71      * there is no available file.
72      *
73      * @return the file used as the persistence store, or <code>null</code>
74      */

75     private File JavaDoc getWorkingSetStateFile() {
76         IPath path = WorkbenchPlugin.getDefault().getDataLocation();
77         if (path == null) {
78             return null;
79         }
80         path = path.append(WORKING_SET_STATE_FILENAME);
81         return path.toFile();
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.eclipse.ui.IWorkingSetManager
88      */

89     public void removeWorkingSet(IWorkingSet workingSet) {
90         if (internalRemoveWorkingSet(workingSet)) {
91             saveState();
92         }
93     }
94
95     /**
96      * Reads the persistence store and creates the working sets stored in it.
97      */

98     public void restoreState() {
99         File JavaDoc stateFile = getWorkingSetStateFile();
100
101         if (stateFile != null && stateFile.exists()) {
102             try {
103                 FileInputStream JavaDoc input = new FileInputStream JavaDoc(stateFile);
104                 BufferedReader JavaDoc reader = new BufferedReader JavaDoc(
105                         new InputStreamReader JavaDoc(input, "utf-8")); //$NON-NLS-1$
106

107                 IMemento memento = XMLMemento.createReadRoot(reader);
108                 restoreWorkingSetState(memento);
109                 restoreMruList(memento);
110                 reader.close();
111             } catch (IOException JavaDoc e) {
112                 MessageDialog
113                         .openError(
114                                 (Shell) null,
115                                 WorkbenchMessages.ProblemRestoringWorkingSetState_title,
116                                 WorkbenchMessages.ProblemRestoringWorkingSetState_message);
117             } catch (WorkbenchException e) {
118                 ErrorDialog
119                         .openError(
120                                 (Shell) null,
121                                 WorkbenchMessages.ProblemRestoringWorkingSetState_title,
122                                 WorkbenchMessages.ProblemRestoringWorkingSetState_message,
123                                 e.getStatus());
124             }
125         }
126     }
127
128     /**
129      * Saves the working sets in the persistence store
130      */

131     private void saveState() {
132
133         File JavaDoc stateFile = getWorkingSetStateFile();
134         if (stateFile == null) {
135             return;
136         }
137         try {
138             saveState(stateFile);
139         } catch (IOException JavaDoc e) {
140             stateFile.delete();
141             MessageDialog.openError((Shell) null,
142                     WorkbenchMessages.ProblemSavingWorkingSetState_title,
143                     WorkbenchMessages.ProblemSavingWorkingSetState_message);
144         }
145     }
146
147     /**
148      * Persists all working sets and fires a property change event for the
149      * changed working set. Should only be called by
150      * org.eclipse.ui.internal.WorkingSet.
151      *
152      * @param changedWorkingSet
153      * the working set that has changed
154      * @param propertyChangeId
155      * the changed property. one of CHANGE_WORKING_SET_CONTENT_CHANGE
156      * and CHANGE_WORKING_SET_NAME_CHANGE
157      */

158     public void workingSetChanged(IWorkingSet changedWorkingSet,
159             String JavaDoc propertyChangeId, Object JavaDoc oldValue) {
160         saveState();
161         super.workingSetChanged(changedWorkingSet, propertyChangeId, oldValue);
162     }
163 }
164
Popular Tags