JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[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         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));
47
48         JPanel row = new JPanel(new GridLayout());
49         row.add(new SaneLabelField(get("m.proxy_host")), BorderLayout.WEST);
50         row.add(_host = new SaneTextField());
51         add(row);
52
53         row = new JPanel(new GridLayout());
54         row.add(new SaneLabelField(get("m.proxy_port")), BorderLayout.WEST);
55         row.add(_port = new SaneTextField());
56         add(row);
57
58         add(new Spacer(5, 5));
59
60         row = new JPanel(new GridLayout());
61         row.add(new SaneLabelField(get("m.proxy_auth_required")), BorderLayout.WEST);
62         _useAuth = new JCheckBox();
63         row.add(_useAuth);
64         add(row);
65
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);
70         row.add(_username);
71         add(row);
72
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);
77         row.add(_password);
78         add(row);
79
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);
85             }
86         });
87
88         add(new Spacer(5, 5));
89
90         row = GroupLayout.makeButtonBox(GroupLayout.CENTER);
91         JButton button;
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);
98         add(row);
99     }
100
101     public void setProxy (String host, String port) {
102         if (host != null) {
103             _host.setText(host);
104         }
105         if (port != null) {
106             _port.setText(port);
107         }
108     }
109
110     // documentation inherited
111     @Override
112     public void addNotify ()
113     {
114         super.addNotify();
115         _host.requestFocusInWindow();
116     }
117
118     // documentation inherited
119     @Override
120     public Dimension getPreferredSize ()
121     {
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
125         // preferred size
126         return new Dimension(500, 320);
127     }
128
129     // documentation inherited from interface
130     @Override
131     public void actionPerformed (ActionEvent e)
132     {
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());
141             }
142             _getdown.configProxy(_host.getText(), _port.getText(), user, pass);
143         } else {
144             // they canceled, we're outta here
145             System.exit(0);
146         }
147     }
148
149     /** Used to look up localized messages. */
150     protected String get (String key)
151     {
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);
156         }
157         try {
158             return _msgs.getString(key);
159         } catch (MissingResourceException mre) {
160             log.warning("Missing translation message '" + key + "'.");
161             return key;
162         }
163     }
164
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);
169         }
170     }
171     protected static class SaneTextField extends JTextField {
172         @Override public Dimension getPreferredSize () {
173             return clampWidth(super.getPreferredSize(), 150);
174         }
175     }
176     protected static class SanePasswordField extends JPasswordField {
177         @Override public Dimension getPreferredSize () {
178             return clampWidth(super.getPreferredSize(), 150);
179         }
180     }
181
182     protected static Dimension clampWidth (Dimension dim, int minWidth) {
183         dim.width = Math.max(dim.width, minWidth);
184         return dim;
185     }
186
187     protected Getdown _getdown;
188     protected ResourceBundle _msgs;
189
190     protected JTextField _host;
191     protected JTextField _port;
192     protected JCheckBox _useAuth;
193     protected JTextField _username;
194     protected JPasswordField _password;
195 }