svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.downloader / org.gvsig.downloader.swing / org.gvsig.downloader.swing.impl / src / main / java / org / gvsig / downloader / swing / impl / config / DownloaderConfigServicePanelImpl.java @ 47845
History | View | Annotate | Download (9.99 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.downloader.swing.impl.config; |
7 |
|
8 |
import java.awt.event.ActionEvent; |
9 |
import java.awt.event.KeyAdapter; |
10 |
import java.awt.event.KeyEvent; |
11 |
import javax.swing.DefaultComboBoxModel; |
12 |
import javax.swing.ImageIcon; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JOptionPane; |
15 |
import javax.swing.SwingUtilities; |
16 |
import javax.swing.event.ChangeListener; |
17 |
import javax.swing.event.DocumentEvent; |
18 |
import javax.swing.event.DocumentListener; |
19 |
import org.apache.commons.io.FilenameUtils; |
20 |
import org.apache.commons.lang3.StringUtils; |
21 |
import org.gvsig.downloader.DownloaderAuthenticationConfig; |
22 |
import org.gvsig.downloader.DownloaderAuthenticationFactory; |
23 |
import org.gvsig.downloader.DownloaderAuthenticationRequester; |
24 |
import org.gvsig.downloader.DownloaderLocator; |
25 |
import org.gvsig.downloader.DownloaderManager; |
26 |
import org.gvsig.downloader.swing.DownloaderConfigServicePanel; |
27 |
import org.gvsig.tools.ToolsLocator; |
28 |
import org.gvsig.tools.i18n.I18nManager; |
29 |
import org.gvsig.tools.lang.CloneableUtils; |
30 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
31 |
import org.gvsig.tools.swing.api.ChangeListenerSupport; |
32 |
import org.gvsig.tools.swing.api.ListElement; |
33 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
34 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
35 |
import org.gvsig.tools.swing.api.ToolsSwingUtils; |
36 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
37 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
38 |
|
39 |
/**
|
40 |
*
|
41 |
* @author jjdelcerro
|
42 |
*/
|
43 |
public class DownloaderConfigServicePanelImpl |
44 |
extends DownloaderConfigServicePanelView
|
45 |
implements ChangeListenerSupport, DownloaderConfigServicePanel
|
46 |
{ |
47 |
|
48 |
private DownloaderAuthenticationConfig config;
|
49 |
private final ChangeListenerHelper changeListenerHelper; |
50 |
private boolean dirty; |
51 |
|
52 |
public DownloaderConfigServicePanelImpl() {
|
53 |
this(null); |
54 |
} |
55 |
|
56 |
public DownloaderConfigServicePanelImpl(String theServiceUrl) { |
57 |
this.changeListenerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
58 |
this.initComponents();
|
59 |
this.txtServiceURL.setText(theServiceUrl);
|
60 |
} |
61 |
|
62 |
private void initComponents() { |
63 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
64 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
65 |
|
66 |
toolsSwingManager.translate(lblAuthenmticationType); |
67 |
toolsSwingManager.translate(lblServiceURL); |
68 |
toolsSwingManager.translate(btnAuthenticationConfig); |
69 |
toolsSwingManager.translate(btnAuthenticationTest); |
70 |
|
71 |
toolsSwingManager.addClearButton(txtServiceURL); |
72 |
toolsSwingManager.setDefaultPopupMenu(txtServiceURL); |
73 |
|
74 |
DefaultComboBoxModel<DownloaderAuthenticationFactory> authenticationTypeModel = new DefaultComboBoxModel<>(); |
75 |
for (DownloaderAuthenticationFactory authenticationType : downloader.getAuthenticationTypes()) {
|
76 |
authenticationTypeModel.addElement(authenticationType); |
77 |
} |
78 |
this.cboAuthenticationType.setModel(authenticationTypeModel);
|
79 |
this.cboAuthenticationType.setSelectedIndex(0); |
80 |
|
81 |
this.cboAuthenticationType.addActionListener((ActionEvent e) -> { |
82 |
SwingUtilities.invokeLater(() -> {
|
83 |
doAuthenticationOrUrlChange(); |
84 |
changeListenerHelper.fireEvent(); |
85 |
}); |
86 |
}); |
87 |
|
88 |
this.btnAuthenticationConfig.addActionListener((ActionEvent e) -> { |
89 |
doAuthenticationConfig(); |
90 |
}); |
91 |
this.btnAuthenticationTest.addActionListener((ActionEvent e) -> { |
92 |
doAuthenticationTest(); |
93 |
}); |
94 |
this.txtServiceURL.addKeyListener(new KeyAdapter() { |
95 |
@Override
|
96 |
public void keyTyped(KeyEvent e) { |
97 |
if( e.getKeyChar()=='\n' ) { |
98 |
doAuthenticationOrUrlChange(); |
99 |
changeListenerHelper.fireEvent(); |
100 |
} |
101 |
} |
102 |
}); |
103 |
this.txtServiceURL.getDocument().addDocumentListener(new DocumentListener() { |
104 |
@Override
|
105 |
public void insertUpdate(DocumentEvent e) { |
106 |
setDirty(true);
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
public void removeUpdate(DocumentEvent e) { |
111 |
setDirty(true);
|
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void changedUpdate(DocumentEvent e) { |
116 |
setDirty(true);
|
117 |
} |
118 |
}); |
119 |
|
120 |
this.cboAuthenticationType.setSelectedIndex(-1); |
121 |
ToolsSwingUtils.ensureRowsCols(this, 4, 100, 6, 200); |
122 |
|
123 |
} |
124 |
|
125 |
private void setDirty(boolean dirty) { |
126 |
this.dirty = dirty;
|
127 |
changeListenerHelper.fireEvent(); |
128 |
} |
129 |
|
130 |
private boolean isDirty() { |
131 |
return this.dirty; |
132 |
} |
133 |
|
134 |
private void doUpdateComponents() { |
135 |
if( config == null ) { |
136 |
this.btnAuthenticationConfig.setEnabled(false); |
137 |
this.btnAuthenticationTest.setEnabled(false); |
138 |
return;
|
139 |
} |
140 |
this.btnAuthenticationConfig.setEnabled(true); |
141 |
this.btnAuthenticationTest.setEnabled(true); |
142 |
} |
143 |
|
144 |
public void clear() { |
145 |
this.config = null; |
146 |
this.txtServiceURL.setText(""); |
147 |
cboAuthenticationType.setSelectedIndex(0);
|
148 |
changeListenerHelper.fireEvent(); |
149 |
} |
150 |
|
151 |
@Override
|
152 |
public void put(DownloaderAuthenticationConfig config) { |
153 |
this.config = (DownloaderAuthenticationConfig) CloneableUtils.cloneQuietly(config);
|
154 |
this.txtServiceURL.setText(this.config.getServiceUrl()); |
155 |
ListElement.setSelected(cboAuthenticationType, this.config.getFactory());
|
156 |
this.doUpdateComponents();
|
157 |
changeListenerHelper.fireEvent(); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public DownloaderAuthenticationConfig fetch() {
|
162 |
if( this.config == null ) { |
163 |
return null; |
164 |
} |
165 |
return (DownloaderAuthenticationConfig) CloneableUtils.cloneQuietly(config);
|
166 |
} |
167 |
|
168 |
private void doAuthenticationOrUrlChange() { |
169 |
DownloaderAuthenticationFactory authenticationType = (DownloaderAuthenticationFactory) this.cboAuthenticationType.getSelectedItem();
|
170 |
if( authenticationType==null ) { |
171 |
return;
|
172 |
} |
173 |
String url = StringUtils.defaultIfBlank(this.txtServiceURL.getText(), null); |
174 |
if( StringUtils.isBlank(url) ) {
|
175 |
return;
|
176 |
} |
177 |
url = url.trim(); |
178 |
if( this.config==null || !StringUtils.equalsIgnoreCase(authenticationType.getProviderName(), this.config.getProviderName())) { |
179 |
this.config = authenticationType.create(url);
|
180 |
} |
181 |
if( !StringUtils.equals(this.config.getServiceUrl(),url) ) { |
182 |
this.config = authenticationType.create(url);
|
183 |
} |
184 |
setDirty(false);
|
185 |
doUpdateComponents(); |
186 |
} |
187 |
|
188 |
private void doAuthenticationConfig() { |
189 |
if( this.config == null ) { |
190 |
return;
|
191 |
} |
192 |
if( !this.config.isConfigurable() ) { |
193 |
return;
|
194 |
} |
195 |
this.config.requestAuthenticationConfig();
|
196 |
this.changeListenerHelper.fireEvent();
|
197 |
} |
198 |
|
199 |
private void doAuthenticationTest() { |
200 |
if( this.config == null ) { |
201 |
return;
|
202 |
} |
203 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
204 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
205 |
DownloaderManager manager = DownloaderLocator.getDownloaderManager(); |
206 |
manager.removeCredentials(this.config.getServiceUrl());
|
207 |
DownloaderAuthenticationRequester requester = this.config.create();
|
208 |
if( !requester.requestAuthorization((Runnable command) -> {command.run();}) ) { |
209 |
dialogs.messageDialog( |
210 |
i18n.getTranslation("_Authentication_test_failed"),
|
211 |
i18n.getTranslation("_Authentication_test"),
|
212 |
JOptionPane.WARNING_MESSAGE
|
213 |
); |
214 |
return;
|
215 |
} |
216 |
dialogs.messageDialog( |
217 |
i18n.getTranslation("_Authentication_test_passed"),
|
218 |
i18n.getTranslation("_Authentication_test"),
|
219 |
JOptionPane.INFORMATION_MESSAGE
|
220 |
); |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public boolean isTheConfigurationValid() { |
225 |
return !this.isDirty() && this.config!=null && this.config.isFilled(); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public ImageIcon loadImage( String imageName ) { |
230 |
String name = FilenameUtils.getBaseName(imageName);
|
231 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
232 |
if (theme.exists(name)) {
|
233 |
return theme.get(name);
|
234 |
} |
235 |
return new ImageIcon(); |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void addChangeListener(ChangeListener listener) { |
240 |
this.changeListenerHelper.addChangeListener(listener);
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public ChangeListener[] getChangeListeners() { |
245 |
return this.changeListenerHelper.getChangeListeners(); |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void removeChangeListener(ChangeListener listener) { |
250 |
this.changeListenerHelper.removeChangeListener(listener);
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public void removeAllChangeListener() { |
255 |
this.changeListenerHelper.removeAllChangeListener();
|
256 |
} |
257 |
|
258 |
@Override
|
259 |
public boolean hasChangeListeners() { |
260 |
return this.changeListenerHelper.hasChangeListeners(); |
261 |
} |
262 |
|
263 |
public static void selfRegister() { |
264 |
ToolsSwingUtils.registerIcons(DownloaderConfigServicePanel.class, |
265 |
"/org/gvsig/downloader/swing/impl/images",
|
266 |
"downloader",
|
267 |
new String[] { "downloader", "downloader-auth-config" }, |
268 |
new String[] { "downloader", "downloader-auth-test" } |
269 |
); |
270 |
// ToolsSwingUtils.registerGroupIconScreenshot(DownloaderConfigServicePanel.class,
|
271 |
// "downloader",
|
272 |
// "/org/gvsig/downloader/swing/impl/screenshots/downloader.png"
|
273 |
// );
|
274 |
} |
275 |
|
276 |
@Override
|
277 |
public JComponent asJComponent() { |
278 |
return this; |
279 |
} |
280 |
|
281 |
} |