Statistics
| Revision:

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 @ 47828

History | View | Annotate | Download (13 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.AbstractListModel;
8
import javax.swing.DefaultListModel;
9
import javax.swing.ImageIcon;
10
import javax.swing.JComponent;
11
import javax.swing.event.ChangeEvent;
12
import javax.swing.table.AbstractTableModel;
13
import org.apache.commons.io.FilenameUtils;
14
import org.gvsig.compat.CompatLocator;
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.tools.swing.api.ToolsSwingLocator;
22
import org.gvsig.tools.swing.api.ToolsSwingManager;
23
import org.gvsig.tools.swing.api.windowmanager.Dialog;
24
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
25
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
26
import org.gvsig.tools.swing.icontheme.IconTheme;
27

    
28
/**
29
 *
30
 * @author jjdelcerro
31
 */
32
public class DownloaderConfigPanelImpl 
33
        extends DownloaderConfigPanelView 
34
        implements DownloaderConfigPanel
35
    {
36

    
37
    private static class AuthenticatedServicesTableModel extends AbstractTableModel {
38

    
39
        private final String[] columnNames;
40
        private final Class[] columnTypes;
41
        private List<DownloaderAuthenticationConfig> services;
42
        
43
        public AuthenticatedServicesTableModel(DownloaderManager downloader) {
44
            this.columnNames = new String[] {
45
                "Servicio",
46
                "Autenticaci?n"
47
            };
48
            this.columnTypes = new Class[] {
49
                String.class,
50
                String.class
51
            };
52
            if( downloader == null ) {
53
                this.services = Collections.EMPTY_LIST;
54
            } else {
55
                this.services = new ArrayList<>(downloader.getAuthenticationConfigurationServices());
56
            }
57
        }
58

    
59
        @Override
60
        public int getRowCount() {
61
            return this.services.size();
62
        }
63

    
64
        @Override
65
        public int getColumnCount() {
66
            return this.columnNames.length;
67
        }
68

    
69
        @Override
70
        public Class<?> getColumnClass(int columnIndex) {
71
            return this.columnTypes[columnIndex];
72
        }
73

    
74
        @Override
75
        public String getColumnName(int column) {
76
            return this.columnNames[column];
77
        }
78
        
79
        @Override
80
        public Object getValueAt(int rowIndex, int columnIndex) {
81
            if( rowIndex<0 || this.services==null || rowIndex>=this.services.size()) {
82
                return null;
83
            }
84
            DownloaderAuthenticationConfig service = this.services.get(rowIndex);
85
            switch(columnIndex) {
86
                case 0:
87
                    return service.getServiceUrl();
88
                case 1:
89
                    return service.getFactory().getProviderName();
90
                default:
91
                    return null;
92
            }
93
        }
94

    
95
        private DownloaderAuthenticationConfig getConfig(int row) {
96
            return this.services.get(row);
97
        }
98
    }
99

    
100
    private static class CredentialsTableModel extends AbstractTableModel {
101

    
102
        private final String[] columnNames;
103
        private final Class[] columnTypes;
104
        private List<DownloaderCredentials> credentials;
105
        
106
        public CredentialsTableModel(DownloaderManager downloader) {
107
            this.columnNames = new String[] {
108
                "Servicio",
109
                "Autenticaci?n"
110
            };
111
            this.columnTypes = new Class[] {
112
                String.class,
113
                String.class
114
            };
115
            if( downloader == null ) {
116
                this.credentials = Collections.EMPTY_LIST;
117
            } else {
118
                this.credentials = new ArrayList<>(downloader.getCredentials());
119
            }
120
        }
121

    
122
        @Override
123
        public int getRowCount() {
124
            return this.credentials.size();
125
        }
126

    
127
        @Override
128
        public int getColumnCount() {
129
            return this.columnNames.length;
130
        }
131

    
132
        @Override
133
        public Class<?> getColumnClass(int columnIndex) {
134
            return this.columnTypes[columnIndex];
135
        }
136

    
137
        @Override
138
        public String getColumnName(int column) {
139
            return this.columnNames[column];
140
        }
141
        
142
        @Override
143
        public Object getValueAt(int rowIndex, int columnIndex) {
144
            if( rowIndex<0 || this.credentials==null || rowIndex>=this.credentials.size()) {
145
                return null;
146
            }
147
            DownloaderCredentials credentials = this.credentials.get(rowIndex);
148
            switch(columnIndex) {
149
                case 0:
150
                    return credentials.getServiceUrl();
151
                case 1:
152
                    return credentials.getProviderName();
153
                default:
154
                    return null;
155
            }
156
        }
157

    
158
        public DownloaderCredentials getCredentials(int index) {
159
            return this.credentials.get(index);
160
        }
161
    }
162

    
163

    
164
//    private static class AutenticationTypesListModel extends AbstractListModel<DownloaderAuthenticationFactory> {
165
//
166
//        private final List<DownloaderAuthenticationFactory> authenticationTypes;
167
//        
168
//        public AutenticationTypesListModel(DownloaderManager downloader) {
169
//            if( downloader == null ) {
170
//                this.authenticationTypes = Collections.EMPTY_LIST;
171
//            } else {
172
//                this.authenticationTypes = new ArrayList<>(downloader.getAuthenticationTypes());
173
//            }
174
//        }
175
//
176
//        @Override
177
//        public int getSize() {
178
//            return this.authenticationTypes.size();
179
//        }
180
//
181
//        @Override
182
//        public DownloaderAuthenticationFactory getElementAt(int index) {
183
//            return this.authenticationTypes.get(index);
184
//        }
185
//    }
186
    
187
    public DownloaderConfigPanelImpl() {
188
        
189
        this.initComponents();
190
    }
191

    
192
    @Override
193
    public JComponent asJComponent() {
194
        return this;
195
    }
196
    
197
    private void initComponents() {
198
        DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
199
        
200
        this.translate();
201
        
202
        this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader));
203
        this.tblCredentials.setModel(new CredentialsTableModel(downloader));
204
        DefaultListModel<DownloaderAuthenticationFactory> authenticationTypesModel = new DefaultListModel<>();
205
        for (DownloaderAuthenticationFactory authType : downloader.getAuthenticationTypes()) {
206
            authenticationTypesModel.addElement(authType);
207
        }        
208
        this.lstAuthenticationTypes.setModel(authenticationTypesModel);
209
        
210
        this.btnAuthenticatedServicesAdd.addActionListener((ActionEvent e) -> {
211
            doAuthenticateServciceAdd();
212
        });
213
        this.btnAuthenticatedServicesEdit.addActionListener((ActionEvent e) -> {
214
            doAuthenticateServciceEdit();
215
        });
216
        this.btnAuthenticatedServicesRemove.addActionListener((ActionEvent e) -> {
217
            doAuthenticateServciceRemove();
218
        });
219
        
220
        this.btnCredentialsRemove.addActionListener((ActionEvent e) -> {
221
            doCredentialRemove();
222
        });
223
        
224
        doUpdateComponents();
225
    }
