001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.filter;
043:
044: import java.io.IOException;
045: import java.util.List;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import java.util.Iterator;
049: import java.util.LinkedList;
050: import java.util.prefs.BackingStoreException;
051: import java.util.prefs.Preferences;
052: import org.openide.util.NbBundle;
053: import org.openide.util.NbPreferences;
054:
055: /**
056: * Set of filters
057: */
058: public final class FilterRepository {
059:
060: private static FilterRepository theInstance;
061:
062: /** the set of filters of this repository **/
063: private LinkedList<TaskFilter> filters = new LinkedList<TaskFilter>();
064: private int active = -1; // index of the active filter
065:
066: /**
067: * Constructor, default.
068: */
069: FilterRepository() {
070: }
071:
072: public static FilterRepository getDefault() {
073: if (null == theInstance) {
074: theInstance = new FilterRepository();
075: }
076: return theInstance;
077: }
078:
079: public void assign(final FilterRepository fr) {
080: if (fr != this ) {
081: filters.clear();
082: Iterator<TaskFilter> it = fr.filters.iterator();
083: while (it.hasNext()) {
084: filters.add((TaskFilter) it.next().clone());
085: }
086:
087: active = fr.active;
088: }
089: }
090:
091: public Object clone() {
092: FilterRepository ret = new FilterRepository();
093: ret.assign(this );
094: return ret;
095: }
096:
097: public List<TaskFilter> getAllFilters() {
098: return new ArrayList<TaskFilter>(filters);
099: }
100:
101: // Implementation of java.util.Set
102:
103: /**
104: * Adds a new filter to the collection, if it was not present
105: * already.
106: * @param f the Filter to be added
107: * @return true iff it was not member before and was added
108: */
109: void add(TaskFilter f) {
110: filters.add(f);
111: }
112:
113: /**
114: * Remove the filter specified by parameter from the collection.
115: *
116: * @param filter the Filter to remove
117: * @return true iff the filter was found and removed
118: */
119: void remove(TaskFilter f) {
120: if (f == getActive())
121: setActive(null);
122: filters.remove(f);
123: }
124:
125: Iterator<TaskFilter> iterator() {
126: return filters.iterator();
127: }
128:
129: int size() {
130: return filters.size();
131: }
132:
133: /**
134: * Returns a filter with the given name or null if not found.
135: * @param name name of the filter to look up
136: * @return Filter with name or null
137: */
138: TaskFilter getFilterByName(String name) {
139: Iterator<TaskFilter> it = filters.iterator();
140: while (it.hasNext()) {
141: TaskFilter f = it.next();
142: if (f.getName().equals(name))
143: return f;
144: }
145: return null;
146: }
147:
148: public TaskFilter getActive() {
149: return (active == -1) ? null : filters.get(active);
150: }
151:
152: public void setActive(TaskFilter newactive) {
153: if (newactive == null) {
154: this .active = -1;
155: } else {
156: int i = filters.indexOf(newactive);
157: if (i != -1) {
158: this .active = i;
159: }
160: }
161: }
162:
163: public void load() throws IOException {
164: filters.clear();
165: active = -1;
166: Preferences prefs = NbPreferences
167: .forModule(FilterRepository.class);
168: prefs = prefs.node("Filters"); //NOI18N
169: active = prefs.getInt("active", -1);
170:
171: int count = prefs.getInt("count", 0); //NOI18N
172: for (int i = 0; i < count; i++) {
173: TaskFilter filter = new TaskFilter();
174: try {
175: filter.load(prefs, "Filter_" + i); //NOI18N
176: } catch (BackingStoreException bsE) {
177: IOException ioE = new IOException(
178: "Cannot load filter repository"); //NOI18N
179: ioE.initCause(bsE);
180: throw ioE;
181: }
182: filters.add(filter);
183: }
184: //make sure there's some default filter on the first start
185: if (prefs.getBoolean("firstTimeStart", true)) { //NOI18N
186: prefs.putBoolean("firstTimeStart", false); //NOI18N
187: TaskFilter filter = createNewFilter();
188: filter.setName(NbBundle.getMessage(FilterRepository.class,
189: "LBL_DefaultFilter")); //NOI18N
190: TypesFilter types = new TypesFilter();
191: types.setTaskCountLimit(100);
192: filter.setTypesFilter(types);
193: filter.setKeywordsFilter(new KeywordsFilter());
194: filters.add(filter);
195: setActive(filter);
196:
197: save();
198: }
199: }
200:
201: public void save() throws IOException {
202: try {
203: Preferences prefs = NbPreferences
204: .forModule(FilterRepository.class);
205: prefs = prefs.node("Filters"); //NOI18N
206: prefs.clear();
207: prefs.putBoolean("firstTimeStart", false); //NOI18N
208:
209: prefs.putInt("count", filters.size()); //NOI18N
210: prefs.putInt("active", active); //NOI18N
211: for (int i = 0; i < filters.size(); i++) {
212: TaskFilter filter = filters.get(i);
213: filter.save(prefs, "Filter_" + i); //NOI18N
214: }
215: } catch (BackingStoreException bsE) {
216: IOException ioE = new IOException(
217: "Cannot save filter repository"); //NOI18N
218: ioE.initCause(bsE);
219: throw ioE;
220: }
221: }
222:
223: TaskFilter createNewFilter() {
224: return new TaskFilter(NbBundle.getMessage(
225: FilterRepository.class, "LBL_NewFilter")); //NOI18N
226: }
227:
228: void clear() {
229: filters.clear();
230: setActive(null);
231: }
232:
233: List<TaskFilter> getFilters() {
234: return new ArrayList<TaskFilter>(filters);
235: }
236: }
|