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

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

sonar - Local variable and method parameter names should comply with a naming convention

  • Property svn:eol-style set to native
File size: 13.2 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 int x, y;
112 JFrame parentWindow = (JFrame) Main.parent;
113 Dimension size = currentNotificationPanel.getPreferredSize();
114 if (Main.isDisplayingMapView() && Main.map.mapView.getHeight() > 0) {
115 MapView mv = Main.map.mapView;
116 Point mapViewPos = SwingUtilities.convertPoint(mv.getParent(), mv.getX(), mv.getY(), Main.parent);
117 x = mapViewPos.x + margin;
118 y = mapViewPos.y + mv.getHeight() - Main.map.statusLine.getHeight() - size.height - margin;
119 } else {
120 x = margin;
121 y = parentWindow.getHeight() - Main.toolbar.control.getSize().height - size.height - margin;
122 }
123 parentWindow.getLayeredPane().add(currentNotificationPanel, JLayeredPane.POPUP_LAYER, 0);
124
125 currentNotificationPanel.setLocation(x, y);
126 currentNotificationPanel.setSize(size);
127
128 currentNotificationPanel.setVisible(true);
129
130 running = true;
131 elapsedTime = 0;
132
133 startHideTimer();
134 }
135
136 private void startHideTimer() {
137 int remaining = (int) (currentNotification.getDuration() - elapsedTime);
138 if (remaining < 300) {
139 remaining = 300;
140 }
141 displayTimeStart = System.currentTimeMillis();
142 hideTimer.setInitialDelay(remaining);
143 hideTimer.restart();
144 }
145
146 private class HideEvent implements ActionListener {
147
148 @Override
149 public void actionPerformed(ActionEvent e) {
150 hideTimer.stop();
151 if (currentNotificationPanel != null) {
152 currentNotificationPanel.setVisible(false);
153 ((JFrame) Main.parent).getLayeredPane().remove(currentNotificationPanel);
154 currentNotificationPanel = null;
155 }
156 pauseTimer.restart();
157 }
158 }
159
160 private class PauseFinishedEvent implements ActionListener {
161
162 @Override
163 public void actionPerformed(ActionEvent e) {
164 synchronized (queue) {
165 running = false;
166 processQueue();
167 }
168 }
169 }
170
171 private class UnfreezeEvent implements ActionListener {
172
173 @Override
174 public void actionPerformed(ActionEvent e) {
175 if (currentNotificationPanel != null) {
176 currentNotificationPanel.setNotificationBackground(PANEL_SEMITRANSPARENT);
177 currentNotificationPanel.repaint();
178 }
179 startHideTimer();
180 }
181 }
182
183 private class NotificationPanel extends JPanel {
184
185 private JPanel innerPanel;
186
187 NotificationPanel(Notification note) {
188 setVisible(false);
189 build(note);
190 }
191
192 public void setNotificationBackground(Color c) {
193 innerPanel.setBackground(c);
194 }
195
196 private void build(final Notification note) {
197 JButton btnClose = new JButton(new HideAction());
198 btnClose.setPreferredSize(new Dimension(50, 50));
199 btnClose.setMargin(new Insets(0, 0, 1, 1));
200 btnClose.setContentAreaFilled(false);
201 // put it in JToolBar to get a better appearance
202 JToolBar tbClose = new JToolBar();
203 tbClose.setFloatable(false);
204 tbClose.setBorderPainted(false);
205 tbClose.setOpaque(false);
206 tbClose.add(btnClose);
207
208 JToolBar tbHelp = null;
209 if (note.getHelpTopic() != null) {
210 JButton btnHelp = new JButton(tr("Help"));
211 btnHelp.setIcon(ImageProvider.get("help"));
212 btnHelp.setToolTipText(tr("Show help information"));
213 HelpUtil.setHelpContext(btnHelp, note.getHelpTopic());
214 btnHelp.addActionListener(new AbstractAction() {
215 @Override
216 public void actionPerformed(ActionEvent e) {
217 SwingUtilities.invokeLater(new Runnable() {
218 @Override
219 public void run() {
220 HelpBrowser.setUrlForHelpTopic(note.getHelpTopic());
221 }
222 });
223 }
224 });
225 btnHelp.setOpaque(false);
226 tbHelp = new JToolBar();
227 tbHelp.setFloatable(false);
228 tbHelp.setBorderPainted(false);
229 tbHelp.setOpaque(false);
230 tbHelp.add(btnHelp);
231 }
232
233 setOpaque(false);
234 innerPanel = new RoundedPanel();
235 innerPanel.setBackground(PANEL_SEMITRANSPARENT);
236 innerPanel.setForeground(Color.BLACK);
237
238 GroupLayout layout = new GroupLayout(innerPanel);
239 innerPanel.setLayout(layout);
240 layout.setAutoCreateGaps(true);
241 layout.setAutoCreateContainerGaps(true);
242
243 innerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
244 add(innerPanel);
245
246 JLabel icon = null;
247 if (note.getIcon() != null) {
248 icon = new JLabel(note.getIcon());
249 }
250 Component content = note.getContent();
251 GroupLayout.SequentialGroup hgroup = layout.createSequentialGroup();
252 if (icon != null) {
253 hgroup.addComponent(icon);
254 }
255 if (tbHelp != null) {
256 hgroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
257 .addComponent(content)
258 .addComponent(tbHelp)
259 );
260 } else {
261 hgroup.addComponent(content);
262 }
263 hgroup.addComponent(tbClose);
264 GroupLayout.ParallelGroup vgroup = layout.createParallelGroup();
265 if (icon != null) {
266 vgroup.addComponent(icon);
267 }
268 vgroup.addComponent(content);
269 vgroup.addComponent(tbClose);
270 layout.setHorizontalGroup(hgroup);
271
272 if (tbHelp != null) {
273 layout.setVerticalGroup(layout.createSequentialGroup()
274 .addGroup(vgroup)
275 .addComponent(tbHelp)
276 );
277 } else {
278 layout.setVerticalGroup(vgroup);
279 }
280
281 /*
282 * The timer stops when the mouse cursor is above the panel.
283 *
284 * This is not straightforward, because the JPanel will get a
285 * mouseExited event when the cursor moves on top of the JButton
286 * inside the panel.
287 *
288 * The current hacky solution is to register the freeze MouseListener
289 * not only to the panel, but to all the components inside the panel.
290 *
291 * Moving the mouse cursor from one component to the next would
292 * cause some flickering (timer is started and stopped for a fraction
293 * of a second, background color is switched twice), so there is
294 * a tiny delay before the timer really resumes.
295 */
296 MouseListener freeze = new FreezeMouseListener();
297 addMouseListenerToAllChildComponents(this, freeze);
298 }
299
300 private void addMouseListenerToAllChildComponents(Component comp, MouseListener listener) {
301 comp.addMouseListener(listener);
302 if (comp instanceof Container) {
303 for (Component c: ((Container) comp).getComponents()) {
304 addMouseListenerToAllChildComponents(c, listener);
305 }
306 }
307 }
308
309 class HideAction extends AbstractAction {
310
311 HideAction() {
312 putValue(SMALL_ICON, ImageProvider.get("misc", "grey_x"));
313 }
314
315 @Override
316 public void actionPerformed(ActionEvent e) {
317 new HideEvent().actionPerformed(null);
318 }
319 }
320
321 class FreezeMouseListener extends MouseAdapter {
322 @Override
323 public void mouseEntered(MouseEvent e) {
324 if (unfreezeDelayTimer.isRunning()) {
325 unfreezeDelayTimer.stop();
326 } else {
327 hideTimer.stop();
328 elapsedTime += System.currentTimeMillis() - displayTimeStart;
329 currentNotificationPanel.setNotificationBackground(PANEL_OPAQUE);
330 currentNotificationPanel.repaint();
331 }
332 }
333
334 @Override
335 public void mouseExited(MouseEvent e) {
336 unfreezeDelayTimer.restart();
337 }
338 }
339 }
340
341 /**
342 * A panel with rounded edges and line border.
343 */
344 public static class RoundedPanel extends JPanel {
345
346 RoundedPanel() {
347 super();
348 setOpaque(false);
349 }
350
351 @Override
352 protected void paintComponent(Graphics graphics) {
353 Graphics2D g = (Graphics2D) graphics;
354 g.setRenderingHint(
355 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
356 g.setColor(getBackground());
357 float lineWidth = 1.4f;
358 Shape rect = new RoundRectangle2D.Double(
359 lineWidth/2d + getInsets().left,
360 lineWidth/2d + getInsets().top,
361 getWidth() - lineWidth/2d - getInsets().left - getInsets().right,
362 getHeight() - lineWidth/2d - getInsets().top - getInsets().bottom,
363 20, 20);
364
365 g.fill(rect);
366 g.setColor(getForeground());
367 g.setStroke(new BasicStroke(lineWidth));
368 g.draw(rect);
369 super.paintComponent(graphics);
370 }
371 }
372}
Note: See TracBrowser for help on using the repository browser.