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

Last change on this file since 2646 was 2523, checked in by stoecker, 14 years ago

minor cleanup

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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;
9
10import javax.swing.JCheckBox;
11import javax.swing.JLabel;
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.tools.GBC;
17
18/**
19 * ConditionalOptionPaneUtil provides static utility methods for displaying modal message dialogs
20 * which can be enabled/disabled by the user.
21 *
22 * They wrap the methods provided by {@see JOptionPane}. Within JOSM you should use these
23 * methods rather than the bare methods from {@see JOptionPane} because the methods provided
24 * by ConditionalOptionPaneUtil ensure that a dialog window is always on top and isn't hidden by one of the
25 * JOSM windows for detached dialogs, relation editors, history browser and the like.
26 *
27 */
28public class ConditionalOptionPaneUtil {
29 static public final int DIALOG_DISABLED_OPTION = Integer.MIN_VALUE;
30
31 /**
32 * this is a static utility class only
33 */
34 private ConditionalOptionPaneUtil() {}
35
36 /**
37 * Replies the preference value for the preference key "message." + <code>prefKey</code>.
38 * The default value if the preference key is missing is true.
39 *
40 * @param the preference key
41 * @return prefKey the preference value for the preference key "message." + <code>prefKey</code>
42 */
43 public static boolean getDialogShowingEnabled(String prefKey) {
44 return Main.pref.getBoolean("message."+prefKey, true);
45 }
46
47 /**
48 * sets the value for the preference key "message." + <code>prefKey</code>.
49 *
50 * @param prefKey the key
51 * @param enabled the value
52 */
53 public static void setDialogShowingEnabled(String prefKey, boolean enabled) {
54 Main.pref.put("message."+prefKey, enabled);
55 }
56
57 /**
58 * Returns the preference value for the preference key "message." + <code>prefKey</code> + ".value".
59 * The default value if the preference key is missing is -1.
60 *
61 * @param the preference key
62 * @return prefKey the preference value for the preference key "message." + <code>prefKey</code> + ".value"
63 */
64 public static Integer getDialogReturnValue(String prefKey) {
65 return Main.pref.getInteger("message."+prefKey+".value", -1);
66 }
67
68 /**
69 * sets the value for the preference key "message." + <code>prefKey</code> + ".value".
70 *
71 * @param prefKey the key
72 * @param value the value
73 */
74 public static void setDialogReturnValue(String prefKey, Integer value) {
75 Main.pref.putInteger("message."+prefKey+".value", value);
76 }
77
78 /**
79 * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
80 * It is always on top even if there are other open windows like detached dialogs,
81 * relation editors, history browsers and the like.
82 *
83 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
84 * a NO button.
85
86 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
87 * a NO and a CANCEL button
88 *
89 * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
90 * Replies true, if the dialog is not displayed because the respective preference option
91 * <code>preferenceKey</code> is set to false.
92 *
93 * @param preferenceKey the preference key
94 * @param parent the parent component
95 * @param message the message
96 * @param title the title
97 * @param optionType the option type
98 * @param messageType the message type
99 * @param options a list of options
100 * @param defaultOption the default option
101 *
102 *
103 * @return the index of the selected option. {@see JOptionPane#CLOSED_OPTION} if the dialog was closed.
104 * {@see ConditionalOptionPaneUtil#DIALOG_DISABLED_OPTION} if the dialog is disabled.
105 *
106 */
107 static public int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, Object [] options, Object defaultOption) throws HeadlessException {
108 if (!getDialogShowingEnabled(preferenceKey))
109 return DIALOG_DISABLED_OPTION;
110 MessagePanel pnl = new MessagePanel(false, message);
111 int ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null,options,defaultOption);
112
113 if(!pnl.getDialogShowingEnabled())
114 setDialogShowingEnabled(preferenceKey, false);
115 return ret;
116 }
117
118 /**
119 * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
120 * It is always on top even if there are other open windows like detached dialogs,
121 * relation editors, history browsers and the like.
122 *
123 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
124 * a NO button.
125
126 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
127 * a NO and a CANCEL button
128 *
129 * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
130 * Replies true, if the dialog is not displayed because the respective preference option
131 * <code>preferenceKey</code> is set to false.
132 *
133 * @param preferenceKey the preference key
134 * @param parent the parent component
135 * @param message the message
136 * @param title the title
137 * @param optionType the option type
138 * @param messageType the message type
139 * @param trueOption if this option is selected the method replies true
140 *
141 *
142 * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false.
143 *
144 * @see JOptionPane#INFORMATION_MESSAGE
145 * @see JOptionPane#WARNING_MESSAGE
146 * @see JOptionPane#ERROR_MESSAGE
147 */
148 static public boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException {
149 boolean donotshow = getDialogShowingEnabled(preferenceKey);
150 if (donotshow && (getDialogReturnValue(preferenceKey) >= 0))
151 return getDialogReturnValue(preferenceKey) == trueOption;
152 MessagePanel pnl = new MessagePanel(donotshow, message);
153 int ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType);
154 if ((ret >= 0) && !pnl.getDialogShowingEnabled()) {
155 setDialogShowingEnabled(preferenceKey, false);
156 setDialogReturnValue(preferenceKey, ret);
157 }
158 return ret == trueOption;
159 }
160
161 /**
162 * Displays an message in modal dialog with an OK button. Makes sure the dialog
163 * is always on top even if there are other open windows like detached dialogs,
164 * relation editors, history browsers and the like.
165 *
166 * If there is a preference with key <code>preferenceKey</code> and value <code>false</code>
167 * the dialog is not show.
168 *
169 * @param preferenceKey the preference key
170 * @param parent the parent component
171 * @param message the message
172 * @param title the title
173 * @param messageType the message type
174 *
175 * @see JOptionPane#INFORMATION_MESSAGE
176 * @see JOptionPane#WARNING_MESSAGE
177 * @see JOptionPane#ERROR_MESSAGE
178 */
179 static public void showMessageDialog(String preferenceKey, Component parent, Object message, String title,int messageType) {
180 if (!getDialogShowingEnabled(preferenceKey))
181 return;
182 MessagePanel pnl = new MessagePanel(false, message);
183 JOptionPane.showMessageDialog(parent, pnl, title, messageType);
184 if(!pnl.getDialogShowingEnabled())
185 setDialogShowingEnabled(preferenceKey, false);
186 }
187
188 /**
189 * This is a message panel used in dialogs which can be enabled/disabled with a preference
190 * setting.
191 * In addition to the normal message any {@see JOptionPane} would display it includes
192 * a checkbox for enabling/disabling this particular dialog.
193 *
194 */
195 private static class MessagePanel extends JPanel {
196 JCheckBox cbShowDialog;
197
198 public MessagePanel(boolean donotshow, Object message) {
199 cbShowDialog = new JCheckBox(tr("Do not show again"));
200 cbShowDialog.setSelected(donotshow);
201 setLayout(new GridBagLayout());
202
203 if (message instanceof Component) {
204 add((Component)message, GBC.eop());
205 } else {
206 add(new JLabel(message.toString()),GBC.eop());
207 }
208 add(cbShowDialog, GBC.eol());
209 }
210
211 public boolean getDialogShowingEnabled() {
212 return !cbShowDialog.isSelected();
213 }
214 }
215}
Note: See TracBrowser for help on using the repository browser.