1 66 67 68 package swingwtx.swing; 69 70 import org.eclipse.swt.SWT; 71 import org.eclipse.swt.widgets.MessageBox; 72 import org.eclipse.swt.widgets.Shell; 73 74 import swingwt.awt.BorderLayout; 75 import swingwt.awt.Container; 76 import swingwt.awt.Dimension; 77 import swingwt.awt.Frame; 78 import swingwt.awt.Toolkit; 79 import swingwt.awt.event.ActionEvent; 80 import swingwt.awt.event.ActionListener; 81 82 public class JOptionPane extends JPanel { 83 84 public final static int YES_OPTION = SWT.YES; 85 public final static int NO_OPTION = SWT.NO; 86 public final static int OK_OPTION = SWT.OK; 87 public final static int OK_CANCEL_OPTION = SWT.OK | SWT.CANCEL; 88 public final static int CLOSED_OPTION = SWT.CLOSE; 89 public final static int DEFAULT_OPTION = SWT.DEFAULT; 90 public final static int YES_NO_OPTION = SWT.YES | SWT.NO; 91 public final static int WARNING_MESSAGE = SWT.ICON_WARNING; 92 public final static int QUESTION_MESSAGE = SWT.ICON_QUESTION; 93 public final static int INFORMATION_MESSAGE = SWT.ICON_INFORMATION; 94 public final static int ERROR_MESSAGE = SWT.ICON_ERROR; 95 96 protected static Object lastInputDialogReturnValue = null; 97 protected static int lastInputDialogReturnIndex = -1; 98 99 private static int confRetVal = 0; 100 private static boolean jobFinished = false; 101 private static JFrame rootFrame = null; 102 103 public static void setRootFrame(JFrame frame) { 104 rootFrame = frame; 105 } 106 107 public static JFrame getRootFrame() { 108 return rootFrame; 109 } 110 111 public static int showConfirmDialog(swingwt.awt.Container parent, Object message) { 112 return showConfirmDialog(parent, message, QUESTION_MESSAGE); 113 } 114 115 public static int showConfirmDialog(final swingwt.awt.Container parent, final Object message, 116 final String title, final int buttons, final int messageType) { 117 confRetVal = 0; 118 SwingUtilities.invokeSync(new Runnable () { 119 public void run() { 120 Shell container = getModalParent(parent); 121 MessageBox m = new MessageBox(container, messageType | SWT.YES | SWT.NO); 122 m.setMessage(getOptionPaneMessage(message)); 123 m.setText(title); 124 confRetVal = m.open(); 125 switch (confRetVal) { 126 case SWT.YES: confRetVal = YES_OPTION; break; 127 case SWT.NO: confRetVal = NO_OPTION; break; 128 } 129 } 130 }); 131 return confRetVal; 132 } 133 134 public static int showConfirmDialog(swingwt.awt.Container parent, Object message, String title, int messageType) { 135 return showConfirmDialog(parent, message, title, 0, messageType); 136 } 137 138 public static int showConfirmDialog(final swingwt.awt.Container parent, final Object message, final int messageType) { 139 return showConfirmDialog(parent, message, "", messageType); 140 } 141 142 public static void showMessageDialog(swingwt.awt.Container parent, Object message) { 143 showMessageDialog(parent, message, "Information", SWT.ICON_INFORMATION); 144 } 145 146 147 public static void showMessageDialog(final swingwt.awt.Container parent, final Object message, final String title, final int messageType) { 148 SwingUtilities.invokeAsync(new Runnable () { 149 public void run() { 150 MessageBox m = new MessageBox(getModalParent(parent), messageType | SWT.OK); 151 m.setMessage(getOptionPaneMessage(message)); 152 m.setText(title); 153 m.open(); 154 } 155 }); 156 } 157 158 public static String showInputDialog(swingwt.awt.Container parent, Object message) { 159 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), "Input", 0, null, null, null); 160 dlg.show(); 161 if (lastInputDialogReturnValue == null) 162 return null; 163 else 164 return lastInputDialogReturnValue.toString(); 165 } 166 public static String showInputDialog(swingwt.awt.Container parent, Object message, Object initialSelectionValue) { 167 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), "Input", 0, null, null, initialSelectionValue); 168 dlg.show(); 169 if (lastInputDialogReturnValue == null) 170 return null; 171 else 172 return lastInputDialogReturnValue.toString(); 173 } 174 public static String showInputDialog(swingwt.awt.Container parent, Object message, String title, int messageType) { 175 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), title, messageType, null, null, null); 176 dlg.show(); 177 if (lastInputDialogReturnValue == null) 178 return null; 179 else 180 return lastInputDialogReturnValue.toString(); 181 } 182 public static Object showInputDialog(final swingwt.awt.Container parent, final Object message, final String title, final int messageType, final Icon icon, final Object [] selectionValues, final Object initialSelectionValue) { 183 SwingUtilities.invokeSync(new Runnable () { 184 public void run() { 185 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), title, messageType, icon, selectionValues, initialSelectionValue); 186 dlg.show(); 187 } 188 }); 189 return lastInputDialogReturnValue; 190 } 191 192 public static int showOptionDialog(final swingwt.awt.Container parent, final Object message, final String title, final int optionType, final int messageType, 193 final Icon icon, final Object [] options, final Object initialValue) { 194 SwingUtilities.invokeSync(new Runnable () { 195 public void run() { 196 SwingStyleInputDialog dlg = new SwingStyleInputDialog(parent, getOptionPaneMessage(message), title, messageType, icon, options, initialValue); 197 dlg.show(); 198 } 199 }); 200 return lastInputDialogReturnIndex; 201 } 202 203 207 protected static String getOptionPaneMessage(Object message) { 208 String out = ""; 209 if (message instanceof String []) { 211 String [] msgs = (String []) message; 212 for (int i = 0; i < msgs.length; i++) { 213 out += msgs[i] + " "; 214 } 215 } 216 else 217 out = message.toString(); 219 220 out = SwingWTUtils.removeHTML(out); 222 223 return out; 224 } 225 226 230 private static void waitToFinish() { 231 while (!isJobFinished()) { 232 try { 233 Thread.sleep(50); 234 } 235 catch(Exception e) {} 236 } 237 } 238 239 protected static synchronized boolean isJobFinished() { return jobFinished; } 240 protected static synchronized void setJobFinished(boolean b) { jobFinished = b; } 241 242 249 private static Shell getModalParent(Container c) { 250 if (c == null) { 251 if (getRootFrame() == null) 252 return getSharedFrame(); 253 else 254 return ((Shell) getRootFrame().getPeer()); 255 } 256 if (!(c.composite instanceof Shell)) 257 return c.composite.getShell(); 258 else 259 return (Shell) c.composite; 260 } 261 private static Shell sharedFrame = null; 262 266 public static Shell getSharedFrame() { 267 if ( sharedFrame == null ) { 268 sharedFrame = new Shell(SwingWTUtils.getDisplay(), SWT.NO_TRIM); 269 } 270 return sharedFrame; 271 } 272 273 } 274 275 278 class SwingStyleInputDialog extends JDialog { 279 280 private boolean isTextEntry = false; 281 private JTextArea text = null; 282 private JList sel = null; 283 284 public SwingStyleInputDialog(swingwt.awt.Component parent, Object message, String title, int messageType, Icon icon, Object [] selectionValues, Object initialSelectionValue) { 285 286 super((Frame) null, title, true); 287 288 isTextEntry = (selectionValues == null); 290 291 setTitle(title); 294 295 JLabel mess = new JLabel(message.toString()); 296 getContentPane().add(mess, BorderLayout.NORTH); 297 298 JPanel pnlButtons = new JPanel(); 300 getContentPane().add(pnlButtons, BorderLayout.SOUTH); 301 302 JButton btnOk = new JButton("Ok"); 303 btnOk.setMnemonic('O'); 304 btnOk.addActionListener( new ActionListener() { 305 public void actionPerformed(ActionEvent e) { 306 btnOk_clicked(); 307 } 308 }); 309 pnlButtons.add(btnOk); 310 JButton btnCancel = new JButton("Cancel"); 311 btnCancel.setMnemonic('C'); 312 btnCancel.addActionListener( new ActionListener() { 313 public void actionPerformed(ActionEvent e) { 314 btnCancel_clicked(); 315 } 316 }); 317 pnlButtons.add(btnCancel); 318 319 320 if (isTextEntry) { 321 JScrollPane scr = new JScrollPane(); 323 text = new JTextArea(); 324 text.setWrapStyleWord(true); 325 text.setLineWrap(true); 326 scr.setViewportView(text); 327 scr.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 328 scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 329 getContentPane().add(scr, BorderLayout.CENTER); 330 setSize(400, 150); 332 } 333 else 334 { 335 sel = new JList(selectionValues); 337 sel.setSelectedValue(initialSelectionValue, true); 338 getContentPane().add(sel, BorderLayout.CENTER); 339 setSize(400, 300); 341 } 342 343 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 345 Dimension frameSize = getSize(); 346 if (frameSize.height > screenSize.height) { 347 frameSize.height = screenSize.height; 348 } 349 if (frameSize.width > screenSize.width) { 350 frameSize.width = screenSize.width; 351 } 352 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 353 } 354 355 protected void btnOk_clicked() { 356 if (isTextEntry) 357 JOptionPane.lastInputDialogReturnValue = text.getText(); 358 else { 359 JOptionPane.lastInputDialogReturnValue = sel.getSelectedValue(); 360 JOptionPane.lastInputDialogReturnIndex = sel.getSelectedIndex(); 361 } 362 JOptionPane.setJobFinished(true); 363 dispose(); 364 } 365 366 protected void btnCancel_clicked() { 367 JOptionPane.lastInputDialogReturnValue = null; 368 JOptionPane.lastInputDialogReturnIndex = -1; 369 JOptionPane.setJobFinished(true); 370 dispose(); 371 } 372 373 } 374 | Popular Tags |