source: josm/trunk/src/org/openstreetmap/josm/gui/Notification.java

Last change on this file was 17628, checked in by simon04, 3 years ago

fix #16709 - Display a notification while/after saving files

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.Color;
5import java.awt.Component;
6import java.util.Objects;
7
8import javax.swing.Icon;
9import javax.swing.JEditorPane;
10import javax.swing.JOptionPane;
11import javax.swing.UIManager;
12import javax.swing.text.JTextComponent;
13
14import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
15import org.openstreetmap.josm.spi.preferences.Config;
16
17/**
18 * A Notification Message similar to a popup window, but without disrupting the
19 * user's workflow.
20 *
21 * Non-modal info panel that vanishes after a certain time.
22 *
23 * This class only holds the data for a notification, {@link NotificationManager}
24 * is responsible for building the message panel and displaying it on screen.
25 *
26 * example:
27 * <pre>
28 * Notification note = new Notification("Hi there!");
29 * note.setIcon(JOptionPane.INFORMATION_MESSAGE); // optional
30 * note.setDuration(Notification.TIME_SHORT); // optional
31 * note.show();
32 * </pre>
33 */
34public class Notification {
35
36 /**
37 * Default width of a notification
38 */
39 public static final int DEFAULT_CONTENT_WIDTH = 350;
40
41 // some standard duration values (in milliseconds)
42
43 /**
44 * Very short and very easy to grasp message (3 s).
45 * E.g. "Please select at least one node".
46 */
47 public static final int TIME_SHORT = Config.getPref().getInt("notification-time-short-ms", 3000);
48
49 /**
50 * Short message of one or two lines (5 s).
51 */
52 public static final int TIME_DEFAULT = Config.getPref().getInt("notification-time-default-ms", 5000);
53
54 /**
55 * Somewhat longer message (10 s).
56 */
57 public static final int TIME_LONG = Config.getPref().getInt("notification-time-long-ms", 10_000);
58
59 /**
60 * Long text.
61 * (Make sure is still sensible to show as a notification)
62 */
63 public static final int TIME_VERY_LONG = Config.getPref().getInt("notification-time-very_long-ms", 20_000);
64
65 private Component content;
66 private int duration = Notification.TIME_DEFAULT;
67 private Icon icon;
68 private String helpTopic;
69
70 /**
71 * Constructs a new {@code Notification} without content.
72 */
73 public Notification() {
74 // nothing to do.
75 }
76
77 /**
78 * Constructs a new {@code Notification} with the given textual content.
79 * @param msg The text to display
80 */
81 public Notification(String msg) {
82 this();
83 setContent(msg);
84 }
85
86 /**
87 * Set the content of the message.
88 *
89 * @param content any Component to be shown
90 *
91 * @return the current Object, for convenience
92 * @see #setContent(java.lang.String)
93 */
94 public Notification setContent(Component content) {
95 this.content = content;
96 return this;
97 }
98
99 /**
100 * Set the notification text. (Convenience method)
101 *
102 * @param msg the message String. Will be wrapped in &lt;html&gt;, so
103 * you can use &lt;br&gt; and other markup directly.
104 *
105 * @return the current Object, for convenience
106 * @see #Notification(java.lang.String)
107 */
108 public Notification setContent(String msg) {
109 JMultilineLabel lbl = new JMultilineLabel(msg);
110 lbl.setMaxWidth(DEFAULT_CONTENT_WIDTH);
111 lbl.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
112 lbl.setForeground(Color.BLACK);
113 content = lbl;
114 return this;
115 }
116
117 /**
118 * Set the time after which the message is hidden.
119 *
120 * @param duration the time (in milliseconds)
121 * Preset values {@link #TIME_SHORT}, {@link #TIME_DEFAULT}, {@link #TIME_LONG}
122 * and {@link #TIME_VERY_LONG} can be used.
123 * @return the current Object, for convenience
124 */
125 public Notification setDuration(int duration) {
126 this.duration = duration;
127 return this;
128 }
129
130 /**
131 * Set an icon to display on the left part of the message window.
132 *
133 * @param icon the icon (null means no icon is displayed)
134 * @return the current Object, for convenience
135 */
136 public Notification setIcon(Icon icon) {
137 this.icon = icon;
138 return this;
139 }
140
141 /**
142 * Set an icon to display on the left part of the message window by
143 * choosing from the default JOptionPane icons.
144 *
145 * @param messageType one of the following: JOptionPane.ERROR_MESSAGE,
146 * JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE,
147 * JOptionPane.QUESTION_MESSAGE, JOptionPane.PLAIN_MESSAGE
148 * @return the current Object, for convenience
149 */
150 public Notification setIcon(int messageType) {
151 switch (messageType) {
152 case JOptionPane.ERROR_MESSAGE:
153 return setIcon(UIManager.getIcon("OptionPane.errorIcon"));
154 case JOptionPane.INFORMATION_MESSAGE:
155 return setIcon(UIManager.getIcon("OptionPane.informationIcon"));
156 case JOptionPane.WARNING_MESSAGE:
157 return setIcon(UIManager.getIcon("OptionPane.warningIcon"));
158 case JOptionPane.QUESTION_MESSAGE:
159 return setIcon(UIManager.getIcon("OptionPane.questionIcon"));
160 case JOptionPane.PLAIN_MESSAGE:
161 return setIcon(null);
162 default:
163 throw new IllegalArgumentException("Unknown message type!");
164 }
165 }
166
167 /**
168 * Display a help button at the bottom of the notification window.
169 * @param helpTopic the help topic
170 * @return the current Object, for convenience
171 */
172 public Notification setHelpTopic(String helpTopic) {
173 this.helpTopic = helpTopic;
174 return this;
175 }
176
177 /**
178 * Gets the content component to use.
179 * @return The content
180 */
181 public Component getContent() {
182 return content;
183 }
184
185 /**
186 * Gets the time the notification should be displayed
187 * @return The time to display the notification
188 */
189 public int getDuration() {
190 return duration;
191 }
192
193 /**
194 * Gets the icon that should be displayed next to the notification
195 * @return The icon to display
196 */
197 public Icon getIcon() {
198 return icon;
199 }
200
201 /**
202 * Gets the help topic for this notification
203 * @return The help topic
204 */
205 public String getHelpTopic() {
206 return helpTopic;
207 }
208
209 /**
210 * Display the notification.
211 */
212 public void show() {
213 NotificationManager.getInstance().showNotification(this);
214 }
215
216 /**
217 * Display the notification by replacing the given queued/displaying notification
218 * @param oldNotification the notification to replace
219 * @since 17628
220 */
221 public void replaceExisting(Notification oldNotification) {
222 NotificationManager.getInstance().replaceExistingNotification(oldNotification, this);
223 }
224
225 private Object getContentTextOrComponent() {
226 return content instanceof JTextComponent ? ((JTextComponent) content).getText() : content;
227 }
228
229 @Override
230 public boolean equals(Object o) {
231 if (this == o) return true;
232 if (o == null || getClass() != o.getClass()) return false;
233 Notification that = (Notification) o;
234 return duration == that.duration
235 && Objects.equals(getContentTextOrComponent(), that.getContentTextOrComponent())
236 && Objects.equals(helpTopic, that.helpTopic);
237 }
238
239 @Override
240 public int hashCode() {
241 return Objects.hash(getContentTextOrComponent(), duration, helpTopic);
242 }
243
244 @Override
245 public String toString() {
246 return "Notification{" +
247 "content=" + getContentTextOrComponent() +
248 ", duration=" + duration +
249 ", helpTopic='" + helpTopic + '\'' +
250 '}';
251 }
252}
Note: See TracBrowser for help on using the repository browser.