Merge branch 'subtree'
authorNiki Roo <niki@nikiroo.be>
Fri, 1 May 2020 09:09:12 +0000 (11:09 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 1 May 2020 09:09:12 +0000 (11:09 +0200)
1  2 
src/be/nikiroo/utils/Progress.java
src/be/nikiroo/utils/ui/DelayWorker.java
src/be/nikiroo/utils/ui/ListModel.java
src/be/nikiroo/utils/ui/UIUtils.java

index dea6be3fa011c351e4990b39e396368a50174e9d,748d4a666c377123ff2eadd29c840ec4baf402e5..748d4a666c377123ff2eadd29c840ec4baf402e5
@@@ -9,6 -9,16 +9,16 @@@ import java.util.Map.Entry
  
  /**
   * Progress reporting system, possibly nested.
+  * <p>
+  * A {@link Progress} can have a name, and that name will be reported through
+  * the event system (it will report the first non-null name in the stack from
+  * the {@link Progress} from which the event originated to the parent the event
+  * is listened on).
+  * <p>
+  * The {@link Progress} also has a table of keys/values shared amongst all the
+  * hierarchy (note that when adding a {@link Progress} to others, its values
+  * will be prioritized if some with the same keys were already present in the
+  * hierarchy).
   * 
   * @author niki
   */
@@@ -35,6 -45,7 +45,7 @@@ public class Progress 
                public void progress(Progress progress, String name);
        }
  
+       private Map<Object, Object> map = new HashMap<Object, Object>();
        private Progress parent = null;
        private Object lock = new Object();
        private String name;
                };
  
                synchronized (lock) {
+                       // Should not happen but just in case
+                       if (this.map != progress.map) {
+                               this.map.putAll(progress.map);
+                       }
+                       progress.map = this.map;
                        progress.parent = this;
                        this.children.put(progress, weight);
                        progress.addProgressListener(progressListener);
                }
        }
+       /**
+        * Set the given value for the given key on this {@link Progress} and it's
+        * children.
+        * 
+        * @param key
+        *            the key
+        * @param value
+        *            the value
+        */
+       public void put(Object key, Object value) {
+               map.put(key, value);
+       }
+       /**
+        * Return the value associated with this key as a {@link String} if any,
+        * NULL if not.
+        * <p>
+        * If the value is not NULL but not a {@link String}, it will be converted
+        * via {@link Object#toString()}.
+        * 
+        * @param key
+        *            the key to check
+        * 
+        * @return the value or NULL
+        */
+       public String getString(Object key) {
+               Object value = map.get(key);
+               if (value == null) {
+                       return null;
+               }
+               return value.toString();
+       }
+       /**
+        * Return the value associated with this key if any, NULL if not.
+        * 
+        * @param key
+        *            the key to check
+        * 
+        * @return the value or NULL
+        */
+       public Object get(Object key) {
+               return map.get(key);
+       }
  }
index 3618b89e51e25e28c6a549181065e89b59d67468,2a16c983b21ef54ee1b09dcc884d68e4ba6f0ee9..2a16c983b21ef54ee1b09dcc884d68e4ba6f0ee9
@@@ -201,7 -201,7 +201,7 @@@ public class DelayWorker 
         * @param worker
         *            the process to delay
         */
