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

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

fix #5382 - add an option to display a notification at each autosave (disabled by default)

  • 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 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 /**
61 * Constructs a new {@code Notification} without content.
62 */
63 public Notification() {
64 duration = NotificationManager.defaultNotificationTime;
65 }
66
67 /**
68 * Constructs a new {@code Notification} with the given textual content.
69 * @param msg The text to display
70 */
71 public Notification(String msg) {
72 this();
73 setContent(msg);
74 }
75
76 /**
77 * Set the content of the message.
78 *
79 * @param content any Component to be shown
80 *
81 * @see #setContent(java.lang.String)
82 * @return the current Object, for convenience
83 */
84 public Notification setContent(Component content) {
85 this.content = content;
86 return this;
87 }
88
89 /**
90 * Set the notification text. (Convenience method)
91 *
92 * @param msg the message String. Will be wrapped in &lt;html&gt;, so
93 * you can use &lt;br&gt; and other markup directly.
94 *
95 * @see #Notification(java.lang.String)
96 * @return the current Object, for convenience
97 */
98 public Notification setContent(String msg) {
99 JMultilineLabel lbl = new JMultilineLabel(msg);
100 lbl.setMaxWidth(DEFAULT_CONTENT_WIDTH);
101 content = lbl;
102 return this;
103 }
104
105 /**
106 * Set the time after which the message is hidden.
107 *
108 * @param duration the time (in milliseconds)
109 * Preset values {@link #TIME_SHORT}, {@link #TIME_DEFAULT}, {@link #TIME_LONG}
110 * and {@link #TIME_VERY_LONG} can be used.
111 * @return the current Object, for convenience
112 */
113 public Notification setDuration(int duration) {
114 this.duration = duration;
115 return this;
116 }
117
118 /**
119 * Set an icon to display on the left part of the message window.
120 *
121 * @param icon the icon (null means no icon is displayed)
122 * @return the current Object, for convenience
123 */
124 public Notification setIcon(Icon icon) {
125 this.icon = icon;
126 return this;
127 }
128
129 /**
130 * Set an icon to display on the left part of the message window by
131 * choosing from the default JOptionPane icons.
132 *
133 * @param messageType one of the following: JOptionPane.ERROR_MESSAGE,
134 * JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE,
135 * JOptionPane.QUESTION_MESSAGE, JOptionPane.PLAIN_MESSAGE
136 * @return the current Object, for convenience
137 */
138 public Notification setIcon(int messageType) {
139 switch (messageType) {
140 case JOptionPane.ERROR_MESSAGE:
141 return setIcon(UIManager.getIcon("OptionPane.errorIcon"));
142 case JOptionPane.INFORMATION_MESSAGE:
143 return setIcon(UIManager.getIcon("OptionPane.informationIcon"));
144 case JOptionPane.WARNING_MESSAGE:
145 return setIcon(UIManager.getIcon("OptionPane.warningIcon"));
146 case JOptionPane.QUESTION_MESSAGE:
147 return setIcon(UIManager.getIcon("OptionPane.questionIcon"));
148 case JOptionPane.PLAIN_MESSAGE:
149 return setIcon(null);
150 default:
151 throw new IllegalArgumentException("Unknown message type!");
152 }
153 }
154
155 /**
156 * Display a help button at the bottom of the notification window.
157 * @param helpTopic the help topic
158 * @return the current Object, for convenience
159 */
160 public Notification setHelpTopic(String helpTopic) {
161 this.helpTopic = helpTopic;
162 return this;
163 }
164
165 public Component getContent() {
166 return content;
167 }
168
169 public int getDuration() {
170 return duration;
171 }
172
173 public Icon getIcon() {
174 return icon;
175 }
176
177 public String getHelpTopic() {
178 return helpTopic;
179 }
180
181 /**
182 * Display the notification.
183 */
184 public void show() {
185 NotificationManager.getInstance().showNotification(this);
186 }
187}
Note: See TracBrowser for help on using the repository browser.