Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / WizardPanel.java @ 45634

History | View | Annotate | Download (13.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.gui;
24

    
25
import java.awt.Window;
26
import javax.swing.JOptionPane;
27
import javax.swing.JPanel;
28
import org.cresques.cts.IProjection;
29
import org.gvsig.app.ApplicationLocator;
30
import org.gvsig.app.ApplicationManager;
31
import org.gvsig.app.gui.wizards.WizardListener;
32
import org.gvsig.app.gui.wizards.WizardListenerSupport;
33
import org.gvsig.app.prepareAction.PrepareContextView_v1;
34
import org.gvsig.app.project.documents.view.toc.actions.LayerErrorsPanel;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataStore;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.dal.DataTypes;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
41
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
import org.gvsig.fmap.mapcontrol.MapControlCreationException;
47
import org.gvsig.fmap.mapcontrol.MapControlLocator;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.dispose.DisposeUtils;
50
import org.gvsig.tools.dynobject.DynField;
51
import org.gvsig.tools.i18n.I18nManager;
52
import org.gvsig.tools.identitymanagement.UnauthorizedException;
53
import org.gvsig.tools.swing.api.ToolsSwingLocator;
54
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
55
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
57

    
58
public abstract class WizardPanel extends JPanel {
59

    
60
    private static final Logger logger = LoggerFactory.getLogger(WizardPanel.class);
61
    private static final long serialVersionUID = 5317749209668850863L;
62
    private String tabName = "TabName";
63
    private MapControl mapCtrl = null;
64
    private final WizardListenerSupport listenerSupport = new WizardListenerSupport();
65
    private MapContext mapContext;
66
    private boolean b_isMapControlAvailable = false;
67

    
68
    public void addWizardListener(WizardListener listener) {
69
        listenerSupport.addWizardListener(listener);
70
    }
71

    
72
    public void callError(Exception descripcion) {
73
        listenerSupport.callError(descripcion);
74
    }
75

    
76
    public void removeWizardListener(WizardListener listener) {
77
        listenerSupport.removeWizardListener(listener);
78
    }
79

    
80
    public void callStateChanged(boolean finishable) {
81
        listenerSupport.callStateChanged(finishable);
82
    }
83

    
84
    protected void setTabName(String name) {
85
        tabName = name;
86
    }
87

    
88
    public String getTabName() {
89
        return tabName;
90
    }
91

    
92
    abstract public void initWizard();
93

    
94
    /**
95
     * @deprecated use {@link #executeWizard()} instead.
96
     */
97
    abstract public void execute();
98

    
99
    /**
100
     * Executes the wizard and returns anything created in the process.
101
     *
102
     * @return anything created in the process
103
     */
104
    public Object executeWizard() {
105
        execute();
106
        return null;
107
    }
108

    
109
    abstract public void close();
110

    
111
    abstract public DataStoreParameters[] getParameters();
112

    
113
    /**
114
     * You can use it to interact with the MapControl component that will
115
     * receive the new layer, in order to get user feedback (for instance a
116
     * bounding box). Check the {@link #isMapControlAvailable()} method before
117
     * accessing the MapControl because it may not be available (for instance
118
     * when adding layers to a MapContext not associated with a View).
119
     *
120
     * For the moment, this method will return a non-null MapControl for
121
     * compatibility reasons, but you should still check
122
     * {@link #isMapControlAvailable()} to be sure it is a valid one, as it
123
     * could only be a fake MapControl.
124
     *
125
     * It is recommended to use {@link #getMapContext()} method when no
126
     * interaction is needed with the map user interface (for instance to get
127
     * the active projection, visible extent, etc)
128
     *
129
     * @return Returns the mapCtrl.
130
     */
131
    public MapControl getMapCtrl() {
132
        if (mapCtrl != null) {
133
            return mapCtrl;
134
        } else if (mapContext != null) {
135
                    // if MapContext has been set, create a fake MapControl
136
            // for compatibility purposes
137
            MapControl mc;
138
            try {
139
                mc = MapControlLocator.getMapControlManager().createJMapControlPanel(mapContext);
140
            } catch (MapControlCreationException ex) {
141
                logger.warn("Can't create a MapControl.", ex);
142
                throw new RuntimeException(ex);
143
            }
144
            mapCtrl = mc;
145
        }
146
        return mapCtrl;
147
    }
148

    
149
    /**
150
     * Sets the MapControl that will receive the new layer
151
     *
152
     * @param mapCtrl The mapCtrl to set.
153
     */
154
    public void setMapCtrl(MapControl mapCtrl) {
155
        this.mapCtrl = mapCtrl;
156
        b_isMapControlAvailable = (mapCtrl != null);
157
    }
158

    
159
    /**
160
     * You can use it to extract information from the MapContext that will
161
     * receive the new layer. For example, projection to use, or visible extent.
162
     *
163
     * @return Returns the MapContext.
164
     */
165
    public MapContext getMapContext() {
166
        if (this.mapContext != null || this.mapCtrl == null) {
167
            return this.mapContext;
168
        } else {
169
            return this.mapCtrl.getMapContext();
170
        }
171

    
172
    }
173

    
174
    /**
175
     * Sets the MapContext that will receive the new layer
176
     *
177
     * @param mapContext The mapContext to set.
178
     */
179
    public void setMapContext(MapContext mapContext) {
180
        this.mapContext = mapContext;
181
    }
182

    
183
    /**
184
     * Checks whether the MapControl is available. The MapControl may not be
185
     * available in some circumstances, for instance when adding layers to a
186
     * MapContext not associated with a View.
187
     *
188
     * A MapContext should always be available on the {@link #getMapContext()}
189
     * method.
190
     *
191
     * @return true if the MapControl is available, false otherwise
192
     */
193
    public boolean isMapControlAvailable() {
194
        return b_isMapControlAvailable;
195
    }
196

    
197
    protected void doAddLayer(
198
            final String layerName, final DataStoreParameters parameters) {
199
        final boolean b_isMapControlAvail = this.isMapControlAvailable();
200
        final MapControl mapControl = this.getMapCtrl();
201
        final MapContext mapContext = this.getMapContext();
202
        final ApplicationManager application = ApplicationLocator.getManager();
203
        final MapContextManager manager
204
                = MapContextLocator.getMapContextManager();
205

    
206
        logger.info("addLayer('{}',...)", layerName);
207
        Thread task = new Thread(new Runnable() {
208

    
209
            @Override
210
            public void run() {
211
                FLayer layer = null;
212
                FLayer preparedLayer = null;
213
                try {
214
                    DataManager dataManager = DALLocator.getDataManager();
215
                    DataStore dataStore = dataManager.openStore(parameters.getDataStoreName(), parameters);
216
                    String layerName = dataStore.getName();
217
                    layer = manager.createLayer(layerName, dataStore);
218
                    DisposeUtils.disposeQuietly(dataStore);
219
                    preparedLayer
220
                            = application.prepareOpenLayer(layer,
221
                                    new PrepareContextView_v1() {
222

    
223
                                        @Override
224
                                        public Window getOwnerWindow() {
225
                                            return null;
226
                                        }
227

    
228
                                        @Override
229
                                        public MapControl getMapControl() {
230
                                            return mapControl;
231
                                        }
232

    
233
                                        @Override
234
                                        public IProjection getViewProjection() {
235
                                            return mapContext.getProjection();
236
                                        }
237

    
238
                                        @Override
239
                                        public MapContext getMapContext() {
240
                                            return mapContext;
241
                                        }
242

    
243
                                        @Override
244
                                        public boolean isMapControlAvailable() {
245
                                            return b_isMapControlAvail;
246
                                        }
247
                                    });
248
                    if (preparedLayer != null) {
249
                        mapContext.getLayers().addLayer(preparedLayer);
250
                    }
251
                } catch (UnauthorizedException e) {
252
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
253
                    ApplicationManager application = ApplicationLocator.getManager();
254
                    String resource = "";
255
                    if (e.getResource() instanceof FilesystemStoreParameters) {
256
                        resource = ((FilesystemStoreParameters) e.getResource()).getFile().getPath();
257
                    }
258
                    application.messageDialog(
259
                            i18nManager.getTranslation("_User_0_is_not_authorized_to_1_on_resource_2_3",
260
                                    new String[]{
261
                                        e.getIdentity().getID(),
262
                                        e.getActionName(),
263
                                        e.getResourceName(),
264
                                        resource
265
                                    }),
266
                            i18nManager.getTranslation("_Unauthorized_access"),
267
                            JOptionPane.WARNING_MESSAGE
268
                    );
269
                    logger.warn("Unauthorized access to layer '" + layerName + "'.", e);
270

    
271
                } catch (Exception e) {
272
                    LayerErrorsPanel panel = new LayerErrorsPanel(layerName, e);
273
                    if (preparedLayer != null) {
274
                        panel.setLayer(preparedLayer);
275
                    } else if(layer!=null){
276
                        panel.setLayer(layer);
277
                    }
278
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
279
                    ToolsSwingLocator.getWindowManager().showWindow(
280
                        panel,
281
                        i18nManager.getTranslation("_Problems_loading_the_layer"),
282
                        WindowManager.MODE.WINDOW
283
                    );
284
                    logger.warn("Can't load layer '" + layerName + "'.", e);
285

    
286
                } finally {
287
                    if (preparedLayer != layer) {
288
                        DisposeUtils.disposeQuietly(preparedLayer);
289
                    }
290
                    DisposeUtils.disposeQuietly(layer);
291
                }
292
            }
293
        });
294
        task.start();
295

    
296
    }
297

    
298
    /**
299
     *
300
     * @param mapControl
301
     * @param layerName
302
     * @param parameters
303
     * @deprecated Use {@link #doAddLayer(String, DataStoreParameters)} in
304
     * combination with {@link #setMapCtrl(MapControl)} if you need to set the
305
     * MapControl. Note that MapControl is automatically initialized when
306
     * creating the panel from the AddLayer extension.
307
     */
308
    protected void doAddLayer(final MapControl mapControl,
309
            final String layerName, final DataStoreParameters parameters) {
310
        this.setMapCtrl(mapControl);
311
        doAddLayer(layerName, parameters);
312
    }
313

    
314
    /**
315
     * This method is called for example when user changes tab in add layer
316
     * dialog (new tab's settings are valid?)
317
     *
318
     * @return whether current wizard settings are enough (for example, to
319
     * enable an Accept button in a container)
320
     */
321
    public boolean areSettingsValid() {
322
        return true;
323
    }
324

    
325
    protected void fixCRS(DataStoreParameters params) {
326
        IProjection proj = this.getMapCtrl().getProjection();
327

    
328
        // Buscamos por el parametro de la proyeccion
329
        // que sean obligatorios y est?n a null
330
        // y le ponemos la proyeccion de la vista
331
        DynField[] fields = params.getDynClass().getDynFields();
332
        int crsfields = 0;
333
        DynField crsfield = null;
334
        for (DynField field : fields) {
335
            if (field.getType() == DataTypes.CRS) {
336
                crsfields++;
337
                crsfield = field;
338
                if (field.isMandatory()) {
339
                    if (params.getDynValue(field.getName()) == null) {
340
                        params.setDynValue(field.getName(), proj);
341
                    }
342
                }
343
            }
344
        }
345
        if (crsfields == 1 && crsfield != null) {
346
            // Si solo hay un parametro CRS y esta a null, aunque no sea
347
            // obligatorio le asigna el CRS de la vista.
348
            if (params.getDynValue(crsfield.getName()) == null) {
349
                params.setDynValue(crsfield.getName(), proj);
350
            }
351
        }
352
    }
353

    
354
}