Ticket #13447: patch-cleanup-notification.patch

File patch-cleanup-notification.patch, 9.7 KB (added by michael2402, 10 years ago)
  • src/org/openstreetmap/josm/gui/Notification.java

    diff --git a/src/org/openstreetmap/josm/gui/Notification.java b/src/org/openstreetmap/josm/gui/Notification.java
    index 19e15d9..70bb0b8 100644
    a b import org.openstreetmap.josm.gui.widgets.JMultilineLabel;  
    3131 */
    3232public class Notification {
    3333
     34    /**
     35     * Default width of a notification
     36     */
    3437    public static final int DEFAULT_CONTENT_WIDTH = 350;
    3538
    3639    // some standard duration values (in milliseconds)
    public class Notification {  
    5861    public static final int TIME_VERY_LONG = Main.pref.getInteger("notification-time-very_long-ms", 20000);
    5962
    6063    private Component content;
    61     private int duration;
     64    private int duration = Notification.TIME_DEFAULT;
    6265    private Icon icon;
    6366    private String helpTopic;
    6467
    public class Notification {  
    6669     * Constructs a new {@code Notification} without content.
    6770     */
    6871    public Notification() {
    69         duration = NotificationManager.defaultNotificationTime;
     72        // nothing to do.
    7073    }
    7174
    7275    /**
    public class Notification {  
    169172        return this;
    170173    }
    171174
     175    /**
     176     * Gets the content component to use.
     177     * @return The content
     178     */
    172179    public Component getContent() {
    173180        return content;
    174181    }
    175182
     183    /**
     184     * Gets the time the notification should be displayed
     185     * @return The time to display the notification
     186     */
    176187    public int getDuration() {
    177188        return duration;
    178189    }
    179190
     191    /**
     192     * Gets the icon that should be displayed next to the notification
     193     * @return The icon to display
     194     */
    180195    public Icon getIcon() {
    181196        return icon;
    182197    }
    183198
     199    /**
     200     * Gets the help topic for this notification
     201     * @return The help topic
     202     */
    184203    public String getHelpTopic() {
    185204        return helpTopic;
    186205    }
  • src/org/openstreetmap/josm/gui/NotificationManager.java

    diff --git a/src/org/openstreetmap/josm/gui/NotificationManager.java b/src/org/openstreetmap/josm/gui/NotificationManager.java
    index 49291a2..142de4f 100644
    a b import javax.swing.SwingUtilities;  
    3636import javax.swing.Timer;
    3737
    3838import org.openstreetmap.josm.Main;
     39import org.openstreetmap.josm.data.preferences.IntegerProperty;
    3940import org.openstreetmap.josm.gui.help.HelpBrowser;
    4041import org.openstreetmap.josm.gui.help.HelpUtil;
    4142import org.openstreetmap.josm.tools.ImageProvider;
    class NotificationManager {  
    6364    private NotificationPanel currentNotificationPanel;
    6465    private final Queue<Notification> queue;
    6566
    66     private static int pauseTime = Main.pref.getInteger("notification-default-pause-time-ms", 300); // milliseconds
    67     static int defaultNotificationTime = Main.pref.getInteger("notification-default-time-ms", 5000); // milliseconds
     67    private static IntegerProperty pauseTime = new IntegerProperty("notification-default-pause-time-ms", 300); // milliseconds
    6868
    6969    private long displayTimeStart;
    7070    private long elapsedTime;
    7171
    72     private static NotificationManager INSTANCE;
     72    private static NotificationManager instance;
    7373
    7474    private static final Color PANEL_SEMITRANSPARENT = new Color(224, 236, 249, 230);
    7575    private static final Color PANEL_OPAQUE = new Color(224, 236, 249);
    7676
    77     public static synchronized NotificationManager getInstance() {
    78         if (INSTANCE == null) {
    79             INSTANCE = new NotificationManager();
    80         }
    81         return INSTANCE;
    82     }
    83 
    8477    NotificationManager() {
    8578        queue = new LinkedList<>();
    86         hideTimer = new Timer(defaultNotificationTime, new HideEvent());
     79        hideTimer = new Timer(Notification.TIME_DEFAULT, e -> this.stopHideTimer());
    8780        hideTimer.setRepeats(false);
    88         pauseTimer = new Timer(pauseTime, new PauseFinishedEvent());
     81        pauseTimer = new Timer(pauseTime.get(), new PauseFinishedEvent());
    8982        pauseTimer.setRepeats(false);
    9083        unfreezeDelayTimer = new Timer(10, new UnfreezeEvent());
    9184        unfreezeDelayTimer.setRepeats(false);
    9285    }
    9386
     87    /**
     88     * Show the given notification
     89     * @param note The note to show.
     90     * @see Notification#show()
     91     */
    9492    public void showNotification(Notification note) {
    9593        synchronized (queue) {
    9694            queue.add(note);
    class NotificationManager {  
    104102        currentNotification = queue.poll();
    105103        if (currentNotification == null) return;
    106104
    107         currentNotificationPanel = new NotificationPanel(currentNotification);
     105        currentNotificationPanel = new NotificationPanel(currentNotification, new FreezeMouseListener(), e -> this.stopHideTimer());
    108106        currentNotificationPanel.validate();
    109107
    110108        int margin = 5;
    class NotificationManager {  
    146144        hideTimer.restart();
    147145    }
    148146
    149     private class HideEvent implements ActionListener {
    150 
    151         @Override
    152         public void actionPerformed(ActionEvent e) {
    153             hideTimer.stop();
    154             if (currentNotificationPanel != null) {
    155                 currentNotificationPanel.setVisible(false);
    156                 JFrame parent = (JFrame) Main.parent;
    157                 if (parent != null) {
    158                     parent.getLayeredPane().remove(currentNotificationPanel);
    159                 }
    160                 currentNotificationPanel = null;
     147    private void stopHideTimer() {
     148        hideTimer.stop();
     149        if (currentNotificationPanel != null) {
     150            currentNotificationPanel.setVisible(false);
     151            JFrame parent = (JFrame) Main.parent;
     152            if (parent != null) {
     153                parent.getLayeredPane().remove(currentNotificationPanel);
    161154            }
    162             pauseTimer.restart();
     155            currentNotificationPanel = null;
    163156        }
     157        pauseTimer.restart();
    164158    }
    165159
    166160    private class PauseFinishedEvent implements ActionListener {
    class NotificationManager {  
    186180        }
    187181    }
    188182
    189     private class NotificationPanel extends JPanel {
     183    private static class NotificationPanel extends JPanel {
    190184
    191185        private JPanel innerPanel;
    192186
    193         NotificationPanel(Notification note) {
     187        NotificationPanel(Notification note, MouseListener freeze, ActionListener hideListener) {
    194188            setVisible(false);
    195             build(note);
     189            build(note, freeze, hideListener);
    196190        }
    197191
    198192        public void setNotificationBackground(Color c) {
    199193            innerPanel.setBackground(c);
    200194        }
    201195
    202         private void build(final Notification note) {
    203             JButton btnClose = new JButton(new HideAction());
     196        private void build(final Notification note, MouseListener freeze,  ActionListener hideListener) {
     197            JButton btnClose = new JButton();
     198            btnClose.addActionListener(hideListener);
     199            btnClose.setIcon(ImageProvider.get("misc", "grey_x"));
    204200            btnClose.setPreferredSize(new Dimension(50, 50));
    205201            btnClose.setMargin(new Insets(0, 0, 1, 1));
    206202            btnClose.setContentAreaFilled(false);
    class NotificationManager {  
    294290             * of a second, background color is switched twice), so there is
    295291             * a tiny delay before the timer really resumes.
    296292             */
    297             MouseListener freeze = new FreezeMouseListener();
    298293            addMouseListenerToAllChildComponents(this, freeze);
    299294        }
    300295
    301         private void addMouseListenerToAllChildComponents(Component comp, MouseListener listener) {
     296        private static void addMouseListenerToAllChildComponents(Component comp, MouseListener listener) {
    302297            comp.addMouseListener(listener);
    303298            if (comp instanceof Container) {
    304299                for (Component c: ((Container) comp).getComponents()) {
    class NotificationManager {  
    306301                }
    307302            }
    308303        }
     304    }
    309305
    310         class HideAction extends AbstractAction {
    311 
    312             HideAction() {
    313                 putValue(SMALL_ICON, ImageProvider.get("misc", "grey_x"));
    314             }
    315 
    316             @Override
    317             public void actionPerformed(ActionEvent e) {
    318                 new HideEvent().actionPerformed(null);
     306    class FreezeMouseListener extends MouseAdapter {
     307        @Override
     308        public void mouseEntered(MouseEvent e) {
     309            if (unfreezeDelayTimer.isRunning()) {
     310                unfreezeDelayTimer.stop();
     311            } else {
     312                hideTimer.stop();
     313                elapsedTime += System.currentTimeMillis() - displayTimeStart;
     314                currentNotificationPanel.setNotificationBackground(PANEL_OPAQUE);
     315                currentNotificationPanel.repaint();
    319316            }
    320317        }
    321318
    322         class FreezeMouseListener extends MouseAdapter {
    323             @Override
    324             public void mouseEntered(MouseEvent e) {
    325                 if (unfreezeDelayTimer.isRunning()) {
    326                     unfreezeDelayTimer.stop();
    327                 } else {
    328                     hideTimer.stop();
    329                     elapsedTime += System.currentTimeMillis() - displayTimeStart;
    330                     currentNotificationPanel.setNotificationBackground(PANEL_OPAQUE);
    331                     currentNotificationPanel.repaint();
    332                 }
    333             }
    334 
    335             @Override
    336             public void mouseExited(MouseEvent e) {
    337                 unfreezeDelayTimer.restart();
    338             }
     319        @Override
     320        public void mouseExited(MouseEvent e) {
     321            unfreezeDelayTimer.restart();
    339322        }
    340323    }
    341324
    class NotificationManager {  
    370353            super.paintComponent(graphics);
    371354        }
    372355    }
     356
     357    public static synchronized NotificationManager getInstance() {
     358        if (instance == null) {
     359            instance = new NotificationManager();
     360        }
     361        return instance;
     362    }
    373363}