1 21 22 package org.armedbear.j; 23 24 import java.awt.Dimension ; 25 import java.awt.Point ; 26 import java.awt.SystemColor ; 27 import java.awt.Toolkit ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.awt.event.KeyEvent ; 31 import java.awt.event.KeyListener ; 32 import java.awt.event.WindowEvent ; 33 import java.awt.event.WindowListener ; 34 import javax.swing.Box ; 35 import javax.swing.BoxLayout ; 36 import javax.swing.JComboBox ; 37 import javax.swing.JComponent ; 38 import javax.swing.JDialog ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JTextField ; 42 import javax.swing.border.EmptyBorder ; 43 44 public abstract class AbstractDialog extends JDialog implements ActionListener , 45 KeyListener , WindowListener 46 { 47 protected boolean cancelled; 48 protected JPanel mainPanel; 49 protected StandardButton okButton; 50 protected StandardButton cancelButton; 51 52 private Frame owner; 53 54 protected AbstractDialog(Editor editor) 55 { 56 super(editor == null ? null : editor.getFrame()); 57 this.owner = editor == null ? null : editor.getFrame(); 58 initialize(); 59 } 60 61 protected AbstractDialog(Editor editor, String title, boolean modal) 62 { 63 super(editor == null ? null : editor.getFrame(), title, modal); 64 this.owner = editor == null ? null : editor.getFrame(); 65 initialize(); 66 } 67 68 protected AbstractDialog(Frame owner) 69 { 70 super(owner); 71 this.owner = owner; 72 initialize(); 73 } 74 75 protected AbstractDialog(Frame owner, String title, boolean modal) 76 { 77 super(owner, title, modal); 78 this.owner = owner; 79 initialize(); 80 } 81 82 private void initialize() 83 { 84 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 85 addWindowListener(this); 86 getContentPane().setLayout(new BoxLayout (getContentPane(), BoxLayout.Y_AXIS)); 87 setBackground(SystemColor.control); 88 mainPanel = new JPanel (); 89 mainPanel.setLayout(new BoxLayout (mainPanel, BoxLayout.Y_AXIS)); 90 mainPanel.setBorder(new EmptyBorder (6, 6, 6, 6)); 91 getContentPane().add(mainPanel); 92 addKeyListener(this); 93 } 94 95 public final boolean cancelled() 96 { 97 return cancelled; 98 } 99 100 protected void addLabelAndTextField(JLabel label, JTextField textField) 102 { 103 label.setLabelFor(textField); 104 if (label.getBorder() == null) 105 label.setBorder(new EmptyBorder (0, 0, 3, 0)); 106 mainPanel.add(label); 107 mainPanel.add(textField); 108 textField.addKeyListener(this); 109 } 110 111 protected void addLabelAndComponent(JLabel label, JComponent component) 112 { 113 if (label.getLabelFor() == null) 114 label.setLabelFor(component); 115 if (label.getBorder() == null) 116 label.setBorder(new EmptyBorder (0, 0, 3, 0)); 117 mainPanel.add(label); 118 mainPanel.add(component); 119 component.addKeyListener(this); 120 } 121 122 protected final void addCheckBox(CheckBox checkBox) 123 { 124 mainPanel.add(checkBox); 125 checkBox.addKeyListener(this); 126 } 127 128 protected final void addVerticalStrut() 129 { 130 mainPanel.add(Box.createVerticalStrut(6)); 131 } 132 133 protected void addOK() 134 { 135 JPanel buttonPanel = new JPanel (); 136 buttonPanel.setAlignmentX(LEFT_ALIGNMENT); 137 mainPanel.add(buttonPanel); 138 okButton = new StandardButton("OK"); 139 okButton.setActionCommand("OK"); 140 okButton.addActionListener(this); 141 okButton.addKeyListener(this); 142 buttonPanel.add(okButton); 143 } 144 145 protected void addCancel() 146 { 147 JPanel buttonPanel = new JPanel (); 148 buttonPanel.setAlignmentX(LEFT_ALIGNMENT); 149 mainPanel.add(buttonPanel); 150 cancelButton = new StandardButton("Cancel"); 151 cancelButton.setActionCommand("Cancel"); 152 cancelButton.addActionListener(this); 153 cancelButton.addKeyListener(this); 154 buttonPanel.add(cancelButton); 155 } 156 157 protected void addOKCancel() 158 { 159 JPanel buttonPanel = new JPanel (); 160 buttonPanel.setAlignmentX(LEFT_ALIGNMENT); 161 mainPanel.add(buttonPanel); 162 okButton = new StandardButton("OK"); 163 okButton.setActionCommand("OK"); 164 okButton.addActionListener(this); 165 okButton.addKeyListener(this); 166 buttonPanel.add(okButton); 167 cancelButton = new StandardButton("Cancel"); 168 cancelButton.setActionCommand("Cancel"); 169 cancelButton.addActionListener(this); 170 cancelButton.addKeyListener(this); 171 buttonPanel.add(cancelButton); 172 } 173 174 protected void ok() 175 { 176 dispose(); 177 } 178 179 protected void cancel() 180 { 181 cancelled = true; 182 dispose(); 183 } 184 185 protected void enter() 186 { 187 ok(); 188 } 189 190 protected void escape() 191 { 192 cancel(); 193 } 194 195 public void dispose() 196 { 197 super.dispose(); 198 Editor.restoreFocus(); 199 } 200 201 public void actionPerformed(ActionEvent e) 202 { 203 if (e.getActionCommand().equals("Cancel")) 204 cancel(); 205 else if (e.getActionCommand().equals("OK")) 206 ok(); 207 } 208 209 public void keyPressed(KeyEvent e) 210 { 211 if (e.getModifiers() == 0) { 212 if (e.getComponent() instanceof JComboBox ) { 214 JComboBox cb = (JComboBox ) e.getComponent(); 215 if (cb.isPopupVisible()) 216 return; 217 } 219 switch (e.getKeyCode()){ 220 case KeyEvent.VK_ENTER: 221 e.consume(); 222 enter(); 223 break; 224 case KeyEvent.VK_ESCAPE: 225 if (!e.isConsumed()) { 228 e.consume(); 229 escape(); 230 } 231 break; 232 } 233 } 234 } 235 236 public void keyReleased(KeyEvent e) {} 237 238 public void keyTyped(KeyEvent e) {} 239 240 public void windowActivated(WindowEvent e) {} 241 242 public void windowDeactivated(WindowEvent e) {} 243 244 public void windowOpened(WindowEvent e) {} 245 246 public void windowClosing(WindowEvent e) 247 { 248 cancelled = true; 249 dispose(); 250 } 251 252 public void windowClosed(WindowEvent e) {} 253 254 public void windowIconified(WindowEvent e) {} 255 256 public void windowDeiconified(WindowEvent e) {} 257 258 protected void centerDialog() 259 { 260 Dimension window = getSize(); 261 Point p; 262 if (owner != null) { 263 p = owner.getLocation(); 264 Dimension parent = owner.getSize(); 265 p.translate((parent.width - window.width) / 2, 266 (parent.height - window.height) / 2); 267 } else { 268 Dimension parent = Toolkit.getDefaultToolkit().getScreenSize(); 269 p = new Point ((parent.width - window.width) / 2, 270 (parent.height - window.height) / 2); 271 } 272 setLocation(p); 273 } 274 } 275 | Popular Tags |