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 @ 42877

History | View | Annotate | Download (12.4 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

    
27
import javax.swing.JOptionPane;
28
import javax.swing.JPanel;
29

    
30
import org.cresques.cts.IProjection;
31

    
32
import org.gvsig.app.ApplicationLocator;
33
import org.gvsig.app.ApplicationManager;
34
import org.gvsig.app.gui.wizards.WizardListener;
35
import org.gvsig.app.gui.wizards.WizardListenerSupport;
36
import org.gvsig.app.prepareAction.PrepareContextView_v1;
37
import org.gvsig.app.project.documents.view.toc.actions.LayerErrorsPanel;
38
import org.gvsig.fmap.dal.DALLocator;
39
import org.gvsig.fmap.dal.DataManager;
40
import org.gvsig.fmap.dal.DataStore;
41
import org.gvsig.fmap.dal.DataStoreParameters;
42
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
43
import org.gvsig.fmap.mapcontext.MapContext;
44
import org.gvsig.fmap.mapcontext.MapContextLocator;
45
import org.gvsig.fmap.mapcontext.MapContextManager;
46
import org.gvsig.fmap.mapcontext.layers.FLayer;
47
import org.gvsig.fmap.mapcontrol.MapControl;
48
import org.gvsig.fmap.mapcontrol.MapControlCreationException;
49
import org.gvsig.fmap.mapcontrol.MapControlLocator;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.dispose.DisposeUtils;
52
import org.gvsig.tools.i18n.I18nManager;
53
import org.gvsig.tools.identitymanagement.UnauthorizedException;
54
import org.gvsig.tools.swing.api.ToolsSwingLocator;
55
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
56

    
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59

    
60
public abstract class WizardPanel extends JPanel {
61

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

    
70
    public void addWizardListener(WizardListener listener) {
71
        listenerSupport.addWizardListener(listener);
72
    }
73

    
74
    public void callError(Exception descripcion) {
75
        listenerSupport.callError(descripcion);
76
    }
77

    
78
    public void removeWizardListener(WizardListener listener) {
79
        listenerSupport.removeWizardListener(listener);
80
    }
81

    
82
    public void callStateChanged(boolean finishable) {
83
        listenerSupport.callStateChanged(finishable);
84
    }
85

    
86
    protected void setTabName(String name) {
87
        tabName = name;
88
    }
89

    
90
    public String getTabName() {
91
        return tabName;
92
    }
93

    
94
    abstract public void initWizard();
95

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

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

    
111
    abstract public void close();
112

    
113
    abstract public DataStoreParameters[] getParameters();
114

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

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

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

    
174
    }
175

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

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

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

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

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

    
225
                                        @Override
226
                                        public Window getOwnerWindow() {
227
                                            return null;
228
                                        }
229

    
230
                                        @Override
231
                                        public MapControl getMapControl() {
232
                                            return mapControl;
233
                                        }
234

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

    
240
                                        @Override
241
                                        public MapContext getMapContext() {
242
                                            return mapContext;
243
                                        }
244

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

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

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

    
298
    }
299

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

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

    
327
}