1 | // License: GPL. For details, see LICENSE file. |
---|
2 | package org.openstreetmap.josm.gui; |
---|
3 | |
---|
4 | import java.awt.Component; |
---|
5 | |
---|
6 | import javax.swing.Icon; |
---|
7 | import javax.swing.JOptionPane; |
---|
8 | import javax.swing.UIManager; |
---|
9 | |
---|
10 | import org.openstreetmap.josm.Main; |
---|
11 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel; |
---|
12 | |
---|
13 | /** |
---|
14 | * A Notification Message similar to a popup window, but without disrupting the |
---|
15 | * user's workflow. |
---|
16 | * |
---|
17 | * Non-modal info panel that vanishes after a certain time. |
---|
18 | * |
---|
19 | * This class only holds the data for a notification, {@link NotificationManager} |
---|
20 | * is responsible for building the message panel and displaying it on screen. |
---|
21 | * |
---|
22 | * example: |
---|
23 | * <pre> |
---|
24 | * Notification note = new Notification("Hi there!"); |
---|
25 | * note.setIcon(JOptionPane.INFORMATION_MESSAGE); // optional |
---|
26 | * note.setDuration(Notification.TIME_SHORT); // optional |
---|
27 | * note.show(); |
---|
28 | * </pre> |
---|
29 | */ |
---|
30 | public class Notification { |
---|
31 | |
---|
32 | public final static int DEFAULT_CONTENT_WIDTH = 350; |
---|
33 | |
---|
34 | // some standard duration values (in milliseconds) |
---|
35 | |
---|
36 | /** |
---|
37 | * Very short and very easy to grasp message (3 s). |
---|
38 | * E.g. "Please select at least one node". |
---|
39 | */ |
---|
40 | public final static int TIME_SHORT = Main.pref.getInteger("notification-time-short-ms", 3000); |
---|
41 | /** |
---|
42 | * Short message of one or two lines (5 s). |
---|
43 | */ |
---|
44 | public final static int TIME_DEFAULT = Main.pref.getInteger("notification-time-default-ms", 5000); |
---|
45 | /** |
---|
46 | * Somewhat longer message (10 s). |
---|
47 | */ |
---|
48 | public final static int TIME_LONG = Main.pref.getInteger("notification-time-long-ms", 10000); |
---|
49 | /** |
---|
50 | * Long text. |
---|
51 | * (Make sure is still sensible to show as a notification) |
---|
52 | */ |
---|
53 | public final static int TIME_VERY_LONG = Main.pref.getInteger("notification-time-very_long-ms", 20000); |
---|
54 | |
---|
55 | private Component content; |
---|
56 | private int duration; |
---|
57 | private Icon icon; |
---|
58 | private String helpTopic; |
---|
59 | |
---|
60 | public Notification() { |
---|
61 | duration = NotificationManager.defaultNotificationTime; |
---|
62 | } |
---|
63 | |
---|
64 | public Notification(String msg) { |
---|
65 | this(); |
---|
66 | setContent(msg); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Set the content of the message. |
---|
71 | * |
---|
72 | * @param content any Component to be shown |
---|
73 | * |
---|
74 | * @see #setContent(java.lang.String) |
---|
75 | * @return the current Object, for convenience |
---|
76 | */ |
---|
77 | public Notification setContent(Component content) { |
---|
78 | this.content = content; |
---|
79 | return this; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Set the notification text. (Convenience method) |
---|
84 | * |
---|
85 | * @param msg the message String. Will be wrapped in <html>, so |
---|
86 | * you can use <br> and other markup directly. |
---|
87 | * |
---|
88 | * @see #Notification(java.lang.String) |
---|
89 | * @return the current Object, for convenience |
---|
90 | */ |
---|
91 | public Notification setContent(String msg) { |
---|
92 | JMultilineLabel lbl = new JMultilineLabel(msg); |
---|
93 | lbl.setMaxWidth(DEFAULT_CONTENT_WIDTH); |
---|
94 | content = lbl; |
---|
95 | return this; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Set the time after which the message is hidden. |
---|
100 | * |
---|
101 | * @param duration the time (in milliseconds) |
---|
102 | * Preset values {@link #TIME_SHORT}, {@link #TIME_DEFAULT}, {@link #TIME_LONG} |
---|
103 | * and {@link #TIME_VERY_LONG} can be used. |
---|
104 | * @return the current Object, for convenience |
---|
105 | */ |
---|
106 | public Notification setDuration(int duration) { |
---|
107 | this.duration = duration; |
---|
108 | return this; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Set an icon to display on the left part of the message window. |
---|
113 | * |
---|
114 | * @param icon the icon (null means no icon is displayed) |
---|
115 | * @return the current Object, for convenience |
---|
116 | */ |
---|
117 | public Notification setIcon(Icon icon) { |
---|
118 | this.icon = icon; |
---|
119 | return this; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Set an icon to display on the left part of the message window by |
---|
124 | * choosing from the default JOptionPane icons. |
---|
125 | * |
---|
126 | * @param messageType one of the following: JOptionPane.ERROR_MESSAGE, |
---|
127 | * JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE, |
---|
128 | * JOptionPane.QUESTION_MESSAGE, JOptionPane.PLAIN_MESSAGE |
---|
129 | * @return the current Object, for convenience |
---|
130 | */ |
---|
131 | public Notification setIcon(int messageType) { |
---|
132 | switch (messageType) { |
---|
133 | case JOptionPane.ERROR_MESSAGE: |
---|
134 | return setIcon(UIManager.getIcon("OptionPane.errorIcon")); |
---|
135 | case JOptionPane.INFORMATION_MESSAGE: |
---|
136 | return setIcon(UIManager.getIcon("OptionPane.informationIcon")); |
---|
137 | case JOptionPane.WARNING_MESSAGE: |
---|
138 | return setIcon(UIManager.getIcon("OptionPane.warningIcon")); |
---|
139 | case JOptionPane.QUESTION_MESSAGE: |
---|
140 | return setIcon(UIManager.getIcon("OptionPane.questionIcon")); |
---|
141 | case JOptionPane.PLAIN_MESSAGE: |
---|
142 | return setIcon(null); |
---|
143 | default: |
---|
144 | throw new IllegalArgumentException("Unknown message type!"); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Display a help button at the bottom of the notification window. |
---|
150 | * @param helpTopic the help topic |
---|
151 | * @return the current Object, for convenience |
---|
152 | */ |
---|
153 | public Notification setHelpTopic(String helpTopic) { |
---|
154 | this.helpTopic = helpTopic; |
---|
155 | return this; |
---|
156 | } |
---|
157 | |
---|
158 | public Component getContent() { |
---|
159 | return content; |
---|
160 | } |
---|
161 | |
---|
162 | public int getDuration() { |
---|
163 | return duration; |
---|
164 | } |
---|
165 | |
---|
166 | public Icon getIcon() { |
---|
167 | return icon; |
---|
168 | } |
---|
169 | |
---|
170 | public String getHelpTopic() { |
---|
171 | return helpTopic; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Display the notification. |
---|
176 | */ |
---|
177 | public void show() { |
---|
178 | NotificationManager.getInstance().showNotification(this); |
---|
179 | } |
---|
180 | } |
---|