1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.Component
;
5 import java
.awt
.Window
;
6 import java
.awt
.event
.MouseAdapter
;
7 import java
.awt
.event
.MouseEvent
;
8 import java
.util
.ArrayList
;
9 import java
.util
.Collection
;
10 import java
.util
.List
;
12 import javax
.swing
.JList
;
13 import javax
.swing
.JPopupMenu
;
14 import javax
.swing
.ListCellRenderer
;
15 import javax
.swing
.SwingWorker
;
17 import be
.nikiroo
.utils
.compat
.DefaultListModel6
;
18 import be
.nikiroo
.utils
.compat
.JList6
;
19 import be
.nikiroo
.utils
.compat
.ListCellRenderer6
;
22 * A {@link javax.swing.ListModel} that can maintain 2 lists; one with the
23 * actual data (the elements), and a second one with the items that are
24 * currently displayed (the items).
26 * It also offers filter options, supports hovered changes and some more utility
32 * the type of elements and items (the same type)
34 public class ListModel
<T
> extends DefaultListModel6
<T
> {
35 private static final long serialVersionUID
= 1L;
37 /** How long to wait before displaying a tooltip, in milliseconds. */
38 private static final int DELAY_TOOLTIP_MS
= 1000;
41 * A filter interface, to check for a condition (note that a Predicate class
42 * already exists in Java 1.8+, and is compatible with this one if you
43 * change the signatures -- but I support java 1.6+).
48 * the type of elements and items (the same type)
50 public interface Predicate
<T
> {
52 * Check if an item or an element pass a filter.
57 * @return TRUE if the test passed, FALSE if not
59 public boolean test(T item
);
63 * A simple interface your elements must implement if you want to use
64 * {@link ListModel#generateRenderer(ListModel)}.
68 public interface Hoverable
{
70 * The element is currently selected.
73 * TRUE for selected, FALSE for unselected
75 public void setSelected(boolean selected
);
78 * The element is currently under the mouse cursor.
81 * TRUE if it is, FALSE if not
83 public void setHovered(boolean hovered
);
87 * An interface required to support tooltips on this {@link ListModel}.
92 * the type of elements and items (the same type)
94 public interface TooltipCreator
<T
> {
96 * Generate a tooltip {@link Window} for this element.
98 * Note that the tooltip can be of two modes: undecorated or standalone.
99 * An undecorated tooltip will be taken care of by this
100 * {@link ListModel}, but a standalone one is supposed to be its own
101 * Dialog or Frame (it won't be automatically closed).
104 * the element to generate a tooltip for
106 * TRUE for undecorated tooltip, FALSE for standalone
109 * @return the generated tooltip or NULL for none
111 public Window
generateTooltip(T t
, boolean undecorated
);
114 private int hoveredIndex
;
115 private List
<T
> items
= new ArrayList
<T
>();
116 private boolean keepSelection
= true;
118 private TooltipCreator
<T
> tooltipCreator
;
119 private Window tooltip
;
121 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
125 * Create a new {@link ListModel}.
128 * the {@link JList6} we will handle the data of (cannot be NULL)
130 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
131 public ListModel(JList6
<T
> list
) {
136 * Create a new {@link ListModel}.
139 * the {@link JList6} we will handle the data of (cannot be NULL)
141 * the popup to use and keep track of (can be NULL)
143 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
144 public ListModel(JList6
<T
> list
, JPopupMenu popup
) {
145 this((JList
) list
, popup
);
149 * Create a new {@link ListModel}.
152 * the {@link JList6} we will handle the data of (cannot be NULL)
153 * @param tooltipCreator
154 * use this if you want the list to display tooltips on hover
157 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
158 public ListModel(JList6
<T
> list
, TooltipCreator
<T
> tooltipCreator
) {
159 this((JList
) list
, null, tooltipCreator
);
163 * Create a new {@link ListModel}.
166 * the {@link JList6} we will handle the data of (cannot be NULL)
168 * the popup to use and keep track of (can be NULL)
169 * @param tooltipCreator
170 * use this if you want the list to display tooltips on hover
173 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
174 public ListModel(JList6
<T
> list
, JPopupMenu popup
,
175 TooltipCreator
<T
> tooltipCreator
) {
176 this((JList
) list
, popup
, tooltipCreator
);
180 * Create a new {@link ListModel}.
182 * Note that you must take care of passing a {@link JList} that only handles
183 * elements of the type of this {@link ListModel} -- you can also use
184 * {@link ListModel#ListModel(JList6)} instead.
187 * the {@link JList} we will handle the data of (cannot be NULL,
188 * must only contain elements of the type of this
191 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
192 public ListModel(JList list
) {
193 this(list
, null, null);
197 * Create a new {@link ListModel}.
199 * Note that you must take care of passing a {@link JList} that only handles
200 * elements of the type of this {@link ListModel} -- you can also use
201 * {@link ListModel#ListModel(JList6, JPopupMenu)} instead.
204 * the {@link JList} we will handle the data of (cannot be NULL,
205 * must only contain elements of the type of this
208 * the popup to use and keep track of (can be NULL)
210 @SuppressWarnings("rawtypes") // JList<?> not in Java 1.6
211 public ListModel(JList list
, JPopupMenu popup
) {
212 this(list
, popup
, null);
216 * Create a new {@link ListModel}.
218 * Note that you must take care of passing a {@link JList} that only handles
219 * elements of the type of this {@link ListModel} -- you can also use
220 * {@link ListModel#ListModel(JList6, JPopupMenu)} instead.
223 * the {@link JList} we will handle the data of (cannot be NULL,
224 * must only contain elements of the type of this
226 * @param tooltipCreator
227 * use this if you want the list to display tooltips on hover
230 @SuppressWarnings("rawtypes") // JList<?> not in Java 1.6
231 public ListModel(JList list
, TooltipCreator
<T
> tooltipCreator
) {
232 this(list
, null, tooltipCreator
);
236 * Create a new {@link ListModel}.
238 * Note that you must take care of passing a {@link JList} that only handles
239 * elements of the type of this {@link ListModel} -- you can also use
240 * {@link ListModel#ListModel(JList6, JPopupMenu)} instead.
243 * the {@link JList} we will handle the data of (cannot be NULL,
244 * must only contain elements of the type of this
247 * the popup to use and keep track of (can be NULL)
248 * @param tooltipCreator
249 * use this if you want the list to display tooltips on hover
252 @SuppressWarnings({ "unchecked", "rawtypes" }) // JList<?> not in Java 1.6
253 public ListModel(final JList list
, final JPopupMenu popup
,
254 TooltipCreator
<T
> tooltipCreator
) {
256 this.tooltipCreator
= tooltipCreator
;
260 final DelayWorker tooltipWatcher
= new DelayWorker(DELAY_TOOLTIP_MS
);
261 if (tooltipCreator
!= null) {
262 tooltipWatcher
.start();
265 list
.addMouseMotionListener(new MouseAdapter() {
267 public void mouseMoved(final MouseEvent me
) {
268 if (popup
!= null && popup
.isShowing())
271 Point p
= new Point(me
.getX(), me
.getY());
272 final int index
= list
.locationToIndex(p
);
273 if (index
!= hoveredIndex
) {
274 int oldIndex
= hoveredIndex
;
275 hoveredIndex
= index
;
276 fireElementChanged(oldIndex
);
277 fireElementChanged(index
);
279 Window oldTooltip
= tooltip
;
281 if (oldTooltip
!= null) {
282 oldTooltip
.setVisible(false);
285 if (ListModel
.this.tooltipCreator
!= null) {
286 tooltipWatcher
.delay("tooltip",
287 new SwingWorker
<Void
, Void
>() {
289 protected Void
doInBackground()
295 protected void done() {
297 || index
!= hoveredIndex
) {
302 && popup
.isShowing()) {
306 tooltip
= newTooltip(index
, me
);
314 list
.addMouseListener(new MouseAdapter() {
316 public void mousePressed(MouseEvent e
) {
321 public void mouseReleased(MouseEvent e
) {
326 public void mouseExited(MouseEvent e
) {
327 if (popup
!= null && popup
.isShowing())
330 if (hoveredIndex
> -1) {
331 int oldIndex
= hoveredIndex
;
333 fireElementChanged(oldIndex
);
337 private void check(MouseEvent e
) {
342 if (e
.isPopupTrigger()) {
343 if (list
.getSelectedIndices().length
<= 1) {
344 list
.setSelectedIndex(
345 list
.locationToIndex(e
.getPoint()));
348 Window oldTooltip
= tooltip
;
350 if (oldTooltip
!= null) {
351 oldTooltip
.setVisible(false);
354 popup
.show(list
, e
.getX(), e
.getY());
362 * (Try and) keep the elements that were selected when filtering.
364 * This will use toString on the elements to identify them, and can be a bit
365 * resource intensive.
367 * @return TRUE if we do
369 public boolean isKeepSelection() {
370 return keepSelection
;
374 * (Try and) keep the elements that were selected when filtering.
376 * This will use toString on the elements to identify them, and can be a bit
377 * resource intensive.
379 * @param keepSelection
380 * TRUE to try and keep them selected
382 public void setKeepSelection(boolean keepSelection
) {
383 this.keepSelection
= keepSelection
;
387 * Check if this element is currently under the mouse.
390 * the element to check
392 * @return TRUE if it is
394 public boolean isHovered(T element
) {
395 return indexOf(element
) == hoveredIndex
;
399 * Check if this element is currently under the mouse.
402 * the index of the element to check
404 * @return TRUE if it is
406 public boolean isHovered(int index
) {
407 return index
== hoveredIndex
;
411 * Add an item to the model.
414 * the new item to add
416 public void addItem(T item
) {
421 * Add items to the model.
424 * the new items to add
426 public void addAllItems(Collection
<T
> items
) {
427 this.items
.addAll(items
);
431 * Removes the first occurrence of the specified element from this list, if
432 * it is present (optional operation).
435 * the item to remove if possible (can be NULL)
437 * @return TRUE if one element was removed, FALSE if not found
439 public boolean removeItem(T item
) {
440 return items
.remove(item
);
444 * Remove the items that pass the given filter (or all items if the filter
448 * the filter (if the filter returns TRUE, the item will be
451 * @return TRUE if at least one item was removed
453 public boolean removeItemIf(Predicate
<T
> filter
) {
454 boolean changed
= false;
455 if (filter
== null) {
456 changed
= !items
.isEmpty();
459 for (int i
= 0; i
< items
.size(); i
++) {
460 if (filter
.test(items
.get(i
))) {
471 * Removes all the items from this model.
473 public void clearItems() {
478 * Filter the current elements.
480 * This method will clear all the elements then look into all the items:
481 * those that pass the given filter will be copied as elements.
484 * the filter to select which elements to keep; an item that pass
485 * the filter will be copied as an element (can be NULL, in that
486 * case all items will be copied as elements)
488 @SuppressWarnings("unchecked") // JList<?> not compatible Java 1.6
489 public void filter(Predicate
<T
> filter
) {
490 ListSnapshot snapshot
= null;
493 snapshot
= new ListSnapshot(list
);
496 for (T item
: items
) {
497 if (filter
== null || filter
.test(item
)) {
509 * Return the currently selected elements.
511 * @return the selected elements
513 public List
<T
> getSelectedElements() {
514 List
<T
> selected
= new ArrayList
<T
>();
515 for (int index
: list
.getSelectedIndices()) {
516 selected
.add(get(index
));
523 * Return the selected element if <b>one</b> and <b>only one</b> element is
524 * selected. I.E., if zero, two or more elements are selected, NULL will be
527 * @return the element if it is the only selected element, NULL otherwise
529 public T
getUniqueSelectedElement() {
530 List
<T
> selected
= getSelectedElements();
531 if (selected
.size() == 1) {
532 return selected
.get(0);
539 * Notify that this element has been changed.
542 * the index of the element
544 public void fireElementChanged(int index
) {
546 fireContentsChanged(this, index
, index
);
551 * Notify that this element has been changed.
556 public void fireElementChanged(T element
) {
557 int index
= indexOf(element
);
559 fireContentsChanged(this, index
, index
);
563 @SuppressWarnings("unchecked") // JList<?> not compatible Java 1.6
565 public T
get(int index
) {
566 return (T
) super.get(index
);
569 private Window
newTooltip(final int index
, final MouseEvent me
) {
570 final T value
= ListModel
.this.get(index
);
572 final Window newTooltip
= tooltipCreator
.generateTooltip(value
, true);
574 if (newTooltip
!= null) {
575 newTooltip
.addMouseListener(new MouseAdapter() {
577 public void mouseClicked(MouseEvent e
) {
579 Window promotedTooltip
= tooltipCreator
580 .generateTooltip(value
, false);
581 promotedTooltip
.setLocation(newTooltip
.getLocation());
582 newTooltip
.setVisible(false);
583 promotedTooltip
.setVisible(true);
586 newTooltip
.setLocation(me
.getXOnScreen(), me
.getYOnScreen());
588 newTooltip
.setVisible(true);
595 * Generate a {@link ListCellRenderer} that supports {@link Hoverable}
599 * the type of elements and items (the same type), which should
600 * implement {@link Hoverable} (it will not cause issues if not,
601 * but then, it will be a default renderer)
605 * @return a suitable, {@link Hoverable} compatible renderer
607 static public <T
extends Component
> ListCellRenderer6
<T
> generateRenderer(
608 final ListModel
<T
> model
) {
609 return new ListCellRenderer6
<T
>() {
611 public Component
getListCellRendererComponent(JList6
<T
> list
,
612 T item
, int index
, boolean isSelected
,
613 boolean cellHasFocus
) {
614 if (item
instanceof Hoverable
) {
615 Hoverable hoverable
= (Hoverable
) item
;
616 hoverable
.setSelected(isSelected
);
617 hoverable
.setHovered(model
.isHovered(index
));