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

History | View | Annotate | Download (12.7 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.ImageIcon;
9
import javax.swing.JComponent;
10
import javax.swing.event.ChangeEvent;
11
import javax.swing.table.AbstractTableModel;
12
import org.apache.commons.io.FilenameUtils;
13
import org.gvsig.compat.CompatLocator;
14
import org.gvsig.downloader.DownloaderAuthenticationConfig;
15
import org.gvsig.downloader.DownloaderAuthenticationFactory;
16
import org.gvsig.downloader.DownloaderCredentials;
17
import org.gvsig.downloader.DownloaderLocator;
18
import org.gvsig.downloader.DownloaderManager;
19
import org.gvsig.downloader.swing.DownloaderConfigPanel;
20
import org.gvsig.tools.swing.api.ToolsSwingLocator;
21
import org.gvsig.tools.swing.api.ToolsSwingManager;
22
import org.gvsig.tools.swing.api.windowmanager.Dialog;
23
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
24
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
25
import org.gvsig.tools.swing.icontheme.IconTheme;
26

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

    
36
    private static class AuthenticatedServicesTableModel extends AbstractTableModel {
37

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

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

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

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

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

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

    
99
    private static class CredentialsTableModel extends AbstractTableModel {
100

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

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

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

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

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

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

    
162

    
163
    private static class AutenticationTypesListModel extends AbstractListModel<DownloaderAuthenticationFactory> {
164

    
165
        private final List<DownloaderAuthenticationFactory> authenticationTypes;
166
        
167
        public AutenticationTypesListModel(DownloaderManager downloader) {
168
            if( downloader == null ) {
169
                this.authenticationTypes = Collections.EMPTY_LIST;
170
            } else {
171
                this.authenticationTypes = new ArrayList<>(downloader.getAuthenticationTypes());
172
            }
173
        }
174

    
175
        @Override
176
        public int getSize() {
177
            return this.authenticationTypes.size();
178
        }
179

    
180
        @Override
181
        public DownloaderAuthenticationFactory getElementAt(int index) {
182
            return this.authenticationTypes.get(index);
183
        }
184
    }
185
    
186
    public DownloaderConfigPanelImpl() {
187
        
188
        this.initComponents();
189
    }
190

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

    
243
    public ImageIcon loadImage( String imageName ) {
244
        String name = FilenameUtils.getBaseName(imageName);
245
        IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault();
246
        if (theme.exists(name)) {
247
            return theme.get(name);
248
        }
249
        return new ImageIcon();
250
    }    
251

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

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

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

    
351
}