001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.gui.config;
018:
019: import java.awt.BorderLayout;
020: import java.awt.GridBagConstraints;
021: import java.awt.GridBagLayout;
022: import java.awt.GridLayout;
023: import java.awt.Insets;
024: import java.awt.Toolkit;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.KeyEvent;
028: import java.awt.event.WindowAdapter;
029: import java.awt.event.WindowEvent;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.Box;
033: import javax.swing.BoxLayout;
034: import javax.swing.ButtonGroup;
035: import javax.swing.JButton;
036: import javax.swing.JComponent;
037: import javax.swing.JDialog;
038: import javax.swing.JFrame;
039: import javax.swing.JLabel;
040: import javax.swing.JPanel;
041: import javax.swing.JRadioButton;
042: import javax.swing.JTextField;
043: import javax.swing.KeyStroke;
044: import javax.swing.event.DocumentEvent;
045: import javax.swing.event.DocumentListener;
046: import javax.swing.text.AbstractDocument;
047: import javax.swing.text.AttributeSet;
048: import javax.swing.text.BadLocationException;
049: import javax.swing.text.DocumentFilter;
050:
051: import org.columba.core.gui.base.ButtonWithMnemonic;
052: import org.columba.core.gui.base.LabelWithMnemonic;
053: import org.columba.core.gui.base.RadioButtonWithMnemonic;
054: import org.columba.core.resourceloader.GlobalResourceLoader;
055:
056: /**
057: * A dialog for configurating the use of a proxy server.
058: */
059: public class ProxyConfigurationDialog extends JDialog implements
060: ActionListener, DocumentListener {
061:
062: private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog";
063: public static final int CANCEL_OPTION = 0;
064: public static final int APPROVE_OPTION = 1;
065:
066: protected int status = CANCEL_OPTION;
067: protected JRadioButton directConnectionRadioButton;
068: protected JRadioButton useProxyRadioButton;
069: protected JLabel proxyHostLabel;
070: protected JTextField proxyHostField;
071: protected JLabel proxyPortLabel;
072: protected JTextField proxyPortField;
073: protected JButton okButton;
074:
075: /**
076: * Creates a new dialog.
077: */
078: public ProxyConfigurationDialog(JFrame parent) {
079: super (parent, GlobalResourceLoader.getString(RESOURCE_PATH,
080: "proxy", "title"), true);
081: }
082:
083: /**
084: * Creates a new dialog.
085: */
086: public ProxyConfigurationDialog(JDialog parent) {
087: super (parent, GlobalResourceLoader.getString(RESOURCE_PATH,
088: "proxy", "title"), true);
089: }
090:
091: protected void dialogInit() {
092: super .dialogInit();
093: addWindowListener(new WindowAdapter() {
094: public void windowClosing(WindowEvent e) {
095: status = CANCEL_OPTION;
096: }
097: });
098: JPanel contentPane = (JPanel) getContentPane();
099: contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12,
100: 11, 11));
101: JPanel centerPanel = new JPanel();
102: centerPanel.setLayout(new BoxLayout(centerPanel,
103: BoxLayout.Y_AXIS));
104: ButtonGroup group = new ButtonGroup();
105: directConnectionRadioButton = new RadioButtonWithMnemonic(
106: GlobalResourceLoader.getString(RESOURCE_PATH, "proxy",
107: "directConnection"));
108: directConnectionRadioButton.setSelected(true);
109: directConnectionRadioButton.setActionCommand("USE_PROXY");
110: directConnectionRadioButton.addActionListener(this );
111: group.add(directConnectionRadioButton);
112: centerPanel.add(directConnectionRadioButton);
113: centerPanel.add(Box.createVerticalStrut(5));
114: useProxyRadioButton = new RadioButtonWithMnemonic(
115: GlobalResourceLoader.getString(RESOURCE_PATH, "proxy",
116: "useProxy"));
117: useProxyRadioButton.setActionCommand("USE_PROXY");
118: useProxyRadioButton.addActionListener(this );
119: group.add(useProxyRadioButton);
120: centerPanel.add(useProxyRadioButton);
121: JPanel proxyDataPanel = new JPanel(new GridBagLayout());
122: proxyDataPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
123: GridBagConstraints c = new GridBagConstraints();
124: proxyHostLabel = new LabelWithMnemonic(GlobalResourceLoader
125: .getString(RESOURCE_PATH, "proxy", "host"));
126: c.anchor = GridBagConstraints.WEST;
127: c.insets = new Insets(2, 10, 2, 0);
128: proxyDataPanel.add(proxyHostLabel, c);
129: proxyHostField = new JTextField(10);
130: proxyHostField.getDocument().addDocumentListener(this );
131: proxyHostLabel.setLabelFor(proxyHostField);
132: c.gridwidth = GridBagConstraints.REMAINDER;
133: proxyDataPanel.add(proxyHostField, c);
134: proxyPortLabel = new LabelWithMnemonic(GlobalResourceLoader
135: .getString(RESOURCE_PATH, "proxy", "port"));
136: c.gridwidth = 1;
137: proxyDataPanel.add(proxyPortLabel, c);
138: proxyPortField = new JTextField(5);
139: ((AbstractDocument) proxyPortField.getDocument())
140: .setDocumentFilter(new DocumentFilter() {
141: public void insertString(
142: DocumentFilter.FilterBypass fb, int offset,
143: String string, AttributeSet attr)
144: throws BadLocationException {
145: if (ensureDigits(string)) {
146: super
147: .insertString(fb, offset, string,
148: attr);
149: } else {
150: Toolkit.getDefaultToolkit().beep();
151: }
152: }
153:
154: public void replace(DocumentFilter.FilterBypass fb,
155: int offset, int length, String string,
156: AttributeSet attr)
157: throws BadLocationException {
158: if (ensureDigits(string)) {
159: super .replace(fb, offset, length, string,
160: attr);
161: } else {
162: Toolkit.getDefaultToolkit().beep();
163: }
164: }
165:
166: private boolean ensureDigits(String string) {
167: for (int i = 0; i < string.length(); i++) {
168: if (!Character.isDigit(string.charAt(i))) {
169: return false;
170: }
171: }
172: return true;
173: }
174: });
175: proxyPortField.getDocument().addDocumentListener(this );
176: proxyPortLabel.setLabelFor(proxyPortField);
177: c.gridwidth = GridBagConstraints.REMAINDER;
178: proxyDataPanel.add(proxyPortField, c);
179: centerPanel.add(proxyDataPanel);
180: centerPanel.add(Box.createVerticalGlue());
181: contentPane.add(centerPanel);
182: JPanel southPanel = new JPanel(new BorderLayout());
183: JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 0));
184: buttonPanel.setBorder(BorderFactory.createEmptyBorder(17, 0, 0,
185: 0));
186: okButton = new ButtonWithMnemonic(GlobalResourceLoader
187: .getString("", "global", "ok"));
188: okButton.addActionListener(this );
189: buttonPanel.add(okButton);
190: JButton cancelButton = new ButtonWithMnemonic(
191: GlobalResourceLoader.getString("", "global", "cancel"));
192: cancelButton.addActionListener(this );
193: buttonPanel.add(cancelButton);
194: southPanel.add(buttonPanel, BorderLayout.EAST);
195: contentPane.add(southPanel, BorderLayout.SOUTH);
196: getRootPane().setDefaultButton(okButton);
197: getRootPane().registerKeyboardAction(this , "CANCEL",
198: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
199: JComponent.WHEN_IN_FOCUSED_WINDOW);
200: pack();
201: setLocationRelativeTo(null);
202: }
203:
204: /**
205: * Shows the dialog and returns an integer indicating how the dialog
206: * was closed.
207: */
208: public int showDialog() {
209: if (getProxyHost() != null && getProxyPort() > 0) {
210: useProxyRadioButton.setSelected(true);
211: } else {
212: directConnectionRadioButton.setSelected(true);
213: }
214: updateProxyDataPanel();
215: setVisible(true);
216: return status;
217: }
218:
219: /**
220: * Returns the host name from the text field.
221: */
222: public String getProxyHost() {
223: String text = proxyHostField.getText().trim();
224: return text.length() == 0 ? null : text;
225: }
226:
227: /**
228: * Sets the value of the host name text field.
229: */
230: public void setProxyHost(String host) {
231: proxyHostField.setText(host);
232: }
233:
234: /**
235: * Returns the port number from the text field.
236: */
237: public int getProxyPort() {
238: String text = proxyPortField.getText();
239: return text.length() == 0 ? -1 : Integer.parseInt(text);
240: }
241:
242: /**
243: * Sets the value of the port number text field.
244: */
245: public void setProxyPort(int port) {
246: if (port > 0) {
247: proxyPortField.setText(Integer.toString(port));
248: }
249: }
250:
251: /**
252: * Updates the enabled state of the components depending on the selection.
253: */
254: protected void updateProxyDataPanel() {
255: boolean enabled = useProxyRadioButton.isSelected();
256: proxyHostLabel.setEnabled(enabled);
257: proxyHostField.setEnabled(enabled);
258: proxyPortLabel.setEnabled(enabled);
259: proxyPortField.setEnabled(enabled);
260: updateOkButton();
261: }
262:
263: protected void updateOkButton() {
264: okButton.setEnabled(directConnectionRadioButton.isSelected()
265: || (getProxyHost() != null && getProxyPort() > 0));
266: }
267:
268: public void actionPerformed(ActionEvent e) {
269: String command = e.getActionCommand();
270: if ("USE_PROXY".equals(command)) {
271: updateProxyDataPanel();
272: } else if ("OK".equals(command)) {
273: status = APPROVE_OPTION;
274: if (directConnectionRadioButton.isSelected()) {
275: proxyHostField.setText(null);
276: proxyPortField.setText(null);
277: }
278: setVisible(false);
279: } else {
280: status = CANCEL_OPTION;
281: setVisible(false);
282: }
283: }
284:
285: public void insertUpdate(DocumentEvent e) {
286: updateOkButton();
287: }
288:
289: public void removeUpdate(DocumentEvent e) {
290: updateOkButton();
291: }
292:
293: public void changedUpdate(DocumentEvent e) {
294: }
295: }
|