KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import edu.umd.cs.findbugs.BugInstance;
34 import edu.umd.cs.findbugs.Project;
35 import edu.umd.cs.findbugs.gui2.BugTreeModel.BranchOperationException;
36
37 /**
38  * This is the .fas file stored when projects are saved
39  * All project related information goes here. Anything that would be shared between multiple projects goes into GUISaveState instead
40  */

41 public class ProjectSettings implements Serializable JavaDoc
42 {
43     private static final long serialVersionUID = 6505872267795979672L;
44
45     // Singleton
46
private ProjectSettings()
47     {
48         allMatchers = new CompoundMatcher();
49         suppressionMatcher = new SuppressionMatcher();
50         filters = new ArrayList JavaDoc<FilterMatcher>();
51         allMatchers.add(suppressionMatcher);
52     }
53     private static ProjectSettings instance;
54     public static ProjectSettings newInstance()
55     {
56         instance = new ProjectSettings();
57         PreferencesFrame.getInstance().updateFilterPanel();
58         PreferencesFrame.getInstance().clearSuppressions();
59         return instance;
60     }
61     public static ProjectSettings getInstance()
62     {
63         if (instance == null)
64             instance = new ProjectSettings();
65         return instance;
66     }
67     
68     /**
69      * The matcher suppressing all user-selected bugs.
70      */

71     private SuppressionMatcher suppressionMatcher;
72     
73     /**
74      * The list of all defined filters
75      */

76     private ArrayList JavaDoc<FilterMatcher> filters;
77     
78     /**
79      * The CompoundMatcher enveloping all enabled matchers.
80      */

81     private CompoundMatcher allMatchers;
82         
83     /**
84      * Max number of previous comments stored.
85      */

86     private int maxSizeOfPreviousComments;
87         
88     public static void loadInstance(InputStream JavaDoc in)
89     {
90         try
91         {
92             instance = (ProjectSettings) new ObjectInputStream JavaDoc(in).readObject();
93             PreferencesFrame.getInstance().updateFilterPanel();
94             for (BugInstance bug: instance.getSuppressionMatcher())
95             {
96                 PreferencesFrame.getInstance().suppressionsChanged(new BugLeafNode(bug));
97             }
98         }
99         catch (ClassNotFoundException JavaDoc e)
100         {
101             if (MainFrame.DEBUG) System.err.println("Error in deserializing Settings:");
102             Debug.println(e);
103         }
104         catch (IOException JavaDoc e)
105         {
106             if (MainFrame.DEBUG) System.err.println("IO error in deserializing Settings:");
107             Debug.println(e);
108             instance=newInstance();
109         }
110     }
111     
112     public void save(OutputStream JavaDoc out)
113     {
114         try
115         {
116             new ObjectOutputStream JavaDoc(out).writeObject(this);
117         }
118         catch (IOException JavaDoc e)
119         {
120             if (MainFrame.DEBUG) System.err.println("Error serializing Settings:");
121             Debug.println(e);
122         }
123     }
124     
125     SuppressionMatcher getSuppressionMatcher()
126     {
127         return suppressionMatcher;
128     }
129     
130     void setSuppressionMatcher(SuppressionMatcher suppressionMatcher)
131     {
132         this.suppressionMatcher = suppressionMatcher;
133     }
134     
135     CompoundMatcher getAllMatchers()
136     {
137         return allMatchers;
138     }
139     
140     public void addFilter(FilterMatcher filter)
141     {
142         filters.add(filter);
143         allMatchers.add(filter);
144         if (!(filter instanceof StackedFilterMatcher))
145             FilterMatcher.notifyListeners(FilterListener.FILTERING,null);
146         else
147         {
148             StackedFilterMatcher theSame= (StackedFilterMatcher) filter;
149             FilterMatcher[] filtersInStack=theSame.getFilters();
150             ArrayList JavaDoc<Sortables> order=MainFrame.getInstance().getSorter().getOrder();
151             int sizeToCheck=filtersInStack.length;
152             List JavaDoc<Sortables> sortablesToCheck=order.subList(0, sizeToCheck);
153             Debug.println("Size to check" + sizeToCheck + " checking list" + sortablesToCheck);
154             Debug.println("checking filters");
155             ArrayList JavaDoc<String JavaDoc> almostPath=new ArrayList JavaDoc<String JavaDoc>();
156             ArrayList JavaDoc<Sortables> almostPathSortables=new ArrayList JavaDoc<Sortables>();
157             for (int x=0; x< sortablesToCheck.size();x++)
158             {
159                 Sortables s=sortablesToCheck.get(x);
160                 for (FilterMatcher fm:filtersInStack)
161                 {
162                     if (s.equals(fm.getFilterBy()))
163                     {
164                         almostPath.add(fm.getValue());
165                         almostPathSortables.add(fm.getFilterBy());
166                     }
167                 }
168             }
169             if (almostPath.size()==filtersInStack.length)
170             {
171                 ArrayList JavaDoc<String JavaDoc> finalPath=new ArrayList JavaDoc<String JavaDoc>();
172                 for (int x=0;x<almostPath.size();x++)
173                 {
174                     Sortables s=almostPathSortables.get(x);
175                     if (MainFrame.getInstance().getSorter().getOrderBeforeDivider().contains(s))
176                         finalPath.add(almostPath.get(x));
177                 }
178                 BugTreeModel model=((BugTreeModel)(MainFrame.getInstance().getTree().getModel()));
179                 try {
180                     model.sendEvent(model.removeBranch(finalPath), 0);//0 is for remove
181
}
182                 catch (BranchOperationException e)
183                 {
184                     throw new IllegalStateException JavaDoc("They added a stacked filter on a branch that doesn't exist... Whaa?");
185                 }
186             }
187             else
188             {
189                 FilterMatcher.notifyListeners(FilterListener.FILTERING,null);
190                 throw new IllegalStateException JavaDoc("What huh? How'd they add a stacked filter matcher bigger than the number of branches in the tree?!");
191             }
192         }
193         PreferencesFrame.getInstance().updateFilterPanel();
194         MainFrame.getInstance().updateStatusBar();
195     }
196     
197     public void addFilters(FilterMatcher[] newFilters)
198     {
199         for (FilterMatcher i : newFilters)
200             if (!filters.contains(i))
201             {
202                 filters.add(i);
203                 allMatchers.add(i);
204             }
205             else //if filters contains i
206
{
207                 filters.get(filters.indexOf(i)).setActive(true);
208                 //FIXME Do I need to do this for allMatchers too? Or are the filters all the same, with both just holding references?
209
}
210         FilterMatcher.notifyListeners(FilterListener.FILTERING, null);
211         PreferencesFrame.getInstance().updateFilterPanel();
212         MainFrame.getInstance().updateStatusBar();
213     }
214     
215     public boolean removeFilter(FilterMatcher filter)
216     {
217         boolean result = filters.remove(filter) && allMatchers.remove(filter);
218         FilterMatcher.notifyListeners(FilterListener.UNFILTERING,null);
219         PreferencesFrame.getInstance().updateFilterPanel();
220         MainFrame.getInstance().updateStatusBar();
221         return result;
222     }
223     
224     ArrayList JavaDoc<FilterMatcher> getAllFilters()
225     {
226         return filters;
227     }
228     
229     /**
230      * @return Returns the maximum number of previous comments stored.
231      */

232     public int getMaxSizeOfPreviousComments(){
233         return maxSizeOfPreviousComments;
234     }
235     
236     /**
237      * Sets the maximum number of previous comments stored.
238      * @param num
239      */

240     public void setMaxSizeOfPreviousComments(int num){
241         maxSizeOfPreviousComments = num;
242     }
243 }
Popular Tags