[6124] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Component;
|
---|
| 5 |
|
---|
| 6 | import javax.swing.Icon;
|
---|
| 7 | import javax.swing.JOptionPane;
|
---|
| 8 | import javax.swing.UIManager;
|
---|
| 9 |
|
---|
[6130] | 10 | import org.openstreetmap.josm.Main;
|
---|
| 11 |
|
---|
[6124] | 12 | /**
|
---|
| 13 | * A Notification Message similar to a popup window, but without disrupting the
|
---|
| 14 | * user's workflow.
|
---|
| 15 | *
|
---|
| 16 | * Non-modal info panel that vanishes after a certain time.
|
---|
| 17 | *
|
---|
| 18 | * This class only holds the data for a notification, {@link NotificationManager}
|
---|
| 19 | * is responsible for building the message panel and displaying it on screen.
|
---|
| 20 | *
|
---|
| 21 | * example:
|
---|
| 22 | * <pre>
|
---|
| 23 | * Notification note = new Notification("Hi there!");
|
---|
| 24 | * note.setIcon(JOptionPane.INFORMATION_MESSAGE); // optional
|
---|
[6131] | 25 | * note.setDuration(Notification.TIME_SHORT); // optional
|
---|
[6124] | 26 | * note.show();
|
---|
| 27 | * </pre>
|
---|
| 28 | */
|
---|
| 29 | public class Notification {
|
---|
| 30 |
|
---|
| 31 | public final static int DEFAULT_CONTENT_WIDTH = 350;
|
---|
| 32 |
|
---|
[6130] | 33 | // some standard duration values (in milliseconds)
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * Very short and very easy to grasp message (3 s).
|
---|
| 37 | * E.g. "Please select at least one node".
|
---|
| 38 | */
|
---|
| 39 | public final static int TIME_SHORT = Main.pref.getInteger("notification-time-short-ms", 3000);
|
---|
| 40 | /**
|
---|
| 41 | * Short message of one or two lines (5 s).
|
---|
| 42 | */
|
---|
| 43 | public final static int TIME_DEFAULT = Main.pref.getInteger("notification-time-default-ms", 5000);
|
---|
| 44 | /**
|
---|
| 45 | * Somewhat longer message (10 s).
|
---|
| 46 | */
|
---|
| 47 | public final static int TIME_LONG = Main.pref.getInteger("notification-time-long-ms", 10000);
|
---|
| 48 | /**
|
---|
| 49 | * Long text.
|
---|
| 50 | * (Make sure is still sensible to show as a notification)
|
---|
| 51 | */
|
---|
| 52 | public final static int TIME_VERY_LONG = Main.pref.getInteger("notification-time-very_long-ms", 20000);
|
---|
| 53 |
|
---|
[6131] | 54 | private Component content;
|
---|
| 55 | private int duration;
|
---|
[6124] | 56 | private Icon icon;
|
---|
[6131] | 57 | private String helpTopic;
|
---|
[6124] | 58 |
|
---|
| 59 | public Notification() {
|
---|
| 60 | duration = NotificationManager.defaultNotificationTime;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public Notification(String msg) {
|
---|
| 64 | this();
|
---|
| 65 | setContent(msg);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Set the content of the message.
|
---|
| 70 | *
|
---|
| 71 | * @param content any Component to be shown
|
---|
| 72 | *
|
---|
| 73 | * @see #setContent(java.lang.String)
|
---|
| 74 | * @return the current Object, for convenience
|
---|
| 75 | */
|
---|
| 76 | public Notification setContent(Component content) {
|
---|
| 77 | this.content = content;
|
---|
| 78 | return this;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Set the notification text. (Convenience method)
|
---|
| 83 | *
|
---|
[6131] | 84 | * @param msg the message String. Will be wrapped in <html>, so
|
---|
| 85 | * you can use <br> and other markup directly.
|
---|
[6124] | 86 | *
|
---|
| 87 | * @see #Notification(java.lang.String)
|
---|
| 88 | * @return the current Object, for convenience
|
---|
| 89 | */
|
---|
| 90 | public Notification setContent(String msg) {
|
---|
| 91 | JMultilineLabel lbl = new JMultilineLabel(msg);
|
---|
| 92 | lbl.setMaxWidth(DEFAULT_CONTENT_WIDTH);
|
---|
| 93 | content = lbl;
|
---|
| 94 | return this;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Set the time after which the message is hidden.
|
---|
| 99 | *
|
---|
| 100 | * @param duration the time (in milliseconds)
|
---|
[6131] | 101 | * Preset values {@link #TIME_SHORT}, {@link #TIME_DEFAULT}, {@link #TIME_LONG}
|
---|
| 102 | * and {@link #TIME_VERY_LONG} can be used.
|
---|
[6124] | 103 | * @return the current Object, for convenience
|
---|
| 104 | */
|
---|
| 105 | public Notification setDuration(int duration) {
|
---|
| 106 | this.duration = duration;
|
---|
| 107 | return this;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Set an icon to display on the left part of the message window.
|
---|
| 112 | *
|
---|
| 113 | * @param icon the icon (null means no icon is displayed)
|
---|
| 114 | * @return the current Object, for convenience
|
---|
| 115 | */
|
---|
| 116 | public Notification setIcon(Icon icon) {
|
---|
| 117 | this.icon = icon;
|
---|
| 118 | return this;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * Set an icon to display on the left part of the message window by
|
---|
| 123 | * choosing from the default JOptionPane icons.
|
---|
| 124 | *
|
---|
| 125 | * @param messageType one of the following: JOptionPane.ERROR_MESSAGE,
|
---|
| 126 | * JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE,
|
---|
| 127 | * JOptionPane.QUESTION_MESSAGE, JOptionPane.PLAIN_MESSAGE
|
---|
| 128 | * @return the current Object, for convenience
|
---|
| 129 | */
|
---|
| 130 | public Notification setIcon(int messageType) {
|
---|
| 131 | switch (messageType) {
|
---|
| 132 | case JOptionPane.ERROR_MESSAGE:
|
---|
| 133 | return setIcon(UIManager.getIcon("OptionPane.errorIcon"));
|
---|
| 134 | case JOptionPane.INFORMATION_MESSAGE:
|
---|
| 135 | return setIcon(UIManager.getIcon("OptionPane.informationIcon"));
|
---|
| 136 | case JOptionPane.WARNING_MESSAGE:
|
---|
| 137 | return setIcon(UIManager.getIcon("OptionPane.warningIcon"));
|
---|
| 138 | case JOptionPane.QUESTION_MESSAGE:
|
---|
| 139 | return setIcon(UIManager.getIcon("OptionPane.questionIcon"));
|
---|
| 140 | case JOptionPane.PLAIN_MESSAGE:
|
---|
| 141 | return setIcon(null);
|
---|
| 142 | default:
|
---|
| 143 | throw new IllegalArgumentException("Unknown message type!");
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[6131] | 147 | /**
|
---|
| 148 | * Display a help button at the bottom of the notification window.
|
---|
| 149 | * @param helpTopic the help topic
|
---|
| 150 | * @return the current Object, for convenience
|
---|
| 151 | */
|
---|
| 152 | public Notification setHelpTopic(String helpTopic) {
|
---|
| 153 | this.helpTopic = helpTopic;
|
---|
| 154 | return this;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[6124] | 157 | public Component getContent() {
|
---|
| 158 | return content;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public int getDuration() {
|
---|
| 162 | return duration;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | public Icon getIcon() {
|
---|
| 166 | return icon;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[6131] | 169 | public String getHelpTopic() {
|
---|
| 170 | return helpTopic;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[6124] | 173 | /**
|
---|
| 174 | * Display the notification.
|
---|
| 175 | */
|
---|
| 176 | public void show() {
|
---|
| 177 | NotificationManager.getInstance().showNotification(this);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|