2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.launcher;
8 import java.awt.BorderLayout;
9 import java.awt.Dimension;
10 import java.awt.GridLayout;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.event.ItemEvent;
14 import java.awt.event.ItemListener;
15 import java.util.MissingResourceException;
16 import java.util.ResourceBundle;
18 import javax.swing.BorderFactory;
19 import javax.swing.JButton;
20 import javax.swing.JCheckBox;
21 import javax.swing.JLabel;
22 import javax.swing.JPanel;
23 import javax.swing.JPasswordField;
24 import javax.swing.JTextField;
26 import com.samskivert.swing.GroupLayout;
27 import com.samskivert.swing.Spacer;
28 import com.samskivert.swing.VGroupLayout;
29 import com.threerings.getdown.util.MessageUtil;
30 import static com.threerings.getdown.Log.log;
33 * Displays an interface with which the user can configure their proxy
36 public final class ProxyPanel extends JPanel implements ActionListener
38 public ProxyPanel (Getdown getdown, ResourceBundle msgs)
43 String[] hostPortAuthUser = ProxyUtil.jalviewProxyProperties(getdown._app);
44 String host = hostPortAuthUser[0];
45 String port = hostPortAuthUser[1];
46 boolean proxyAuth = Boolean.parseBoolean(hostPortAuthUser[2]);
47 String username = hostPortAuthUser[3];
49 setLayout(new VGroupLayout());
50 setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
51 add(new SaneLabelField(get("m.configure_proxy")));
52 add(new Spacer(5, 5));
54 JPanel row = new JPanel(new GridLayout());
55 row.add(new SaneLabelField(get("m.proxy_host")), BorderLayout.WEST);
56 row.add(_host = new SaneTextField());
60 row = new JPanel(new GridLayout());
61 row.add(new SaneLabelField(get("m.proxy_port")), BorderLayout.WEST);
62 row.add(_port = new SaneTextField());
66 add(new Spacer(5, 5));
68 row = new JPanel(new GridLayout());
69 row.add(new SaneLabelField(get("m.proxy_auth_required")), BorderLayout.WEST);
70 _useAuth = new JCheckBox();
71 _useAuth.setSelected(proxyAuth);
75 row = new JPanel(new GridLayout());
76 row.add(new SaneLabelField(get("m.proxy_username")), BorderLayout.WEST);
77 _username = new SaneTextField();
78 _username.setText(username);
79 _username.setEnabled(_useAuth.isSelected());
83 row = new JPanel(new GridLayout());
84 row.add(new SaneLabelField(get("m.proxy_password")), BorderLayout.WEST);
85 _password = new SanePasswordField();
86 _password.setEnabled(_useAuth.isSelected());
90 _useAuth.addItemListener(new ItemListener() {
91 @Override public void itemStateChanged (ItemEvent event) {
92 boolean selected = (event.getStateChange() == ItemEvent.SELECTED);
93 _username.setEnabled(selected);
94 _password.setEnabled(selected);
98 add(new Spacer(5, 5));
100 row = GroupLayout.makeButtonBox(GroupLayout.CENTER);
102 row.add(button = new JButton(get("m.proxy_ok")));
103 button.setActionCommand("ok");
104 button.addActionListener(this);
105 row.add(button = new JButton(get("m.proxy_cancel")));
106 button.setActionCommand("cancel");
107 button.addActionListener(this);
111 public void setProxy (String host, String port) {
120 // documentation inherited
122 public void addNotify ()
125 _host.requestFocusInWindow();
128 // documentation inherited
130 public Dimension getPreferredSize ()
132 // this is annoyingly hardcoded, but we can't just force the width
133 // or the JLabel will claim a bogus height thinking it can lay its
134 // text out all on one line which will booch the whole UI's
136 return new Dimension(500, 320);
139 // documentation inherited from interface
141 public void actionPerformed (ActionEvent e)
143 String cmd = e.getActionCommand();
144 if (cmd.equals("ok")) {
145 String user = null, pass = null;
146 if (_useAuth.isSelected()) {
147 user = _username.getText();
148 // we have to keep the proxy password around for every HTTP request, so having it
149 // in a char[] that gets zeroed out after use is not viable for this use case
150 pass = new String(_password.getPassword());
152 _getdown.configProxy(_host.getText(), _port.getText(), user, pass);
154 // they canceled, we're outta here
159 /** Used to look up localized messages. */
160 protected String get (String key)
162 // if this string is tainted, we don't translate it, instead we
163 // simply remove the taint character and return it to the caller
164 if (MessageUtil.isTainted(key)) {
165 return MessageUtil.untaint(key);
168 return _msgs.getString(key);
169 } catch (MissingResourceException mre) {
170 log.warning("Missing translation message '" + key + "'.");
175 protected static class SaneLabelField extends JLabel {
176 public SaneLabelField(String message) { super(message); }
177 @Override public Dimension getPreferredSize () {
178 return clampWidth(super.getPreferredSize(), 200);
181 protected static class SaneTextField extends JTextField {
182 @Override public Dimension getPreferredSize () {
183 return clampWidth(super.getPreferredSize(), 150);
186 protected static class SanePasswordField extends JPasswordField {
187 @Override public Dimension getPreferredSize () {
188 return clampWidth(super.getPreferredSize(), 150);
192 protected static Dimension clampWidth (Dimension dim, int minWidth) {
193 dim.width = Math.max(dim.width, minWidth);
197 protected Getdown _getdown;
198 protected ResourceBundle _msgs;
200 protected JTextField _host;
201 protected JTextField _port;
202 protected JCheckBox _useAuth;
203 protected JTextField _username;
204 protected JPasswordField _password;