1 | package nu.fw.jeti.util; |
---|
2 | |
---|
3 | import javax.swing.*; |
---|
4 | import java.awt.*; |
---|
5 | import java.awt.event.*; |
---|
6 | |
---|
7 | import nu.fw.jeti.util.Popups; |
---|
8 | |
---|
9 | import java.beans.PropertyChangeEvent; |
---|
10 | import java.beans.PropertyChangeListener; |
---|
11 | |
---|
12 | |
---|
13 | /** |
---|
14 | * @author student |
---|
15 | * |
---|
16 | * To change this generated comment edit the template variable "typecomment": |
---|
17 | * Window>Preferences>Java>Templates. |
---|
18 | * To enable and disable the creation of type comments go to |
---|
19 | * Window>Preferences>Java>Code Generation. |
---|
20 | */ |
---|
21 | public class JOptionPaneNonModal |
---|
22 | { |
---|
23 | |
---|
24 | // |
---|
25 | // |
---|
26 | // /** |
---|
27 | // * Brings up a dialog with the options <i>Yes</i>, |
---|
28 | // * <i>No</i> and <i>Cancel</i>; with the |
---|
29 | // * title, <b>Select an Option</b>. |
---|
30 | // * |
---|
31 | // * @param parentComponent determines the <code>Frame</code> in which the |
---|
32 | // * dialog is displayed; if <code>null</code>, |
---|
33 | // * or if the <code>parentComponent</code> has no |
---|
34 | // * <code>Frame</code>, a |
---|
35 | // * default <code>Frame</code> is used |
---|
36 | // * @param message the <code>Object</code> to display |
---|
37 | // * @return an integer indicating the option selected by the user |
---|
38 | // * @exception HeadlessException if |
---|
39 | // * <code>GraphicsEnvironment.isHeadless</code> returns |
---|
40 | // * <code>true</code> |
---|
41 | // * @see java.awt.GraphicsEnvironment#isHeadless |
---|
42 | // */ |
---|
43 | // public static int showConfirmDialog(Component parentComponent, |
---|
44 | // Object message) throws HeadlessException { |
---|
45 | // return showConfirmDialog(parentComponent, message, |
---|
46 | // UIManager.getString("OptionPane.titleText"), |
---|
47 | // JOptionPane.YES_NO_CANCEL_OPTION); |
---|
48 | // } |
---|
49 | // |
---|
50 | // /** |
---|
51 | // * Brings up a dialog where the number of choices is determined |
---|
52 | // * by the <code>optionType</code> parameter. |
---|
53 | // * |
---|
54 | // * @param parentComponent determines the <code>Frame</code> in which the |
---|
55 | // * dialog is displayed; if <code>null</code>, |
---|
56 | // * or if the <code>parentComponent</code> has no |
---|
57 | // * <code>Frame</code>, a |
---|
58 | // * default <code>Frame</code> is used |
---|
59 | // * @param message the <code>Object</code> to display |
---|
60 | // * @param title the title string for the dialog |
---|
61 | // * @param optionType an int designating the options available on the dialog: |
---|
62 | // * <code>YES_NO_OPTION</code>, or |
---|
63 | // * <code>YES_NO_CANCEL_OPTION</code> |
---|
64 | // * @return an int indicating the option selected by the user |
---|
65 | // * @exception HeadlessException if |
---|
66 | // * <code>GraphicsEnvironment.isHeadless</code> returns |
---|
67 | // * <code>true</code> |
---|
68 | // * @see java.awt.GraphicsEnvironment#isHeadless |
---|
69 | // */ |
---|
70 | // public static int showConfirmDialog(Component parentComponent, |
---|
71 | // Object message, String title, int optionType) |
---|
72 | // throws HeadlessException { |
---|
73 | // return showConfirmDialog(parentComponent, message, title, optionType, |
---|
74 | // JOptionPane.QUESTION_MESSAGE); |
---|
75 | // } |
---|
76 | // |
---|
77 | // /** |
---|
78 | // * Brings up a dialog where the number of choices is determined |
---|
79 | // * by the <code>optionType</code> parameter, where the |
---|
80 | // * <code>messageType</code> |
---|
81 | // * parameter determines the icon to display. |
---|
82 | // * The <code>messageType</code> parameter is primarily used to supply |
---|
83 | // * a default icon from the Look and Feel. |
---|
84 | // * |
---|
85 | // * @param parentComponent determines the <code>Frame</code> in |
---|
86 | // * which the dialog is displayed; if <code>null</code>, |
---|
87 | // * or if the <code>parentComponent</code> has no |
---|
88 | // * <code>Frame</code>, a |
---|
89 | // * default <code>Frame</code> is used. |
---|
90 | // * @param message the <code>Object</code> to display |
---|
91 | // * @param title the title string for the dialog |
---|
92 | // * @param optionType an integer designating the options available |
---|
93 | // * on the dialog: <code>YES_NO_OPTION</code>, |
---|
94 | // * or <code>YES_NO_CANCEL_OPTION</code> |
---|
95 | // * @param messageType an integer designating the kind of message this is; |
---|
96 | // * primarily used to determine the icon from the pluggable |
---|
97 | // * Look and Feel: <code>ERROR_MESSAGE</code>, |
---|
98 | // * <code>INFORMATION_MESSAGE</code>, |
---|
99 | // * <code>WARNING_MESSAGE</code>, |
---|
100 | // * <code>QUESTION_MESSAGE</code>, |
---|
101 | // * or <code>PLAIN_MESSAGE</code> |
---|
102 | // * @return an integer indicating the option selected by the user |
---|
103 | // * @exception HeadlessException if |
---|
104 | // * <code>GraphicsEnvironment.isHeadless</code> returns |
---|
105 | // * <code>true</code> |
---|
106 | // * @see java.awt.GraphicsEnvironment#isHeadless |
---|
107 | // */ |
---|
108 | // public static int showConfirmDialog(Component parentComponent, |
---|
109 | // Object message, String title, int optionType, int messageType) |
---|
110 | // throws HeadlessException { |
---|
111 | // return showConfirmDialog(parentComponent, message, title, optionType, |
---|
112 | // messageType, null); |
---|
113 | // } |
---|
114 | // |
---|
115 | // /** |
---|
116 | // * Brings up a dialog with a specified icon, where the number of |
---|
117 | // * choices is determined by the <code>optionType</code> parameter. |
---|
118 | // * The <code>messageType</code> parameter is primarily used to supply |
---|
119 | // * a default icon from the look and feel. |
---|
120 | // * |
---|
121 | // * @param parentComponent determines the <code>Frame</code> in which the |
---|
122 | // * dialog is displayed; if <code>null</code>, |
---|
123 | // * or if the <code>parentComponent</code> has no |
---|
124 | // * <code>Frame</code>, a |
---|
125 | // * default <code>Frame</code> is used |
---|
126 | // * @param message the Object to display |
---|
127 | // * @param title the title string for the dialog |
---|
128 | // * @param optionType an int designating the options available on the dialog: |
---|
129 | // * <code>YES_NO_OPTION</code>, |
---|
130 | // * or <code>YES_NO_CANCEL_OPTION</code> |
---|
131 | // * @param messageType an int designating the kind of message this is, |
---|
132 | // * primarily used to determine the icon from the pluggable |
---|
133 | // * Look and Feel: <code>ERROR_MESSAGE</code>, |
---|
134 | // * <code>INFORMATION_MESSAGE</code>, |
---|
135 | // * <code>WARNING_MESSAGE</code>, |
---|
136 | // * <code>QUESTION_MESSAGE</code>, |
---|
137 | // * or <code>PLAIN_MESSAGE</code> |
---|
138 | // * @param icon the icon to display in the dialog |
---|
139 | // * @return an int indicating the option selected by the user |
---|
140 | // * @exception HeadlessException if |
---|
141 | // * <code>GraphicsEnvironment.isHeadless</code> returns |
---|
142 | // * <code>true</code> |
---|
143 | // * @see java.awt.GraphicsEnvironment#isHeadless |
---|
144 | // */ |
---|
145 | // public static int showConfirmDialog(Component parentComponent, |
---|
146 | // Object message, String title, int optionType, |
---|
147 | // int messageType, Icon icon) throws HeadlessException { |
---|
148 | // return showOptionDialog(parentComponent, message, title, optionType, |
---|
149 | // messageType, icon, null, null); |
---|
150 | // } |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | public static void showOptionDialog(Component parentComponent, |
---|
156 | Object message, String title, int optionType, int messageType, |
---|
157 | Icon icon,final Object[] options, Object initialValue,final Popups.OptionChoosed choosed) |
---|
158 | throws HeadlessException |
---|
159 | { |
---|
160 | final JOptionPane pane = new JOptionPane(message, messageType, |
---|
161 | optionType, icon, |
---|
162 | options, initialValue); |
---|
163 | |
---|
164 | pane.setInitialValue(initialValue); |
---|
165 | // pane.setComponentOrientation(((parentComponent == null) ? |
---|
166 | // getRootFrame() : parentComponent).getComponentOrientation()); |
---|
167 | |
---|
168 | int style = styleFromMessageType(messageType); |
---|
169 | //JDialog dialog = pane.createDialog(parentComponent, title, style); |
---|
170 | |
---|
171 | |
---|
172 | final JDialog dialog; |
---|
173 | |
---|
174 | //Window window = JOptionPane.getWindowForComponent(parentComponent); |
---|
175 | if (parentComponent instanceof Dialog) { |
---|
176 | dialog = new JDialog((Dialog)parentComponent, title, true); |
---|
177 | } |
---|
178 | else if (parentComponent instanceof Frame) { |
---|
179 | dialog = new JDialog((Frame)parentComponent , title, false); |
---|
180 | } else { |
---|
181 | dialog = new JDialog(new JFrame() , title, false); |
---|
182 | } |
---|
183 | Container contentPane = dialog.getContentPane(); |
---|
184 | contentPane.setLayout(new BorderLayout()); |
---|
185 | contentPane.add(pane, BorderLayout.CENTER); |
---|
186 | dialog.setResizable(false); |
---|
187 | if (JDialog.isDefaultLookAndFeelDecorated()) { |
---|
188 | boolean supportsWindowDecorations = |
---|
189 | UIManager.getLookAndFeel().getSupportsWindowDecorations(); |
---|
190 | if (supportsWindowDecorations) { |
---|
191 | dialog.setUndecorated(true); |
---|
192 | pane.getRootPane().setWindowDecorationStyle(style); |
---|
193 | } |
---|
194 | } |
---|
195 | dialog.pack(); |
---|
196 | dialog.setLocationRelativeTo(parentComponent); |
---|
197 | dialog.addWindowListener(new WindowAdapter() { |
---|
198 | private boolean gotFocus = false; |
---|
199 | public void windowClosing(WindowEvent we) { |
---|
200 | pane.setValue(null); |
---|
201 | returnOption(pane.getValue(),options,choosed); |
---|
202 | } |
---|
203 | public void windowGainedFocus(WindowEvent we) { |
---|
204 | // Once window gets focus, set initial focus |
---|
205 | if (!gotFocus) { |
---|
206 | pane.selectInitialValue(); |
---|
207 | gotFocus = true; |
---|
208 | } |
---|
209 | } |
---|
210 | }); |
---|
211 | dialog.addComponentListener(new ComponentAdapter() { |
---|
212 | public void componentShown(ComponentEvent ce) { |
---|
213 | // reset value to ensure closing works properly |
---|
214 | pane.setValue(JOptionPane.UNINITIALIZED_VALUE); |
---|
215 | } |
---|
216 | }); |
---|
217 | pane.addPropertyChangeListener(new PropertyChangeListener() { |
---|
218 | public void propertyChange(PropertyChangeEvent event) { |
---|
219 | // Let the defaultCloseOperation handle the closing |
---|
220 | // if the user closed the window without selecting a button |
---|
221 | // (newValue = null in that case). Otherwise, close the dialog. |
---|
222 | if(dialog.isVisible() && event.getSource() == pane && |
---|
223 | (event.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) && |
---|
224 | event.getNewValue() != null && |
---|
225 | event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) { |
---|
226 | |
---|
227 | //System.out.println(event.getNewValue() ); |
---|
228 | returnOption(pane.getValue(),options,choosed); |
---|
229 | dialog.dispose(); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | } |
---|
234 | }); |
---|
235 | pane.selectInitialValue(); |
---|
236 | dialog.show(); |
---|
237 | } |
---|
238 | |
---|
239 | private static void returnOption(Object selectedValue, Object[] options,Popups.OptionChoosed choosed) |
---|
240 | { |
---|
241 | //System.out.println(selectedValue ); |
---|
242 | if(choosed == null) return;//messagedialog does not use choosed |
---|
243 | if(selectedValue == null) choosed.optionChoosed(JOptionPane.CLOSED_OPTION); |
---|
244 | else if(options == null) { |
---|
245 | if(selectedValue instanceof Integer) |
---|
246 | choosed.optionChoosed(((Integer)selectedValue).intValue()); |
---|
247 | else choosed.optionChoosed(JOptionPane.CLOSED_OPTION); |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | for(int counter = 0, maxCounter = options.length;counter < maxCounter; counter++) |
---|
252 | { |
---|
253 | if(options[counter].equals(selectedValue)) |
---|
254 | { |
---|
255 | choosed.optionChoosed(counter); |
---|
256 | return; |
---|
257 | } |
---|
258 | } |
---|
259 | choosed.optionChoosed(JOptionPane.CLOSED_OPTION); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | private static int styleFromMessageType(int messageType) { |
---|
264 | switch (messageType) { |
---|
265 | case JOptionPane.ERROR_MESSAGE: |
---|
266 | return JRootPane.ERROR_DIALOG; |
---|
267 | case JOptionPane.QUESTION_MESSAGE: |
---|
268 | return JRootPane.QUESTION_DIALOG; |
---|
269 | case JOptionPane.WARNING_MESSAGE: |
---|
270 | return JRootPane.WARNING_DIALOG; |
---|
271 | case JOptionPane.INFORMATION_MESSAGE: |
---|
272 | return JRootPane.INFORMATION_DIALOG; |
---|
273 | case JOptionPane.PLAIN_MESSAGE: |
---|
274 | default: |
---|
275 | return JRootPane.PLAIN_DIALOG; |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | /* |
---|
282 | * Overrides for emacs |
---|
283 | * Local variables: |
---|
284 | * tab-width: 4 |
---|
285 | * End: |
---|
286 | */ |
---|