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