1 19 20 package edu.umd.cs.findbugs.gui2; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.OutputStream ; 31 import java.io.Serializable ; 32 import java.io.StreamTokenizer ; 33 import java.io.StringReader ; 34 import java.awt.Rectangle ; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.LinkedList ; 38 import java.util.Map ; 39 import java.util.prefs.BackingStoreException ; 40 import java.util.prefs.Preferences ; 41 42 import edu.umd.cs.findbugs.Project; 43 import edu.umd.cs.findbugs.SystemProperties; 44 45 54 public class GUISaveState{ 56 57 private static GUISaveState instance; 58 private static final String SORTERTABLELENGTH="Sorter Length"; 60 private static final String PREVCOMMENTSSIZE="Previous Comments Size"; 61 private static final String PREFERENCESDIRECTORY="Preference Directory"; 62 private static final String DOCKINGLAYOUT="Docking Layout"; 63 private static final String FRAME_BOUNDS="Frame Bounds"; 64 65 private static final int MAXNUMRECENTPROJECTS= 5; 66 private static final Sortables[] DEFAULT_COLUMN_HEADERS = new Sortables[] { 67 Sortables.CATEGORY, Sortables.BUGCODE, Sortables.TYPE, Sortables.DIVIDER, Sortables.PRIORITY }; 68 69 private static final String [] RECENTPROJECTKEYS=new String [MAXNUMRECENTPROJECTS]; static 71 { 72 for (int x=0; x<RECENTPROJECTKEYS.length;x++) 73 { 74 RECENTPROJECTKEYS[x]="Project"+x; 75 } 76 } 77 private static final int MAXNUMPREVCOMMENTS= 10; 78 private static final String [] COMMENTKEYS= new String [MAXNUMPREVCOMMENTS]; 79 static 80 { 81 for (int x=0; x<COMMENTKEYS.length;x++) 82 { 83 COMMENTKEYS[x]="Comment"+x; 84 } 85 } 86 private static final String NUMPROJECTS= "NumberOfProjectsToLoad"; 87 private static final String STARTERDIRECTORY= "Starter Directory"; 88 private File starterDirectoryForLoadBugs; 89 92 private LinkedList <String > previousComments; 93 private boolean useDefault=false; 94 private SorterTableColumnModel starterTable; 95 private ArrayList <File > recentProjects; 96 private byte[] dockingLayout; 97 private Rectangle frameBounds; 98 99 public byte[] getDockingLayout() 100 { 101 return dockingLayout; 102 } 103 104 public void setDockingLayout(byte[] dockingLayout) 105 { 106 this.dockingLayout = dockingLayout; 107 } 108 109 private static String [] generateSorterKeys(int numSorters) 110 { 111 String [] result= new String [numSorters]; 112 for (int x=0; x< result.length;x++) 113 { 114 result[x]="Sorter"+x; 115 } 116 return result; 117 } 118 119 SorterTableColumnModel getStarterTable() 120 { 121 if (useDefault || (starterTable == null)) 122 starterTable=new SorterTableColumnModel(GUISaveState.DEFAULT_COLUMN_HEADERS); 123 124 return starterTable; 125 } 126 127 private GUISaveState() 128 { 129 recentProjects=new ArrayList <File >(); 130 previousComments=new LinkedList <String >(); 132 } 133 134 public static GUISaveState getInstance() 135 { 136 if (instance==null) 137 instance=new GUISaveState(); 138 return instance; 139 } 140 141 public ArrayList <File > getRecentProjects() 142 { 143 return recentProjects; 144 } 145 146 public void addRecentProject(File f) 147 { 148 recentProjects.add(f); 149 } 150 151 public void projectReused(File f) 152 { 153 if (!recentProjects.contains(f)) 154 { 155 throw new IllegalStateException ("Selected a recent project that doesn't exist?"); 156 } 157 else 158 { 159 recentProjects.remove(f); 160 recentProjects.add(f); 161 } 162 } 163 164 public void projectNotFound(File f) 165 { 166 if (!recentProjects.contains(f)) 167 { 168 throw new IllegalStateException ("Well no wonder it wasn't found, its not in the list."); 169 } 170 else 171 recentProjects.remove(f); 172 } 173 177 public File getStarterDirectoryForLoadBugs() { 178 return starterDirectoryForLoadBugs; 179 } 180 181 184 public void setStarterDirectoryForLoadBugs(File f) { 185 this.starterDirectoryForLoadBugs = f; 186 } 187 188 189 public static void loadInstance() 190 { 191 GUISaveState newInstance=new GUISaveState(); 192 newInstance.recentProjects=new ArrayList <File >(); 193 Preferences p=Preferences.userNodeForPackage(GUISaveState.class); 194 195 newInstance.starterDirectoryForLoadBugs=new File (p.get(GUISaveState.STARTERDIRECTORY, SystemProperties.getProperty("user.dir"))); 196 197 int prevCommentsSize=p.getInt(GUISaveState.PREVCOMMENTSSIZE, 0); 198 199 for (int x=0;x<prevCommentsSize;x++) 200 { 201 String comment=p.get(GUISaveState.COMMENTKEYS[x], ""); 202 newInstance.previousComments.add(comment); 203 } 204 205 int size=Math.min(MAXNUMRECENTPROJECTS,p.getInt(GUISaveState.NUMPROJECTS,0)); 206 for (int x=0;x<size;x++) 207 { 208 newInstance.recentProjects.add(new File (p.get(GUISaveState.RECENTPROJECTKEYS[x],""))); 209 } 210 211 int sorterSize=p.getInt(GUISaveState.SORTERTABLELENGTH,-1); 212 if (sorterSize!=-1) 213 { 214 Sortables[] sortColumns=new Sortables[sorterSize]; 215 String [] sortKeys=GUISaveState.generateSorterKeys(sorterSize); 216 for (int x=0;x<sorterSize;x++) 217 { 218 sortColumns[x]=Sortables.getSortableByPrettyName(p.get(sortKeys[x], "*none*")); 219 if (sortColumns[x]==null) 220 { 221 if (MainFrame.DEBUG) System.err.println("Sort order was corrupted, using default sort order"); 222 newInstance.useDefault=true; 223 } 224 } 225 if(!newInstance.useDefault) 226 newInstance.starterTable=new SorterTableColumnModel(sortColumns); 227 } 228 else 229 newInstance.useDefault=true; 230 231 newInstance.dockingLayout = p.getByteArray(DOCKINGLAYOUT, new byte[0]); 232 233 String boundsString = p.get(FRAME_BOUNDS, null); 234 Rectangle r = new Rectangle (0, 0, 800, 650); 235 if (boundsString != null) { 236 String [] a = boundsString.split(",", 4); 237 if (a.length > 0) try { 238 r.x = Math.max(0, Integer.parseInt(a[0])); 239 } catch (NumberFormatException nfe) { assert true; } 240 if (a.length > 1) try { 241 r.y = Math.max(0, Integer.parseInt(a[1])); 242 } catch (NumberFormatException nfe) { assert true; } 243 if (a.length > 2) try { 244 r.width = Math.max(40, Integer.parseInt(a[2])); 245 } catch (NumberFormatException nfe) { assert true; } 246 if (a.length > 3) try { 247 r.height = Math.max(40, Integer.parseInt(a[3])); 248 } catch (NumberFormatException nfe) { assert true; } 249 } 250 newInstance.frameBounds = r; 251 252 instance=newInstance; 253 } 254 255 public void save() 256 { 257 Preferences p=Preferences.userNodeForPackage(GUISaveState.class); 258 259 int sorterLength=MainFrame.getInstance().getSorter().getColumnCount(); 260 ArrayList <Sortables> sortables=MainFrame.getInstance().getSorter().getOrder(); 261 p.putInt(GUISaveState.SORTERTABLELENGTH, sorterLength); 262 263 String [] sorterKeys=GUISaveState.generateSorterKeys(sorterLength); 264 for (int x=0; x<sorterKeys.length;x++) 265 { 266 p.put(sorterKeys[x], sortables.get(x).prettyName); 267 } 268 269 p.putInt(GUISaveState.PREVCOMMENTSSIZE, previousComments.size()); 270 271 for (int x=0; x<previousComments.size();x++) 272 { 273 String comment=previousComments.get(x); 274 p.put(GUISaveState.COMMENTKEYS[x], comment); 275 } 276 277 int size=recentProjects.size(); 278 while (recentProjects.size()>MAXNUMRECENTPROJECTS) 279 { 280 recentProjects.remove(0); 281 } 282 p.putInt(GUISaveState.NUMPROJECTS,Math.min(size,MAXNUMRECENTPROJECTS)); 283 for (int x=0; x<Math.min(size,MAXNUMRECENTPROJECTS);x++) 284 { 285 File file=recentProjects.get(x); 286 p.put(GUISaveState.RECENTPROJECTKEYS[x],file.getAbsolutePath()); 287 } 288 289 p.putByteArray(DOCKINGLAYOUT, dockingLayout); 290 291 p.put(FRAME_BOUNDS, frameBounds.x+","+frameBounds.y+","+frameBounds.width+","+frameBounds.height); 292 } 293 294 static void clear() 295 { 296 Preferences p=Preferences.userNodeForPackage(GUISaveState.class); 297 try { 298 p.clear(); 299 } catch (BackingStoreException e) { 300 Debug.println(e); 301 } 302 instance=new GUISaveState(); 303 } 304 305 308 public LinkedList <String > getPreviousComments() { 309 return previousComments; 310 } 311 312 315 public void setPreviousComments(LinkedList <String > previousComments) { 316 this.previousComments = previousComments; 317 } 318 319 322 public Rectangle getFrameBounds() { 323 return frameBounds; 324 } 325 326 329 public void setFrameBounds(Rectangle frameBounds) { 330 this.frameBounds = frameBounds; 331 } 332 } | Popular Tags |