226
    
227
    private void doUpdateComponents() {
228
        DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
229
        boolean hasDownloader = downloader!=null;
230
        this.btnAuthenticatedServicesAdd.setEnabled(hasDownloader);
231
        this.btnAuthenticatedServicesEdit.setEnabled(hasDownloader);
232
        this.btnAuthenticatedServicesRemove.setEnabled(hasDownloader);
233
        this.btnCredentialsEdit.setEnabled(hasDownloader);
234
        this.btnCredentialsRemove.setEnabled(hasDownloader);
235
    }
236
    
237
    private void translate() {
238
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
239
        
240
        toolsSwingManager.translate(tabDownloaderConfig);
241
        toolsSwingManager.translate(btnAuthenticatedServicesAdd);
242
        toolsSwingManager.translate(btnAuthenticatedServicesEdit);
243
        toolsSwingManager.translate(btnAuthenticatedServicesRemove);
244
        toolsSwingManager.translate(btnCredentialsEdit);
245
        toolsSwingManager.translate(btnCredentialsRemove);
246
    }
247

    
248
    public ImageIcon loadImage( String imageName ) {
249
        String name = FilenameUtils.getBaseName(imageName);
250
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
251
        if (theme.exists(name)) {
252
            return theme.get(name);
253
        }
254
        return new ImageIcon();
255
    }    
