1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.Component
;
5 import java
.awt
.event
.MouseAdapter
;
6 import java
.awt
.event
.MouseEvent
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collection
;
11 import javax
.swing
.JList
;
12 import javax
.swing
.JPopupMenu
;
13 import javax
.swing
.ListCellRenderer
;
15 import be
.nikiroo
.utils
.compat
.DefaultListModel6
;
16 import be
.nikiroo
.utils
.compat
.JList6
;
17 import be
.nikiroo
.utils
.compat
.ListCellRenderer6
;
20 * A {@link javax.swing.ListModel} that can maintain 2 lists; one with the
21 * actual data (the elements), and a second one with the items that are
22 * currently displayed (the items).
24 * It also offers filter options, supports hovered changes and some more utility
30 * the type of elements and items (the same type)
32 public class ListModel
<T
> extends DefaultListModel6
<T
> {
33 private static final long serialVersionUID
= 1L;
36 * A filter interface, to check for a condition (note that a Predicate class
37 * already exists in Java 1.8+, and is compatible with this one if you
38 * change the signatures -- but I support java 1.6+).
43 * the type of elements and items (the same type)
45 public interface Predicate
<T
> {
47 * Check if an item or an element pass a filter.
52 * @return TRUE if the test passed, FALSE if not
54 public boolean test(T item
);
58 * A simple interface your elements must implement if you want to use
59 * {@link ListModel#generateRenderer(ListModel)}.
63 public interface Hoverable
{
65 * The element is currently selected.
68 * TRUE for selected, FALSE for unselected
70 public void setSelected(boolean selected
);
73 * The element is currently under the mouse cursor.
76 * TRUE if it is, FALSE if not
78 public void setHovered(boolean hovered
);
81 private int hoveredIndex
;
82 private List
<T
> items
= new ArrayList
<T
>();
83 private JList6
<T
> list
;
84 private boolean keepSelection
= true;
87 * Create a new {@link ListModel}.
90 * the {@link JList} we will handle the data of (cannot be NULL)
92 @SuppressWarnings({ "unchecked", "rawtypes" }) // not compatible Java 1.6
93 public ListModel(JList list
) {
94 this((JList6
<T
>) list
);
98 * Create a new {@link ListModel}.
101 * the {@link JList} we will handle the data of (cannot be NULL)
103 * the popup to use and keep track of (can be NULL)
105 @SuppressWarnings({ "unchecked", "rawtypes" }) // not compatible Java 1.6
106 public ListModel(final JList list
, final JPopupMenu popup
) {
107 this((JList6
<T
>) list
, popup
);
111 * Create a new {@link ListModel}.
114 * the {@link JList6} we will handle the data of (cannot be NULL)
116 public ListModel(JList6
<T
> list
) {
121 * Create a new {@link ListModel}.
124 * the {@link JList6} we will handle the data of (cannot be NULL)
126 * the popup to use and keep track of (can be NULL)
128 public ListModel(final JList6
<T
> list
, final JPopupMenu popup
) {
132 list
.addMouseMotionListener(new MouseAdapter() {
134 public void mouseMoved(MouseEvent me
) {
135 if (popup
!= null && popup
.isShowing())
138 Point p
= new Point(me
.getX(), me
.getY());
139 int index
= list
.locationToIndex(p
);
140 if (index
!= hoveredIndex
) {
141 int oldIndex
= hoveredIndex
;
142 hoveredIndex
= index
;
143 fireElementChanged(oldIndex
);
144 fireElementChanged(index
);
149 list
.addMouseListener(new MouseAdapter() {
151 public void mousePressed(MouseEvent e
) {
156 public void mouseReleased(MouseEvent e
) {
161 public void mouseExited(MouseEvent e
) {
162 if (popup
!= null && popup
.isShowing())
165 if (hoveredIndex
> -1) {
166 int oldIndex
= hoveredIndex
;
168 fireElementChanged(oldIndex
);
172 private void check(MouseEvent e
) {
177 if (e
.isPopupTrigger()) {
178 if (list
.getSelectedIndices().length
<= 1) {
179 list
.setSelectedIndex(
180 list
.locationToIndex(e
.getPoint()));
183 popup
.show(list
, e
.getX(), e
.getY());
190 * (Try and) keep the elements that were selected when filtering.
192 * This will use toString on the elements to identify them, and can be a bit
193 * resource intensive.
195 * @return TRUE if we do
197 public boolean isKeepSelection() {
198 return keepSelection
;
202 * (Try and) keep the elements that were selected when filtering.
204 * This will use toString on the elements to identify them, and can be a bit
205 * resource intensive.
207 * @param keepSelection
208 * TRUE to try and keep them selected
210 public void setKeepSelection(boolean keepSelection
) {
211 this.keepSelection
= keepSelection
;
215 * Check if this element is currently under the mouse.
218 * the element to check
220 * @return TRUE if it is
222 public boolean isHovered(T element
) {
223 return indexOf(element
) == hoveredIndex
;
227 * Check if this element is currently under the mouse.
230 * the index of the element to check
232 * @return TRUE if it is
234 public boolean isHovered(int index
) {
235 return index
== hoveredIndex
;
239 * Add an item to the model.
242 * the new item to add
244 public void addItem(T item
) {
249 * Add items to the model.
252 * the new items to add
254 public void addAllItems(Collection
<T
> items
) {
255 this.items
.addAll(items
);
259 * Removes the first occurrence of the specified element from this list, if
260 * it is present (optional operation).
263 * the item to remove if possible (can be NULL)
265 * @return TRUE if one element was removed, FALSE if not found
267 public boolean removeItem(T item
) {
268 return items
.remove(item
);
272 * Remove the items that pass the given filter (or all items if the filter
276 * the filter (if the filter returns TRUE, the item will be
279 * @return TRUE if at least one item was removed
281 public boolean removeItemIf(Predicate
<T
> filter
) {
282 boolean changed
= false;
283 if (filter
== null) {
284 changed
= !items
.isEmpty();
287 for (int i
= 0; i
< items
.size(); i
++) {
288 if (filter
.test(items
.get(i
))) {
299 * Removes all the items from this model.
301 public void clearItems() {
306 * Filter the current elements.
308 * This method will clear all the elements then look into all the items:
309 * those that pass the given filter will be copied as elements.
312 * the filter to select which elements to keep; an item that pass
313 * the filter will be copied as an element (can be NULL, in that
314 * case all items will be copied as elements)
316 @SuppressWarnings("unchecked") // ListModel<T> and JList<T> are not java 1.6
317 public void filter(Predicate
<T
> filter
) {
318 ListSnapshot snapshot
= null;
321 snapshot
= new ListSnapshot(list
);
324 for (T item
: items
) {
325 if (filter
== null || filter
.test(item
)) {
337 * Return the currently selected elements.
339 * @return the selected elements
341 public List
<T
> getSelectedElements() {
342 List
<T
> selected
= new ArrayList
<T
>();
343 for (int index
: list
.getSelectedIndices()) {
344 selected
.add(get(index
));
351 * Return the selected element if <b>one</b> and <b>only one</b> element is
352 * selected. I.E., if zero, two or more elements are selected, NULL will be
355 * @return the element if it is the only selected element, NULL otherwise
357 public T
getUniqueSelectedElement() {
358 List
<T
> selected
= getSelectedElements();
359 if (selected
.size() == 1) {
360 return selected
.get(0);
367 * Notify that this element has been changed.
370 * the index of the element
372 public void fireElementChanged(int index
) {
374 fireContentsChanged(this, index
, index
);
379 * Notify that this element has been changed.
384 public void fireElementChanged(T element
) {
385 int index
= indexOf(element
);
387 fireContentsChanged(this, index
, index
);
391 @SuppressWarnings("unchecked") // ListModel<T> and JList<T> are not java 1.6
393 public T
get(int index
) {
394 return (T
) super.get(index
);
398 * Generate a {@link ListCellRenderer} that supports {@link Hoverable}
402 * the type of elements and items (the same type), which should
403 * implement {@link Hoverable} (it will not cause issues if not,
404 * but then, it will be a default renderer)
408 * @return a suitable, {@link Hoverable} compatible renderer
410 static public <T
extends Component
> ListCellRenderer6
<T
> generateRenderer(
411 final ListModel
<T
> model
) {
412 return new ListCellRenderer6
<T
>() {
414 public Component
getListCellRendererComponent(JList6
<T
> list
,
415 T item
, int index
, boolean isSelected
,
416 boolean cellHasFocus
) {
417 if (item
instanceof Hoverable
) {
418 Hoverable hoverable
= (Hoverable
) item
;
419 hoverable
.setSelected(isSelected
);
420 hoverable
.setHovered(model
.isHovered(index
));