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 / DownloaderConfigServicePanelImpl.java @ 47845

History | View | Annotate | Download (9.99 KB)

1 47821 jjdelcerro
/*
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 47836 jjdelcerro
import javax.swing.JComponent;
14 47821 jjdelcerro
import javax.swing.JOptionPane;
15 47845 fdiaz
import javax.swing.SwingUtilities;
16 47821 jjdelcerro
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 47836 jjdelcerro
import org.gvsig.downloader.swing.DownloaderConfigServicePanel;
27 47838 fdiaz
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.i18n.I18nManager;
29 47821 jjdelcerro
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 47823 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingUtils;
36 47821 jjdelcerro
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
37
import org.gvsig.tools.swing.icontheme.IconTheme;
38
39
/**
40
 *
41
 * @author jjdelcerro
42
 */
43 47836 jjdelcerro
public class DownloaderConfigServicePanelImpl
44 47821 jjdelcerro
        extends DownloaderConfigServicePanelView
45 47836 jjdelcerro
        implements ChangeListenerSupport, DownloaderConfigServicePanel
46 47821 jjdelcerro
    {
47
48
    private DownloaderAuthenticationConfig config;
49
    private final ChangeListenerHelper changeListenerHelper;
50
    private boolean dirty;
51
52 47836 jjdelcerro
    public DownloaderConfigServicePanelImpl() {
53
        this(null);
54
    }
55
56
    public DownloaderConfigServicePanelImpl(String theServiceUrl) {
57 47821 jjdelcerro
        this.changeListenerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
58
        this.initComponents();
59 47836 jjdelcerro
        this.txtServiceURL.setText(theServiceUrl);
60 47821 jjdelcerro
    }
61
62
    private void initComponents() {
63
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
64
        DownloaderManager downloader = DownloaderLocator.getDownloaderManager();
65 47838 fdiaz
66
        toolsSwingManager.translate(lblAuthenmticationType);
67
        toolsSwingManager.translate(lblServiceURL);
68
        toolsSwingManager.translate(btnAuthenticationConfig);
69
        toolsSwingManager.translate(btnAuthenticationTest);
70 47821 jjdelcerro
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 47845 fdiaz
            SwingUtilities.invokeLater(() -> {
83
                doAuthenticationOrUrlChange();
84
                changeListenerHelper.fireEvent();
85
            });
86 47821 jjdelcerro
        });
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 47833 fdiaz
120
        this.cboAuthenticationType.setSelectedIndex(-1);
121
        ToolsSwingUtils.ensureRowsCols(this, 4, 100, 6, 200);
122
123 47821 jjdelcerro
    }
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 47845 fdiaz
        changeListenerHelper.fireEvent();
149 47821 jjdelcerro
    }
150
151 47836 jjdelcerro
    @Override
152 47821 jjdelcerro
    public void put(DownloaderAuthenticationConfig config) {
153
        this.config = (DownloaderAuthenticationConfig) CloneableUtils.cloneQuietly(config);
154 47828 jjdelcerro
        this.txtServiceURL.setText(this.config.getServiceUrl());
155 47821 jjdelcerro
        ListElement.setSelected(cboAuthenticationType, this.config.getFactory());
156
        this.doUpdateComponents();
157 47845 fdiaz
        changeListenerHelper.fireEvent();
158 47821 jjdelcerro
    }
159
160 47838 fdiaz
    @Override
161 47821 jjdelcerro
    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 47828 jjdelcerro
        if( !StringUtils.equals(this.config.getServiceUrl(),url) ) {
182 47821 jjdelcerro
            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 47845 fdiaz
        this.changeListenerHelper.fireEvent();
197 47821 jjdelcerro
    }
198
199
    private void doAuthenticationTest() {
200
        if( this.config == null ) {
201
            return;
202
        }
203 47838 fdiaz
        I18nManager i18n = ToolsLocator.getI18nManager();
204 47821 jjdelcerro
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
205 47838 fdiaz
        DownloaderManager manager = DownloaderLocator.getDownloaderManager();
206
        manager.removeCredentials(this.config.getServiceUrl());
207 47821 jjdelcerro
        DownloaderAuthenticationRequester requester = this.config.create();
208 47824 fdiaz
        if( !requester.requestAuthorization((Runnable command) -> {command.run();}) ) {
209 47821 jjdelcerro
            dialogs.messageDialog(
210 47838 fdiaz
                    i18n.getTranslation("_Authentication_test_failed"),
211
                    i18n.getTranslation("_Authentication_test"),
212 47821 jjdelcerro
                    JOptionPane.WARNING_MESSAGE
213
            );
214
            return;
215
        }
216
        dialogs.messageDialog(
217 47838 fdiaz
                i18n.getTranslation("_Authentication_test_passed"),
218
                i18n.getTranslation("_Authentication_test"),
219 47821 jjdelcerro
                JOptionPane.INFORMATION_MESSAGE
220
        );
221
    }
222
223 47836 jjdelcerro
    @Override
224 47821 jjdelcerro
    public boolean isTheConfigurationValid() {
225 47845 fdiaz
        return !this.isDirty() && this.config!=null && this.config.isFilled();
226 47821 jjdelcerro
    }
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 47823 jjdelcerro
    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 47836 jjdelcerro
276
    @Override
277
    public JComponent asJComponent() {
278
        return this;
279
    }
280 47821 jjdelcerro
281
}