View Javadoc

1   /*
2    * Created on Oct 4, 2004 
3    * Original filename was ProxyConfiguration.java
4    * 
5    * Magic-Project is a turn based strategy simulator Copyright (C) 2003-2007 Fabrice
6    * Daugan
7    * 
8    * This program is free software; you can redistribute it and/or modify it under
9    * the terms of the GNU General Public License as published by the Free Software
10   * Foundation; either version 2 of the License, or (at your option) any later
11   * version.
12   * 
13   * This program is distributed in the hope that it will be useful, but WITHOUT
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16   * details.
17   * 
18   * You should have received a copy of the GNU General Public License along with
19   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20   * Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  package net.sf.magicproject.ui.component;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Dimension;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  
29  import javax.swing.BoxLayout;
30  import javax.swing.JButton;
31  import javax.swing.JCheckBox;
32  import javax.swing.JDialog;
33  import javax.swing.JLabel;
34  import javax.swing.JPanel;
35  import javax.swing.JPasswordField;
36  import javax.swing.JTextField;
37  import javax.swing.SwingConstants;
38  import javax.swing.WindowConstants;
39  
40  import net.sf.magicproject.tools.Configuration;
41  import net.sf.magicproject.ui.ToolKit;
42  import net.sf.magicproject.ui.i18n.LanguageManager;
43  
44  import org.mortbay.util.Password;
45  
46  /***
47   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
48   */
49  public class ProxyConfiguration extends JDialog implements ActionListener {
50  
51  	/***
52  	 * Create a new instance of JPrompt
53  	 */
54  	public ProxyConfiguration() {
55  		setModal(true);
56  		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
57  		setTitle(LanguageManager.getString("wiz_proxy.title"));
58  		getContentPane().add(new JPanel(), BorderLayout.NORTH);
59  		getContentPane().add(new JPanel(), BorderLayout.EAST);
60  		getContentPane().add(new JPanel(), BorderLayout.WEST);
61  
62  		JPanel contentPanel = new JPanel();
63  		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
64  
65  		// checkbox
66  		contentPanel.add(useProxy);
67  
68  		// address
69  		JPanel proxyAddressPanel = new JPanel();
70  		proxyAddressPanel.setLayout(new BoxLayout(proxyAddressPanel,
71  				BoxLayout.X_AXIS));
72  		proxyAddressPanel.add(new JLabel(LanguageManager
73  				.getString("wiz_proxy.address")
74  				+ " : ", SwingConstants.RIGHT));
75  		proxyAddressTxt.setMaximumSize(new Dimension(2100, 20));
76  		proxyAddressPanel.add(proxyAddressTxt);
77  		contentPanel.add(proxyAddressPanel);
78  
79  		// port
80  		JPanel proxyPortPanel = new JPanel();
81  		proxyPortPanel.setLayout(new BoxLayout(proxyPortPanel, BoxLayout.X_AXIS));
82  		proxyPortPanel.add(new JLabel(LanguageManager.getString("wiz_proxy.port")
83  				+ " : ", SwingConstants.RIGHT));
84  		proxyPortTxt.setMaximumSize(new Dimension(2100, 20));
85  		proxyPortPanel.add(proxyPortTxt);
86  		contentPanel.add(proxyPortPanel);
87  
88  		// login
89  		JPanel proxyLoginPanel = new JPanel();
90  		proxyLoginPanel.setLayout(new BoxLayout(proxyLoginPanel, BoxLayout.X_AXIS));
91  		proxyLoginPanel.add(new JLabel(LanguageManager
92  				.getString("wiz_proxy.username")
93  				+ " : ", SwingConstants.RIGHT));
94  		proxyLoginTxt.setMaximumSize(new Dimension(2100, 20));
95  		proxyLoginPanel.add(proxyLoginTxt);
96  		contentPanel.add(proxyLoginPanel);
97  
98  		// password
99  		JPanel proxyPasswordPanel = new JPanel();
100 		proxyPasswordPanel.setLayout(new BoxLayout(proxyPasswordPanel,
101 				BoxLayout.X_AXIS));
102 		proxyPasswordPanel.add(new JLabel(LanguageManager
103 				.getString("wiz_proxy.password")
104 				+ " : ", SwingConstants.RIGHT));
105 		proxyPasswordTxt.setMaximumSize(new Dimension(2100, 20));
106 		proxyPasswordPanel.add(proxyPasswordTxt);
107 		contentPanel.add(proxyPasswordPanel);
108 
109 		getContentPane().add(contentPanel, BorderLayout.CENTER);
110 
111 		// buttons
112 		JPanel buttonPanel = new JPanel();
113 		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
114 		pressme.setMnemonic('o'); // associate hotkey
115 		pressme.addActionListener(this); // register button
116 		pressme.setPreferredSize(new Dimension(50, 20));
117 		buttonPanel.add(pressme);
118 		cancel.setMnemonic('c'); // associate hotkey
119 		cancel.addActionListener(this); // register button
120 		cancel.setPreferredSize(new Dimension(50, 20));
121 		ToolKit.addCancelByEscapeKey(this, cancel);
122 		buttonPanel.add(cancel);
123 		setBounds(100, 100, 350, 200); // position frame
124 		pressme.requestFocus();
125 		getRootPane().setDefaultButton(pressme);
126 		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
127 
128 	}
129 
130 	public void actionPerformed(ActionEvent event) {
131 		Object source = event.getSource();
132 		if (source == pressme) {
133 			// save the settings
134 			Configuration.setProperty("proxyObfuscatedLoginPwd", Password
135 					.obfuscate(proxyLoginTxt.getText() + ':'
136 							+ new String(proxyPasswordTxt.getPassword())));
137 			Configuration.setProperty("proxyHost", proxyAddressTxt.getText());
138 			Configuration.setProperty("proxyPort", proxyPortTxt.getText());
139 			Configuration.setProperty("useProxy", this.useProxy.isSelected());
140 			dispose();
141 		} else if (source == cancel) {
142 			dispose();
143 		}
144 	}
145 
146 	private JButton pressme = new JButton(LanguageManager.getString("ok"));
147 
148 	private JButton cancel = new JButton(LanguageManager.getString("cancel"));
149 
150 	private String clearLoginPwd = Password.deobfuscate(Configuration.getString(
151 			"proxyObfuscatedLoginPwd", ""));
152 
153 	private JTextField proxyAddressTxt = new JTextField(Configuration.getString(
154 			"proxyHost", "192.168.0.252"));
155 
156 	private JTextField proxyPortTxt = new JTextField(Configuration.getInt(
157 			"proxyPort", 1299));
158 
159 	private JPasswordField proxyPasswordTxt = new JPasswordField(clearLoginPwd
160 			.substring(clearLoginPwd.indexOf(':') + 1));
161 
162 	private JTextField proxyLoginTxt = new JTextField(clearLoginPwd.substring(0,
163 			clearLoginPwd.indexOf(':')));
164 
165 	/***
166 	 * Proxy checkBox
167 	 */
168 	private JCheckBox useProxy = new JCheckBox(LanguageManager
169 			.getString("wiz_proxy.enable"), Configuration.getBoolean("useProxy",
170 			false));
171 
172 }