1 11 package org.eclipse.ui.internal; 12 13 import java.io.BufferedReader ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStreamReader ; 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 39 public class WorkingSetManager extends AbstractWorkingSetManager implements 40 IWorkingSetManager, BundleListener { 41 42 public static final String WORKING_SET_STATE_FILENAME = "workingsets.xml"; 45 public WorkingSetManager(BundleContext context) { 46 super(context); 47 } 48 49 54 public void addRecentWorkingSet(IWorkingSet workingSet) { 55 internalAddRecentWorkingSet(workingSet); 56 saveState(); 57 } 58 59 64 public void addWorkingSet(IWorkingSet workingSet) { 65 super.addWorkingSet(workingSet); 66 saveState(); 67 } 68 69 75 private File 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 89 public void removeWorkingSet(IWorkingSet workingSet) { 90 if (internalRemoveWorkingSet(workingSet)) { 91 saveState(); 92 } 93 } 94 95 98 public void restoreState() { 99 File stateFile = getWorkingSetStateFile(); 100 101 if (stateFile != null && stateFile.exists()) { 102 try { 103 FileInputStream input = new FileInputStream (stateFile); 104 BufferedReader reader = new BufferedReader ( 105 new InputStreamReader (input, "utf-8")); 107 IMemento memento = XMLMemento.createReadRoot(reader); 108 restoreWorkingSetState(memento); 109 restoreMruList(memento); 110 reader.close(); 111 } catch (IOException 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 131 private void saveState() { 132 133 File stateFile = getWorkingSetStateFile(); 134 if (stateFile == null) { 135 return; 136 } 137 try { 138 saveState(stateFile); 139 } catch (IOException e) { 140 stateFile.delete(); 141 MessageDialog.openError((Shell) null, 142 WorkbenchMessages.ProblemSavingWorkingSetState_title, 143 WorkbenchMessages.ProblemSavingWorkingSetState_message); 144 } 145 } 146 147 158 public void workingSetChanged(IWorkingSet changedWorkingSet, 159 String propertyChangeId, Object oldValue) { 160 saveState(); 161 super.workingSetChanged(changedWorkingSet, propertyChangeId, oldValue); 162 } 163 } 164 | Popular Tags |