aboutsummaryrefslogtreecommitdiffstats
path: root/netx/net/sourceforge/jnlp/controlpanel/NetworkSettingsPanel.java
blob: b1a6f95d8a9f69ca3b4da0be66806d7e6a8d50c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* NetworkSettingsPanel.java -- Sets proxy settings for network.
Copyright (C) 2010 Red Hat

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package net.sourceforge.jnlp.controlpanel;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import net.sourceforge.jnlp.config.DeploymentConfiguration;
import net.sourceforge.jnlp.runtime.Translator;

/**
 * This is the pane used with creating a JDialog version. This allows changing
 * the network configuration: Proxy
 * 
 * @author Andrew Su (asu@redhat.com, andrew.su@utoronto.ca)
 * 
 */
@SuppressWarnings("serial")
public class NetworkSettingsPanel extends JPanel implements ActionListener {

    private DeploymentConfiguration config;

    private JPanel description;
    private ArrayList<JPanel> proxyPanels = new ArrayList<JPanel>(); // The stuff with editable fields

    /** List of properties used by this panel */
    public static String[] properties = { "deployment.proxy.type",
            "deployment.proxy.http.host",
            "deployment.proxy.http.port",
            "deployment.proxy.bypass.local",
            "deployment.proxy.auto.config.url", };

    /**
     * Creates a new instance of the network settings panel.
     * 
     * @param config
     *            Loaded DeploymentConfiguration file.
     */
    public NetworkSettingsPanel(DeploymentConfiguration config) {
        super();
        this.config = config;
        setLayout(new BorderLayout());

        addComponents();
    }

