source: josm/trunk/src/org/openstreetmap/josm/gui/ConditionalOptionPaneUtil.java@ 8949

Last change on this file since 8949 was 8926, checked in by Don-vip, 9 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 12.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagLayout;
8import java.awt.HeadlessException;
9import java.util.HashMap;
10import java.util.HashSet;
11import java.util.Map;
12import java.util.Set;
13
14import javax.swing.ButtonGroup;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JRadioButton;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
21import org.openstreetmap.josm.tools.GBC;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * ConditionalOptionPaneUtil provides static utility methods for displaying modal message dialogs
26 * which can be enabled/disabled by the user.
27 *
28 * They wrap the methods provided by {@link JOptionPane}. Within JOSM you should use these
29 * methods rather than the bare methods from {@link JOptionPane} because the methods provided
30 * by ConditionalOptionPaneUtil ensure that a dialog window is always on top and isn't hidden by one of the
31 * JOSM windows for detached dialogs, relation editors, history browser and the like.
32 *
33 */
34public final class ConditionalOptionPaneUtil {
35 public static final int DIALOG_DISABLED_OPTION = Integer.MIN_VALUE;
36
37 /** (preference key => return value) mappings valid for the current operation (no, those two maps cannot be combined) */
38 private static final Map<String, Integer> sessionChoices = new HashMap<>();
39 /** (preference key =&gt; return value) mappings valid for the current session */
40 private static final Map<String, Integer> immediateChoices = new HashMap<>();
41 /** a set indication that (preference key) is or may be stored for the currently active bulk operation */
42 private static final Set<String> immediateActive = new HashSet<>();
43
44 /**
45 * this is a static utility class only
46 */
47 private ConditionalOptionPaneUtil() {}
48
49 /**
50 * Returns the preference value for the preference key "message." + <code>prefKey</code> + ".value".
51 * The default value if the preference key is missing is -1.
52 *
53 * @param prefKey the preference key
54 * @return the preference value for the preference key "message." + <code>prefKey</code> + ".value"
55 */
56 public static int getDialogReturnValue(String prefKey) {
57 return Utils.firstNonNull(
58 immediateChoices.get(prefKey),
59 sessionChoices.get(prefKey),
60 !Main.pref.getBoolean("message." + prefKey, true) ? Main.pref.getInteger("message." + prefKey + ".value", -1) : -1
61 );
62 }
63
64 /**
65 * Marks the beginning of a bulk operation in order to provide a "Do not show again (this operation)" option.
66 * @param prefKey the preference key
67 */
68 public static void startBulkOperation(final String prefKey) {
69 immediateActive.add(prefKey);
70 }
71
72 /**
73 * Determines whether the key has been marked to be part of a bulk operation
74 * (in order to provide a "Do not show again (this operation)" option).
75 * @param prefKey the preference key
76 */
77 public static boolean isInBulkOperation(final String prefKey) {
78 return immediateActive.contains(prefKey);
79 }
80
81 /**
82 * Marks the ending of a bulk operation. Removes the "Do not show again (this operation)" result value.
83 * @param prefKey the preference key
84 */
85 public static void endBulkOperation(final String prefKey) {
86 immediateActive.remove(prefKey);
87 immediateChoices.remove(prefKey);
88 }
89
90 /**
91 * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
92 * It is always on top even if there are other open windows like detached dialogs,
93 * relation editors, history browsers and the like.
94 *
95 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_OPTION} for a dialog with a YES and
96 * a NO button.
97
98 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
99 * a NO and a CANCEL button
100 *
101 * Returns one of the constants JOptionPane.YES_OPTION, JOptionPane.NO_OPTION,
102 * JOptionPane.CANCEL_OPTION or JOptionPane.CLOSED_OPTION depending on the action chosen by
103 * the user.
104 *
105 * @param preferenceKey the preference key
106 * @param parent the parent component
107 * @param message the message
108 * @param title the title
109 * @param optionType the option type
110 * @param messageType the message type
111 * @param options a list of options
112 * @param defaultOption the default option; only meaningful if options is used; can be null
113 *
114 * @return the option selected by user. {@link JOptionPane#CLOSED_OPTION} if the dialog was closed.
115 * @throws HeadlessException if <code>GraphicsEnvironment.isHeadless</code> returns <code>true</code>
116 */
117 public static int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType,
118 int messageType, Object[] options, Object defaultOption) throws HeadlessException {
119 int ret = getDialogReturnValue(preferenceKey);
120 if (isYesOrNo(ret))
121 return ret;
122 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey));
123 ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null, options, defaultOption);
124 if (isYesOrNo(ret)) {
125 pnl.getNotShowAgain().store(preferenceKey, ret);
126 }
127 return ret;
128 }
129
130 /**
131 * Displays a confirmation dialog with some option buttons given by <code>optionType</code>.
132 * It is always on top even if there are other open windows like detached dialogs,
133 * relation editors, history browsers and the like.
134 *
135 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_OPTION} for a dialog with a YES and
136 * a NO button.
137
138 * Set <code>optionType</code> to {@link JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
139 * a NO and a CANCEL button
140 *
141 * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
142 * Replies true, if the dialog is not displayed because the respective preference option
143 * <code>preferenceKey</code> is set to false and the user has previously chosen
144 * <code>trueOption</code>.
145 *
146 * @param preferenceKey the preference key
147 * @param parent the parent component
148 * @param message the message
149 * @param title the title
150 * @param optionType the option type
151 * @param messageType the message type
152 * @param trueOption if this option is selected the method replies true
153 *
154 *
155 * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false.
156 * @throws HeadlessException if <code>GraphicsEnvironment.isHeadless</code> returns <code>true</code>
157 *
158 * @see JOptionPane#INFORMATION_MESSAGE
159 * @see JOptionPane#WARNING_MESSAGE
160 * @see JOptionPane#ERROR_MESSAGE
161 */
162 public static boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title,
163 int optionType, int messageType, int trueOption) throws HeadlessException {
164 int ret = getDialogReturnValue(preferenceKey);
165 if (isYesOrNo(ret))
166 return ret == trueOption;
167 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey));
168 ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType);
169 if (isYesOrNo(ret)) {
170 pnl.getNotShowAgain().store(preferenceKey, ret);
171 }
172 return ret == trueOption;
173 }
174
175 private static boolean isYesOrNo(int returnCode) {
176 return (returnCode == JOptionPane.YES_OPTION) || (returnCode == JOptionPane.NO_OPTION);
177 }
178
179 /**
180 * Displays an message in modal dialog with an OK button. Makes sure the dialog
181 * is always on top even if there are other open windows like detached dialogs,
182 * relation editors, history browsers and the like.
183 *
184 * If there is a preference with key <code>preferenceKey</code> and value <code>false</code>
185 * the dialog is not show.
186 *
187 * @param preferenceKey the preference key
188 * @param parent the parent component
189 * @param message the message
190 * @param title the title
191 * @param messageType the message type
192 *
193 * @see JOptionPane#INFORMATION_MESSAGE
194 * @see JOptionPane#WARNING_MESSAGE
195 * @see JOptionPane#ERROR_MESSAGE
196 */
197 public static void showMessageDialog(String preferenceKey, Component parent, Object message, String title, int messageType) {
198 if (getDialogReturnValue(preferenceKey) == Integer.MAX_VALUE)
199 return;
200 MessagePanel pnl = new MessagePanel(message, isInBulkOperation(preferenceKey));
201 JOptionPane.showMessageDialog(parent, pnl, title, messageType);
202 pnl.getNotShowAgain().store(preferenceKey, Integer.MAX_VALUE);
203 }
204
205 /**
206 * An enum designating how long to not show this message again, i.e., for how long to store
207 */
208 enum NotShowAgain {
209 NO, OPERATION, SESSION, PERMANENT;
210
211 /**
212 * Stores the dialog result {@code value} at the corresponding place.
213 * @param prefKey the preference key
214 * @param value the dialog result
215 */
216 void store(String prefKey, Integer value) {
217 switch (this) {
218 case NO:
219 break;
220 case OPERATION:
221 immediateChoices.put(prefKey, value);
222 break;
223 case SESSION:
224 sessionChoices.put(prefKey, value);
225 break;
226 case PERMANENT:
227 Main.pref.put("message." + prefKey, false);
228 Main.pref.putInteger("message." + prefKey + ".value", value);
229 break;
230 }
231 }
232
233 String getLabel() {
234 switch (this) {
235 case NO:
236 return tr("Show this dialog again the next time");
237 case OPERATION:
238 return tr("Do not show again (this operation)");
239 case SESSION:
240 return tr("Do not show again (this session)");
241 case PERMANENT:
242 return tr("Do not show again (remembers choice)");
243 }
244 throw new IllegalStateException();
245 }
246 }
247
248 /**
249 * This is a message panel used in dialogs which can be enabled/disabled with a preference setting.
250 * In addition to the normal message any {@link JOptionPane} would display it includes
251 * a checkbox for enabling/disabling this particular dialog.
252 *
253 */
254 static class MessagePanel extends JPanel {
255 private final JRadioButton cbShowPermanentDialog = new JRadioButton(NotShowAgain.PERMANENT.getLabel());
256 private final JRadioButton cbShowSessionDialog = new JRadioButton(NotShowAgain.SESSION.getLabel());
257 private final JRadioButton cbShowImmediateDialog = new JRadioButton(NotShowAgain.OPERATION.getLabel());
258 private final JRadioButton cbStandard = new JRadioButton(NotShowAgain.NO.getLabel());
259
260 /**
261 * Constructs a new panel.
262 * @param message the the message (null to add no message, Component instances are added directly,
263 * otherwise a JLabel with the string representation is added)
264 * @param displayImmediateOption whether to provide "Do not show again (this session)"
265 */
266 MessagePanel(Object message, boolean displayImmediateOption) {
267 cbStandard.setSelected(true);
268 ButtonGroup group = new ButtonGroup();
269 group.add(cbShowPermanentDialog);
270 group.add(cbShowSessionDialog);
271 group.add(cbShowImmediateDialog);
272 group.add(cbStandard);
273
274 setLayout(new GridBagLayout());
275 if (message instanceof Component) {
276 add((Component) message, GBC.eop());
277 } else if (message != null) {
278 add(new JMultilineLabel(message.toString()), GBC.eop());
279 }
280 add(cbShowPermanentDialog, GBC.eol());
281 add(cbShowSessionDialog, GBC.eol());
282 if (displayImmediateOption) {
283 add(cbShowImmediateDialog, GBC.eol());
284 }
285 add(cbStandard, GBC.eol());
286 }
287
288 NotShowAgain getNotShowAgain() {
289 return cbStandard.isSelected()
290 ? NotShowAgain.NO
291 : cbShowImmediateDialog.isSelected()
292 ? NotShowAgain.OPERATION
293 : cbShowSessionDialog.isSelected()
294 ? NotShowAgain.SESSION
295 : cbShowPermanentDialog.isSelected()
296 ? NotShowAgain.PERMANENT
297 : null;
298 }
299 }
300}
Note: See TracBrowser for help on using the repository browser.