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
.ui
.compat
.DefaultListModel6
;
18 import be
.nikiroo
.utils
.ui
.compat
.JList6
;
19 import be
.nikiroo
.utils
.ui
.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 DelayWorker tooltipWatcher
;
119 private JPopupMenu popup
;
120 private TooltipCreator
<T
> tooltipCreator
;
121 private Window tooltip
;
123 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
127 * Create a new {@link ListModel}.
130 * the {@link JList6} we will handle the data of (cannot be NULL)
132 @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
133 public ListModel(JList6
<T
> list
) {
138 * Create a new {@link ListModel}.
140 * Note that you must take care of passing a {@link JList} that only handles
141 * elements of the type of this {@link ListModel} -- you can also use
142 * {@link ListModel#ListModel(JList6)} instead.
145 * the {@link JList} we will handle the data of (cannot be NULL,
146 * must only contain elements of the type of this
149 @SuppressWarnings({ "unchecked", "rawtypes" }) // JList<?> not in Java 1.6
150 public ListModel(final JList list
) {
155 // We always have it ready
156 tooltipWatcher
= new DelayWorker(DELAY_TOOLTIP_MS
);
157 tooltipWatcher
.start();
159 list
.addMouseMotionListener(new MouseAdapter() {
161 public void mouseMoved(final MouseEvent me
) {
162 if (ListModel
.this.popup
!= null
163 && ListModel
.this.popup
.isShowing())
166 Point p
= new Point(me
.getX(), me
.getY());
167 final int index
= list
.locationToIndex(p
);
168 if (index
!= hoveredIndex
) {
169 int oldIndex
= hoveredIndex
;
170 hoveredIndex
= index
;
171 fireElementChanged(oldIndex
);
172 fireElementChanged(index
);
174 if (ListModel
.this.tooltipCreator
!= null) {
177 tooltipWatcher
.delay("tooltip",
178 new SwingWorker
<Void
, Void
>() {
180 protected Void
doInBackground()
186 protected void done() {
190 || index
!= hoveredIndex
) {
194 if (ListModel
.this.popup
!= null
195 && ListModel
.this.popup
200 showTooltip(newTooltip(index
, me
));
208 list
.addMouseListener(new MouseAdapter() {
210 public void mousePressed(MouseEvent e
) {
215 public void mouseReleased(MouseEvent e
) {
220 public void mouseExited(MouseEvent e
) {
221 if (ListModel
.this.popup
!= null
222 && ListModel
.this.popup
.isShowing())
225 if (hoveredIndex
> -1) {
226 int oldIndex
= hoveredIndex
;
228 fireElementChanged(oldIndex
);
232 private void check(MouseEvent e
) {
233 if (ListModel
.this.popup
== null) {
237 if (e
.isPopupTrigger()) {
238 if (list
.getSelectedIndices().length
<= 1) {
239 list
.setSelectedIndex(
240 list
.locationToIndex(e
.getPoint()));
244 ListModel
.this.popup
.show(list
, e
.getX(), e
.getY());
252 * (Try and) keep the elements that were selected when filtering.
254 * This will use toString on the elements to identify them, and can be a bit
255 * resource intensive.
257 * @return TRUE if we do
259 public boolean isKeepSelection() {
260 return keepSelection
;
264 * (Try and) keep the elements that were selected when filtering.
266 * This will use toString on the elements to identify them, and can be a bit
267 * resource intensive.
269 * @param keepSelection
270 * TRUE to try and keep them selected
272 public void setKeepSelection(boolean keepSelection
) {
273 this.keepSelection
= keepSelection
;
277 * The popup to use and keep track of (can be NULL).
279 * @return the current popup
281 public JPopupMenu
getPopup() {
286 * The popup to use and keep track of (can be NULL).
291 public void setPopup(JPopupMenu popup
) {
296 * You can use a {@link TooltipCreator} if you want the list to display
297 * tooltips on mouse hover (can be NULL).
299 * @return the current {@link TooltipCreator}
301 public TooltipCreator
<T
> getTooltipCreator() {
302 return tooltipCreator
;
306 * You can use a {@link TooltipCreator} if you want the list to display
307 * tooltips on mouse hover (can be NULL).
309 * @param tooltipCreator
310 * the new {@link TooltipCreator}
312 public void setTooltipCreator(TooltipCreator
<T
> tooltipCreator
) {
313 this.tooltipCreator
= tooltipCreator
;
317 * Check if this element is currently under the mouse.
320 * the element to check
322 * @return TRUE if it is
324 public boolean isHovered(T element
) {
325 return indexOf(element
) == hoveredIndex
;
329 * Check if this element is currently under the mouse.
332 * the index of the element to check
334 * @return TRUE if it is
336 public boolean isHovered(int index
) {
337 return index
== hoveredIndex
;
341 * Add an item to the model.
344 * the new item to add
346 public void addItem(T item
) {
351 * Add items to the model.
354 * the new items to add
356 public void addAllItems(Collection
<T
> items
) {
357 this.items
.addAll(items
);
361 * Removes the first occurrence of the specified element from this list, if
362 * it is present (optional operation).
365 * the item to remove if possible (can be NULL)
367 * @return TRUE if one element was removed, FALSE if not found
369 public boolean removeItem(T item
) {
370 return items
.remove(item
);
374 * Remove the items that pass the given filter (or all items if the filter
378 * the filter (if the filter returns TRUE, the item will be
381 * @return TRUE if at least one item was removed
383 public boolean removeItemIf(Predicate
<T
> filter
) {
384 boolean changed
= false;
385 if (filter
== null) {
386 changed
= !items
.isEmpty();
389 for (int i
= 0; i
< items
.size(); i
++) {
390 if (filter
.test(items
.get(i
))) {
401 * Removes all the items from this model.
403 public void clearItems() {
408 * Filter the current elements.
410 * This method will clear all the elements then look into all the items:
411 * those that pass the given filter will be copied as elements.
414 * the filter to select which elements to keep; an item that pass
415 * the filter will be copied as an element (can be NULL, in that
416 * case all items will be copied as elements)
418 @SuppressWarnings("unchecked") // JList<?> not compatible Java 1.6
419 public void filter(Predicate
<T
> filter
) {
420 ListSnapshot snapshot
= null;
423 snapshot
= new ListSnapshot(list
);
426 for (T item
: items
) {
427 if (filter
== null || filter
.test(item
)) {
439 * Return the currently selected elements.
441 * @return the selected elements
443 public List
<T
> getSelectedElements() {
444 List
<T
> selected
= new ArrayList
<T
>();
445 for (int index
: list
.getSelectedIndices()) {
446 selected
.add(get(index
));
453 * Return the selected element if <b>one</b> and <b>only one</b> element is
454 * selected. I.E., if zero, two or more elements are selected, NULL will be
457 * @return the element if it is the only selected element, NULL otherwise
459 public T
getUniqueSelectedElement() {
460 List
<T
> selected
= getSelectedElements();
461 if (selected
.size() == 1) {
462 return selected
.get(0);
469 * Notify that this element has been changed.
472 * the index of the element
474 public void fireElementChanged(int index
) {
476 fireContentsChanged(this, index
, index
);
481 * Notify that this element has been changed.
486 public void fireElementChanged(T element
) {
487 int index
= indexOf(element
);
489 fireContentsChanged(this, index
, index
);
493 @SuppressWarnings("unchecked") // JList<?> not compatible Java 1.6
495 public T
get(int index
) {
496 return (T
) super.get(index
);
499 private Window
newTooltip(final int index
, final MouseEvent me
) {
500 final T value
= ListModel
.this.get(index
);
501 final Window newTooltip
= tooltipCreator
.generateTooltip(value
, true);
502 if (newTooltip
!= null) {
503 newTooltip
.addMouseListener(new MouseAdapter() {
505 public void mouseClicked(MouseEvent e
) {
506 Window promotedTooltip
= tooltipCreator
507 .generateTooltip(value
, false);
508 if (promotedTooltip
!= null) {
509 promotedTooltip
.setLocation(me
.getXOnScreen(),
511 promotedTooltip
.setVisible(true);
514 newTooltip
.setVisible(false);
518 newTooltip
.setLocation(me
.getXOnScreen(), me
.getYOnScreen());
519 showTooltip(newTooltip
);
525 private void showTooltip(Window tooltip
) {
526 synchronized (tooltipWatcher
) {
527 if (this.tooltip
!= null) {
528 this.tooltip
.setVisible(false);
529 this.tooltip
.dispose();
532 this.tooltip
= tooltip
;
534 if (tooltip
!= null) {
535 tooltip
.setVisible(true);
541 * Generate a {@link ListCellRenderer} that supports {@link Hoverable}
545 * the type of elements and items (the same type), which should
546 * implement {@link Hoverable} (it will not cause issues if not,
547 * but then, it will be a default renderer)
551 * @return a suitable, {@link Hoverable} compatible renderer
553 static public <T
extends Component
> ListCellRenderer6
<T
> generateRenderer(
554 final ListModel
<T
> model
) {
555 return new ListCellRenderer6
<T
>() {
557 public Component
getListCellRendererComponent(JList6
<T
> list
,
558 T item
, int index
, boolean isSelected
,
559 boolean cellHasFocus
) {
560 if (item
instanceof Hoverable
) {
561 Hoverable hoverable
= (Hoverable
) item
;
562 hoverable
.setSelected(isSelected
);
563 hoverable
.setHovered(model
.isHovered(index
));