-       public void delay(final String id, final SwingWorker worker) {
+       public void delay(String id, SwingWorker worker) {
                synchronized (lazyEnCoursLock) {
                        lazyEnCours.put(id, worker);
                }
index e349deef344a0bdbe33078da7c5cd7fdc19edbce,cf16d5f0a111e660171102b3efad3bdefe1d6d48..cf16d5f0a111e660171102b3efad3bdefe1d6d48
@@@ -2,6 -2,7 +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 +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;
@@@ -78,10 -80,41 +80,41 @@@ public class ListModel<T> extends Defau
                public void setHovered(boolean hovered);
        }
  
+       /**
+        * An interface required to support tooltips on this {@link ListModel}.
+        * 
+        * @author niki
+        *
+        * @param <T>
+        *            the type of elements and items (the same type)
+        */
+       public interface TooltipCreator<T> {
+               /**
+                * Generate a tooltip {@link Window} for this element.
+                * <p>
+                * 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<T> items = new ArrayList<T>();
        private boolean keepSelection = true;
  
+       private TooltipCreator<T> tooltipCreator;
+       private Window tooltip;
        @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
        private JList list;
  
         *            the popup to use and keep track of (can be NULL)
         */
        @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
-       public ListModel(final JList6<T> list, final JPopupMenu popup) {
+       public ListModel(JList6<T> list, JPopupMenu popup) {
                this((JList) list, popup);
        }
  
+       /**
+        * Create a new {@link ListModel}.
+        * 
+        * @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)
+        */
+       @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
+       public ListModel(JList6<T> list, TooltipCreator<T> tooltipCreator) {
+               this((JList) list, null, tooltipCreator);
+       }
+       /**
+        * Create a new {@link ListModel}.
+        * 
+        * @param list
+        *            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<T> list, JPopupMenu popup,
+                       TooltipCreator<T> tooltipCreator) {
+               this((JList) list, popup, tooltipCreator);
+       }
        /**
         * Create a new {@link ListModel}.
         * <p>
         */
        @SuppressWarnings("rawtypes") // JList<?> not compatible Java 1.6
        public ListModel(JList list) {
-               this(list, null);
+               this(list, null, null);
        }
  
        /**
         * @param popup
         *            the popup to use and keep track of (can be NULL)
         */
+       @SuppressWarnings("rawtypes") // JList<?> not in Java 1.6
+       public ListModel(JList list, JPopupMenu popup) {
+               this(list, popup, null);
+       }
+       /**
+        * Create a new {@link ListModel}.
+        * <p>
+        * 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<T> tooltipCreator) {
+               this(list, null, tooltipCreator);
+       }
+       /**
+        * Create a new {@link ListModel}.
+        * <p>
+        * 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) {
+       public ListModel(final JList list, final JPopupMenu popup,
+                       TooltipCreator<T> tooltipCreator) {
                this.list = list;
+               this.tooltipCreator = tooltipCreator;
                list.setModel(this);
  
+               final DelayWorker tooltipWatcher = new DelayWorker(500);
+               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 (ListModel.this.tooltipCreator != null) {
+                                               tooltipWatcher.delay("tooltip",
+                                                               new SwingWorker<Void, Void>() {
+                                                                       @Override
+                                                                       protected Void doInBackground()
+                                                                                       throws Exception {
+                                                                               return null;
+                                                                       }
+                                                                       @Override
+                                                                       protected void done() {
+                                                                               Window oldTooltip = tooltip;
+                                                                               tooltip = null;
+                                                                               if (oldTooltip != null) {
+                                                                                       oldTooltip.setVisible(false);
+                                                                               }
+                                                                               if (index < 0
+                                                                                               || index != hoveredIndex) {
+                                                                                       return;
+                                                                               }
+                                                                               tooltip = newTooltip(index, me);
+                                                                       }
+                                                               });
+                                       }
                                }
                        }
                });
                                        popup.show(list, e.getX(), e.getY());
                                }
                        }
                });
        }
  
                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);
+                                       promotedTooltip.setLocation(newTooltip.getLocation());
+                                       newTooltip.setVisible(false);
+                                       promotedTooltip.setVisible(true);
+                               }
+                       });
+                       newTooltip.setLocation(me.getXOnScreen(), me.getYOnScreen());
+                       newTooltip.setVisible(true);
+               }
+               return newTooltip;
+       }
        /**
         * Generate a {@link ListCellRenderer} that supports {@link Hoverable}
         * elements.
index 9f16aabe79c3ba8dc80e8221bace21b2ec5f0d4a,5861d00fc3a6e3a254230c8bd5d43a17d26299db..5861d00fc3a6e3a254230c8bd5d43a17d26299db
@@@ -178,12 -178,39 +178,39 @@@ public class UIUtils 
         * @return the {@link JScrollPane}
         */
        static public JScrollPane scroll(JComponent pane, boolean allowHorizontal) {
+               return scroll(pane, allowHorizontal, true);
+       }
+       /**
+        * Add a {@link JScrollPane} around the given panel and use a sensible (for
+        * me) increment for the mouse wheel.
+        * 
+        * @param pane
+        *            the panel to wrap in a {@link JScrollPane}
+        * @param allowHorizontal
+        *            allow horizontal scrolling (not always desired)
+        * @param allowVertical
+        *            allow vertical scrolling (usually yes, but sometimes you only
+        *            want horizontal)
+        * 
+        * @return the {@link JScrollPane}
+        */
+       static public JScrollPane scroll(JComponent pane, boolean allowHorizontal,
+                       boolean allowVertical) {
                JScrollPane scroll = new JScrollPane(pane);
                scroll.getVerticalScrollBar().setUnitIncrement(16);
+               scroll.getHorizontalScrollBar().setUnitIncrement(16);
                if (!allowHorizontal) {
                        scroll.setHorizontalScrollBarPolicy(
                                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                }
+               if (!allowVertical) {
+                       scroll.setVerticalScrollBarPolicy(
+                                       JScrollPane.VERTICAL_SCROLLBAR_NEVER);
+               }
                return scroll;
        }
  }