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

Last change on this file since 10620 was 9221, checked in by Don-vip, 8 years ago

fix #12256 - adjust contrast of text displayed in imagery alignment warning, notifications and color preferences. Useful for dark themes

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