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.awt.BorderLayout;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.List;
048: import java.util.prefs.BackingStoreException;
049: import java.util.prefs.Preferences;
050: import javax.swing.BorderFactory;
051: import javax.swing.JComponent;
052: import javax.swing.JPanel;
053: import javax.swing.UIManager;
054: import javax.swing.event.ListDataEvent;
055: import javax.swing.event.ListDataListener;
056: import org.netbeans.modules.tasklist.trampoline.TaskGroup;
057: import org.netbeans.modules.tasklist.ui.checklist.CheckList;
058: import org.netbeans.modules.tasklist.ui.checklist.DefaultCheckListModel;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * "Task Group is" - condition
063: *
064: * @author tl
065: */
066: class TaskGroupCondition extends FilterCondition {
067: /**
068: * Creates an array of filter conditions for the specified property
069: *
070: * @param index index of the property
071: */
072: public static TaskGroupCondition[] createConditions() {
073: return new TaskGroupCondition[] { new TaskGroupCondition() };
074: };
075:
076: private boolean[] groupState;
077: private TaskGroup[] groups;
078:
079: /**
080: * Creates a new instance
081: *
082: * @param prop index of a property
083: */
084: public TaskGroupCondition() {
085: List<TaskGroup> groupList = new ArrayList<TaskGroup>(TaskGroup
086: .getGroups());
087: groups = groupList.toArray(new TaskGroup[groupList.size()]);
088: groupState = new boolean[groups.length];
089: Arrays.fill(groupState, true);
090: }
091:
092: public TaskGroupCondition(final TaskGroupCondition rhs) {
093: super (rhs);
094: this .groups = new TaskGroup[rhs.groups.length];
095: this .groupState = new boolean[rhs.groupState.length];
096: assert this .groups.length == this .groupState.length;
097: for (int i = 0; i < groups.length; i++) {
098: groups[i] = rhs.groups[i];
099: groupState[i] = rhs.groupState[i];
100: }
101: }
102:
103: public Object clone() {
104: return new TaskGroupCondition(this );
105: }
106:
107: public boolean isTrue(Object o1) {
108: TaskGroup g = (TaskGroup) o1;
109: for (int i = 0; i < groups.length; i++) {
110: if (groups[i].equals(g)) {
111: return groupState[i];
112: }
113: }
114: return true; //new or unknown Groups are always visible
115: }
116:
117: public JComponent createConstantComponent() {
118: CheckList list = new CheckList(new DefaultCheckListModel(
119: groupState, groups));
120: final JPanel panel = new JPanel(new BorderLayout());
121: panel.setOpaque(false);
122: panel.setBorder(BorderFactory.createCompoundBorder(UIManager
123: .getBorder("TextField.border"), // NOI18N
124: BorderFactory.createEmptyBorder(2, 2, 2, 2)));
125: panel.add(list, BorderLayout.CENTER);
126: panel.setToolTipText(Util.getString("group_desc")); //NOI18N
127:
128: list.getAccessibleContext().setAccessibleName(
129: Util.getString("LBL_PriorityCheckList"));
130: list.getAccessibleContext().setAccessibleDescription(
131: Util.getString("LBL_PriorityCheckList"));
132:
133: list.getModel().addListDataListener(new ListDataListener() {
134: public void intervalAdded(ListDataEvent e) {
135: }
136:
137: public void intervalRemoved(ListDataEvent e) {
138: }
139:
140: public void contentsChanged(ListDataEvent e) {
141: boolean atLeastOneGroupSelected = false;
142: for (int i = 0; i < groupState.length; i++) {
143: if (groupState[i]) {
144: atLeastOneGroupSelected = true;
145: break;
146: }
147: }
148: panel.putClientProperty(
149: FilterCondition.PROP_VALUE_VALID, new Boolean(
150: atLeastOneGroupSelected));
151: }
152: });
153:
154: return panel;
155: }
156:
157: public void getConstantFrom(JComponent cmp) {
158: // Nothing to do. The array of booleans will not be cloned in
159: // DefaultCheckListModel
160: }
161:
162: protected String getDisplayName() {
163: return NbBundle.getMessage(TaskGroupCondition.class, "IsOneOf"); //NOI18N
164: }
165:
166: void load(Preferences prefs, String prefix)
167: throws BackingStoreException {
168: for (int i = 0; i < groups.length; i++) {
169: String groupName = groups[i].getName();
170: groupState[i] = prefs.getBoolean(prefix + "_enabled_"
171: + groupName, true); //NOI18N
172: }
173: }
174:
175: void save(Preferences prefs, String prefix)
176: throws BackingStoreException {
177: for (int i = 0; i < groups.length; i++) {
178: String groupName = groups[i].getName();
179: prefs.putBoolean(prefix + "_enabled_" + groupName,
180: groupState[i]); //NOI18N
181: }
182: }
183: }
|