    /**
     * This adds the components to the panel.
     */
    protected void addComponents() {
        JPanel settingPanel = new NamedBorderPanel(Translator.R("CPHeadNetworkSettings"));
        settingPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;

        JLabel networkDesc = new JLabel("<html>" + Translator.R("CPNetworkSettingsDescription") + "<hr /></html>");

        JLabel[] description = { new JLabel("<html>" + Translator.R("NSDescription-1") + "</html>"),
                new JLabel("<html>" + Translator.R("NSDescription0") + "</html>"),
                new JLabel("<html>" + Translator.R("NSDescription1") + "</html>"),
                new JLabel("<html>" + Translator.R("NSDescription2") + "</html>"),
                new JLabel("<html>" + Translator.R("NSDescription3") + "</html>") };

        this.description = new JPanel(new CardLayout());
        for (int i = 0; i < description.length; i++)
            this.description.add(description[i], String.valueOf(i - 1));

        // Settings for selecting Proxy Server
        JPanel proxyServerPanel = new JPanel(new BorderLayout());
        JPanel proxyLocationPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel proxyBypassPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));

        JLabel addressLabel = new JLabel(Translator.R("NSAddress") + ":");
        JLabel portLabel = new JLabel(Translator.R("NSPort") + ":");
        final JTextField addressField = new JTextField(config.getProperty(properties[1]), 10);
        addressField.getDocument().addDocumentListener(new DocumentAdapter(config, properties[1]));

        final JTextField portField = new JTextField(config.getProperty(properties[2]), 3);
        portField.getDocument().addDocumentListener(new DocumentAdapter(config, properties[1]));

        // Create the button which allows setting of other types of proxy.
        JButton advancedProxyButton = new JButton(Translator.R("NSAdvanced") + "...");
        advancedProxyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdvancedProxySettingsDialog.showAdvancedProxySettingsDialog(config);
                addressField.setText(config.getProperty(properties[1]));
                portField.setText(config.getProperty(properties[2]));
            }
        });

        JCheckBox bypassCheckBox = new JCheckBox(Translator.R("NSBypassLocal"), Boolean.parseBoolean(config.getProperty(properties[3])));
        bypassCheckBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                config.setProperty(properties[3], String.valueOf(e.getStateChange() == ItemEvent.SELECTED));
            }
        });
        proxyLocationPanel.add(Box.createRigidArea(new Dimension(13, 0)));
        proxyLocationPanel.add(addressLabel);
        proxyLocationPanel.add(addressField);
        proxyLocationPanel.add(portLabel);
        proxyLocationPanel.add(portField);
        proxyLocationPanel.add(advancedProxyButton);
        proxyBypassPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        proxyBypassPanel.add(bypassCheckBox);

        proxyServerPanel.add(proxyLocationPanel, BorderLayout.CENTER);
        proxyServerPanel.add(proxyBypassPanel, BorderLayout.SOUTH);

        JRadioButton directConnection = new JRadioButton(Translator.R("NSDirectConnection"), config.getProperty(properties[0]).equals("0"));
        directConnection.setActionCommand("0");
        directConnection.addActionListener(this);

        JRadioButton useProxyServer = new JRadioButton(Translator.R("NSManualProxy"), config.getProperty(properties[0]).equals("1"));
        useProxyServer.setActionCommand("1");
        useProxyServer.addActionListener(this);

        JRadioButton useAutoProxyConfigScript = new JRadioButton(Translator.R("NSAutoProxy"), config.getProperty(properties[0]).equals("2"));
        useAutoProxyConfigScript.setActionCommand("2");
        useAutoProxyConfigScript.addActionListener(this);

        JRadioButton useBrowserSettings = new JRadioButton(Translator.R("NSBrowserProxy"), config.getProperty(properties[0]).equals("3"));
        useBrowserSettings.setActionCommand("3");
        useBrowserSettings.addActionListener(this);

        ButtonGroup modeSelect = new ButtonGroup();
        modeSelect.add(useBrowserSettings);
        modeSelect.add(useProxyServer);
        modeSelect.add(useAutoProxyConfigScript);
        modeSelect.add(directConnection);

        // Settings for Automatic Proxy Configuration Script
        JPanel proxyAutoPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JLabel locationLabel = new JLabel(Translator.R("NSScriptLocation") + ":");
        final JTextField locationField = new JTextField(config.getProperty(properties[4]), 20);
        locationField.getDocument().addDocumentListener(new DocumentAdapter(config, properties[4]));

        proxyAutoPanel.add(Box.createRigidArea(new Dimension(13, 0)));
        proxyAutoPanel.add(locationLabel);
        proxyAutoPanel.add(locationField);

        c.gridy = 0;
        settingPanel.add(networkDesc, c);
        c.gridy = 1;
        settingPanel.add(this.description, c);
        c.gridy = 2;
        settingPanel.add(directConnection, c);
        c.gridy = 3;
        settingPanel.add(useBrowserSettings, c);
        c.gridy = 4;
        settingPanel.add(useProxyServer, c);
        c.gridy = 5;
        settingPanel.add(proxyServerPanel, c);
        proxyPanels.add(proxyServerPanel);
        c.gridy = 6;
        settingPanel.add(useAutoProxyConfigScript, c);
        c.gridy = 7;
        settingPanel.add(proxyAutoPanel, c);
        proxyPanels.add(proxyAutoPanel);

        // Filler to pack the bottom of the panel.
        Component filler = Box.createRigidArea(new Dimension(1, 1));
        c.gridy++;
        c.weighty = 1;
        settingPanel.add(filler, c);

        setState(); // depending on default setting we will enable or disable

        add(settingPanel, BorderLayout.CENTER);

    }

    /**
     * Enable/Disable the panel and all its children recursively.
     * 
     * @param panel
     *            JPanel which needs to be enabled or disabled.
     * @param enable
     *            true if the panel and its children are to be enabled, false
     *            otherwise.
     */
    private void enablePanel(JPanel panel, boolean enable) {
        // This will be used to enable all components in this panel recursively.
        // Ridiculously slow if lots of nested panels.
        for (Component c : panel.getComponents()) {
            if (c instanceof JPanel) {
                enablePanel((JPanel) c, enable);
            }
            c.setEnabled(enable);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        config.setProperty(properties[0], e.getActionCommand());
        setState();
    }

    /**
     * This enables and disables the appropriate panels.
     */
    private void setState() {
        ((CardLayout) this.description.getLayout()).show(this.description, config.getProperty(properties[0]));
        if (config.getProperty(properties[0]).equals("0")) {
            for (JPanel panel : proxyPanels)
                enablePanel(panel, false);
        } else if (config.getProperty(properties[0]).equals("1")) {
            enablePanel(proxyPanels.get(1), false);
            enablePanel(proxyPanels.get(0), true);
        } else if (config.getProperty(properties[0]).equals("2")) {
            enablePanel(proxyPanels.get(0), false);
            enablePanel(proxyPanels.get(1), true);
        } else if (config.getProperty(properties[0]).equals("3")) {
            for (JPanel panel : proxyPanels)
                enablePanel(panel, false);
        }
    }
}