1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.HeadlessException;
|
---|
6 |
|
---|
7 | import javax.swing.JDialog;
|
---|
8 | import javax.swing.JOptionPane;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * OptionPaneUtil provides static utility methods for displaying modal message dialogs.
|
---|
14 | *
|
---|
15 | * They wrap the methods provided by {@see JOptionPane}. Within JOSM you should use these
|
---|
16 | * methods rather than the bare methods from {@see JOptionPane} because the methods provided
|
---|
17 | * by OptionPaneUtil ensure that a dialog window is always on top and isn't hidden by one of the
|
---|
18 | * JOSM windows for detached dialogs, relation editors, history browser and the like.
|
---|
19 | *
|
---|
20 | * Typical usage for a message dialog:
|
---|
21 | * <pre>
|
---|
22 | * OptionPaneUtil.showMessageDialog(Main.parent, tr("My message"), tr("My title"), JOptionPane.WARNING_MESSAGE);
|
---|
23 | * </pre>
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class OptionPaneUtil {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * this is static utility class, no instances allowed
|
---|
30 | */
|
---|
31 | private OptionPaneUtil () {}
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * prepares a dialog as message dialog which is always on the top of the windows
|
---|
35 | * stack
|
---|
36 | *
|
---|
37 | * @param dialog the dialog
|
---|
38 | */
|
---|
39 | static protected void prepareDialog(JDialog dialog) {
|
---|
40 | dialog.setAlwaysOnTop(true);
|
---|
41 | dialog.setModal(true);
|
---|
42 | dialog.toFront();
|
---|
43 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Displays an message in modal dialog with an OK button. Makes sure the dialog
|
---|
48 | * is always on top even if there are other open windows like detached dialogs,
|
---|
49 | * relation editors, history browsers and the like.
|
---|
50 | *
|
---|
51 | * @param parent the parent component
|
---|
52 | * @param message the message
|
---|
53 | * @param title the title
|
---|
54 | * @param messageType the message type
|
---|
55 | *
|
---|
56 | * @see JOptionPane#INFORMATION_MESSAGE
|
---|
57 | * @see JOptionPane#WARNING_MESSAGE
|
---|
58 | * @see JOptionPane#ERROR_MESSAGE
|
---|
59 | */
|
---|
60 | static public void showMessageDialog(Component parent, Object message, String title, int messageType) {
|
---|
61 | JOptionPane op = new JOptionPane(
|
---|
62 | message,
|
---|
63 | messageType
|
---|
64 | );
|
---|
65 | JDialog dialog = op.createDialog(Main.parent, title);
|
---|
66 | prepareDialog(dialog);
|
---|
67 | dialog.setVisible(true);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Displays an confirmation dialog with two or three buttons. Makes sure the dialog
|
---|
73 | * is always on top even if there are other open windows like detached dialogs,
|
---|
74 | * relation editors, history browsers and the like.
|
---|
75 | *
|
---|
76 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
|
---|
77 | * a NO button.
|
---|
78 |
|
---|
79 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
|
---|
80 | * a NO and a CANCEL button
|
---|
81 | *
|
---|
82 | * @param parent the parent component
|
---|
83 | * @param message the message
|
---|
84 | * @param title the title
|
---|
85 | * @param optionType the option type
|
---|
86 | * @param messageType the message type
|
---|
87 | *
|
---|
88 | * @see JOptionPane#INFORMATION_MESSAGE
|
---|
89 | * @see JOptionPane#WARNING_MESSAGE
|
---|
90 | * @see JOptionPane#ERROR_MESSAGE
|
---|
91 | */
|
---|
92 | static public int showConfirmationDialog(Component parent, Object message, String title, int optionType, int messageType) throws HeadlessException {
|
---|
93 | JOptionPane op = new JOptionPane(
|
---|
94 | message,
|
---|
95 | messageType,
|
---|
96 | optionType
|
---|
97 | );
|
---|
98 | JDialog dialog = op.createDialog(Main.parent, title);
|
---|
99 | prepareDialog(dialog);
|
---|
100 | dialog.setVisible(true);
|
---|
101 | Object value = op.getValue();
|
---|
102 | if (value == null || ! (value instanceof Integer))
|
---|
103 | return JOptionPane.CLOSED_OPTION;
|
---|
104 | return (Integer)value;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Displays an confirmation dialog with some option buttons given by <code>optionType</code>.
|
---|
109 | * It is always on top even if there are other open windows like detached dialogs,
|
---|
110 | * relation editors, history browsers and the like.
|
---|
111 | *
|
---|
112 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_OPTION} for a dialog with a YES and
|
---|
113 | * a NO button.
|
---|
114 |
|
---|
115 | * Set <code>optionType</code> to {@see JOptionPane#YES_NO_CANCEL_OPTION} for a dialog with a YES,
|
---|
116 | * a NO and a CANCEL button
|
---|
117 | *
|
---|
118 | * Replies true, if the selected option is equal to <code>trueOption</code>, otherwise false.
|
---|
119 | *
|
---|
120 | * @param parent the parent component
|
---|
121 | * @param message the message
|
---|
122 | * @param title the title
|
---|
123 | * @param optionType the option type
|
---|
124 | * @param messageType the message type
|
---|
125 | * @param trueOption if this option is selected the method replies true
|
---|
126 | *
|
---|
127 | *
|
---|
128 | * @return true, if the selected option is equal to <code>trueOption</code>, otherwise false.
|
---|
129 | *
|
---|
130 | * @see JOptionPane#INFORMATION_MESSAGE
|
---|
131 | * @see JOptionPane#WARNING_MESSAGE
|
---|
132 | * @see JOptionPane#ERROR_MESSAGE
|
---|
133 | */
|
---|
134 | static public boolean showConfirmationDialog(Component parent, Object message, String title, int optionType, int messageType, int trueOption) throws HeadlessException {
|
---|
135 | int ret = showConfirmationDialog(parent, message, title, optionType, messageType);
|
---|
136 | return ret == trueOption;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Displays an confirmation dialog with three buttons: YES, NO and CANCEL. Makes sure the dialog
|
---|
141 | * is always on top even if there are other open windows like detached dialogs,
|
---|
142 | * relation editors, history browsers and the like. The dialog is of type {@see JOptionPane#QUESTION_MESSAGE}
|
---|
143 | *
|
---|
144 | * @param parent the parent component
|
---|
145 | * @param message the message
|
---|
146 | * @param title the title
|
---|
147 | *
|
---|
148 | */
|
---|
149 | static public int showConfirmationDialog(Component parent, Object message, String title) throws HeadlessException {
|
---|
150 | return showConfirmationDialog(parent, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Displays a confirmation dialog with arbitrary confirmation options. Makes sure the dialog
|
---|
155 | * is always on top even if there are other open windows like detached dialogs,
|
---|
156 | * relation editors, history browsers and the like.
|
---|
157 | *
|
---|
158 | * Provide an array of option labels in <code>options</code>, i.e.
|
---|
159 | * <pre>
|
---|
160 | * options = new String[] {"Yes, do it!", "No, not yet", "Abort"};
|
---|
161 | * </pre>
|
---|
162 | *
|
---|
163 | * Replies the index of the selected option or {@see JOptionPane#CLOSED_OPTION} if
|
---|
164 | * the dialog was closed.
|
---|
165 | *
|
---|
166 | * @param parent the parent component
|
---|
167 | * @param message the message
|
---|
168 | * @param title the title
|
---|
169 | * @param optionType
|
---|
170 | * @param messageType
|
---|
171 | * @param options
|
---|
172 | * @param initialValue
|
---|
173 | * @return the index of the selected option or {@see JOptionPane#CLOSED_OPTION}
|
---|
174 | */
|
---|
175 | static public int showOptionDialog(Component parent,
|
---|
176 | Object message,
|
---|
177 | String title,
|
---|
178 | int optionType,
|
---|
179 | int messageType,
|
---|
180 | Object[] options,
|
---|
181 | Object initialValue) {
|
---|
182 |
|
---|
183 | JOptionPane op = new JOptionPane(
|
---|
184 | message,
|
---|
185 | messageType,
|
---|
186 | optionType,
|
---|
187 | null /* no icon */,
|
---|
188 | options,
|
---|
189 | initialValue
|
---|
190 | );
|
---|
191 | JDialog dialog = op.createDialog(Main.parent, title);
|
---|
192 | prepareDialog(dialog);
|
---|
193 | dialog.setVisible(true);
|
---|
194 | Object value = op.getValue();
|
---|
195 | if (value == null)
|
---|
196 | return JOptionPane.CLOSED_OPTION;
|
---|
197 | if (options == null) {
|
---|
198 | if (value instanceof Integer)
|
---|
199 | return (Integer)value;
|
---|
200 | return JOptionPane.CLOSED_OPTION;
|
---|
201 | }
|
---|
202 | for(int i = 0; i < options.length; i++) {
|
---|
203 | if(options[i].equals(value))
|
---|
204 | return i;
|
---|
205 | }
|
---|
206 | return JOptionPane.CLOSED_OPTION;
|
---|
207 | }
|
---|
208 | }
|
---|