001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.io.OutputStream;
027: import java.io.Serializable;
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import edu.umd.cs.findbugs.Project;
032: import edu.umd.cs.findbugs.filter.Filter;
033: import edu.umd.cs.findbugs.filter.LastVersionMatcher;
034: import edu.umd.cs.findbugs.gui2.BugTreeModel.BranchOperationException;
035:
036: /**
037: * This is the .fas file stored when projects are saved
038: * All project related information goes here. Anything that would be shared between multiple projects goes into GUISaveState instead
039: */
040: @Deprecated
041: public class ProjectSettings implements Serializable {
042: private static final long serialVersionUID = 6505872267795979672L;
043:
044: // Singleton
045: private ProjectSettings() {
046: allMatchers = new CompoundMatcher();
047: filters = new ArrayList<FilterMatcher>();
048: }
049:
050: private static ProjectSettings instance;
051:
052: public static ProjectSettings newInstance() {
053: instance = new ProjectSettings();
054: LastVersionMatcher dbf = LastVersionMatcher.DEAD_BUG_MATCHER;
055:
056: //Important: add the deadbug filter directly to filters and allmatchers, dont go through addFilter, otherwise it causes a
057: //tree to rebuild.
058: MainFrame.getInstance().getProject().getSuppressionFilter()
059: .addChild(dbf);
060: PreferencesFrame.getInstance().updateFilterPanel();
061: return instance;
062: }
063:
064: public static synchronized ProjectSettings getInstance() {
065: if (instance == null)
066: instance = new ProjectSettings();
067: return instance;
068: }
069:
070: /**
071: * The list of all defined filters
072: */
073: private ArrayList<FilterMatcher> filters;
074:
075: /**
076: * The CompoundMatcher enveloping all enabled matchers.
077: */
078: private CompoundMatcher allMatchers;
079:
080: /**
081: * Max number of previous comments stored.
082: */
083: private int maxSizeOfPreviousComments;
084:
085: public static void loadInstance(InputStream in) {
086: try {
087: instance = (ProjectSettings) new ObjectInputStream(in)
088: .readObject();
089: PreferencesFrame.getInstance().updateFilterPanel();
090: in.close();
091: } catch (ClassNotFoundException e) {
092: if (MainFrame.DEBUG)
093: System.err.println("Error in deserializing Settings:");
094: Debug.println(e);
095: } catch (IOException e) {
096: if (MainFrame.DEBUG)
097: System.err
098: .println("IO error in deserializing Settings:");
099: Debug.println(e);
100: instance = newInstance();
101: }
102: }
103:
104: public void save(OutputStream out) {
105: try {
106: new ObjectOutputStream(out).writeObject(this );
107: } catch (IOException e) {
108: if (MainFrame.DEBUG)
109: System.err.println("Error serializing Settings:");
110: Debug.println(e);
111: } finally {
112: try {
113: out.close();
114: } catch (IOException e) {
115: // nothing to do
116: assert true;
117: }
118: }
119: }
120:
121: @Deprecated
122: Filter getSuppressionFilter() {
123: Project project = MainFrame.getInstance().getProject();
124: if (project == null)
125: throw new NullPointerException("project is null");
126: return project.getSuppressionFilter();
127: }
128:
129: public void addFilter(FilterMatcher filter) {
130: filters.add(filter);
131: allMatchers.add(filter);
132: if (!(filter instanceof StackedFilterMatcher))
133: FilterActivity.notifyListeners(
134: FilterListener.Action.FILTERING, null);
135: else {
136: StackedFilterMatcher theSame = (StackedFilterMatcher) filter;
137: FilterMatcher[] filtersInStack = theSame.getFilters();
138: ArrayList<Sortables> order = MainFrame.getInstance()
139: .getSorter().getOrder();
140: int sizeToCheck = filtersInStack.length;
141: List<Sortables> sortablesToCheck = order.subList(0,
142: sizeToCheck);
143: Debug.println("Size to check" + sizeToCheck
144: + " checking list" + sortablesToCheck);
145: Debug.println("checking filters");
146: ArrayList<String> almostPath = new ArrayList<String>();
147: ArrayList<Sortables> almostPathSortables = new ArrayList<Sortables>();
148: for (int x = 0; x < sortablesToCheck.size(); x++) {
149: Sortables s = sortablesToCheck.get(x);
150: for (FilterMatcher fm : filtersInStack) {
151: if (s.equals(fm.getFilterBy())) {
152: almostPath.add(fm.getValue());
153: almostPathSortables.add(fm.getFilterBy());
154: }
155: }
156: }
157: if (almostPath.size() == filtersInStack.length) {
158: ArrayList<String> finalPath = new ArrayList<String>();
159: for (int x = 0; x < almostPath.size(); x++) {
160: Sortables s = almostPathSortables.get(x);
161: if (MainFrame.getInstance().getSorter()
162: .getOrderBeforeDivider().contains(s))
163: finalPath.add(almostPath.get(x));
164: }
165: BugTreeModel model = (MainFrame.getInstance()
166: .getBugTreeModel());
167: try {
168: model.sendEvent(model.removeBranch(finalPath),
169: BugTreeModel.TreeModification.REMOVE);
170: } catch (BranchOperationException e) {
171: throw new IllegalStateException(
172: "They added a stacked filter on a branch that doesn't exist... Whaa?");
173: }
174: } else {
175: FilterActivity.notifyListeners(
176: FilterListener.Action.FILTERING, null);
177: throw new IllegalStateException(
178: "What huh? How'd they add a stacked filter matcher bigger than the number of branches in the tree?!");
179: }
180: }
181: PreferencesFrame.getInstance().updateFilterPanel();
182: MainFrame.getInstance().updateStatusBar();
183: }
184:
185: public void addFilters(FilterMatcher[] newFilters) {
186: for (FilterMatcher i : newFilters)
187: if (!filters.contains(i)) {
188: filters.add(i);
189: allMatchers.add(i);
190: } else //if filters contains i
191: {
192: filters.get(filters.indexOf(i)).setActive(true);
193: //FIXME Do I need to do this for allMatchers too? Or are the filters all the same, with both just holding references?
194: }
195: FilterActivity.notifyListeners(FilterListener.Action.FILTERING,
196: null);
197: PreferencesFrame.getInstance().updateFilterPanel();
198: MainFrame.getInstance().updateStatusBar();
199: }
200:
201: public boolean removeFilter(FilterMatcher filter) {
202: boolean result = filters.remove(filter)
203: && allMatchers.remove(filter);
204: FilterActivity.notifyListeners(
205: FilterListener.Action.UNFILTERING, null);
206: PreferencesFrame.getInstance().updateFilterPanel();
207: MainFrame.getInstance().updateStatusBar();
208: return result;
209: }
210:
211: ArrayList<FilterMatcher> getAllFilters() {
212: return filters;
213: }
214:
215: /**
216: * @return Returns the maximum number of previous comments stored.
217: */
218: public int getMaxSizeOfPreviousComments() {
219: return maxSizeOfPreviousComments;
220: }
221:
222: /**
223: * Sets the maximum number of previous comments stored.
224: * @param num
225: */
226: public void setMaxSizeOfPreviousComments(int num) {
227: maxSizeOfPreviousComments = num;
228: }
229: }
|