Back to index

wims  3.65+svn20090927
SharpDialog.java
Go to the documentation of this file.
00001 
00008 package SharpTools;
00009 import javax.swing.*;
00010 import java.awt.*;
00011 import java.awt.event.*;
00012 
00021 class SharpDialog extends JDialog {
00022     static final public int CLOSED_OPTION = JOptionPane.CLOSED_OPTION;
00023     static final public int YES_OPTION = JOptionPane.YES_OPTION;
00024     static final public int NO_OPTION = JOptionPane.NO_OPTION;
00025     static final public int OK_OPTION = JOptionPane.OK_OPTION;
00026     static final public int CANCEL_OPTION = JOptionPane.CANCEL_OPTION;
00027     final ImageIcon okIcon=new ImageIcon(getClass().getResource("/images/ok.gif"));                                                        
00028     final ImageIcon cancelIcon=new ImageIcon(getClass().getResource("/images/cancel.gif"));                                                
00029     final ImageIcon noIcon=new ImageIcon(getClass().getResource("/images/no.gif"));                                                        
00030     //static final private ImageIcon okIcon = SharpTools.getImageIcon("ok.gif");
00031     //static final private ImageIcon cancelIcon = SharpTools.getImageIcon("cancel.gif");
00032     //static final private ImageIcon noIcon = SharpTools.getImageIcon("no.gif");
00033 
00034     // get string from the UIManager - support locale
00035     final private JButton okButton = new JButton(UIManager.get("OptionPane.okButtonText").toString(), okIcon);
00036     final private JButton cancelButton = new JButton(UIManager.get("OptionPane.cancelButtonText").toString(), cancelIcon);
00037     final private JButton yesButton = new JButton(UIManager.get("OptionPane.yesButtonText").toString(), okIcon);
00038     final private JButton noButton = new JButton(UIManager.get("OptionPane.noButtonText").toString(), noIcon);
00039 
00040     final private Object[] ok = { okButton };
00041     final private Object[] yesno = { yesButton, noButton };
00042     final private Object[] yesnocancel = { yesButton, noButton, cancelButton };
00043     final private Object[] okcancel = { okButton, cancelButton };
00044 
00045     private int choice = CLOSED_OPTION;
00046 
00047 
00048     // override all constructors
00054     SharpDialog(Dialog c) {
00055        super(c);
00056     }
00057 
00062     SharpDialog(Dialog c, boolean modal) {
00063        super(c, modal);
00064     }
00065 
00072     SharpDialog(Dialog c, String title) {
00073        super(c, title);
00074     }
00075 
00081     SharpDialog(Dialog c, String title, boolean modal) {
00082        super(c, title, modal);
00083     }
00084 
00090     SharpDialog(Frame c) {
00091        super(c);
00092     }
00093 
00098     SharpDialog(Frame c, boolean modal) {
00099        super(c, modal);
00100     }
00101 
00108     SharpDialog(Frame c, String title) {
00109        super(c, title);
00110     }
00111 
00117     SharpDialog(Frame c, String title, boolean modal) {
00118        super(c, title, modal);
00119     }
00120 
00126     protected JRootPane createRootPane() {
00127        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
00128        JRootPane rootPane = new JRootPane();
00129        rootPane.registerKeyboardAction(
00130                                    new ActionListener() {
00131                                           public void actionPerformed
00132                                               (ActionEvent ev) {    
00133                                               if (onClose()) {
00134                                                  choice = CLOSED_OPTION;
00135                                                  setVisible(false);
00136                                               }
00137                                           }
00138                                        },
00139                                    stroke,
00140                                    JComponent.WHEN_IN_FOCUSED_WINDOW);
00141        return rootPane;
00142     }
00143 
00153     protected void onOpen() {
00154        
00155     }
00156     
00167     protected boolean onClose() {
00168        return onCancel();
00169     }
00170 
00177     protected boolean onOK() {     
00178        return true;
00179     }
00180     
00187     protected boolean onCancel() {
00188        return true;
00189     }
00190 
00197     protected boolean onYes() {
00198        return true;
00199     }
00200 
00207     protected boolean onNo() {
00208        return true;
00209     }
00210 
00220     protected void setOptionPane(Object message,
00221                              int messageType,
00222                              int optionType,
00223                              Icon icon) {
00224        
00225        setOptionPane(message, messageType, optionType, icon, 0);
00226     }
00227     
00238     protected void setOptionPane(Object message,
00239                              int messageType,
00240                              int optionType,
00241                              Icon icon,                        
00242                              int defaultIndex) {
00243        
00244        addWindowListener(new WindowAdapter() {
00245 
00246               public void windowOpened(WindowEvent we) {
00247                   // set the initial focus to rowField
00248                   onOpen();
00249               }
00250               
00251                 public void windowClosing(WindowEvent we) {
00252                   if (onClose()) {
00253                      choice = CLOSED_OPTION;
00254                      setVisible(false);
00255                   }
00256               }
00257            });
00258        
00259        // handle this by onClose()
00260        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
00261 
00262        okButton.addActionListener(new ActionListener() {
00263               public void actionPerformed(ActionEvent evt) {
00264                   if (onOK()) {
00265                      choice = OK_OPTION;
00266                      setVisible(false);
00267                   }
00268               }
00269            });
00270 
00271        cancelButton.addActionListener(new ActionListener() {
00272               public void actionPerformed(ActionEvent evt) {
00273                   if (onCancel()) {
00274                      choice = CANCEL_OPTION;
00275                      setVisible(false);
00276                   }
00277               }
00278            });
00279        
00280        yesButton.addActionListener(new ActionListener() {
00281               public void actionPerformed(ActionEvent evt) {
00282                   if (onYes()) {
00283                      choice = YES_OPTION;
00284                      setVisible(false);
00285                   }
00286               }
00287            });
00288        
00289        noButton.addActionListener(new ActionListener() {
00290               public void actionPerformed(ActionEvent evt) {
00291                   if (onNo()) {
00292                      choice = NO_OPTION;
00293                      setVisible(false);
00294                   }
00295               }
00296            });
00297 
00298        // setup Mnemonic
00299        okButton.setMnemonic (KeyEvent.VK_O);
00300        cancelButton.setMnemonic (KeyEvent.VK_C);
00301        yesButton.setMnemonic (KeyEvent.VK_Y);
00302        noButton.setMnemonic (KeyEvent.VK_N);
00303        
00304        Object[] buttons = setButtons(optionType);
00305        JOptionPane optionPane = new JOptionPane(message, messageType,
00306                                            optionType, icon,
00307                                            buttons,
00308                                            buttons[defaultIndex]);
00309        
00310        setContentPane(optionPane);
00311 
00312        pack();
00313        setLocationRelativeTo(getParent());
00314     }
00315 
00319     private Object[] setButtons(int type) {
00320 
00321        Object[] buttons;
00322        
00323        switch (type) {
00324        case JOptionPane.YES_NO_CANCEL_OPTION:
00325            buttons = yesnocancel;
00326            break;
00327        case JOptionPane.YES_NO_OPTION:
00328            buttons = yesno;
00329            break;
00330        case JOptionPane.OK_CANCEL_OPTION:     
00331            buttons = okcancel;
00332            break;
00333        default:
00334            buttons = ok;
00335            break;
00336        }
00337 
00338        return buttons;
00339     }
00340 
00344     public int getChoice() {
00345        return choice;
00346     }
00347 
00351     public boolean isCancelled() {
00352        return choice == CANCEL_OPTION || choice == CLOSED_OPTION;
00353     }
00354 }
00355 
00356 
00357 
00358