001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    
016    package lapisx.swing;
017    
018    import java.awt.*;
019    import java.awt.event.*;
020    import javax.swing.*;
021    
022    public class OkCancelDialog extends JDialog {
023        
024        JPanel controlsPane;
025        JButton okButton;
026        JButton cancelButton;
027    
028        public OkCancelDialog (Frame owner, String title) {
029            this (owner, title, true);
030        }
031    
032        public OkCancelDialog (Frame owner, String title, boolean modal) {
033            super (owner, title, modal);
034    
035            Container me = getContentPane ();
036            me.setLayout (new BorderLayout ());
037            
038            me.add (controlsPane = new JPanel (), BorderLayout.CENTER);
039            
040            JPanel panel;
041            me.add (panel = new JPanel (), BorderLayout.SOUTH);
042            panel.setLayout (new FlowLayout ());
043            
044            JPanel subpanel;
045            panel.add (subpanel = new JPanel ());
046            subpanel.setLayout (new GridLayout (1, 0, 4, 0));
047    
048            subpanel.add (okButton = new JButton ("OK"));
049            okButton.addActionListener (new ActionListener () {
050                public void actionPerformed (ActionEvent event) {
051                    ok ();
052                }
053            });
054            getRootPane ().setDefaultButton (okButton);
055            
056            subpanel.add (cancelButton = new JButton ("Cancel"));
057            cancelButton.addActionListener (new ActionListener () {
058                public void actionPerformed (ActionEvent event) {
059                    cancel ();
060                }
061            });
062            
063            addWindowListener (new WindowAdapter () {
064                public void windowClosing (WindowEvent event) {
065                    cancel ();
066                }
067            });
068        }        
069    
070        protected void setInitialPosition () {
071            Component parent = getParent ();
072            Dimension size = getSize();
073            Dimension parentSize = (parent != null) ? parent.getSize() : Toolkit.getDefaultToolkit().getScreenSize();
074            Point origin = (parent != null) ? parent.getLocationOnScreen () : new Point (0, 0);
075            
076            if (parentSize != null) {
077                int x = Math.max (0, origin.x + (parentSize.width - size.width) / 2);
078                int y = Math.max (0, origin.y + (parentSize.height - size.height) / 2);
079                setLocation (x, y);
080            }
081        }
082        
083        public void show () {
084            setInitialPosition ();
085            registerKeystrokes ();
086            super.show ();
087        }
088        
089        public JPanel getControlsPane () {
090            return controlsPane;
091        }
092    
093        public JButton getOKButton () {
094            return okButton;
095        }
096    
097        public JButton getCancelButton () {
098            return cancelButton;
099        }
100    
101        void registerKeystrokes () {
102            JRootPane root = getRootPane ();
103            root.registerKeyboardAction(new ButtonPressAction (root, cancelButton, true),
104                                   KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), 
105                                   JComponent.WHEN_IN_FOCUSED_WINDOW);
106            root.registerKeyboardAction(new ButtonPressAction (root, cancelButton, false),
107                                   KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), 
108                                   JComponent.WHEN_IN_FOCUSED_WINDOW);
109    
110            JButton defaultButton = root.getDefaultButton ();
111            if (defaultButton != null)
112                registerActionInAllTextFields (root, 
113                                               new ButtonPressAction (root, defaultButton));
114        }
115    
116        void registerActionInAllTextFields (Container container, Action action) {
117            for (int i = 0, n = container.getComponentCount ();
118                 i < n;
119                 ++i) {
120                Component comp = container.getComponent (i);
121                
122                if (comp instanceof TextField)
123                    ((TextField)comp).addActionListener (action);
124                else if (comp instanceof JTextField)
125                    ((JTextField)comp).addActionListener (action);
126                else if (comp instanceof Container)
127                    registerActionInAllTextFields ((Container)comp, action);
128            }
129        }
130     
131        static class ButtonPressAction extends AbstractAction {
132            JButton owner;
133            JRootPane root;
134            boolean press;
135            boolean pressAndRelease;
136            ButtonPressAction(JRootPane root, JButton owner) {
137                super("pressAndReleaseAction");
138                this.root = root;
139                this.owner = owner;
140                this.press = true;
141                this.pressAndRelease = true;
142            }
143                
144            ButtonPressAction(JRootPane root, JButton owner, boolean press) {
145                super(press? "pressedAction" : "releasedAction");
146                this.root = root;
147                this.owner = owner;
148                this.press = press;
149                this.pressAndRelease = false;
150            }
151            public void actionPerformed(ActionEvent e) {
152                if (owner != null && SwingUtilities.getRootPane(owner) == root) {
153                    ButtonModel model = owner.getModel();
154                    if (pressAndRelease) {
155                        model.setArmed(true);
156                        model.setPressed(true);
157                        model.setPressed(false);
158                    } else if (press) {
159                        model.setArmed(true);
160                        model.setPressed(true);
161                    } else {
162                        model.setPressed(false);
163                    }
164                }
165            }
166            public boolean isEnabled() {
167                return owner.getModel().isEnabled();
168            }
169        }
170    
171        public void ok () {
172            dispose ();
173        }
174        
175        public void cancel () {
176            dispose ();            
177        }
178    }