|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.util.*; |
|
29 import javax.swing.ListModel; |
|
30 import javax.swing.event.*; |
|
31 import com.oracle.solaris.vp.util.misc.CollectionUtil; |
|
32 import com.oracle.solaris.vp.util.misc.predicate.*; |
|
33 import com.oracle.solaris.vp.util.swing.event.ListDataListeners; |
|
34 |
|
35 /** |
|
36 * The {@code FilteredListModel} class applies a {@link Predicate} filter to a |
|
37 * {@code Collection} to produce the contents of a {@code ListModel}. |
|
38 */ |
|
39 public class FilteredListModel<T> implements ListModel { |
|
40 // |
|
41 // Instance data |
|
42 // |
|
43 |
|
44 private ListDataListeners listeners = new ListDataListeners(); |
|
45 private Collection<T> items; |
|
46 private List<T> filtered = Collections.emptyList(); |
|
47 private Predicate<T> predicate; |
|
48 |
|
49 // |
|
50 // Constructors |
|
51 // |
|
52 |
|
53 /** |
|
54 * Constructs a {@code FilteredListModel} for the given items and filter. |
|
55 * Note: the behavior of this class is undefined if the given {@code |
|
56 * Collection} changes after this object's construction. |
|
57 * |
|
58 * @param items |
|
59 * the unfiltered data {@code Collection} |
|
60 * |
|
61 * @param predicate |
|
62 * an initial filter to apply, or {@code null} to apply no |
|
63 * filter |
|
64 */ |
|
65 public FilteredListModel(Collection<T> items, Predicate<T> predicate) { |
|
66 this.items = items; |
|
67 setPredicate(predicate); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Constructs a {@code FilteredListModel} for the given items and no filter. |
|
72 * |
|
73 * @param items |
|
74 * the unfiltered data {@code Collection} |
|
75 */ |
|
76 public FilteredListModel(Collection<T> items) { |
|
77 this(items, null); |
|
78 } |
|
79 |
|
80 // |
|
81 // ListModel methods |
|
82 // |
|
83 |
|
84 @Override |
|
85 public void addListDataListener(ListDataListener l) { |
|
86 listeners.add(l); |
|
87 } |
|
88 |
|
89 @Override |
|
90 public T getElementAt(int index) { |
|
91 return filtered.get(index); |
|
92 } |
|
93 |
|
94 @Override |
|
95 public int getSize() { |
|
96 return filtered.size(); |
|
97 } |
|
98 |
|
99 @Override |
|
100 public void removeListDataListener(ListDataListener l) { |
|
101 listeners.remove(l); |
|
102 } |
|
103 |
|
104 // |
|
105 // FilteredListModel methods |
|
106 // |
|
107 |
|
108 protected void fireContentsChanged(int start, int end) { |
|
109 ListDataEvent event = new ListDataEvent(this, |
|
110 ListDataEvent.CONTENTS_CHANGED, start, end); |
|
111 listeners.contentsChanged(event); |
|
112 } |
|
113 |
|
114 protected void fireIntervalAdded(int start, int end) { |
|
115 ListDataEvent event = new ListDataEvent(this, |
|
116 ListDataEvent.INTERVAL_ADDED, start, end); |
|
117 listeners.intervalAdded(event); |
|
118 } |
|
119 |
|
120 protected void fireIntervalRemoved(int start, int end) { |
|
121 ListDataEvent event = new ListDataEvent(this, |
|
122 ListDataEvent.INTERVAL_REMOVED, start, end); |
|
123 listeners.intervalRemoved(event); |
|
124 } |
|
125 |
|
126 protected List<T> getFiltered() { |
|
127 return Collections.unmodifiableList(filtered); |
|
128 } |
|
129 |
|
130 protected ListDataListeners getListeners() { |
|
131 return listeners; |
|
132 } |
|
133 |
|
134 /** |
|
135 * Gets the {@link Predicate} applied to the data {@code Collection} for |
|
136 * this {@code FilteredListModel}. |
|
137 * |
|
138 * @return a {@link Predicate}, or {@code null} if no filter is applied |
|
139 */ |
|
140 public Predicate<T> getPredicate() { |
|
141 return predicate; |
|
142 } |
|
143 |
|
144 /** |
|
145 * Sets the {@link Predicate} to apply to the data {@code Collection} for |
|
146 * this {@code FilteredListModel}. |
|
147 * |
|
148 * @param predicate |
|
149 * a {@link Predicate}, or {@code null} if no filter should be |
|
150 * applied |
|
151 */ |
|
152 public void setPredicate(Predicate<T> predicate) { |
|
153 this.predicate = predicate; |
|
154 |
|
155 if (predicate == null) { |
|
156 predicate = TruePredicate.getInstance(); |
|
157 } |
|
158 |
|
159 int size = filtered.size(); |
|
160 if (size > 0) { |
|
161 filtered = Collections.emptyList(); |
|
162 fireIntervalRemoved(0, size - 1); |
|
163 } |
|
164 |
|
165 filtered = CollectionUtil.filter(items, predicate); |
|
166 size = filtered.size(); |
|
167 if (size > 0) { |
|
168 fireIntervalAdded(0, size - 1); |
|
169 } |
|
170 } |
|
171 } |