[6124] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[9221] | 4 | import java.awt.Color;
|
---|
[6124] | 5 | import java.awt.Component;
|
---|
| 6 |
|
---|
| 7 | import javax.swing.Icon;
|
---|
[9221] | 8 | import javax.swing.JEditorPane;
|
---|
[6124] | 9 | import javax.swing.JOptionPane;
|
---|
| 10 | import javax.swing.UIManager;
|
---|
| 11 |
|
---|
[6130] | 12 | import org.openstreetmap.josm.Main;
|
---|
[6340] | 13 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
[6130] | 14 |
|
---|
[6124] | 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
|
---|
[6131] | 28 | * note.setDuration(Notification.TIME_SHORT); // optional
|
---|
[6124] | 29 | * note.show();
|
---|
| 30 | * </pre>
|
---|
| 31 | */
|
---|
| 32 | public class Notification {
|
---|
| 33 |
|
---|
[10893] | 34 | /**
|
---|
| 35 | * Default width of a notification
|
---|
| 36 | */
|
---|
[6889] | 37 | public static final int DEFAULT_CONTENT_WIDTH = 350;
|
---|
[6124] | 38 |
|
---|
[6130] | 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 | */
|
---|
[12841] | 45 | public static final int TIME_SHORT = Main.pref.getInt("notification-time-short-ms", 3000);
|
---|
[6889] | 46 |
|
---|
[6130] | 47 | /**
|
---|
| 48 | * Short message of one or two lines (5 s).
|
---|
| 49 | */
|
---|
[12841] | 50 | public static final int TIME_DEFAULT = Main.pref.getInt("notification-time-default-ms", 5000);
|
---|
[6889] | 51 |
|
---|
[6130] | 52 | /**
|
---|
| 53 | * Somewhat longer message (10 s).
|
---|
| 54 | */
|
---|
[12841] | 55 | public static final int TIME_LONG = Main.pref.getInt("notification-time-long-ms", 10_000);
|
---|
[6889] | 56 |
|
---|
[6130] | 57 | /**
|
---|
| 58 | * Long text.
|
---|
| 59 | * (Make sure is still sensible to show as a notification)
|
---|
| 60 | */
|
---|
[12841] | 61 | public static final int TIME_VERY_LONG = Main.pref.getInt("notification-time-very_long-ms", 20_000);
|
---|
[6130] | 62 |
|
---|
[6131] | 63 | private Component content;
|
---|
[10893] | 64 | private int duration = Notification.TIME_DEFAULT;
|
---|
[6124] | 65 | private Icon icon;
|
---|
[6131] | 66 | private String helpTopic;
|
---|
[6124] | 67 |
|
---|
[6357] | 68 | /**
|
---|
| 69 | * Constructs a new {@code Notification} without content.
|
---|
| 70 | */
|
---|
[6124] | 71 | public Notification() {
|
---|
[10893] | 72 | // nothing to do.
|
---|
[6124] | 73 | }
|
---|
| 74 |
|
---|
[6357] | 75 | /**
|
---|
| 76 | * Constructs a new {@code Notification} with the given textual content.
|
---|
| 77 | * @param msg The text to display
|
---|
| 78 | */
|
---|
[6124] | 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 | *
|
---|
[8419] | 89 | * @return the current Object, for convenience
|
---|
[6124] | 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 | *
|
---|
[6131] | 100 | * @param msg the message String. Will be wrapped in <html>, so
|
---|
| 101 | * you can use <br> and other markup directly.
|
---|
[6124] | 102 | *
|
---|
[8419] | 103 | * @return the current Object, for convenience
|
---|
[6124] | 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);
|
---|
[9221] | 109 | lbl.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
|
---|
| 110 | lbl.setForeground(Color.BLACK);
|
---|
[6124] | 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)
|
---|
[6131] | 119 | * Preset values {@link #TIME_SHORT}, {@link #TIME_DEFAULT}, {@link #TIME_LONG}
|
---|
| 120 | * and {@link #TIME_VERY_LONG} can be used.
|
---|
[6124] | 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 |
|
---|
[6131] | 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 |
|
---|
[10893] | 175 | /**
|
---|
| 176 | * Gets the content component to use.
|
---|
| 177 | * @return The content
|
---|
| 178 | */
|
---|
[6124] | 179 | public Component getContent() {
|
---|
| 180 | return content;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[10893] | 183 | /**
|
---|
| 184 | * Gets the time the notification should be displayed
|
---|
| 185 | * @return The time to display the notification
|
---|
| 186 | */
|
---|
[6124] | 187 | public int getDuration() {
|
---|
| 188 | return duration;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[10893] | 191 | /**
|
---|
| 192 | * Gets the icon that should be displayed next to the notification
|
---|
| 193 | * @return The icon to display
|
---|
| 194 | */
|
---|
[6124] | 195 | public Icon getIcon() {
|
---|
| 196 | return icon;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[10893] | 199 | /**
|
---|
| 200 | * Gets the help topic for this notification
|
---|
| 201 | * @return The help topic
|
---|
| 202 | */
|
---|
[6131] | 203 | public String getHelpTopic() {
|
---|
| 204 | return helpTopic;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[6124] | 207 | /**
|
---|
| 208 | * Display the notification.
|
---|
| 209 | */
|
---|
| 210 | public void show() {
|
---|
| 211 | NotificationManager.getInstance().showNotification(this);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|