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