Changeset 10893 in josm for trunk


Ignore:
Timestamp:
2016-08-24T23:13:14+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13447 - Clean notification manager, merge properties and remove unneded action (patch by michael2402) - gsoc-core

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/Notification.java

    r9221 r10893  
    3232public class Notification {
    3333
     34    /**
     35     * Default width of a notification
     36     */
    3437    public static final int DEFAULT_CONTENT_WIDTH = 350;
    3538
     
    5962
    6063    private Component content;
    61     private int duration;
     64    private int duration = Notification.TIME_DEFAULT;
    6265    private Icon icon;
    6366    private String helpTopic;
     
    6770     */
    6871    public Notification() {
    69         duration = NotificationManager.defaultNotificationTime;
     72        // nothing to do.
    7073    }
    7174
     
    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;
  • trunk/src/org/openstreetmap/josm/gui/NotificationManager.java

    r10633 r10893  
    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;
     
    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());
     
    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) {
     
    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
     
    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;
    161             }
    162             pauseTimer.restart();
    163         }
     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);
     154            }
     155            currentNotificationPanel = null;
     156        }
     157        pauseTimer.restart();
    164158    }
    165159
     
    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
     
    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));
     
    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) {
     
    307302            }
    308303        }
    309 
    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);
    319             }
    320         }
    321 
    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             }
     304    }
     305
     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();
     316            }
     317        }
     318
     319        @Override
     320        public void mouseExited(MouseEvent e) {
     321            unfreezeDelayTimer.restart();
    339322        }
    340323    }
     
    371354        }
    372355    }
     356
     357    public static synchronized NotificationManager getInstance() {
     358        if (instance == null) {
     359            instance = new NotificationManager();
     360        }
     361        return instance;
     362    }
    373363}
Note: See TracChangeset for help on using the changeset viewer.