001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.io.BufferedReader;
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.IOException;
016: import java.io.InputStreamReader;
017:
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.jface.dialogs.ErrorDialog;
020: import org.eclipse.jface.dialogs.MessageDialog;
021: import org.eclipse.swt.widgets.Shell;
022: import org.eclipse.ui.IMemento;
023: import org.eclipse.ui.IWorkingSet;
024: import org.eclipse.ui.IWorkingSetManager;
025: import org.eclipse.ui.WorkbenchException;
026: import org.eclipse.ui.XMLMemento;
027: import org.osgi.framework.BundleContext;
028: import org.osgi.framework.BundleListener;
029:
030: /**
031: * A working set manager stores working sets and provides property change
032: * notification when a working set is added or removed. Working sets are
033: * persisted whenever one is added or removed.
034: *
035: * @see IWorkingSetManager
036: * @since 2.0
037: */
038: public class WorkingSetManager extends AbstractWorkingSetManager
039: implements IWorkingSetManager, BundleListener {
040:
041: // Working set persistence
042: public static final String WORKING_SET_STATE_FILENAME = "workingsets.xml"; //$NON-NLS-1$
043:
044: public WorkingSetManager(BundleContext context) {
045: super (context);
046: }
047:
048: /*
049: * (non-Javadoc)
050: *
051: * @see org.eclipse.ui.IWorkingSetManager
052: */
053: public void addRecentWorkingSet(IWorkingSet workingSet) {
054: internalAddRecentWorkingSet(workingSet);
055: saveState();
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.eclipse.ui.IWorkingSetManager
062: */
063: public void addWorkingSet(IWorkingSet workingSet) {
064: super .addWorkingSet(workingSet);
065: saveState();
066: }
067:
068: /**
069: * Returns the file used as the persistence store, or <code>null</code> if
070: * there is no available file.
071: *
072: * @return the file used as the persistence store, or <code>null</code>
073: */
074: private File getWorkingSetStateFile() {
075: IPath path = WorkbenchPlugin.getDefault().getDataLocation();
076: if (path == null) {
077: return null;
078: }
079: path = path.append(WORKING_SET_STATE_FILENAME);
080: return path.toFile();
081: }
082:
083: /*
084: * (non-Javadoc)
085: *
086: * @see org.eclipse.ui.IWorkingSetManager
087: */
088: public void removeWorkingSet(IWorkingSet workingSet) {
089: if (internalRemoveWorkingSet(workingSet)) {
090: saveState();
091: }
092: }
093:
094: /**
095: * Reads the persistence store and creates the working sets stored in it.
096: */
097: public void restoreState() {
098: File stateFile = getWorkingSetStateFile();
099:
100: if (stateFile != null && stateFile.exists()) {
101: try {
102: FileInputStream input = new FileInputStream(stateFile);
103: BufferedReader reader = new BufferedReader(
104: new InputStreamReader(input, "utf-8")); //$NON-NLS-1$
105:
106: IMemento memento = XMLMemento.createReadRoot(reader);
107: restoreWorkingSetState(memento);
108: restoreMruList(memento);
109: reader.close();
110: } catch (IOException e) {
111: MessageDialog
112: .openError(
113: (Shell) null,
114: WorkbenchMessages.ProblemRestoringWorkingSetState_title,
115: WorkbenchMessages.ProblemRestoringWorkingSetState_message);
116: } catch (WorkbenchException e) {
117: ErrorDialog
118: .openError(
119: (Shell) null,
120: WorkbenchMessages.ProblemRestoringWorkingSetState_title,
121: WorkbenchMessages.ProblemRestoringWorkingSetState_message,
122: e.getStatus());
123: }
124: }
125: }
126:
127: /**
128: * Saves the working sets in the persistence store
129: */
130: private void saveState() {
131:
132: File stateFile = getWorkingSetStateFile();
133: if (stateFile == null) {
134: return;
135: }
136: try {
137: saveState(stateFile);
138: } catch (IOException e) {
139: stateFile.delete();
140: MessageDialog
141: .openError(
142: (Shell) null,
143: WorkbenchMessages.ProblemSavingWorkingSetState_title,
144: WorkbenchMessages.ProblemSavingWorkingSetState_message);
145: }
146: }
147:
148: /**
149: * Persists all working sets and fires a property change event for the
150: * changed working set. Should only be called by
151: * org.eclipse.ui.internal.WorkingSet.
152: *
153: * @param changedWorkingSet
154: * the working set that has changed
155: * @param propertyChangeId
156: * the changed property. one of CHANGE_WORKING_SET_CONTENT_CHANGE
157: * and CHANGE_WORKING_SET_NAME_CHANGE
158: */
159: public void workingSetChanged(IWorkingSet changedWorkingSet,
160: String propertyChangeId, Object oldValue) {
161: saveState();
162: super.workingSetChanged(changedWorkingSet, propertyChangeId,
163: oldValue);
164: }
165: }
|