1 | package fr.mael.jiwigo.ui; |
---|
2 | |
---|
3 | import java.awt.Dimension; |
---|
4 | import java.awt.FlowLayout; |
---|
5 | import java.awt.GridBagConstraints; |
---|
6 | import java.awt.GridBagLayout; |
---|
7 | import java.awt.Insets; |
---|
8 | import java.awt.event.ItemEvent; |
---|
9 | import java.awt.event.ItemListener; |
---|
10 | |
---|
11 | import javax.swing.JCheckBox; |
---|
12 | import javax.swing.JLabel; |
---|
13 | import javax.swing.JPanel; |
---|
14 | import javax.swing.JPasswordField; |
---|
15 | import javax.swing.JTextField; |
---|
16 | |
---|
17 | import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum; |
---|
18 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
19 | import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; |
---|
20 | |
---|
21 | /** |
---|
22 | Copyright (c) 2010, Mael |
---|
23 | All rights reserved. |
---|
24 | |
---|
25 | Redistribution and use in source and binary forms, with or without |
---|
26 | modification, are permitted provided that the following conditions are met: |
---|
27 | * Redistributions of source code must retain the above copyright |
---|
28 | notice, this list of conditions and the following disclaimer. |
---|
29 | * Redistributions in binary form must reproduce the above copyright |
---|
30 | notice, this list of conditions and the following disclaimer in the |
---|
31 | documentation and/or other materials provided with the distribution. |
---|
32 | * Neither the name of jiwigo nor the |
---|
33 | names of its contributors may be used to endorse or promote products |
---|
34 | derived from this software without specific prior written permission. |
---|
35 | |
---|
36 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
37 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
38 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
39 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
40 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
41 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
42 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
43 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
44 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
45 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
46 | |
---|
47 | * @author mael |
---|
48 | * |
---|
49 | */ |
---|
50 | public class ProxyPanel extends JPanel { |
---|
51 | |
---|
52 | private JPanel panel; |
---|
53 | private HeaderPanel headerPanel_; |
---|
54 | private JTextField fieldUrl = new JTextField(); |
---|
55 | private JLabel labelUrl = new JLabel(); |
---|
56 | private JTextField fieldPort = new JTextField(); |
---|
57 | private JTextField fieldLogin = new JTextField(); |
---|
58 | private JLabel labelCredentials = new JLabel(); |
---|
59 | private JPasswordField fieldPassword = new JPasswordField(); |
---|
60 | private ConnexionDialog connectionDialog; |
---|
61 | private JCheckBox checkBox; |
---|
62 | private boolean useProxy = false; |
---|
63 | |
---|
64 | public ProxyPanel(ConnexionDialog connexionDialog) { |
---|
65 | super(new GridBagLayout()); |
---|
66 | |
---|
67 | String urlProxy = PreferencesManagement.getValue(PreferencesEnum.URL_PROXY.getLabel()); |
---|
68 | String portProxy = PreferencesManagement.getValue(PreferencesEnum.PORT_PROXY.getLabel()); |
---|
69 | String loginProxy = PreferencesManagement.getValue(PreferencesEnum.USER_PROXY.getLabel()); |
---|
70 | String passProxy = PreferencesManagement.getValue(PreferencesEnum.PASS_PROXY.getLabel()); |
---|
71 | fieldUrl.setText(urlProxy); |
---|
72 | fieldPort.setText(portProxy); |
---|
73 | fieldLogin.setText(loginProxy); |
---|
74 | fieldPassword.setText(passProxy); |
---|
75 | |
---|
76 | this.connectionDialog = connexionDialog; |
---|
77 | labelUrl.setText(Messages.getMessage("proxyPanel_address")); |
---|
78 | labelCredentials.setText(Messages.getMessage("proxyPanel_credentials")); |
---|
79 | panel = new JPanel(new GridBagLayout()); |
---|
80 | GridBagConstraints c = new GridBagConstraints(); |
---|
81 | c.gridx = 0; |
---|
82 | c.gridy = 0; |
---|
83 | c.insets = new Insets(4, 4, 4, 4); |
---|
84 | panel.add(labelUrl, c); |
---|
85 | c.gridx++; |
---|
86 | fieldUrl.setPreferredSize(new Dimension(150, 30)); |
---|
87 | panel.add(fieldUrl, c); |
---|
88 | c.gridx++; |
---|
89 | fieldPort.setPreferredSize(new Dimension(150, 30)); |
---|
90 | panel.add(fieldPort, c); |
---|
91 | |
---|
92 | c.gridy++; |
---|
93 | c.gridx = 0; |
---|
94 | panel.add(labelCredentials, c); |
---|
95 | c.gridx++; |
---|
96 | fieldLogin.setPreferredSize(new Dimension(150, 30)); |
---|
97 | panel.add(fieldLogin, c); |
---|
98 | c.gridx++; |
---|
99 | fieldPassword.setPreferredSize(new Dimension(150, 30)); |
---|
100 | panel.add(fieldPassword, c); |
---|
101 | |
---|
102 | GridBagConstraints gbc = new GridBagConstraints(); |
---|
103 | gbc.insets = new Insets(1, 3, 0, 3); |
---|
104 | gbc.weightx = 1.0; |
---|
105 | gbc.fill = gbc.HORIZONTAL; |
---|
106 | gbc.gridwidth = gbc.REMAINDER; |
---|
107 | |
---|
108 | try { |
---|
109 | useProxy = Boolean.valueOf(PreferencesManagement.getValue(PreferencesEnum.USE_PROXY.getLabel())); |
---|
110 | } catch (Exception e) { |
---|
111 | useProxy = false; |
---|
112 | } |
---|
113 | |
---|
114 | headerPanel_ = new HeaderPanel(Messages.getMessage("proxyPanel_useProxy")); |
---|
115 | |
---|
116 | add(headerPanel_, gbc); |
---|
117 | add(panel, gbc); |
---|
118 | panel.setVisible(false); |
---|
119 | |
---|
120 | JLabel padding = new JLabel(); |
---|
121 | gbc.weighty = 1.0; |
---|
122 | add(padding, gbc); |
---|
123 | if (useProxy) { |
---|
124 | checkBox.setSelected(true); |
---|
125 | toggleSelection(); |
---|
126 | } |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | public void toggleSelection() { |
---|
131 | |
---|
132 | panel.setVisible(!panel.isShowing()); |
---|
133 | |
---|
134 | validate(); |
---|
135 | |
---|
136 | headerPanel_.repaint(); |
---|
137 | connectionDialog.pack(); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * @return the fieldUrl |
---|
142 | */ |
---|
143 | public JTextField getFieldUrl() { |
---|
144 | return fieldUrl; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * @return the fieldPort |
---|
149 | */ |
---|
150 | public JTextField getFieldPort() { |
---|
151 | return fieldPort; |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * @return the fieldLogin |
---|
156 | */ |
---|
157 | public JTextField getFieldLogin() { |
---|
158 | return fieldLogin; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * @return the fieldPassword |
---|
163 | */ |
---|
164 | public JPasswordField getFieldPassword() { |
---|
165 | return fieldPassword; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * @return the checkBox |
---|
170 | */ |
---|
171 | public JCheckBox getCheckBox() { |
---|
172 | return checkBox; |
---|
173 | } |
---|
174 | |
---|
175 | private class HeaderPanel extends JPanel implements ItemListener { |
---|
176 | |
---|
177 | public HeaderPanel(String text) { |
---|
178 | |
---|
179 | setPreferredSize(new Dimension(200, 30)); |
---|
180 | this.setLayout(new FlowLayout(FlowLayout.LEFT)); |
---|
181 | checkBox = new JCheckBox(text); |
---|
182 | checkBox.setSelected(useProxy); |
---|
183 | checkBox.addItemListener(this); |
---|
184 | this.add(checkBox); |
---|
185 | } |
---|
186 | |
---|
187 | @Override |
---|
188 | public void itemStateChanged(ItemEvent arg0) { |
---|
189 | toggleSelection(); |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | } |
---|