KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > GUISaveState


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.gui2;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.io.StreamTokenizer JavaDoc;
33 import java.io.StringReader JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.LinkedList JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.prefs.BackingStoreException JavaDoc;
40 import java.util.prefs.Preferences JavaDoc;
41
42 import edu.umd.cs.findbugs.Project;
43 import edu.umd.cs.findbugs.SystemProperties;
44
45 /**
46  * Saves all the stuff that should be saved for each run,
47  * like recent projects, previous comments, the current docking layout
48  * and the sort order
49  *
50  * For project related things, look in ProjectSettings
51  * @author Dan
52  *
53  */

54 //GUISaveState uses the Preferences API, dont look for a file anywhere, there isn't one, well... there might be, but its all system dependent where it is and how its stored
55
public class GUISaveState{
56
57     private static GUISaveState instance;
58 // private static final String PREVCOMMENTS="Previous Comments";
59
private static final String JavaDoc SORTERTABLELENGTH="Sorter Length";
60     private static final String JavaDoc PREVCOMMENTSSIZE="Previous Comments Size";
61     private static final String JavaDoc PREFERENCESDIRECTORY="Preference Directory";
62     private static final String JavaDoc DOCKINGLAYOUT="Docking Layout";
63     private static final String JavaDoc 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 JavaDoc[] RECENTPROJECTKEYS=new String JavaDoc[MAXNUMRECENTPROJECTS];//{"Project1","Project2","Project3","Project4","Project5"};//Make MAXNUMRECENTPROJECTS of these
70
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 JavaDoc[] COMMENTKEYS= new String JavaDoc[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 JavaDoc NUMPROJECTS= "NumberOfProjectsToLoad";
87     private static final String JavaDoc STARTERDIRECTORY= "Starter Directory";
88     private File JavaDoc starterDirectoryForLoadBugs;
89     /**
90      * List of previous comments by the user.
91      */

92     private LinkedList JavaDoc<String JavaDoc> previousComments;
93     private boolean useDefault=false;
94     private SorterTableColumnModel starterTable;
95     private ArrayList JavaDoc<File JavaDoc> recentProjects;
96     private byte[] dockingLayout;
97     private Rectangle JavaDoc 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 JavaDoc[] generateSorterKeys(int numSorters)
110     {
111         String JavaDoc[] result= new String JavaDoc[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 JavaDoc<File JavaDoc>();
130 // projectsToLocations=new HashMap<String,String>();
131
previousComments=new LinkedList JavaDoc<String JavaDoc>();
132     }
133     
134     public static GUISaveState getInstance()
135     {
136         if (instance==null)
137             instance=new GUISaveState();
138         return instance;
139     }
140     
141     public ArrayList JavaDoc<File JavaDoc> getRecentProjects()
142     {
143         return recentProjects;
144     }
145         
146     public void addRecentProject(File JavaDoc f)
147     {
148         recentProjects.add(f);
149     }
150     
151     public void projectReused(File JavaDoc f)
152     {
153         if (!recentProjects.contains(f))
154         {
155             throw new IllegalStateException JavaDoc("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 JavaDoc f)
165     {
166         if (!recentProjects.contains(f))
167         {
168             throw new IllegalStateException JavaDoc("Well no wonder it wasn't found, its not in the list.");
169         }
170         else
171             recentProjects.remove(f);
172     }
173     /**
174      * The file to start the loading of Bugs from.
175      * @return Returns the starterDirectoryForLoadBugs.
176      */

177     public File JavaDoc getStarterDirectoryForLoadBugs() {
178         return starterDirectoryForLoadBugs;
179     }
180
181     /**
182      * @param f The starterDirectoryForLoadBugs to set.
183      */

184     public void setStarterDirectoryForLoadBugs(File JavaDoc f) {
185         this.starterDirectoryForLoadBugs = f;
186     }
187
188     
189     public static void loadInstance()
190     {
191         GUISaveState newInstance=new GUISaveState();
192         newInstance.recentProjects=new ArrayList JavaDoc<File JavaDoc>();
193         Preferences JavaDoc p=Preferences.userNodeForPackage(GUISaveState.class);
194         
195         newInstance.starterDirectoryForLoadBugs=new File JavaDoc(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 JavaDoc 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 JavaDoc(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 JavaDoc[] 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 JavaDoc boundsString = p.get(FRAME_BOUNDS, null);
234         Rectangle JavaDoc r = new Rectangle JavaDoc(0, 0, 800, 650);
235         if (boundsString != null) {
236             String JavaDoc[] a = boundsString.split(",", 4);
237             if (a.length > 0) try {
238                 r.x = Math.max(0, Integer.parseInt(a[0]));
239             } catch (NumberFormatException JavaDoc nfe) { assert true; }
240             if (a.length > 1) try {
241                 r.y = Math.max(0, Integer.parseInt(a[1]));
242             } catch (NumberFormatException JavaDoc nfe) { assert true; }
243             if (a.length > 2) try {
244                 r.width = Math.max(40, Integer.parseInt(a[2]));
245             } catch (NumberFormatException JavaDoc nfe) { assert true; }
246             if (a.length > 3) try {
247                 r.height = Math.max(40, Integer.parseInt(a[3]));
248             } catch (NumberFormatException JavaDoc nfe) { assert true; }
249         }
250         newInstance.frameBounds = r;
251         
252         instance=newInstance;
253     }
254     
255     public void save()
256     {
257         Preferences JavaDoc p=Preferences.userNodeForPackage(GUISaveState.class);
258         
259         int sorterLength=MainFrame.getInstance().getSorter().getColumnCount();
260         ArrayList JavaDoc<Sortables> sortables=MainFrame.getInstance().getSorter().getOrder();
261         p.putInt(GUISaveState.SORTERTABLELENGTH, sorterLength);
262         
263         String JavaDoc[] 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 JavaDoc 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 JavaDoc 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 JavaDoc p=Preferences.userNodeForPackage(GUISaveState.class);
297         try {
298             p.clear();
299         } catch (BackingStoreException JavaDoc e) {
300             Debug.println(e);
301         }
302         instance=new GUISaveState();
303     }
304     
305     /**
306      * @return Returns the previousComments.
307      */

308     public LinkedList JavaDoc<String JavaDoc> getPreviousComments() {
309         return previousComments;
310     }
311
312     /**
313      * @param previousComments The previousComments to set.
314      */

315     public void setPreviousComments(LinkedList JavaDoc<String JavaDoc> previousComments) {
316         this.previousComments = previousComments;
317     }
318
319     /**
320      * @return Returns the frame bounds Rectangle.
321      */

322     public Rectangle JavaDoc getFrameBounds() {
323         return frameBounds;
324     }
325
326     /**
327      * @param frameBounds The frame bourds Rectangle to set.
328      */

329     public void setFrameBounds(Rectangle JavaDoc frameBounds) {
330         this.frameBounds = frameBounds;
331     }
332 }
Popular Tags