256

    
257
    private void doAuthenticateServciceAdd() {
258
        WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
259
        DownloaderConfigServicePanel panel = new DownloaderConfigServicePanel();
260
        Dialog dialog = winmanager.createDialog(
261
                panel,
262
                "_Add_service",
263
                null, 
264
                WindowManager_v2.BUTTONS_OK_CANCEL
265
        );
266
        dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid());
267
        panel.addChangeListener((ChangeEvent e) -> {
268
            dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid());
269
        });
270
        dialog.addActionListener((ActionEvent e) -> {
271
            if( dialog.getAction()!=WindowManager_v2.BUTTON_OK ) {
272
                return;
273
            }
274
            DownloaderAuthenticationConfig config = panel.fetch();
275
            DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
276
            downloader.registerAuthenticationConfigurationService(config);
277
            this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader));
278
        });
279
        dialog.show(WindowManager.MODE.DIALOG);
280
    }
281

    
282
    private DownloaderAuthenticationConfig getSelectedService() {
283
        AuthenticatedServicesTableModel model = (AuthenticatedServicesTableModel) this.tblAuthenticatedServices.getModel();
284
        
285
        int row = this.tblAuthenticatedServices.getSelectedRow();
286
        if( row < 0 ) {
287
            return null;
288
        }
289
        DownloaderAuthenticationConfig selectedConfig = model.getConfig(row);
290
        return selectedConfig;
291
    }
292
    
293
    private DownloaderCredentials getSelectedCredential() {
294
        CredentialsTableModel model = (CredentialsTableModel) this.tblCredentials.getModel();
295
        
296
        int row = this.tblCredentials.getSelectedRow();
297
        if( row < 0 ) {
298
            return null;
299
        }
300
        DownloaderCredentials selected = model.getCredentials(row);
301
        return selected;
302
    }
303
    
304
    private void doAuthenticateServciceEdit() {
305
        DownloaderAuthenticationConfig selectedConfig = this.getSelectedService();
306
        if( selectedConfig == null ) {
307
            return;
308
        }
309
        
310
        WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
311
        DownloaderConfigServicePanel panel = new DownloaderConfigServicePanel();
312
        panel.put(selectedConfig);
313
        Dialog dialog = winmanager.createDialog(
314
                panel,
315
                "_Edit_service",
316
                null, 
317
                WindowManager_v2.BUTTONS_OK_CANCEL
318
        );
319
        dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid());
320
        panel.addChangeListener((ChangeEvent e) -> {
321
            dialog.setButtonEnabled(WindowManager_v2.BUTTON_OK, panel.isTheConfigurationValid());
322
        });
323
        dialog.addActionListener((ActionEvent e) -> {
324
            if( dialog.getAction()!=WindowManager_v2.BUTTON_OK ) {
325
                return;
326
            }
327
            DownloaderAuthenticationConfig config = panel.fetch();
328
            DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
329
            downloader.removeAuthenticationConfigurationService(selectedConfig);
330
            downloader.registerAuthenticationConfigurationService(config);
331
            this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader));
332
        });
333
        dialog.show(WindowManager.MODE.DIALOG);
334
    }
335

    
336
    private void doAuthenticateServciceRemove() {
337
        DownloaderAuthenticationConfig selectedConfig = this.getSelectedService();
338
        if( selectedConfig == null ) {
339
            return;
340
        }
341
        DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
342
        downloader.removeAuthenticationConfigurationService(selectedConfig);
343
        this.tblAuthenticatedServices.setModel(new AuthenticatedServicesTableModel(downloader));
344
    }
345
    
346
    private void doCredentialRemove() {
347
        DownloaderCredentials selected = this.getSelectedCredential();
348
        if( selected == null ) {
349
            return;
350
        }
351
        DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
352
        downloader.removeCredentials(selected);
353
        this.tblCredentials.setModel(new CredentialsTableModel(downloader));
354
    }
355

    
356
}