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

Last change on this file since 2512 was 2512, checked in by stoecker, 15 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

  • Property svn:eol-style set to native
File size: 7.9 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 * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
59 * It is always on top even if there are other open windows like detached dialogs,
60 * relation editors, history browsers and the like.
61 *
62 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
63 * a NO button.
64
65 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
66 * a NO and a CANCEL button
67 *
68 * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
69 * Replies true, if the dialog is not displayed because the respective preference option
70 * <code>preferenceKey</code> is set to false.
71 *
72 * @param preferenceKey the preference key
73 * @param parent the parent component
74 * @param message the message
75 * @param title the title
76 * @param optionType the option type
77 * @param messageType the message type
78 * @param options a list of options
79 * @param defaultOption the default option
80 *
81 *
82 * @return the index of the selected option. {@see JOptionPane#CLOSED_OPTION} if the dialog was closed.
83 * {@see ConditionalOptionPaneUtil#DIALOG_DISABLED_OPTION} if the dialog is disabled.
84 *
85 */
86 static public int showOptionDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, Object [] options, Object defaultOption) throws HeadlessException {
87 if (!getDialogShowingEnabled(preferenceKey))
88 return DIALOG_DISABLED_OPTION;
89 MessagePanel pnl = new MessagePanel(preferenceKey, message);
90 int ret = JOptionPane.showOptionDialog(parent, pnl, title, optionType, messageType, null,options,defaultOption);
91 pnl.remeberDialogShowingEnabled();
92 return ret;
93 }
94
95 /**
96 * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
97 * It is always on top even if there are other open windows like detached dialogs,
98 * relation editors, history browsers and the like.
99 *
100 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
101 * a NO button.
102
103 * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
104 * a NO and a CANCEL button
105 *
106 * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
107 * Replies true, if the dialog is not displayed because the respective preference option
108 * <code>preferenceKey</code> is set to false.
109 *
110 * @param preferenceKey the preference key
111 * @param parent the parent component
112 * @param message the message
113 * @param title the title
114 * @param optionType the option type
115 * @param messageType the message type
116 * @param trueOption if this option is selected the method replies true
117 *
118 *
119 * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false.
120 *
121 * @see JOptionPane#INFORMATION_MESSAGE
122 * @see JOptionPane#WARNING_MESSAGE
123 * @see JOptionPane#ERROR_MESSAGE
124 */
125 static public boolean showConfirmationDialog(String preferenceKey, Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException {
126 if (!getDialogShowingEnabled(preferenceKey))
127 return true;
128 MessagePanel pnl = new MessagePanel(preferenceKey, message);
129 int ret = JOptionPane.showConfirmDialog(parent, pnl, title, optionType, messageType);
130 pnl.remeberDialogShowingEnabled();
131 return ret == trueOption;
132 }
133
134 /**
135 * Displays an message in modal dialog with an OK button. Makes sure the dialog
136 * is always on top even if there are other open windows like detached dialogs,
137 * relation editors, history browsers and the like.
138 *
139 * If there is a preference with key <code>preferenceKey</code> and value <code>false</code>
140 * the dialog is not show.
141 *
142 * @param preferenceKey the preference key
143 * @param parent the parent component
144 * @param message the message
145 * @param title the title
146 * @param messageType the message type
147 *
148 * @see JOptionPane#INFORMATION_MESSAGE
149 * @see JOptionPane#WARNING_MESSAGE
150 * @see JOptionPane#ERROR_MESSAGE
151 */
152 static public void showMessageDialog(String preferenceKey, Component parent, Object message, String title,int messageType) {
153 if (!getDialogShowingEnabled(preferenceKey))
154 return;
155 MessagePanel pnl = new MessagePanel(preferenceKey, message);
156 JOptionPane.showMessageDialog(parent, pnl, title, messageType);
157 pnl.remeberDialogShowingEnabled();
158 }
159
160 /**
161 * This is a message panel used in dialogs which can be enabled/disabled with a preference
162 * setting.
163 * In addition to the normal message any {@see JOptionPane} would display it includes
164 * a checkbox for enabling/disabling this particular dialog.
165 *
166 */
167 private static class MessagePanel extends JPanel {
168 JCheckBox cbShowDialog;
169 String preferenceKey;
170
171 public MessagePanel(String preferenceKey, Object message) {
172 this.preferenceKey = preferenceKey;
173 cbShowDialog = new JCheckBox(tr("Do not show again"));
174 cbShowDialog.setSelected(!ConditionalOptionPaneUtil.getDialogShowingEnabled(preferenceKey));
175 setLayout(new GridBagLayout());
176
177 if (message instanceof Component) {
178 add((Component)message, GBC.eop());
179 } else {
180 add(new JLabel(message.toString()),GBC.eop());
181 }
182 add(cbShowDialog, GBC.eol());
183 }
184
185 public boolean getDialogShowingEnabled() {
186 return cbShowDialog.isSelected();
187 }
188
189 public void remeberDialogShowingEnabled() {
190 ConditionalOptionPaneUtil.setDialogShowingEnabled(preferenceKey, !getDialogShowingEnabled());
191 }
192 }
193}
Note: See TracBrowser for help on using the repository browser.