KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > config > ProxyConfigurationDialog


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.gui.config;
18
19 import java.awt.BorderLayout JavaDoc;
20 import java.awt.GridBagConstraints JavaDoc;
21 import java.awt.GridBagLayout JavaDoc;
22 import java.awt.GridLayout JavaDoc;
23 import java.awt.Insets JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.WindowAdapter JavaDoc;
29 import java.awt.event.WindowEvent JavaDoc;
30
31 import javax.swing.BorderFactory JavaDoc;
32 import javax.swing.Box JavaDoc;
33 import javax.swing.BoxLayout JavaDoc;
34 import javax.swing.ButtonGroup JavaDoc;
35 import javax.swing.JButton JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JDialog JavaDoc;
38 import javax.swing.JFrame JavaDoc;
39 import javax.swing.JLabel JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JRadioButton JavaDoc;
42 import javax.swing.JTextField JavaDoc;
43 import javax.swing.KeyStroke JavaDoc;
44 import javax.swing.event.DocumentEvent JavaDoc;
45 import javax.swing.event.DocumentListener JavaDoc;
46 import javax.swing.text.AbstractDocument JavaDoc;
47 import javax.swing.text.AttributeSet JavaDoc;
48 import javax.swing.text.BadLocationException JavaDoc;
49 import javax.swing.text.DocumentFilter JavaDoc;
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 /**
57  * A dialog for configurating the use of a proxy server.
58  */

59 public class ProxyConfigurationDialog extends JDialog JavaDoc
60 implements ActionListener JavaDoc, DocumentListener JavaDoc {
61     
62     private static final String JavaDoc 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 JavaDoc directConnectionRadioButton;
68     protected JRadioButton JavaDoc useProxyRadioButton;
69     protected JLabel JavaDoc proxyHostLabel;
70     protected JTextField JavaDoc proxyHostField;
71     protected JLabel JavaDoc proxyPortLabel;
72     protected JTextField JavaDoc proxyPortField;
73     protected JButton JavaDoc okButton;
74     
75     /**
76      * Creates a new dialog.
77      */

78     public ProxyConfigurationDialog(JFrame JavaDoc parent) {
79         super(parent, GlobalResourceLoader.getString(
80                 RESOURCE_PATH, "proxy", "title"), true);
81     }
82     
83     /**
84      * Creates a new dialog.
85      */

86     public ProxyConfigurationDialog(JDialog JavaDoc 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 JavaDoc() {
94             public void windowClosing(WindowEvent JavaDoc e) {
95                 status = CANCEL_OPTION;
96             }
97         });
98         JPanel JavaDoc contentPane = (JPanel JavaDoc)getContentPane();
99         contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
100         JPanel JavaDoc centerPanel = new JPanel JavaDoc();
101         centerPanel.setLayout(new BoxLayout JavaDoc(centerPanel, BoxLayout.Y_AXIS));
102         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
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 JavaDoc proxyDataPanel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
120         proxyDataPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
121         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
122         proxyHostLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
123                 RESOURCE_PATH, "proxy", "host"));
124         c.anchor = GridBagConstraints.WEST;
125         c.insets = new Insets JavaDoc(2, 10, 2, 0);
126         proxyDataPanel.add(proxyHostLabel, c);
127         proxyHostField = new JTextField JavaDoc(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 JavaDoc(5);
137         ((AbstractDocument JavaDoc)proxyPortField.getDocument()).setDocumentFilter(
138                 new DocumentFilter JavaDoc() {
139                     public void insertString(DocumentFilter.FilterBypass JavaDoc fb,
140                                 int offset, String JavaDoc string, AttributeSet JavaDoc attr)
141                                 throws BadLocationException JavaDoc {
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 JavaDoc fb,
150                             int offset, int length, String JavaDoc string, AttributeSet JavaDoc attr)
151                             throws BadLocationException JavaDoc {
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 JavaDoc 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 JavaDoc southPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
176         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(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 JavaDoc 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     /**
197      * Shows the dialog and returns an integer indicating how the dialog
198      * was closed.
199      */

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     /**
212      * Returns the host name from the text field.
213      */

214     public String JavaDoc getProxyHost() {
215         String JavaDoc text = proxyHostField.getText().trim();
216         return text.length() == 0 ? null : text;
217     }
218     
219     /**
220      * Sets the value of the host name text field.
221      */

222     public void setProxyHost(String JavaDoc host) {
223         proxyHostField.setText(host);
224     }
225     
226     /**
227      * Returns the port number from the text field.
228      */

229     public int getProxyPort() {
230         String JavaDoc text = proxyPortField.getText();
231         return text.length() == 0 ? -1 : Integer.parseInt(text);
232     }
233     
234     /**
235      * Sets the value of the port number text field.
236      */

237     public void setProxyPort(int port) {
238         if (port > 0) {
239             proxyPortField.setText(Integer.toString(port));
240         }
241     }
242     
243     /**
244      * Updates the enabled state of the components depending on the selection.
245      */

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 JavaDoc e) {
261         String JavaDoc 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 JavaDoc e) {
278         updateOkButton();
279     }
280     
281     public void removeUpdate(DocumentEvent JavaDoc e) {
282         updateOkButton();
283     }
284     
285     public void changedUpdate(DocumentEvent JavaDoc e) {}
286 }
287
Popular Tags