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 / DownloaderConfigPanelImpl.java @ 47841
History | View | Annotate | Download (15.2 KB)
1 |
package org.gvsig.downloader.swing.impl.config; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Collections; |
6 |
import java.util.List; |
7 |
import javax.swing.DefaultListModel; |
8 |
import javax.swing.ImageIcon; |
9 |
import javax.swing.JComponent; |
10 |
import javax.swing.event.ChangeEvent; |
11 |
import javax.swing.event.ListSelectionEvent; |
12 |
import javax.swing.event.ListSelectionListener; |
13 |
import javax.swing.table.AbstractTableModel; |
14 |
import org.apache.commons.io.FilenameUtils; |
15 |
import org.gvsig.downloader.DownloaderAuthenticationConfig; |
16 |
import org.gvsig.downloader.DownloaderAuthenticationFactory; |
17 |
import org.gvsig.downloader.DownloaderCredentials; |
18 |
import org.gvsig.downloader.DownloaderLocator; |
19 |
import org.gvsig.downloader.DownloaderManager; |
20 |
import org.gvsig.downloader.swing.DownloaderConfigPanel; |
21 |
import org.gvsig.downloader.swing.DownloaderConfigServicePanel; |
22 |
import org.gvsig.tools.ToolsLocator; |
23 |
import org.gvsig.tools.i18n.I18nManager; |
24 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
25 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
26 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
27 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
28 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
29 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
/**
|
34 |
*
|
35 |
* @author jjdelcerro
|
36 |
*/
|
37 |
public class DownloaderConfigPanelImpl |
38 |
extends DownloaderConfigPanelView
|
39 |
implements DownloaderConfigPanel
|
40 |
{ |
41 |
|
42 |
public static final Logger LOGGER = LoggerFactory.getLogger(DownloaderConfigPanelImpl.class); |
43 |
|
44 |
private static class AuthenticatedServicesTableModel extends AbstractTableModel { |
45 |
|
46 |
private final String[] columnNames; |
47 |
private final Class[] columnTypes; |
48 |
private List<DownloaderAuthenticationConfig> services; |
49 |
|
50 |
public AuthenticatedServicesTableModel(DownloaderManager downloader) {
|
51 |
this.columnNames = new String[] { |
52 |
"Servicio",
|
53 |
"Autenticaci?n"
|
54 |
}; |
55 |
this.columnTypes = new Class[] { |
56 |
String.class,
|
57 |
String.class
|
58 |
}; |
59 |
if( downloader == null ) { |
60 |
this.services = Collections.EMPTY_LIST; |
61 |
} else {
|
62 |
this.services = new ArrayList<>(downloader.getAuthenticationConfigurationServices()); |
63 |
} |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public int getRowCount() { |
68 |
return this.services.size(); |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public int getColumnCount() { |
73 |
return this.columnNames.length; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public Class<?> getColumnClass(int columnIndex) { |
78 |
return this.columnTypes[columnIndex]; |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public String getColumnName(int column) { |
83 |
return this.columnNames[column]; |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public Object getValueAt(int rowIndex, int columnIndex) { |
88 |
try {
|
89 |
if (rowIndex < 0 || this.services == null || rowIndex >= this.services.size()) { |
90 |
return null; |
91 |
} |
92 |
DownloaderAuthenticationConfig service = this.services.get(rowIndex);
|
93 |
switch (columnIndex) {
|
94 |
case 0: |
95 |
return service.getServiceUrl();
|
96 |
case 1: |
97 |
return service.getFactory().getProviderName();
|
98 |
default:
|
99 |
return null; |
100 |
} |
101 |
} catch (Exception ex) { |
102 |
return null; |
103 |
} |
104 |
} |
105 |
|
106 |
private DownloaderAuthenticationConfig getConfig(int row) { |
107 |
return this.services.get(row); |
108 |
} |
109 |
} |
110 |
|
111 |
private static class CredentialsTableModel extends AbstractTableModel { |
112 |
|
113 |
private final String[] columnNames; |
114 |
private final Class[] columnTypes; |
115 |
private List<DownloaderCredentials> credentials; |
116 |
|
117 |
public CredentialsTableModel(DownloaderManager downloader) {
|
118 |
this.columnNames = new String[] { |
119 |
"Servicio",
|
120 |
"Autenticaci?n"
|
121 |
}; |
122 |
this.columnTypes = new Class[] { |
123 |
String.class,
|
124 |
String.class
|
125 |
}; |
126 |
if( downloader == null ) { |
127 |
this.credentials = Collections.EMPTY_LIST; |
128 |
} else {
|
129 |
this.credentials = new ArrayList<>(downloader.getCredentials()); |
130 |
} |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public int getRowCount() { |
135 |
return this.credentials.size(); |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public int getColumnCount() { |
140 |
return this.columnNames.length; |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public Class<?> getColumnClass(int columnIndex) { |
145 |
return this.columnTypes[columnIndex]; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public String getColumnName(int column) { |
150 |
return this.columnNames[column]; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public Object getValueAt(int rowIndex, int columnIndex) { |
155 |
if( rowIndex<0 || this.credentials==null || rowIndex>=this.credentials.size()) { |
156 |
return null; |
157 |
} |
158 |
DownloaderCredentials credentials = this.credentials.get(rowIndex);
|
159 |
switch(columnIndex) {
|
160 |
case 0: |
161 |
return credentials.getServiceUrl();
|
162 |
case 1: |
163 |
return credentials.getProviderName();
|
164 |
default:
|
165 |
return null; |
166 |
} |
167 |
} |
168 |
|
169 |
public DownloaderCredentials getCredentials(int index) { |
170 |
return this.credentials.get(index); |
171 |
} |
172 |
} |
173 |
|
174 |
|
175 |
// private static class AutenticationTypesListModel extends AbstractListModel<DownloaderAuthenticationFactory> {
|
176 |
//
|
177 |
// private final List<DownloaderAuthenticationFactory> authenticationTypes;
|
178 |
//
|
179 |
// public AutenticationTypesListModel(DownloaderManager downloader) {
|
180 |
// if( downloader == null ) {
|
181 |
// this.authenticationTypes = Collections.EMPTY_LIST;
|
182 |
// } else {
|
183 |
// this.authenticationTypes = new ArrayList<>(downloader.getAuthenticationTypes());
|
184 |
// }
|
185 |
// }
|
186 |
//
|
187 |
// @Override
|
188 |
// public int getSize() {
|
189 |
// return this.authenticationTypes.size();
|
190 |
// }
|
191 |
//
|
192 |
// @Override
|
193 |
// public DownloaderAuthenticationFactory getElementAt(int index) {
|
194 |
// return this.authenticationTypes.get(index);
|
195 |
// }
|
196 |
// }
|
197 |
|
198 |
public DownloaderConfigPanelImpl() {
|
199 |
|
200 |
this.initComponents();
|
201 |
} |
202 |
|
203 |
@Override
|
204 |
public JComponent asJComponent() { |
205 |
return this; |
206 |
} |
207 |
|
208 |
private void initComponents() { |
209 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
210 |
|
211 |
this.translate();
|
212 |
|
213 |
this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader)); |
214 |
this.tblCredentials.setModel(new CredentialsTableModel(downloader)); |
215 |
DefaultListModel<DownloaderAuthenticationFactory> authenticationTypesModel = new DefaultListModel<>(); |
216 |
for (DownloaderAuthenticationFactory authType : downloader.getAuthenticationTypes()) {
|
217 |
authenticationTypesModel.addElement(authType); |
218 |
} |
219 |
this.lstAuthenticationTypes.setModel(authenticationTypesModel);
|
220 |
|
221 |
this.lstAuthenticationTypes.addListSelectionListener((ListSelectionEvent e) -> { |
222 |
doAuthenticationTypeSelected(); |
223 |
}); |
224 |
|
225 |
this.btnAuthenticatedServicesAdd.addActionListener((ActionEvent e) -> { |
226 |
doAuthenticateServiceAdd(); |
227 |
}); |
228 |
this.btnAuthenticatedServicesEdit.addActionListener((ActionEvent e) -> { |
229 |
doAuthenticateServiceEdit(); |
230 |
}); |
231 |
this.btnAuthenticatedServicesRemove.addActionListener((ActionEvent e) -> { |
232 |
doAuthenticateServiceRemove(); |
233 |
}); |
234 |
|
235 |
this.btnCredentialsRemove.addActionListener((ActionEvent e) -> { |
236 |
doCredentialRemove(); |
237 |
}); |
238 |
|
239 |
this.tblCredentials.getSelectionModel().addListSelectionListener((ListSelectionEvent e) -> { |
240 |
doCredentialSelectionChanged(); |
241 |
}); |
242 |
doUpdateComponents(); |
243 |
} |
244 |
|
245 |
private void doCredentialSelectionChanged() { |
246 |
int row = this.tblCredentials.getSelectedRow(); |
247 |
if(row < 0) { |
248 |
return;
|
249 |
} |
250 |
CredentialsTableModel model = (CredentialsTableModel) this.tblCredentials.getModel();
|
251 |
DownloaderCredentials credentials = model.getCredentials(row); |
252 |
LOGGER.info(credentials.toString()); |
253 |
|
254 |
} |
255 |
private void doUpdateComponents() { |
256 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
257 |
boolean hasDownloader = downloader!=null; |
258 |
this.btnAuthenticatedServicesAdd.setEnabled(hasDownloader);
|
259 |
this.btnAuthenticatedServicesEdit.setEnabled(hasDownloader);
|
260 |
this.btnAuthenticatedServicesRemove.setEnabled(hasDownloader);
|
261 |
this.btnCredentialsRemove.setEnabled(hasDownloader);
|
262 |
} |
263 |
|
264 |
private void translate() { |
265 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
266 |
|
267 |
toolsSwingManager.translate(tabDownloaderConfig); |
268 |
toolsSwingManager.translate(btnAuthenticatedServicesAdd); |
269 |
toolsSwingManager.translate(btnAuthenticatedServicesEdit); |
270 |
toolsSwingManager.translate(btnAuthenticatedServicesRemove); |
271 |
toolsSwingManager.translate(btnCredentialsRemove); |
272 |
} |
273 |
|
274 |
public ImageIcon loadImage( String imageName ) { |
275 |
String name = FilenameUtils.getBaseName(imageName);
|
276 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
277 |
if (theme.exists(name)) {
|
278 |
return theme.get(name);
|
279 |
} |
280 |
return new ImageIcon(); |
281 |
} |
282 |
|
283 |
private void doAuthenticateServiceAdd() { |
284 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
285 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
286 |
DownloaderConfigServicePanel panel = new DownloaderConfigServicePanelImpl();
|
287 |
Dialog dialog = winmanager.createDialog(
|
288 |
panel.asJComponent(), |
289 |
i18n.getTranslation("_Add_service"),
|
290 |
null,
|
291 |
WindowManager_v2.BUTTONS_OK_CANCEL |
292 |
); |
293 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid()); |
294 |
panel.addChangeListener((ChangeEvent e) -> {
|
295 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid()); |
296 |
}); |
297 |
dialog.addActionListener((ActionEvent e) -> {
|
298 |
if( dialog.getAction()!=WindowManager_v2.BUTTON_OK ) {
|
299 |
return;
|
300 |
} |
301 |
DownloaderAuthenticationConfig config = panel.fetch(); |
302 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
303 |
downloader.registerAuthenticationConfigurationService(config); |
304 |
this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader)); |
305 |
}); |
306 |
dialog.show(WindowManager.MODE.DIALOG); |
307 |
} |
308 |
|
309 |
private DownloaderAuthenticationConfig getSelectedService() {
|
310 |
AuthenticatedServicesTableModel model = (AuthenticatedServicesTableModel) this.tblAuthenticatedServices.getModel();
|
311 |
|
312 |
int row = this.tblAuthenticatedServices.getSelectedRow(); |
313 |
if( row < 0 ) { |
314 |
return null; |
315 |
} |
316 |
DownloaderAuthenticationConfig selectedConfig = model.getConfig(row); |
317 |
return selectedConfig;
|
318 |
} |
319 |
|
320 |
private DownloaderCredentials getSelectedCredential() {
|
321 |
CredentialsTableModel model = (CredentialsTableModel) this.tblCredentials.getModel();
|
322 |
|
323 |
int row = this.tblCredentials.getSelectedRow(); |
324 |
if( row < 0 ) { |
325 |
return null; |
326 |
} |
327 |
DownloaderCredentials selected = model.getCredentials(row); |
328 |
return selected;
|
329 |
} |
330 |
|
331 |
private void doAuthenticateServiceEdit() { |
332 |
DownloaderAuthenticationConfig selectedConfig = this.getSelectedService();
|
333 |
if( selectedConfig == null ) { |
334 |
return;
|
335 |
} |
336 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
337 |
|
338 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
339 |
DownloaderConfigServicePanel panel = new DownloaderConfigServicePanelImpl();
|
340 |
panel.put(selectedConfig); |
341 |
Dialog dialog = winmanager.createDialog(
|
342 |
panel.asJComponent() |
343 |
, |
344 |
i18n.getTranslation("_Edit_service"),
|
345 |
null,
|
346 |
WindowManager_v2.BUTTONS_OK_CANCEL |
347 |
); |
348 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid()); |
349 |
panel.addChangeListener((ChangeEvent e) -> {
|
350 |
dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid()); |
351 |
}); |
352 |
dialog.addActionListener((ActionEvent e) -> {
|
353 |
if( dialog.getAction()!=WindowManager_v2.BUTTON_OK ) {
|
354 |
return;
|
355 |
} |
356 |
DownloaderAuthenticationConfig config = panel.fetch(); |
357 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
358 |
downloader.removeAuthenticationConfigurationService(selectedConfig); |
359 |
downloader.registerAuthenticationConfigurationService(config); |
360 |
this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader)); |
361 |
}); |
362 |
dialog.show(WindowManager.MODE.DIALOG); |
363 |
} |
364 |
|
365 |
private void doAuthenticateServiceRemove() { |
366 |
DownloaderAuthenticationConfig selectedConfig = this.getSelectedService();
|
367 |
if( selectedConfig == null ) { |
368 |
return;
|
369 |
} |
370 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
371 |
downloader.removeAuthenticationConfigurationService(selectedConfig); |
372 |
this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader)); |
373 |
} |
374 |
|
375 |
private void doCredentialRemove() { |
376 |
DownloaderCredentials selected = this.getSelectedCredential();
|
377 |
if( selected == null ) { |
378 |
return;
|
379 |
} |
380 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
381 |
downloader.removeCredentials(selected); |
382 |
this.tblCredentials.setModel(new CredentialsTableModel(downloader)); |
383 |
} |
384 |
|
385 |
@Override
|
386 |
public void refresh() { |
387 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
388 |
|
389 |
this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader)); |
390 |
this.tblCredentials.setModel(new CredentialsTableModel(downloader)); |
391 |
DefaultListModel<DownloaderAuthenticationFactory> authenticationTypesModel = new DefaultListModel<>(); |
392 |
for (DownloaderAuthenticationFactory authType : downloader.getAuthenticationTypes()) {
|
393 |
authenticationTypesModel.addElement(authType); |
394 |
} |
395 |
this.lstAuthenticationTypes.setModel(authenticationTypesModel);
|
396 |
} |
397 |
|
398 |
private void doAuthenticationTypeSelected() { |
399 |
DownloaderAuthenticationFactory item = (DownloaderAuthenticationFactory) this.lstAuthenticationTypes.getSelectedValue();
|
400 |
if(item == null) { |
401 |
this.txtAuthenticationTypesDescription.setText(""); |
402 |
return;
|
403 |
} |
404 |
this.txtAuthenticationTypesDescription.setText(item.getDescription());
|
405 |
} |
406 |
|
407 |
} |