Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / getdown / src / getdown / launcher / src / main / java / com / threerings / getdown / launcher / ProxyPanel.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.launcher;
7
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;
17
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;
25
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;
31
32 /**
33  * Displays an interface with which the user can configure their proxy
34  * settings.
35  */
36 public final class ProxyPanel extends JPanel implements ActionListener
37 {
38     public ProxyPanel (Getdown getdown, ResourceBundle msgs)
39     {
40         _getdown = getdown;
41         _msgs = msgs;
42
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];
48
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));
53
54         JPanel row = new JPanel(new GridLayout());
55         row.add(new SaneLabelField(get("m.proxy_host")), BorderLayout.WEST);
56         row.add(_host = new SaneTextField());
57         _host.setText(host);
58         add(row);
59
60         row = new JPanel(new GridLayout());
61         row.add(new SaneLabelField(get("m.proxy_port")), BorderLayout.WEST);
62         row.add(_port = new SaneTextField());
63         _port.setText(port);
64         add(row);
65
66         add(new Spacer(5, 5));
67
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);
72         row.add(_useAuth);
73         add(row);
74
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());
80         row.add(_username);
81         add(row);
82
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());
87         row.add(_password);
88         add(row);
89
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);
95             }
96         });
97
98         add(new Spacer(5, 5));
99
100         row = GroupLayout.makeButtonBox(GroupLayout.CENTER);
101         JButton button;
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);
108         add(row);
109     }
110
111     public void setProxy (String host, String port) {
112         if (host != null) {
113             _host.setText(host);
114         }
115         if (port != null) {
116             _port.setText(port);
117         }
118     }
119
120     // documentation inherited
121     @Override
122     public void addNotify ()
123     {
124         super.addNotify();
125         _host.requestFocusInWindow();
126     }
127
128     // documentation inherited
129     @Override
130     public Dimension getPreferredSize ()
131     {
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
135         // preferred size
136         return new Dimension(500, 320);
137     }
138
139     // documentation inherited from interface
140     @Override
141     public void actionPerformed (ActionEvent e)
142     {
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());
151             }
152             _getdown.configProxy(_host.getText(), _port.getText(), user, pass);
153         } else {
154             // they canceled, we're outta here
155             System.exit(0);
156         }
157     }
158
159     /** Used to look up localized messages. */
160     protected String get (String key)
161     {
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);
166         }
167         try {
168             return _msgs.getString(key);
169         } catch (MissingResourceException mre) {
170             log.warning("Missing translation message '" + key + "'.");
171             return key;
172         }
173     }
174
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);
179         }
180     }
181     protected static class SaneTextField extends JTextField {
182         @Override public Dimension getPreferredSize () {
183             return clampWidth(super.getPreferredSize(), 150);
184         }
185     }
186     protected static class SanePasswordField extends JPasswordField {
187         @Override public Dimension getPreferredSize () {
188             return clampWidth(super.getPreferredSize(), 150);
189         }
190     }
191
192     protected static Dimension clampWidth (Dimension dim, int minWidth) {
193         dim.width = Math.max(dim.width, minWidth);
194         return dim;
195     }
196
197     protected Getdown _getdown;
198     protected ResourceBundle _msgs;
199
200     protected JTextField _host;
201     protected JTextField _port;
202     protected JCheckBox _useAuth;
203     protected JTextField _username;
204     protected JPasswordField _password;
205 }