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