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

Last change on this file since 13111 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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