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