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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.common.filters;
042:
043: //import org.openide.util.NbBundle;
044: import java.util.Map;
045: import java.util.ResourceBundle;
046: import java.util.Vector;
047:
048: /**
049: * This class represents one defined filterset and the functionality around
050: *
051: * @author Tomas Hurka
052: * @author Jiri Sedlacek
053: */
054: public final class FilterSet {
055: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
056:
057: // -----
058: // I18N String constants
059: private static final ResourceBundle bundle = ResourceBundle
060: .getBundle("org.netbeans.lib.profiler.common.filters.Bundle"); // NOI18N
061: public static final String DEFAULT_FILTERSET_NAME = bundle
062: .getString("FilterSet_DefaultFilterSetName"); // NOI18N
063: // -----
064: public static final String PROP_FILTER_TYPE_VALUE = "profiler.filter.set"; //NOI18N
065: public static final boolean FILTER_SET_EXCLUSIVE = false;
066: public static final boolean FILTER_SET_INCLUSIVE = true;
067:
068: //~ Instance fields ----------------------------------------------------------------------------------------------------------
069:
070: private String filterSetName;
071: private String[] activeGlobalFilters;
072: private boolean filterSetType;
073:
074: //~ Constructors -------------------------------------------------------------------------------------------------------------
075:
076: /** Creates a new instance of FilterSet */
077: public FilterSet() {
078: this (DEFAULT_FILTERSET_NAME);
079: }
080:
081: public FilterSet(final String name) {
082: this (name, FILTER_SET_EXCLUSIVE, new String[0]);
083: }
084:
085: public FilterSet(final String name, final boolean type,
086: final String[] activeFilters) {
087: setFilterSetName(name);
088: setFilterSetType(type);
089: activeGlobalFilters = activeFilters;
090: }
091:
092: public FilterSet(final FilterSet filterSet) {
093: setValuesFrom(filterSet);
094: }
095:
096: //~ Methods ------------------------------------------------------------------------------------------------------------------
097:
098: public int getActiveGlobalFilterIndex(final String globalFilterName) {
099: for (int i = 0; i < activeGlobalFilters.length; i++) {
100: if (activeGlobalFilters[i].equals(globalFilterName)) {
101: return i;
102: }
103: }
104:
105: return -1;
106: }
107:
108: public void setActiveGlobalFilters(final String[] globalFilters) {
109: if (globalFilters == null) {
110: return;
111: }
112:
113: activeGlobalFilters = globalFilters;
114: }
115:
116: public String[] getActiveGlobalFilters() {
117: return activeGlobalFilters;
118: }
119:
120: public void setExclusive() {
121: filterSetType = FILTER_SET_EXCLUSIVE;
122: }
123:
124: public boolean isExclusive() {
125: return filterSetType == FILTER_SET_EXCLUSIVE;
126: }
127:
128: public void setFilterSetName(final String name) {
129: if (name == null) {
130: return;
131: }
132:
133: filterSetName = name;
134: }
135:
136: public String getFilterSetName() {
137: return filterSetName;
138: }
139:
140: public void setFilterSetType(final boolean type) {
141: filterSetType = type;
142: }
143:
144: public boolean getFilterSetType() {
145: return filterSetType;
146: }
147:
148: public void setInclusive() {
149: filterSetType = FILTER_SET_INCLUSIVE;
150: }
151:
152: public boolean isInclusive() {
153: return filterSetType == FILTER_SET_INCLUSIVE;
154: }
155:
156: public void setValuesFrom(final FilterSet filterSet) {
157: if (filterSet == null) {
158: return;
159: }
160:
161: setFilterSetName(filterSet.getFilterSetName());
162: setFilterSetType(filterSet.getFilterSetType());
163:
164: final int nGlobalFilters = filterSet.getActiveGlobalFilters().length;
165: activeGlobalFilters = new String[nGlobalFilters];
166:
167: for (int i = 0; i < nGlobalFilters; i++) {
168: activeGlobalFilters[i] = filterSet.getActiveGlobalFilters()[i];
169: }
170: }
171:
172: public void addActiveGlobalFilter(final String globalFilterName) {
173: final int nCurrentFilters = activeGlobalFilters.length;
174: final String[] newFilters = new String[nCurrentFilters + 1];
175: System.arraycopy(activeGlobalFilters, 0, newFilters, 0,
176: nCurrentFilters);
177: newFilters[nCurrentFilters] = globalFilterName;
178: activeGlobalFilters = newFilters;
179: }
180:
181: public void clear() {
182: setFilterSetName(DEFAULT_FILTERSET_NAME);
183: setFilterSetType(FILTER_SET_EXCLUSIVE);
184: activeGlobalFilters = new String[0];
185: }
186:
187: public boolean containsActiveGlobalFilter(
188: final String globalFilterName) {
189: return (getActiveGlobalFilterIndex(globalFilterName) != -1);
190: }
191:
192: public String debug() {
193: final StringBuffer sb = new StringBuffer();
194:
195: sb.append("FilterSet name: " + filterSetName); //NOI18N
196: sb.append("\n"); //NOI18N
197: sb
198: .append(" Type: "
199: + ((filterSetType == FILTER_SET_EXCLUSIVE) ? "Exclusive"
200: : "Inclusive")); //NOI18N
201: sb.append("\n"); //NOI18N
202:
203: for (int i = 0; i < activeGlobalFilters.length; i++) {
204: sb.append(" GlobalFilter " + i + ": "
205: + activeGlobalFilters[i]); //NOI18N
206: sb.append("\n"); //NOI18N
207: }
208:
209: return sb.toString();
210: }
211:
212: public boolean equals(Object o) {
213: if (o == this ) {
214: return true;
215: }
216:
217: if (!(o instanceof FilterSet)) {
218: return false;
219: }
220:
221: FilterSet filterSet = (FilterSet) o;
222:
223: if (!filterSetName.equals(filterSet.filterSetName)) {
224: return false;
225: }
226:
227: if (filterSetType != filterSet.filterSetType) {
228: return false;
229: }
230:
231: if (activeGlobalFilters.length != filterSet.activeGlobalFilters.length) {
232: return false;
233: }
234:
235: for (int i = 0; i < activeGlobalFilters.length; i++) {
236: if (!activeGlobalFilters[i]
237: .equals(filterSet.activeGlobalFilters[i])) {
238: return false;
239: }
240: }
241:
242: return true;
243: }
244:
245: // TODO: just to keep backward compatibility, should be removed after code cleanup!!!
246: public FilterSet load(final Map props, final int index) {
247: return load(props, index, ""); //NOI18N
248: }
249:
250: public FilterSet load(final Map props, final String prefix) {
251: if (!getProperty(props, prefix + FilterUtils.PROP_FILTER_TYPE,
252: "").equals(PROP_FILTER_TYPE_VALUE)) {
253: throw new RuntimeException(
254: "Trying to load incompatible filter"); //NOI18N
255: }
256:
257: return load(props, -1, prefix);
258: }
259:
260: public FilterSet load(final Map props, final int index,
261: final String prefix) {
262: final String itemPrefix = "FilterSet-"
263: + ((index == -1) ? ""
264: : (new Integer(index).toString() + "-")); //NOI18N
265:
266: filterSetName = getProperty(props,
267: prefix + itemPrefix + "name", DEFAULT_FILTERSET_NAME); //NOI18N
268: filterSetType = Boolean.valueOf(
269: getProperty(props, prefix + itemPrefix + "type",
270: "false")).booleanValue(); //NOI18N
271:
272: final Vector activeFilters = new Vector();
273: String activeFilterName;
274:
275: while ((activeFilterName = (String) props.get(prefix
276: + itemPrefix + "active_filter-" + activeFilters.size())) != null) { //NOI18N
277: activeFilters.add(activeFilterName);
278: }
279:
280: activeGlobalFilters = new String[activeFilters.size()];
281:
282: for (int i = 0; i < activeFilters.size(); i++) {
283: activeGlobalFilters[i] = (String) activeFilters.get(i);
284: }
285:
286: return this ;
287: }
288:
289: public void removeActiveGlobalFilter(final String globalFilterName) {
290: final int filterIndex = getActiveGlobalFilterIndex(globalFilterName);
291:
292: if (filterIndex == -1) {
293: return;
294: }
295:
296: final int nCurrentFilters = activeGlobalFilters.length;
297: final String[] newFilters = new String[nCurrentFilters - 1];
298:
299: if (filterIndex > 0) {
300: System.arraycopy(activeGlobalFilters, 0, newFilters, 0,
301: filterIndex);
302: }
303:
304: if (filterIndex < (nCurrentFilters - 1)) {
305: System.arraycopy(activeGlobalFilters, filterIndex + 1,
306: newFilters, filterIndex, nCurrentFilters
307: - filterIndex - 1);
308: }
309:
310: activeGlobalFilters = newFilters;
311: }
312:
313: // TODO: just to keep backward compatibility, should be removed after code cleanup!!!
314: public void store(final Map props, final int index) {
315: store(props, index, ""); //NOI18N
316: }
317:
318: public void store(final Map props, final String prefix) {
319: props.put(prefix + FilterUtils.PROP_FILTER_TYPE,
320: PROP_FILTER_TYPE_VALUE);
321: store(props, -1, prefix);
322: }
323:
324: public void store(final Map props, final int index,
325: final String prefix) {
326: final String itemPrefix = "FilterSet-"
327: + ((index == -1) ? ""
328: : (new Integer(index).toString() + "-")); //NOI18N
329: props.put(prefix + itemPrefix + "name", filterSetName); //NOI18N
330: props.put(prefix + itemPrefix + "type", Boolean
331: .toString(filterSetType)); //NOI18N
332:
333: for (int i = 0; i < activeGlobalFilters.length; i++) {
334: props.put(prefix + itemPrefix + "active_filter-" + i,
335: activeGlobalFilters[i]); //NOI18N
336: }
337: }
338:
339: public String toString() {
340: return getFilterSetName();
341: }
342:
343: private static String getProperty(final Map props,
344: final Object key, final String defaultValue) {
345: final Object ret = props.get(key);
346:
347: return (ret != null) ? (String) ret : defaultValue;
348: }
349: }
|