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