svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerWizardPanel.java @ 44198
History | View | Annotate | Download (15.7 KB)
1 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.serverexplorer.filesystem.swing; |
---|---|---|---|
2 | |||
3 | import java.awt.GridBagConstraints; |
||
4 | import java.awt.GridBagLayout; |
||
5 | 43152 | fdiaz | import java.awt.Window; |
6 | 40435 | jjdelcerro | import java.io.File; |
7 | import java.util.ArrayList; |
||
8 | import java.util.Iterator; |
||
9 | import java.util.List; |
||
10 | import java.util.prefs.Preferences; |
||
11 | |||
12 | 43152 | fdiaz | import javax.swing.JOptionPane; |
13 | 41258 | jjdelcerro | import javax.swing.event.AncestorEvent; |
14 | import javax.swing.event.AncestorListener; |
||
15 | 40435 | jjdelcerro | import javax.swing.filechooser.FileFilter; |
16 | |||
17 | 43152 | fdiaz | import org.cresques.cts.IProjection; |
18 | 44198 | jjdelcerro | import org.gvsig.andami.PluginsLocator; |
19 | import org.gvsig.andami.PluginsManager; |
||
20 | 43152 | fdiaz | import org.slf4j.Logger; |
21 | import org.slf4j.LoggerFactory; |
||
22 | |||
23 | 44198 | jjdelcerro | //import org.gvsig.andami.Launcher;
|
24 | 40435 | jjdelcerro | import org.gvsig.app.ApplicationLocator; |
25 | import org.gvsig.app.ApplicationManager; |
||
26 | import org.gvsig.app.gui.WizardPanel; |
||
27 | import org.gvsig.app.prepareAction.PrepareContext; |
||
28 | 43152 | fdiaz | import org.gvsig.app.prepareAction.PrepareContextView_v1; |
29 | import org.gvsig.app.project.documents.view.toc.actions.LayerErrorsPanel; |
||
30 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
31 | import org.gvsig.fmap.dal.DataManager; |
||
32 | 43152 | fdiaz | import org.gvsig.fmap.dal.DataStore; |
33 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DataStoreParameters; |
34 | 43152 | fdiaz | import org.gvsig.fmap.dal.DataStoreProviderFactory; |
35 | import org.gvsig.fmap.dal.DataTypes; |
||
36 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
37 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter; |
||
38 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
39 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
40 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
||
41 | 43152 | fdiaz | import org.gvsig.fmap.mapcontext.MapContext; |
42 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
43 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
44 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
45 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
46 | 42775 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
47 | 43152 | fdiaz | import org.gvsig.tools.dispose.DisposeUtils; |
48 | import org.gvsig.tools.dynobject.DynField; |
||
49 | 40435 | jjdelcerro | import org.gvsig.tools.dynobject.DynObject; |
50 | 42775 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
51 | 43152 | fdiaz | import org.gvsig.tools.identitymanagement.UnauthorizedException; |
52 | 40435 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
53 | 43152 | fdiaz | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
54 | 40435 | jjdelcerro | |
55 | 43152 | fdiaz | |
56 | 40435 | jjdelcerro | /**
|
57 | 43152 | fdiaz | * @author fdiaz
|
58 | *
|
||
59 | 40435 | jjdelcerro | */
|
60 | 43152 | fdiaz | public class FilesystemExplorerWizardPanel extends WizardPanel { |
61 | 40435 | jjdelcerro | |
62 | /**
|
||
63 | 43152 | fdiaz | *
|
64 | */
|
||
65 | private static final long serialVersionUID = 788115220334290212L; |
||
66 | 40435 | jjdelcerro | |
67 | 43152 | fdiaz | private static final Logger logger = LoggerFactory.getLogger(FilesystemExplorerWizardPanel.class); |
68 | 40435 | jjdelcerro | |
69 | 43152 | fdiaz | private static final String DEFAULT_FILTER = "All_supported"; |
70 | |||
71 | 40435 | jjdelcerro | public static final String OPEN_LAYER_FILE_CHOOSER_ID = |
72 | "OPEN_LAYER_FILE_CHOOSER_ID";
|
||
73 | |||
74 | 43152 | fdiaz | protected FilesystemServerExplorer explorer;
|
75 | private ArrayList<MyFileFilter> filters; |
||
76 | 40435 | jjdelcerro | |
77 | private static String lastPath = null; |
||
78 | 43152 | fdiaz | private FilesystemExplorerWizardPanelController panel = null; |
79 | 43534 | jjdelcerro | private boolean tabularOnly; |
80 | 43152 | fdiaz | /**
|
81 | *
|
||
82 | */
|
||
83 | 41258 | jjdelcerro | public FilesystemExplorerWizardPanel() {
|
84 | 43534 | jjdelcerro | this(false); |
85 | } |
||
86 | |||
87 | public FilesystemExplorerWizardPanel(boolean tabularOnly) { |
||
88 | 41258 | jjdelcerro | super();
|
89 | 43534 | jjdelcerro | this.tabularOnly = tabularOnly;
|
90 | 41258 | jjdelcerro | this.addAncestorListener(new AncestorListener() { |
91 | public void ancestorAdded(AncestorEvent ae) { |
||
92 | 41416 | jjdelcerro | initExplorer(); |
93 | initFilters(); |
||
94 | 43152 | fdiaz | updatePanel(); |
95 | 41258 | jjdelcerro | } |
96 | public void ancestorRemoved(AncestorEvent ae) { |
||
97 | dispose(); |
||
98 | } |
||
99 | public void ancestorMoved(AncestorEvent ae) { |
||
100 | } |
||
101 | }); |
||
102 | } |
||
103 | 40435 | jjdelcerro | |
104 | 43152 | fdiaz | protected void updatePanel() { |
105 | this.panel.setExplorer(this.explorer); |
||
106 | this.panel.setFilters(this.filters); |
||
107 | 40435 | jjdelcerro | } |
108 | |||
109 | 43152 | fdiaz | @Override
|
110 | 40435 | jjdelcerro | public void initWizard() { |
111 | 43152 | fdiaz | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
112 | setTabName(i18nManager.getTranslation("Fichero"));
|
||
113 | 40435 | jjdelcerro | if (lastPath == null) { |
114 | Preferences prefs = Preferences.userRoot().node("gvsig.foldering"); |
||
115 | 43486 | jjdelcerro | lastPath = prefs.get("DataFolder", System.getProperty("user.home")); |
116 | 40435 | jjdelcerro | } |
117 | 41416 | jjdelcerro | initExplorer(); |
118 | initFilters(); |
||
119 | initUI(); |
||
120 | } |
||
121 | 40435 | jjdelcerro | |
122 | 43152 | fdiaz | private void initUI() { |
123 | |||
124 | setLayout(new GridBagLayout()); |
||
125 | GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
||
126 | gridBagConstraints.fill = GridBagConstraints.BOTH;
|
||
127 | gridBagConstraints.weightx = 1.0;
|
||
128 | gridBagConstraints.weighty = 1.0;
|
||
129 | |||
130 | panel = new FilesystemExplorerWizardPanelController(getMapContext(),explorer, filters);
|
||
131 | 43534 | jjdelcerro | panel.setTabularOnly(this.tabularOnly);
|
132 | 43152 | fdiaz | this.add(panel, gridBagConstraints);
|
133 | |||
134 | } |
||
135 | |||
136 | @Override
|
||
137 | public void execute() { |
||
138 | if (this.getMapCtrl() == null) { |
||
139 | throw new IllegalArgumentException("MapControl need"); |
||
140 | } |
||
141 | String layerName;
|
||
142 | |||
143 | for (DataStoreParameters params : this.getParameters()) { |
||
144 | IProjection proj = this.getMapCtrl().getProjection();
|
||
145 | |||
146 | // Buscamos por el parametro de la proyeccion
|
||
147 | // que sean obligatorios y est?n a null
|
||
148 | // y le ponemos la proyeccion de la vista
|
||
149 | DynField[] fields = params.getDynClass().getDynFields();
|
||
150 | 43466 | jjdelcerro | int crsfields = 0; |
151 | DynField crsfield = null;
|
||
152 | 43152 | fdiaz | for (DynField field : fields) {
|
153 | 43466 | jjdelcerro | if (field.getType() == DataTypes.CRS ) {
|
154 | crsfields++; |
||
155 | crsfield = field; |
||
156 | if( field.isMandatory() ) {
|
||
157 | if (params.getDynValue(field.getName()) == null) { |
||
158 | params.setDynValue(field.getName(), proj); |
||
159 | } |
||
160 | 43152 | fdiaz | } |
161 | } |
||
162 | } |
||
163 | 43466 | jjdelcerro | if( crsfields == 1 && crsfield!=null ) { |
164 | // Si solo hay un parametro CRS y esta a null, aunque no sea
|
||
165 | // obligatorio le asigna el CRS de la vista.
|
||
166 | if (params.getDynValue(crsfield.getName()) == null) { |
||
167 | params.setDynValue(crsfield.getName(), proj); |
||
168 | } |
||
169 | } |
||
170 | 43152 | fdiaz | |
171 | layerName = ((FilesystemStoreParameters) params).getFile().getName(); |
||
172 | |||
173 | this.doAddLayer(layerName, params);
|
||
174 | } |
||
175 | } |
||
176 | |||
177 | @Override
|
||
178 | public void close() { |
||
179 | this.dispose();
|
||
180 | } |
||
181 | |||
182 | /**
|
||
183 | * Dispose filters and explorer
|
||
184 | */
|
||
185 | public void dispose() { |
||
186 | if (explorer != null) { |
||
187 | explorer.dispose(); |
||
188 | explorer = null;
|
||
189 | } |
||
190 | if (filters != null) { |
||
191 | filters.clear(); |
||
192 | filters = null;
|
||
193 | } |
||
194 | panel.dispose(); |
||
195 | } |
||
196 | |||
197 | @Override
|
||
198 | public DataStoreParameters[] getParameters() { |
||
199 | return this.panel.getParameters(); |
||
200 | } |
||
201 | |||
202 | /**
|
||
203 | * @param files
|
||
204 | */
|
||
205 | public void addFiles(List<File> files) { |
||
206 | this.panel.addFiles(files);
|
||
207 | } |
||
208 | |||
209 | |||
210 | 41416 | jjdelcerro | private void initExplorer() { |
211 | if (this.explorer == null) { |
||
212 | DataManager dm = DALLocator.getDataManager(); |
||
213 | FilesystemServerExplorerParameters param; |
||
214 | try {
|
||
215 | 43152 | fdiaz | param = (FilesystemServerExplorerParameters) dm |
216 | 41416 | jjdelcerro | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
217 | param.setInitialpath(lastPath); |
||
218 | 43152 | fdiaz | explorer = (FilesystemServerExplorer) dm.openServerExplorer( |
219 | 41416 | jjdelcerro | FilesystemServerExplorer.NAME, param); |
220 | } catch (Exception e) { |
||
221 | throw new RuntimeException(e); |
||
222 | } |
||
223 | 40435 | jjdelcerro | } |
224 | 41416 | jjdelcerro | } |
225 | 40435 | jjdelcerro | |
226 | 41416 | jjdelcerro | private void initFilters() { |
227 | if (this.filters == null) { |
||
228 | int mode = FilesystemServerExplorer.MODE_ALL;
|
||
229 | this.filters = new ArrayList<MyFileFilter>(); |
||
230 | if (this.getMapCtrl() == null) { |
||
231 | mode = FilesystemServerExplorer.MODE_FEATURE; |
||
232 | } else {
|
||
233 | 43152 | fdiaz | mode = FilesystemServerExplorer.MODE_GEOMETRY |
234 | 41416 | jjdelcerro | | FilesystemServerExplorer.MODE_RASTER; |
235 | } |
||
236 | |||
237 | 43152 | fdiaz | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
238 | |||
239 | 41416 | jjdelcerro | // First filter in list will be 'All formats' filter
|
240 | this.filters.add(new MyFileFilter(explorer.getFilter(mode, |
||
241 | 43152 | fdiaz | i18nManager.getTranslation(DEFAULT_FILTER)))); |
242 | 41416 | jjdelcerro | |
243 | @SuppressWarnings("unchecked") |
||
244 | Iterator<FilesystemFileFilter> iter = explorer.getFilters(mode);
|
||
245 | while (iter.hasNext()) {
|
||
246 | this.filters.add(new MyFileFilter(iter.next())); |
||
247 | } |
||
248 | 40435 | jjdelcerro | } |
249 | } |
||
250 | |||
251 | 43152 | fdiaz | boolean isTiled(DataStoreParameters parameters){
|
252 | return this.panel.isTiled(parameters); |
||
253 | 40435 | jjdelcerro | } |
254 | |||
255 | 43152 | fdiaz | @Override
|
256 | protected void doAddLayer(final String layerName, final DataStoreParameters parameters) { |
||
257 | final boolean b_isMapControlAvail = this.isMapControlAvailable(); |
||
258 | final MapControl mapControl = this.getMapCtrl(); |
||
259 | final MapContext mapContext = this.getMapContext(); |
||
260 | final ApplicationManager application = ApplicationLocator.getManager();
|
||
261 | final MapContextManager manager
|
||
262 | = MapContextLocator.getMapContextManager(); |
||
263 | 40435 | jjdelcerro | |
264 | 43152 | fdiaz | logger.info("addLayer('{}',...)", layerName);
|
265 | Thread task = new Thread(new Runnable() { |
||
266 | 40435 | jjdelcerro | |
267 | 43152 | fdiaz | @Override
|
268 | public void run() { |
||
269 | FLayer layer = null;
|
||
270 | FLayer preparedLayer = null;
|
||
271 | try {
|
||
272 | DataManager dataManager = DALLocator.getDataManager(); |
||
273 | DataStore dataStore = dataManager.openStore(parameters.getDataStoreName(), parameters); |
||
274 | 43190 | jjdelcerro | DataStoreProviderFactory factory = dataStore.getProviderFactory(); |
275 | 43321 | fdiaz | if (factory.isTiledSupported() != DataStoreProviderFactory.NO && !factory.getClass().getSimpleName().equals("TileProviderFactory")) { |
276 | 43152 | fdiaz | if (isTiled(parameters)) {
|
277 | DynObject tileParameters = dataManager.createStoreParameters("TileCache");
|
||
278 | 40435 | jjdelcerro | |
279 | 44198 | jjdelcerro | PluginsManager pluginsManager = PluginsLocator.getManager(); |
280 | File rootFolder = new File(pluginsManager.getApplicationHomeFolder(),"gvsig_rcache"); |
||
281 | 40435 | jjdelcerro | |
282 | 43152 | fdiaz | if (tileParameters.getDynClass().getDynField("rootFolder") != null) { |
283 | tileParameters.setDynValue("rootFolder", rootFolder);
|
||
284 | } |
||
285 | 40435 | jjdelcerro | |
286 | 43152 | fdiaz | try {
|
287 | dataStore.useCache("TileCache", tileParameters);
|
||
288 | } catch (DataException e) {
|
||
289 | logger.warn("Can't cache the layer.", e);
|
||
290 | } |
||
291 | 40435 | jjdelcerro | |
292 | 43152 | fdiaz | } |
293 | } |
||
294 | 40435 | jjdelcerro | |
295 | 43152 | fdiaz | String layerName = dataStore.getName();
|
296 | layer = manager.createLayer(layerName, dataStore); |
||
297 | DisposeUtils.disposeQuietly(dataStore); |
||
298 | preparedLayer |
||
299 | = application.prepareOpenLayer(layer, |
||
300 | new PrepareContextView_v1() {
|
||
301 | 40435 | jjdelcerro | |
302 | 43152 | fdiaz | @Override
|
303 | public Window getOwnerWindow() { |
||
304 | return null; |
||
305 | } |
||
306 | 40435 | jjdelcerro | |
307 | 43152 | fdiaz | @Override
|
308 | public MapControl getMapControl() {
|
||
309 | return mapControl;
|
||
310 | } |
||
311 | 40435 | jjdelcerro | |
312 | 43152 | fdiaz | @Override
|
313 | public IProjection getViewProjection() {
|
||
314 | return mapContext.getProjection();
|
||
315 | } |
||
316 | 40435 | jjdelcerro | |
317 | 43152 | fdiaz | @Override
|
318 | public MapContext getMapContext() {
|
||
319 | return mapContext;
|
||
320 | } |
||
321 | 40435 | jjdelcerro | |
322 | 43152 | fdiaz | @Override
|
323 | public boolean isMapControlAvailable() { |
||
324 | return b_isMapControlAvail;
|
||
325 | } |
||
326 | }); |
||
327 | if (preparedLayer != null) { |
||
328 | 43639 | jjdelcerro | mapControl.addLayer(preparedLayer); |
329 | 43152 | fdiaz | } |
330 | } catch (UnauthorizedException e) {
|
||
331 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
332 | ApplicationManager application = ApplicationLocator.getManager(); |
||
333 | String resource = ""; |
||
334 | if (e.getResource() instanceof FilesystemStoreParameters) { |
||
335 | resource = ((FilesystemStoreParameters) e.getResource()).getFile().getPath(); |
||
336 | } |
||
337 | application.messageDialog( |
||
338 | i18nManager.getTranslation("_User_0_is_not_authorized_to_1_on_resource_2_3",
|
||
339 | new String[]{ |
||
340 | e.getIdentity().getID(), |
||
341 | e.getActionName(), |
||
342 | e.getResourceName(), |
||
343 | resource |
||
344 | }), |
||
345 | i18nManager.getTranslation("_Unauthorized_access"),
|
||
346 | JOptionPane.WARNING_MESSAGE
|
||
347 | ); |
||
348 | logger.warn("Unauthorized access to layer '" + layerName + "'.", e); |
||
349 | 40435 | jjdelcerro | |
350 | 43152 | fdiaz | } catch (Exception e) { |
351 | LayerErrorsPanel panel = new LayerErrorsPanel(layerName, e);
|
||
352 | if (preparedLayer != null) { |
||
353 | panel.setLayer(preparedLayer); |
||
354 | } else if(layer!=null){ |
||
355 | panel.setLayer(layer); |
||
356 | } |
||
357 | I18nManager i18nManager = ToolsLocator.getI18nManager(); |
||
358 | ToolsSwingLocator.getWindowManager().showWindow( |
||
359 | panel, |
||
360 | i18nManager.getTranslation("_Problems_loading_the_layer"),
|
||
361 | WindowManager.MODE.WINDOW |
||
362 | ); |
||
363 | logger.warn("Can't load layer '" + layerName + "'.", e); |
||
364 | 40435 | jjdelcerro | |
365 | 43152 | fdiaz | } finally {
|
366 | if (preparedLayer != layer) {
|
||
367 | DisposeUtils.disposeQuietly(preparedLayer); |
||
368 | 40435 | jjdelcerro | } |
369 | 43152 | fdiaz | DisposeUtils.disposeQuietly(layer); |
370 | } |
||
371 | } |
||
372 | }); |
||
373 | task.start(); |
||
374 | 40435 | jjdelcerro | |
375 | } |
||
376 | |||
377 | 42775 | jjdelcerro | |
378 | 40435 | jjdelcerro | /**
|
379 | 43152 | fdiaz | * @author fdiaz
|
380 | *
|
||
381 | 40435 | jjdelcerro | */
|
382 | public class MyFileFilter extends FileFilter { |
||
383 | |||
384 | 43152 | fdiaz | private FilesystemFileFilter filter = null; |
385 | 40435 | jjdelcerro | |
386 | 43152 | fdiaz | /**
|
387 | * @param params
|
||
388 | */
|
||
389 | 40435 | jjdelcerro | public MyFileFilter(FilesystemFileFilter params) {
|
390 | 43152 | fdiaz | this.setFilter(params);
|
391 | 40435 | jjdelcerro | } |
392 | |||
393 | /**
|
||
394 | * @see javax.swing.filechooser.FileFilter#accept(java.io.File)
|
||
395 | */
|
||
396 | public boolean accept(File f) { |
||
397 | if (f.isDirectory()) {
|
||
398 | return true; |
||
399 | } |
||
400 | 43152 | fdiaz | return getFilter().accept(f);
|
401 | 40435 | jjdelcerro | |
402 | } |
||
403 | |||
404 | /**
|
||
405 | * @see javax.swing.filechooser.FileFilter#getDescription()
|
||
406 | */
|
||
407 | public String getDescription() { |
||
408 | 43152 | fdiaz | return getFilter().getDescription();
|
409 | 40435 | jjdelcerro | } |
410 | |||
411 | 43152 | fdiaz | /**
|
412 | * @return
|
||
413 | */
|
||
414 | 40435 | jjdelcerro | public String getName() { |
415 | 43152 | fdiaz | return getFilter().getDataStoreProviderName();
|
416 | 40435 | jjdelcerro | } |
417 | |||
418 | 43152 | fdiaz | /**
|
419 | * @return the filter
|
||
420 | */
|
||
421 | public FilesystemFileFilter getFilter() {
|
||
422 | return filter;
|
||
423 | 40435 | jjdelcerro | } |
424 | |||
425 | 43152 | fdiaz | /**
|
426 | * @param filter the filter to set
|
||
427 | */
|
||
428 | public void setFilter(FilesystemFileFilter filter) { |
||
429 | this.filter = filter;
|
||
430 | 40435 | jjdelcerro | } |
431 | 41634 | jjdelcerro | } |
432 | |||
433 | 43152 | fdiaz | protected void showPropertiesDialog(DynObject parameters) { |
434 | panel.showPropertiesDialog(parameters); |
||
435 | 40435 | jjdelcerro | } |
436 | |||
437 | 43152 | fdiaz | protected PrepareContext getPrepareDataStoreContext() {
|
438 | return panel.getPrepareDataStoreContext();
|
||
439 | 40435 | jjdelcerro | } |
440 | |||
441 | } |