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

Last change on this file since 7505 was 6889, checked in by Don-vip, 10 years ago

fix some Sonar issues (JLS order)

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.Component;
5
6import javax.swing.Icon;
7import javax.swing.JOptionPane;
8import javax.swing.UIManager;
9
10import org.openstreetmap.josm.Main;
11import 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 */
30public class Notification {
31
32 public static final 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 static final int TIME_SHORT = Main.pref.getInteger("notification-time-short-ms", 3000);
41
42 /**
43 * Short message of one or two lines (5 s).
44 */
45 public static final int TIME_DEFAULT = Main.pref.getInteger("notification-time-default-ms", 5000);
46
47 /**
48 * Somewhat longer message (10 s).
49 */
50 public static final int TIME_LONG = Main.pref.getInteger("notification-time-long-ms", 10000);
51
52 /**
53 * Long text.
54 * (Make sure is still sensible to show as a notification)
55 */
56 public static final int TIME_VERY_LONG = Main.pref.getInteger("notification-time-very_long-ms", 20000);
57
58 private Component content;
59 private int duration;
60 private Icon icon;
61 private String helpTopic;
62
63 /**
64 * Constructs a new {@code Notification} without content.
65 */
66 public Notification() {
67 duration = NotificationManager.defaultNotificationTime;
68 }
69
70 /**
71 * Constructs a new {@code Notification} with the given textual content.
72 * @param msg The text to display
73 */
74 public Notification(String msg) {
75 this();
76 setContent(msg);
77 }
78
79 /**
80 * Set the content of the message.
81 *
82 * @param content any Component to be shown
83 *
84 * @see #setContent(java.lang.String)
85 * @return the current Object, for convenience
86 */
87 public Notification setContent(Component content) {
88 this.content = content;
89 return this;
90 }
91
92 /**
93 * Set the notification text. (Convenience method)
94 *
95 * @param msg the message String. Will be wrapped in &lt;html&gt;, so
96 * you can use &lt;br&gt; and other markup directly.
97 *
98 * @see #Notification(java.lang.String)
99 * @return the current Object, for convenience
100 */
101 public Notification setContent(String msg) {
102 JMultilineLabel lbl = new JMultilineLabel(msg);
103 lbl.setMaxWidth(DEFAULT_CONTENT_WIDTH);
104 content = lbl;
105 return this;
106 }
107
108 /**
109 * Set the time after which the message is hidden.
110 *
111 * @param duration the time (in milliseconds)
112 * Preset values {@link #TIME_SHORT}, {@link #TIME_DEFAULT}, {@link #TIME_LONG}
113 * and {@link #TIME_VERY_LONG} can be used.
114 * @return the current Object, for convenience
115 */
116 public Notification setDuration(int duration) {
117 this.duration = duration;
118 return this;
119 }
120
121 /**
122 * Set an icon to display on the left part of the message window.
123 *
124 * @param icon the icon (null means no icon is displayed)
125 * @return the current Object, for convenience
126 */
127 public Notification setIcon(Icon icon) {
128 this.icon = icon;
129 return this;
130 }
131
132 /**
133 * Set an icon to display on the left part of the message window by
134 * choosing from the default JOptionPane icons.
135 *
136 * @param messageType one of the following: JOptionPane.ERROR_MESSAGE,
137 * JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE,
138 * JOptionPane.QUESTION_MESSAGE, JOptionPane.PLAIN_MESSAGE
139 * @return the current Object, for convenience
140 */
141 public Notification setIcon(int messageType) {
142 switch (messageType) {
143 case JOptionPane.ERROR_MESSAGE:
144 return setIcon(UIManager.getIcon("OptionPane.errorIcon"));
145 case JOptionPane.INFORMATION_MESSAGE:
146 return setIcon(UIManager.getIcon("OptionPane.informationIcon"));
147 case JOptionPane.WARNING_MESSAGE:
148 return setIcon(UIManager.getIcon("OptionPane.warningIcon"));
149 case JOptionPane.QUESTION_MESSAGE:
150 return setIcon(UIManager.getIcon("OptionPane.questionIcon"));
151 case JOptionPane.PLAIN_MESSAGE:
152 return setIcon(null);
153 default:
154 throw new IllegalArgumentException("Unknown message type!");
155 }
156 }
157
158 /**
159 * Display a help button at the bottom of the notification window.
160 * @param helpTopic the help topic
161 * @return the current Object, for convenience
162 */
163 public Notification setHelpTopic(String helpTopic) {
164 this.helpTopic = helpTopic;
165 return this;
166 }
167
168 public Component getContent() {
169 return content;
170 }
171
172 public int getDuration() {
173 return duration;
174 }
175
176 public Icon getIcon() {
177 return icon;
178 }
179
180 public String getHelpTopic() {
181 return helpTopic;
182 }
183
184 /**
185 * Display the notification.
186 */
187 public void show() {
188 NotificationManager.getInstance().showNotification(this);
189 }
190}
Note: See TracBrowser for help on using the repository browser.