X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=ui%2FListModel.java;h=12f6aa6217b0d66cda7a0fafe45997935284e6c6;hb=143d16e3caae370e6108aeb77680d11308d10e08;hp=52fa816a55a577aa1c8b3d27e7c9132443083029;hpb=f2573c3765c5e2d436c4d2871f3a450ee3ada1ed;p=fanfix.git diff --git a/ui/ListModel.java b/ui/ListModel.java index 52fa816..12f6aa6 100644 --- a/ui/ListModel.java +++ b/ui/ListModel.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.ui; import java.awt.Component; import java.awt.Point; +import java.awt.Window; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; @@ -11,6 +12,7 @@ import java.util.List; import javax.swing.JList; import javax.swing.JPopupMenu; import javax.swing.ListCellRenderer; +import javax.swing.SwingWorker; import be.nikiroo.utils.compat.DefaultListModel6; import be.nikiroo.utils.compat.JList6; @@ -32,6 +34,9 @@ import be.nikiroo.utils.compat.ListCellRenderer6; public class ListModel extends DefaultListModel6 { private static final long serialVersionUID = 1L; + /** How long to wait before displaying a tooltip, in milliseconds. */ + private static final int DELAY_TOOLTIP_MS = 1000; + /** * A filter interface, to check for a condition (note that a Predicate class * already exists in Java 1.8+, and is compatible with this one if you @@ -78,33 +83,66 @@ public class ListModel extends DefaultListModel6 { public void setHovered(boolean hovered); } + /** + * An interface required to support tooltips on this {@link ListModel}. + * + * @author niki + * + * @param + * the type of elements and items (the same type) + */ + public interface TooltipCreator { + /** + * Generate a tooltip {@link Window} for this element. + *

+ * Note that the tooltip can be of two modes: undecorated or standalone. + * An undecorated tooltip will be taken care of by this + * {@link ListModel}, but a standalone one is supposed to be its own + * Dialog or Frame (it won't be automatically closed). + * + * @param t + * the element to generate a tooltip for + * @param undecorated + * TRUE for undecorated tooltip, FALSE for standalone + * tooltips + * + * @return the generated tooltip or NULL for none + */ + public Window generateTooltip(T t, boolean undecorated); + } + private int hoveredIndex; private List items = new ArrayList(); - private JList6 list; private boolean keepSelection = true; + private TooltipCreator tooltipCreator; + private Window tooltip; + + @SuppressWarnings("rawtypes") // JList not compatible Java 1.6 + private JList list; + /** * Create a new {@link ListModel}. * * @param list - * the {@link JList} we will handle the data of (cannot be NULL) + * the {@link JList6} we will handle the data of (cannot be NULL) */ - @SuppressWarnings({ "unchecked", "rawtypes" }) // not compatible Java 1.6 - public ListModel(JList list) { - this((JList6) list); + @SuppressWarnings("rawtypes") // JList not compatible Java 1.6 + public ListModel(JList6 list) { + this((JList) list); } /** * Create a new {@link ListModel}. * * @param list - * the {@link JList} we will handle the data of (cannot be NULL) + * the {@link JList6} we will handle the data of (cannot be NULL) * @param popup * the popup to use and keep track of (can be NULL) */ - @SuppressWarnings({ "unchecked", "rawtypes" }) // not compatible Java 1.6 - public ListModel(final JList list, final JPopupMenu popup) { - this((JList6) list, popup); + @SuppressWarnings("rawtypes") // JList not compatible Java 1.6 + public ListModel(JList6 list, JPopupMenu popup) { + this((JList) list, popup); } /** @@ -112,9 +150,13 @@ public class ListModel extends DefaultListModel6 { * * @param list * the {@link JList6} we will handle the data of (cannot be NULL) + * @param tooltipCreator + * use this if you want the list to display tooltips on hover + * (can be NULL) */ - public ListModel(JList6 list) { - this(list, null); + @SuppressWarnings("rawtypes") // JList not compatible Java 1.6 + public ListModel(JList6 list, TooltipCreator tooltipCreator) { + this((JList) list, null, tooltipCreator); } /** @@ -124,24 +166,145 @@ public class ListModel extends DefaultListModel6 { * the {@link JList6} we will handle the data of (cannot be NULL) * @param popup * the popup to use and keep track of (can be NULL) + * @param tooltipCreator + * use this if you want the list to display tooltips on hover + * (can be NULL) + */ + @SuppressWarnings("rawtypes") // JList not compatible Java 1.6 + public ListModel(JList6 list, JPopupMenu popup, + TooltipCreator tooltipCreator) { + this((JList) list, popup, tooltipCreator); + } + + /** + * Create a new {@link ListModel}. + *

+ * Note that you must take care of passing a {@link JList} that only handles + * elements of the type of this {@link ListModel} -- you can also use + * {@link ListModel#ListModel(JList6)} instead. + * + * @param list + * the {@link JList} we will handle the data of (cannot be NULL, + * must only contain elements of the type of this + * {@link ListModel}) + */ + @SuppressWarnings("rawtypes") // JList not compatible Java 1.6 + public ListModel(JList list) { + this(list, null, null); + } + + /** + * Create a new {@link ListModel}. + *

+ * Note that you must take care of passing a {@link JList} that only handles + * elements of the type of this {@link ListModel} -- you can also use + * {@link ListModel#ListModel(JList6, JPopupMenu)} instead. + * + * @param list + * the {@link JList} we will handle the data of (cannot be NULL, + * must only contain elements of the type of this + * {@link ListModel}) + * @param popup + * the popup to use and keep track of (can be NULL) */ - public ListModel(final JList6 list, final JPopupMenu popup) { + @SuppressWarnings("rawtypes") // JList not in Java 1.6 + public ListModel(JList list, JPopupMenu popup) { + this(list, popup, null); + } + + /** + * Create a new {@link ListModel}. + *

+ * Note that you must take care of passing a {@link JList} that only handles + * elements of the type of this {@link ListModel} -- you can also use + * {@link ListModel#ListModel(JList6, JPopupMenu)} instead. + * + * @param list + * the {@link JList} we will handle the data of (cannot be NULL, + * must only contain elements of the type of this + * {@link ListModel}) + * @param tooltipCreator + * use this if you want the list to display tooltips on hover + * (can be NULL) + */ + @SuppressWarnings("rawtypes") // JList not in Java 1.6 + public ListModel(JList list, TooltipCreator tooltipCreator) { + this(list, null, tooltipCreator); + } + + /** + * Create a new {@link ListModel}. + *

+ * Note that you must take care of passing a {@link JList} that only handles + * elements of the type of this {@link ListModel} -- you can also use + * {@link ListModel#ListModel(JList6, JPopupMenu)} instead. + * + * @param list + * the {@link JList} we will handle the data of (cannot be NULL, + * must only contain elements of the type of this + * {@link ListModel}) + * @param popup + * the popup to use and keep track of (can be NULL) + * @param tooltipCreator + * use this if you want the list to display tooltips on hover + * (can be NULL) + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) // JList not in Java 1.6 + public ListModel(final JList list, final JPopupMenu popup, + final TooltipCreator tooltipCreator) { this.list = list; + this.tooltipCreator = tooltipCreator; + list.setModel(this); + final DelayWorker tooltipWatcher = new DelayWorker(DELAY_TOOLTIP_MS); + if (tooltipCreator != null) { + tooltipWatcher.start(); + } + list.addMouseMotionListener(new MouseAdapter() { @Override - public void mouseMoved(MouseEvent me) { + public void mouseMoved(final MouseEvent me) { if (popup != null && popup.isShowing()) return; Point p = new Point(me.getX(), me.getY()); - int index = list.locationToIndex(p); + final int index = list.locationToIndex(p); if (index != hoveredIndex) { int oldIndex = hoveredIndex; hoveredIndex = index; fireElementChanged(oldIndex); fireElementChanged(index); + + if (tooltipCreator != null) { + showTooltip(null); + + tooltipWatcher.delay("tooltip", + new SwingWorker() { + @Override + protected Void doInBackground() + throws Exception { + return null; + } + + @Override + protected void done() { + showTooltip(null); + + if (index < 0 + || index != hoveredIndex) { + return; + } + + if (popup != null + && popup.isShowing()) { + return; + } + + showTooltip(newTooltip(index, me)); + } + }); + } } } }); @@ -180,9 +343,11 @@ public class ListModel extends DefaultListModel6 { list.locationToIndex(e.getPoint())); } + showTooltip(null); popup.show(list, e.getX(), e.getY()); } } + }); } @@ -313,7 +478,7 @@ public class ListModel extends DefaultListModel6 { * the filter will be copied as an element (can be NULL, in that * case all items will be copied as elements) */ - @SuppressWarnings("unchecked") // ListModel and JList are not java 1.6 + @SuppressWarnings("unchecked") // JList not compatible Java 1.6 public void filter(Predicate filter) { ListSnapshot snapshot = null; @@ -388,12 +553,53 @@ public class ListModel extends DefaultListModel6 { } } - @SuppressWarnings("unchecked") // ListModel and JList are not java 1.6 + @SuppressWarnings("unchecked") // JList not compatible Java 1.6 @Override public T get(int index) { return (T) super.get(index); } + private Window newTooltip(final int index, final MouseEvent me) { + final T value = ListModel.this.get(index); + final Window newTooltip = tooltipCreator.generateTooltip(value, true); + if (newTooltip != null) { + newTooltip.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + Window promotedTooltip = tooltipCreator + .generateTooltip(value, false); + if (promotedTooltip != null) { + promotedTooltip.setLocation(me.getXOnScreen(), + me.getYOnScreen()); + promotedTooltip.setVisible(true); + } + + newTooltip.setVisible(false); + } + }); + + newTooltip.setLocation(me.getXOnScreen(), me.getYOnScreen()); + showTooltip(newTooltip); + } + + return newTooltip; + } + + private void showTooltip(Window tooltip) { + synchronized (tooltipCreator) { + if (this.tooltip != null) { + this.tooltip.setVisible(false); + this.tooltip.dispose(); + } + + this.tooltip = tooltip; + + if (tooltip != null) { + tooltip.setVisible(true); + } + } + } + /** * Generate a {@link ListCellRenderer} that supports {@link Hoverable} * elements.