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