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 setLayout(new VGroupLayout());
44 setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
45 add(new SaneLabelField(get("m.configure_proxy")));
46 add(new Spacer(5, 5));
48 JPanel row = new JPanel(new GridLayout());
49 row.add(new SaneLabelField(get("m.proxy_host")), BorderLayout.WEST);
50 row.add(_host = new SaneTextField());
53 row = new JPanel(new GridLayout());
54 row.add(new SaneLabelField(get("m.proxy_port")), BorderLayout.WEST);
55 row.add(_port = new SaneTextField());
58 add(new Spacer(5, 5));
60 row = new JPanel(new GridLayout());
61 row.add(new SaneLabelField(get("m.proxy_auth_required")), BorderLayout.WEST);
62 _useAuth = new JCheckBox();
66 row = new JPanel(new GridLayout());
67 row.add(new SaneLabelField(get("m.proxy_username")), BorderLayout.WEST);
68 _username = new SaneTextField();
69 _username.setEnabled(false);
73 row = new JPanel(new GridLayout());
74 row.add(new SaneLabelField(get("m.proxy_password")), BorderLayout.WEST);
75 _password = new SanePasswordField();
76 _password.setEnabled(false);
80 _useAuth.addItemListener(new ItemListener() {
81 @Override public void itemStateChanged (ItemEvent event) {
82 boolean selected = (event.getStateChange() == ItemEvent.SELECTED);
83 _username.setEnabled(selected);
84 _password.setEnabled(selected);
88 add(new Spacer(5, 5));
90 row = GroupLayout.makeButtonBox(GroupLayout.CENTER);
92 row.add(button = new JButton(get("m.proxy_ok")));
93 button.setActionCommand("ok");
94 button.addActionListener(this);
95 row.add(button = new JButton(get("m.proxy_cancel")));
96 button.setActionCommand("cancel");
97 button.addActionListener(this);
101 public void setProxy (String host, String port) {
110 // documentation inherited
112 public void addNotify ()
115 _host.requestFocusInWindow();
118 // documentation inherited
120 public Dimension getPreferredSize ()
122 // this is annoyingly hardcoded, but we can't just force the width
123 // or the JLabel will claim a bogus height thinking it can lay its
124 // text out all on one line which will booch the whole UI's
126 return new Dimension(500, 320);
129 // documentation inherited from interface
131 public void actionPerformed (ActionEvent e)
133 String cmd = e.getActionCommand();
134 if (cmd.equals("ok")) {
135 String user = null, pass = null;
136 if (_useAuth.isSelected()) {
137 user = _username.getText();
138 // we have to keep the proxy password around for every HTTP request, so having it
139 // in a char[] that gets zeroed out after use is not viable for this use case
140 pass = new String(_password.getPassword());
142 _getdown.configProxy(_host.getText(), _port.getText(), user, pass);
144 // they canceled, we're outta here
149 /** Used to look up localized messages. */
150 protected String get (String key)
152 // if this string is tainted, we don't translate it, instead we
153 // simply remove the taint character and return it to the caller
154 if (MessageUtil.isTainted(key)) {
155 return MessageUtil.untaint(key);
158 return _msgs.getString(key);
159 } catch (MissingResourceException mre) {
160 log.warning("Missing translation message '" + key + "'.");
165 protected static class SaneLabelField extends JLabel {
166 public SaneLabelField(String message) { super(message); }
167 @Override public Dimension getPreferredSize () {
168 return clampWidth(super.getPreferredSize(), 200);
171 protected static class SaneTextField extends JTextField {
172 @Override public Dimension getPreferredSize () {
173 return clampWidth(super.getPreferredSize(), 150);
176 protected static class SanePasswordField extends JPasswordField {
177 @Override public Dimension getPreferredSize () {
178 return clampWidth(super.getPreferredSize(), 150);
182 protected static Dimension clampWidth (Dimension dim, int minWidth) {
183 dim.width = Math.max(dim.width, minWidth);
187 protected Getdown _getdown;
188 protected ResourceBundle _msgs;
190 protected JTextField _host;
191 protected JTextField _port;
192 protected JCheckBox _useAuth;
193 protected JTextField _username;
194 protected JPasswordField _password;