source: josm/trunk/src/org/openstreetmap/josm/gui/NotificationManager.java@ 10619

Last change on this file since 10619 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 13.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BasicStroke;
7import java.awt.Color;
8import java.awt.Component;
9import java.awt.Container;
10import java.awt.Dimension;
11import java.awt.Graphics;
12import java.awt.Graphics2D;
13import java.awt.Insets;
14import java.awt.Point;
15import java.awt.RenderingHints;
16import java.awt.Shape;
17import java.awt.event.ActionEvent;
18import java.awt.event.ActionListener;
19import java.awt.event.MouseAdapter;
20import java.awt.event.MouseEvent;
21import java.awt.event.MouseListener;
22import java.awt.geom.RoundRectangle2D;
23import java.util.LinkedList;
24import java.util.Queue;
25
26import javax.swing.AbstractAction;
27import javax.swing.BorderFactory;
28import javax.swing.GroupLayout;
29import javax.swing.JButton;
30import javax.swing.JFrame;
31import javax.swing.JLabel;
32import javax.swing.JLayeredPane;
33import javax.swing.JPanel;
34import javax.swing.JToolBar;
35import javax.swing.SwingUtilities;
36import javax.swing.Timer;
37
38import org.openstreetmap.josm.Main;
39import org.openstreetmap.josm.gui.help.HelpBrowser;
40import org.openstreetmap.josm.gui.help.HelpUtil;
41import org.openstreetmap.josm.tools.ImageProvider;
42
43/**
44 * Manages {@link Notification}s, i.e. displays them on screen.
45 *
46 * Don't use this class directly, but use {@link Notification#show()}.
47 *
48 * If multiple messages are sent in a short period of time, they are put in
49 * a queue and displayed one after the other.
50 *
51 * The user can stop the timer (freeze the message) by moving the mouse cursor
52 * above the panel. As a visual cue, the background color changes from
53 * semi-transparent to opaque while the timer is frozen.
54 */
55class NotificationManager {
56
57 private final Timer hideTimer; // started when message is shown, responsible for hiding the message
58 private final Timer pauseTimer; // makes sure, there is a small pause between two consecutive messages
59 private final Timer unfreezeDelayTimer; // tiny delay before resuming the timer when mouse cursor is moved off the panel
60 private boolean running;
61
62 private Notification currentNotification;
63 private NotificationPanel currentNotificationPanel;
64 private final Queue<Notification> queue;
65
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
68
69 private long displayTimeStart;
70 private long elapsedTime;
71
72 private static NotificationManager INSTANCE;
73
74 private static final Color PANEL_SEMITRANSPARENT = new Color(224, 236, 249, 230);
75 private static final Color PANEL_OPAQUE = new Color(224, 236, 249);
76
77 public static synchronized NotificationManager getInstance() {
78 if (INSTANCE == null) {
79 INSTANCE = new NotificationManager();
80 }
81 return INSTANCE;
82 }
83
84 NotificationManager() {
85 queue = new LinkedList<>();
86 hideTimer = new Timer(defaultNotificationTime, new HideEvent());
87 hideTimer.setRepeats(false);
88 pauseTimer = new Timer(pauseTime, new PauseFinishedEvent());
89 pauseTimer.setRepeats(false);
90 unfreezeDelayTimer = new Timer(10, new UnfreezeEvent());
91 unfreezeDelayTimer.setRepeats(false);
92 }
93
94 public void showNotification(Notification note) {
95 synchronized (queue) {
96 queue.add(note);
97 processQueue();
98 }
99 }
100
101 private void processQueue() {
102 if (running) return;
103
104 currentNotification = queue.poll();
105 if (currentNotification == null) return;
106
107 currentNotificationPanel = new NotificationPanel(currentNotification);
108 currentNotificationPanel.validate();
109
110 int margin = 5;
111 JFrame parentWindow = (JFrame) Main.parent;
112 Dimension size = currentNotificationPanel.getPreferredSize();
113 if (parentWindow != null) {
114 int x;
115 int y;
116 if (Main.isDisplayingMapView() && Main.map.mapView.getHeight() > 0) {
117 MapView mv = Main.map.mapView;
118 Point mapViewPos = SwingUtilities.convertPoint(mv.getParent(), mv.getX(), mv.getY(), Main.parent);
119 x = mapViewPos.x + margin;
120 y = mapViewPos.y + mv.getHeight() - Main.map.statusLine.getHeight() - size.height - margin;
121 } else {
122 x = margin;
123 y = parentWindow.getHeight() - Main.toolbar.control.getSize().height - size.height - margin;
124 }
125 parentWindow.getLayeredPane().add(currentNotificationPanel, JLayeredPane.POPUP_LAYER, 0);
126
127 currentNotificationPanel.setLocation(x, y);
128 }
129 currentNotificationPanel.setSize(size);
130
131 currentNotificationPanel.setVisible(true);
132
133 running = true;
134 elapsedTime = 0;
135
136 startHideTimer();
137 }
138
139 private void startHideTimer() {
140 int remaining = (int) (currentNotification.getDuration() - elapsedTime);
141 if (remaining < 300) {
142 remaining = 300;
143 }
144 displayTimeStart = System.currentTimeMillis();
145 hideTimer.setInitialDelay(remaining);
146 hideTimer.restart();
147 }
148
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) Main.parent).getLayeredPane().remove(currentNotificationPanel);
157 currentNotificationPanel = null;
158 }
159 pauseTimer.restart();
160 }
161 }
162
163 private class PauseFinishedEvent implements ActionListener {
164
165 @Override
166 public void actionPerformed(ActionEvent e) {
167 synchronized (queue) {
168 running = false;
169 processQueue();
170 }
171 }
172 }
173
174 private class UnfreezeEvent implements ActionListener {
175
176 @Override
177 public void actionPerformed(ActionEvent e) {
178 if (currentNotificationPanel != null) {
179 currentNotificationPanel.setNotificationBackground(PANEL_SEMITRANSPARENT);
180 currentNotificationPanel.repaint();
181 }
182 startHideTimer();
183 }
184 }
185
186 private class NotificationPanel extends JPanel {
187
188 private JPanel innerPanel;
189
190 NotificationPanel(Notification note) {
191 setVisible(false);
192 build(note);
193 }
194
195 public void setNotificationBackground(Color c) {
196 innerPanel.setBackground(c);
197 }
198
199 private void build(final Notification note) {
200 JButton btnClose = new JButton(new HideAction());
201 btnClose.setPreferredSize(new Dimension(50, 50));
202 btnClose.setMargin(new Insets(0, 0, 1, 1));
203 btnClose.setContentAreaFilled(false);
204 // put it in JToolBar to get a better appearance
205 JToolBar tbClose = new JToolBar();
206 tbClose.setFloatable(false);
207 tbClose.setBorderPainted(false);
208 tbClose.setOpaque(false);
209 tbClose.add(btnClose);
210
211 JToolBar tbHelp = null;
212 if (note.getHelpTopic() != null) {
213 JButton btnHelp = new JButton(tr("Help"));
214 btnHelp.setIcon(ImageProvider.get("help"));
215 btnHelp.setToolTipText(tr("Show help information"));
216 HelpUtil.setHelpContext(btnHelp, note.getHelpTopic());
217 btnHelp.addActionListener(new AbstractAction() {
218 @Override
219 public void actionPerformed(ActionEvent e) {
220 SwingUtilities.invokeLater(() -> HelpBrowser.setUrlForHelpTopic(note.getHelpTopic()));
221 }
222 });
223 btnHelp.setOpaque(false);
224 tbHelp = new JToolBar();
225 tbHelp.setFloatable(false);
226 tbHelp.setBorderPainted(false);
227 tbHelp.setOpaque(false);
228 tbHelp.add(btnHelp);
229 }
230
231 setOpaque(false);
232 innerPanel = new RoundedPanel();
233 innerPanel.setBackground(PANEL_SEMITRANSPARENT);
234 innerPanel.setForeground(Color.BLACK);
235
236 GroupLayout layout = new GroupLayout(innerPanel);
237 innerPanel.setLayout(layout);
238 layout.setAutoCreateGaps(true);
239 layout.setAutoCreateContainerGaps(true);
240
241 innerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
242 add(innerPanel);
243
244 JLabel icon = null;
245 if (note.getIcon() != null) {
246 icon = new JLabel(note.getIcon());
247 }
248 Component content = note.getContent();
249 GroupLayout.SequentialGroup hgroup = layout.createSequentialGroup();
250 if (icon != null) {
251 hgroup.addComponent(icon);
252 }
253 if (tbHelp != null) {
254 hgroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
255 .addComponent(content)
256 .addComponent(tbHelp)
257 );
258 } else {
259 hgroup.addComponent(content);
260 }
261 hgroup.addComponent(tbClose);
262 GroupLayout.ParallelGroup vgroup = layout.createParallelGroup();
263 if (icon != null) {
264 vgroup.addComponent(icon);
265 }
266 vgroup.addComponent(content);
267 vgroup.addComponent(tbClose);
268 layout.setHorizontalGroup(hgroup);
269
270 if (tbHelp != null) {
271 layout.setVerticalGroup(layout.createSequentialGroup()
272 .addGroup(vgroup)
273 .addComponent(tbHelp)
274 );
275 } else {
276 layout.setVerticalGroup(vgroup);
277 }
278
279 /*
280 * The timer stops when the mouse cursor is above the panel.
281 *
282 * This is not straightforward, because the JPanel will get a
283 * mouseExited event when the cursor moves on top of the JButton
284 * inside the panel.
285 *
286 * The current hacky solution is to register the freeze MouseListener
287 * not only to the panel, but to all the components inside the panel.
288 *
289 * Moving the mouse cursor from one component to the next would
290 * cause some flickering (timer is started and stopped for a fraction
291 * of a second, background color is switched twice), so there is
292 * a tiny delay before the timer really resumes.
293 */
294 MouseListener freeze = new FreezeMouseListener();
295 addMouseListenerToAllChildComponents(this, freeze);
296 }
297
298 private void addMouseListenerToAllChildComponents(Component comp, MouseListener listener) {
299 comp.addMouseListener(listener);
300 if (comp instanceof Container) {
301 for (Component c: ((Container) comp).getComponents()) {
302 addMouseListenerToAllChildComponents(c, listener);
303 }
304 }
305 }
306
307 class HideAction extends AbstractAction {
308
309 HideAction() {
310 putValue(SMALL_ICON, ImageProvider.get("misc", "grey_x"));
311 }
312
313 @Override
314 public void actionPerformed(ActionEvent e) {
315 new HideEvent().actionPerformed(null);
316 }
317 }
318
319 class FreezeMouseListener extends MouseAdapter {
320 @Override
321 public void mouseEntered(MouseEvent e) {
322 if (unfreezeDelayTimer.isRunning()) {
323 unfreezeDelayTimer.stop();
324 } else {
325 hideTimer.stop();
326 elapsedTime += System.currentTimeMillis() - displayTimeStart;
327 currentNotificationPanel.setNotificationBackground(PANEL_OPAQUE);
328 currentNotificationPanel.repaint();
329 }
330 }
331
332 @Override
333 public void mouseExited(MouseEvent e) {
334 unfreezeDelayTimer.restart();
335 }
336 }
337 }
338
339 /**
340 * A panel with rounded edges and line border.
341 */
342 public static class RoundedPanel extends JPanel {
343
344 RoundedPanel() {
345 super();
346 setOpaque(false);
347 }
348
349 @Override
350 protected void paintComponent(Graphics graphics) {
351 Graphics2D g = (Graphics2D) graphics;
352 g.setRenderingHint(
353 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
354 g.setColor(getBackground());
355 float lineWidth = 1.4f;
356 Shape rect = new RoundRectangle2D.Double(
357 lineWidth/2d + getInsets().left,
358 lineWidth/2d + getInsets().top,
359 getWidth() - lineWidth/2d - getInsets().left - getInsets().right,
360 getHeight() - lineWidth/2d - getInsets().top - getInsets().bottom,
361 20, 20);
362
363 g.fill(rect);
364 g.setColor(getForeground());
365 g.setStroke(new BasicStroke(lineWidth));
366 g.draw(rect);
367 super.paintComponent(graphics);
368 }
369 }
370}
Note: See TracBrowser for help on using the repository browser.