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 / DownloaderConfigServicePanel.java @ 47821
History | View | Annotate | Download (7.96 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.JOptionPane; |
14 |
import javax.swing.event.ChangeListener; |
15 |
import javax.swing.event.DocumentEvent; |
16 |
import javax.swing.event.DocumentListener; |
17 |
import org.apache.commons.io.FilenameUtils; |
18 |
import org.apache.commons.lang3.StringUtils; |
19 |
import org.gvsig.downloader.DownloaderAuthenticationConfig; |
20 |
import org.gvsig.downloader.DownloaderAuthenticationFactory; |
21 |
import org.gvsig.downloader.DownloaderAuthenticationRequester; |
22 |
import org.gvsig.downloader.DownloaderLocator; |
23 |
import org.gvsig.downloader.DownloaderManager; |
24 |
import org.gvsig.tools.lang.CloneableUtils; |
25 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
26 |
import org.gvsig.tools.swing.api.ChangeListenerSupport; |
27 |
import org.gvsig.tools.swing.api.ListElement; |
28 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
29 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
30 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
31 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
32 |
|
33 |
/**
|
34 |
*
|
35 |
* @author jjdelcerro
|
36 |
*/
|
37 |
public class DownloaderConfigServicePanel |
38 |
extends DownloaderConfigServicePanelView
|
39 |
implements ChangeListenerSupport
|
40 |
{ |
41 |
|
42 |
private DownloaderAuthenticationConfig config;
|
43 |
private final ChangeListenerHelper changeListenerHelper; |
44 |
private boolean dirty; |
45 |
|
46 |
public DownloaderConfigServicePanel() {
|
47 |
this.changeListenerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
48 |
this.initComponents();
|
49 |
} |
50 |
|
51 |
private void initComponents() { |
52 |
ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
53 |
DownloaderManager downloader = DownloaderLocator.getDownloaderManager(); |
54 |
|
55 |
toolsSwingManager.addClearButton(txtServiceURL); |
56 |
toolsSwingManager.setDefaultPopupMenu(txtServiceURL); |
57 |
|
58 |
DefaultComboBoxModel<DownloaderAuthenticationFactory> authenticationTypeModel = new DefaultComboBoxModel<>(); |
59 |
for (DownloaderAuthenticationFactory authenticationType : downloader.getAuthenticationTypes()) {
|
60 |
authenticationTypeModel.addElement(authenticationType); |
61 |
} |
62 |
this.cboAuthenticationType.setModel(authenticationTypeModel);
|
63 |
this.cboAuthenticationType.setSelectedIndex(0); |
64 |
|
65 |
this.cboAuthenticationType.addActionListener((ActionEvent e) -> { |
66 |
doAuthenticationOrUrlChange(); |
67 |
changeListenerHelper.fireEvent(); |
68 |
}); |
69 |
|
70 |
this.btnAuthenticationConfig.addActionListener((ActionEvent e) -> { |
71 |
doAuthenticationConfig(); |
72 |
}); |
73 |
this.btnAuthenticationTest.addActionListener((ActionEvent e) -> { |
74 |
doAuthenticationTest(); |
75 |
}); |
76 |
this.txtServiceURL.addKeyListener(new KeyAdapter() { |
77 |
@Override
|
78 |
public void keyTyped(KeyEvent e) { |
79 |
if( e.getKeyChar()=='\n' ) { |
80 |
doAuthenticationOrUrlChange(); |
81 |
changeListenerHelper.fireEvent(); |
82 |
} |
83 |
} |
84 |
}); |
85 |
this.txtServiceURL.getDocument().addDocumentListener(new DocumentListener() { |
86 |
@Override
|
87 |
public void insertUpdate(DocumentEvent e) { |
88 |
setDirty(true);
|
89 |
} |
90 |
|
91 |
@Override
|
92 |
public void removeUpdate(DocumentEvent e) { |
93 |
setDirty(true);
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void changedUpdate(DocumentEvent e) { |
98 |
setDirty(true);
|
99 |
} |
100 |
}); |
101 |
} |
102 |
|
103 |
private void setDirty(boolean dirty) { |
104 |
this.dirty = dirty;
|
105 |
changeListenerHelper.fireEvent(); |
106 |
} |
107 |
|
108 |
private boolean isDirty() { |
109 |
return this.dirty; |
110 |
} |
111 |
|
112 |
private void doUpdateComponents() { |
113 |
if( config == null ) { |
114 |
this.btnAuthenticationConfig.setEnabled(false); |
115 |
this.btnAuthenticationTest.setEnabled(false); |
116 |
return;
|
117 |
} |
118 |
this.btnAuthenticationConfig.setEnabled(true); |
119 |
this.btnAuthenticationTest.setEnabled(true); |
120 |
} |
121 |
|
122 |
public void clear() { |
123 |
this.config = null; |
124 |
this.txtServiceURL.setText(""); |
125 |
cboAuthenticationType.setSelectedIndex(0);
|
126 |
} |
127 |
|
128 |
public void put(DownloaderAuthenticationConfig config) { |
129 |
this.config = (DownloaderAuthenticationConfig) CloneableUtils.cloneQuietly(config);
|
130 |
this.txtServiceURL.setText(this.config.getBaseUrl()); |
131 |
ListElement.setSelected(cboAuthenticationType, this.config.getFactory());
|
132 |
this.doUpdateComponents();
|
133 |
} |
134 |
|
135 |
public DownloaderAuthenticationConfig fetch() {
|
136 |
if( this.config == null ) { |
137 |
return null; |
138 |
} |
139 |
return (DownloaderAuthenticationConfig) CloneableUtils.cloneQuietly(config);
|
140 |
} |
141 |
|
142 |
private void doAuthenticationOrUrlChange() { |
143 |
DownloaderAuthenticationFactory authenticationType = (DownloaderAuthenticationFactory) this.cboAuthenticationType.getSelectedItem();
|
144 |
if( authenticationType==null ) { |
145 |
return;
|
146 |
} |
147 |
String url = StringUtils.defaultIfBlank(this.txtServiceURL.getText(), null); |
148 |
if( StringUtils.isBlank(url) ) {
|
149 |
return;
|
150 |
} |
151 |
url = url.trim(); |
152 |
if( this.config==null || !StringUtils.equalsIgnoreCase(authenticationType.getProviderName(), this.config.getProviderName())) { |
153 |
this.config = authenticationType.create(url);
|
154 |
} |
155 |
if( !StringUtils.equals(this.config.getBaseUrl(),url) ) { |
156 |
this.config = authenticationType.create(url);
|
157 |
} |
158 |
setDirty(false);
|
159 |
doUpdateComponents(); |
160 |
} |
161 |
|
162 |
private void doAuthenticationConfig() { |
163 |
if( this.config == null ) { |
164 |
return;
|
165 |
} |
166 |
if( !this.config.isConfigurable() ) { |
167 |
return;
|
168 |
} |
169 |
this.config.requestAuthenticationConfig();
|
170 |
} |
171 |
|
172 |
private void doAuthenticationTest() { |
173 |
if( this.config == null ) { |
174 |
return;
|
175 |
} |
176 |
ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager(); |
177 |
DownloaderAuthenticationRequester requester = this.config.create();
|
178 |
if( !requester.requestAuthorization() ) {
|
179 |
dialogs.messageDialog( |
180 |
"Authentication test failed",
|
181 |
"Authentication test",
|
182 |
JOptionPane.WARNING_MESSAGE
|
183 |
); |
184 |
return;
|
185 |
} |
186 |
dialogs.messageDialog( |
187 |
"Authentication test passed",
|
188 |
"Authentication test",
|
189 |
JOptionPane.INFORMATION_MESSAGE
|
190 |
); |
191 |
} |
192 |
|
193 |
public boolean isTheConfigurationValid() { |
194 |
return this.config!=null && !this.isDirty(); |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public ImageIcon loadImage( String imageName ) { |
199 |
String name = FilenameUtils.getBaseName(imageName);
|
200 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getDefault(); |
201 |
if (theme.exists(name)) {
|
202 |
return theme.get(name);
|
203 |
} |
204 |
return new ImageIcon(); |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void addChangeListener(ChangeListener listener) { |
209 |
this.changeListenerHelper.addChangeListener(listener);
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public ChangeListener[] getChangeListeners() { |
214 |
return this.changeListenerHelper.getChangeListeners(); |
215 |
} |
216 |
|
217 |
@Override
|
218 |
public void removeChangeListener(ChangeListener listener) { |
219 |
this.changeListenerHelper.removeChangeListener(listener);
|
220 |
} |
221 |
|
222 |
@Override
|
223 |
public void removeAllChangeListener() { |
224 |
this.changeListenerHelper.removeAllChangeListener();
|
225 |
} |
226 |
|
227 |
@Override
|
228 |
public boolean hasChangeListeners() { |
229 |
return this.changeListenerHelper.hasChangeListeners(); |
230 |
} |
231 |
|
232 |
|
233 |
} |