1 17 package org.columba.core.gui.config; 18 19 import java.awt.BorderLayout ; 20 import java.awt.GridBagConstraints ; 21 import java.awt.GridBagLayout ; 22 import java.awt.GridLayout ; 23 import java.awt.Insets ; 24 import java.awt.Toolkit ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 31 import javax.swing.BorderFactory ; 32 import javax.swing.Box ; 33 import javax.swing.BoxLayout ; 34 import javax.swing.ButtonGroup ; 35 import javax.swing.JButton ; 36 import javax.swing.JComponent ; 37 import javax.swing.JDialog ; 38 import javax.swing.JFrame ; 39 import javax.swing.JLabel ; 40 import javax.swing.JPanel ; 41 import javax.swing.JRadioButton ; 42 import javax.swing.JTextField ; 43 import javax.swing.KeyStroke ; 44 import javax.swing.event.DocumentEvent ; 45 import javax.swing.event.DocumentListener ; 46 import javax.swing.text.AbstractDocument ; 47 import javax.swing.text.AttributeSet ; 48 import javax.swing.text.BadLocationException ; 49 import javax.swing.text.DocumentFilter ; 50 51 import org.columba.core.gui.base.ButtonWithMnemonic; 52 import org.columba.core.gui.base.LabelWithMnemonic; 53 import org.columba.core.gui.base.RadioButtonWithMnemonic; 54 import org.columba.core.resourceloader.GlobalResourceLoader; 55 56 59 public class ProxyConfigurationDialog extends JDialog 60 implements ActionListener , DocumentListener { 61 62 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 63 public static final int CANCEL_OPTION = 0; 64 public static final int APPROVE_OPTION = 1; 65 66 protected int status = CANCEL_OPTION; 67 protected JRadioButton directConnectionRadioButton; 68 protected JRadioButton useProxyRadioButton; 69 protected JLabel proxyHostLabel; 70 protected JTextField proxyHostField; 71 protected JLabel proxyPortLabel; 72 protected JTextField proxyPortField; 73 protected JButton okButton; 74 75 78 public ProxyConfigurationDialog(JFrame parent) { 79 super(parent, GlobalResourceLoader.getString( 80 RESOURCE_PATH, "proxy", "title"), true); 81 } 82 83 86 public ProxyConfigurationDialog(JDialog parent) { 87 super(parent, GlobalResourceLoader.getString( 88 RESOURCE_PATH, "proxy", "title"), true); 89 } 90 91 protected void dialogInit() { 92 super.dialogInit(); 93 addWindowListener(new WindowAdapter () { 94 public void windowClosing(WindowEvent e) { 95 status = CANCEL_OPTION; 96 } 97 }); 98 JPanel contentPane = (JPanel )getContentPane(); 99 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11)); 100 JPanel centerPanel = new JPanel (); 101 centerPanel.setLayout(new BoxLayout (centerPanel, BoxLayout.Y_AXIS)); 102 ButtonGroup group = new ButtonGroup (); 103 directConnectionRadioButton = 104 new RadioButtonWithMnemonic(GlobalResourceLoader.getString( 105 RESOURCE_PATH, "proxy", "directConnection")); 106 directConnectionRadioButton.setSelected(true); 107 directConnectionRadioButton.setActionCommand("USE_PROXY"); 108 directConnectionRadioButton.addActionListener(this); 109 group.add(directConnectionRadioButton); 110 centerPanel.add(directConnectionRadioButton); 111 centerPanel.add(Box.createVerticalStrut(5)); 112 useProxyRadioButton = 113 new RadioButtonWithMnemonic(GlobalResourceLoader.getString( 114 RESOURCE_PATH, "proxy", "useProxy")); 115 useProxyRadioButton.setActionCommand("USE_PROXY"); 116 useProxyRadioButton.addActionListener(this); 117 group.add(useProxyRadioButton); 118 centerPanel.add(useProxyRadioButton); 119 JPanel proxyDataPanel = new JPanel (new GridBagLayout ()); 120 proxyDataPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 121 GridBagConstraints c = new GridBagConstraints (); 122 proxyHostLabel = new LabelWithMnemonic(GlobalResourceLoader.getString( 123 RESOURCE_PATH, "proxy", "host")); 124 c.anchor = GridBagConstraints.WEST; 125 c.insets = new Insets (2, 10, 2, 0); 126 proxyDataPanel.add(proxyHostLabel, c); 127 proxyHostField = new JTextField (10); 128 proxyHostField.getDocument().addDocumentListener(this); 129 proxyHostLabel.setLabelFor(proxyHostField); 130 c.gridwidth = GridBagConstraints.REMAINDER; 131 proxyDataPanel.add(proxyHostField, c); 132 proxyPortLabel = new LabelWithMnemonic(GlobalResourceLoader.getString( 133 RESOURCE_PATH, "proxy", "port")); 134 c.gridwidth = 1; 135 proxyDataPanel.add(proxyPortLabel, c); 136 proxyPortField = new JTextField (5); 137 ((AbstractDocument )proxyPortField.getDocument()).setDocumentFilter( 138 new DocumentFilter () { 139 public void insertString(DocumentFilter.FilterBypass fb, 140 int offset, String string, AttributeSet attr) 141 throws BadLocationException { 142 if (ensureDigits(string)) { 143 super.insertString(fb, offset, string, attr); 144 } else { 145 Toolkit.getDefaultToolkit().beep(); 146 } 147 } 148 149 public void replace(DocumentFilter.FilterBypass fb, 150 int offset, int length, String string, AttributeSet attr) 151 throws BadLocationException { 152 if (ensureDigits(string)) { 153 super.replace(fb, offset, length, string, attr); 154 } else { 155 Toolkit.getDefaultToolkit().beep(); 156 } 157 } 158 159 private boolean ensureDigits(String string) { 160 for (int i = 0; i < string.length(); i++) { 161 if (!Character.isDigit(string.charAt(i))) { 162 return false; 163 } 164 } 165 return true; 166 } 167 }); 168 proxyPortField.getDocument().addDocumentListener(this); 169 proxyPortLabel.setLabelFor(proxyPortField); 170 c.gridwidth = GridBagConstraints.REMAINDER; 171 proxyDataPanel.add(proxyPortField, c); 172 centerPanel.add(proxyDataPanel); 173 centerPanel.add(Box.createVerticalGlue()); 174 contentPane.add(centerPanel); 175 JPanel southPanel = new JPanel (new BorderLayout ()); 176 JPanel buttonPanel = new JPanel (new GridLayout (1, 2, 5, 0)); 177 buttonPanel.setBorder(BorderFactory.createEmptyBorder(17, 0, 0, 0)); 178 okButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 179 "", "global", "ok")); 180 okButton.addActionListener(this); 181 buttonPanel.add(okButton); 182 JButton cancelButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 183 "", "global", "cancel")); 184 cancelButton.addActionListener(this); 185 buttonPanel.add(cancelButton); 186 southPanel.add(buttonPanel, BorderLayout.EAST); 187 contentPane.add(southPanel, BorderLayout.SOUTH); 188 getRootPane().setDefaultButton(okButton); 189 getRootPane().registerKeyboardAction(this, "CANCEL", 190 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 191 JComponent.WHEN_IN_FOCUSED_WINDOW); 192 pack(); 193 setLocationRelativeTo(null); 194 } 195 196 200 public int showDialog() { 201 if (getProxyHost() != null && getProxyPort() > 0) { 202 useProxyRadioButton.setSelected(true); 203 } else { 204 directConnectionRadioButton.setSelected(true); 205 } 206 updateProxyDataPanel(); 207 setVisible(true); 208 return status; 209 } 210 211 214 public String getProxyHost() { 215 String text = proxyHostField.getText().trim(); 216 return text.length() == 0 ? null : text; 217 } 218 219 222 public void setProxyHost(String host) { 223 proxyHostField.setText(host); 224 } 225 226 229 public int getProxyPort() { 230 String text = proxyPortField.getText(); 231 return text.length() == 0 ? -1 : Integer.parseInt(text); 232 } 233 234 237 public void setProxyPort(int port) { 238 if (port > 0) { 239 proxyPortField.setText(Integer.toString(port)); 240 } 241 } 242 243 246 protected void updateProxyDataPanel() { 247 boolean enabled = useProxyRadioButton.isSelected(); 248 proxyHostLabel.setEnabled(enabled); 249 proxyHostField.setEnabled(enabled); 250 proxyPortLabel.setEnabled(enabled); 251 proxyPortField.setEnabled(enabled); 252 updateOkButton(); 253 } 254 255 protected void updateOkButton() { 256 okButton.setEnabled(directConnectionRadioButton.isSelected() || 257 (getProxyHost() != null && getProxyPort() > 0)); 258 } 259 260 public void actionPerformed(ActionEvent e) { 261 String command = e.getActionCommand(); 262 if ("USE_PROXY".equals(command)) { 263 updateProxyDataPanel(); 264 } else if ("OK".equals(command)) { 265 status = APPROVE_OPTION; 266 if (directConnectionRadioButton.isSelected()) { 267 proxyHostField.setText(null); 268 proxyPortField.setText(null); 269 } 270 setVisible(false); 271 } else { 272 status = CANCEL_OPTION; 273 setVisible(false); 274 } 275 } 276 277 public void insertUpdate(DocumentEvent e) { 278 updateOkButton(); 279 } 280 281 public void removeUpdate(DocumentEvent e) { 282 updateOkButton(); 283 } 284 285 public void changedUpdate(DocumentEvent e) {} 286 } 287 | Popular Tags |