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

History | View | Annotate | Download (11.7 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.gui;
25

    
26
import java.awt.Window;
27
import java.util.logging.Level;
28
import javax.swing.JOptionPane;
29

    
30
import javax.swing.JPanel;
31
import org.apache.commons.lang3.StringUtils;
32

    
33
import org.cresques.cts.IProjection;
34
import org.gvsig.app.ApplicationLocator;
35
import org.gvsig.app.ApplicationManager;
36
import org.gvsig.app.gui.wizards.WizardListener;
37
import org.gvsig.app.gui.wizards.WizardListenerSupport;
38
import org.gvsig.app.prepareAction.PrepareContextView_v1;
39
import org.gvsig.fmap.dal.DALLocator;
40
import org.gvsig.fmap.dal.DataManager;
41
import org.gvsig.fmap.dal.DataStore;
42
import org.gvsig.fmap.dal.DataStoreParameters;
43
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
44
import org.gvsig.fmap.mapcontext.MapContext;
45
import org.gvsig.fmap.mapcontext.MapContextLocator;
46
import org.gvsig.fmap.mapcontext.MapContextManager;
47
import org.gvsig.fmap.mapcontext.layers.FLayer;
48
import org.gvsig.fmap.mapcontrol.MapControl;
49
import org.gvsig.fmap.mapcontrol.MapControlCreationException;
50
import org.gvsig.fmap.mapcontrol.MapControlLocator;
51
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.i18n.I18nManager;
53
import org.gvsig.tools.identitymanagement.UnauthorizedException;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
public abstract class WizardPanel extends JPanel {
58

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

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

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

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

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

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

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

    
91
    abstract public void initWizard();
92

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

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

    
108
    abstract public void close();
109

    
110
    abstract public DataStoreParameters[] getParameters();
111

    
112
    /**
113
     * You can use it to interact with the MapControl component that will
114
     * receive the new layer, in order to get user feedback
115
     * (for instance a bounding box). Check the
116
     * {@link #isMapControlAvailable()} method before accessing the MapControl
117
     * because it may not be available (for instance when adding layers
118
     * 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,
123
     * as it could only be a fake MapControl.
124
     * 
125
     * It is recommended to use {@link #getMapContext()} method
126
     * when no interaction is needed with the map user interface
127
     * (for instance to get 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
            }
135
            else if (mapContext!=null) {
136
                    // if MapContext has been set, create a fake MapControl
137
                    // for compatibility purposes
138
                    MapControl mc;
139
                try {
140
                    mc = MapControlLocator.getMapControlManager().createJMapControlPanel(mapContext);
141
                } catch (MapControlCreationException ex) {
142
                    logger.warn("Can't create a MapControl.", ex);
143
                    throw  new RuntimeException(ex);
144
                }
145
                    mapCtrl=mc;
146
            }
147
            return mapCtrl;
148
    }
149

    
150
    /**
151
     * Sets the MapControl that will receive the new layer
152
     * @param mapCtrl
153
     *            The mapCtrl to set.
154
     */
155
    public void setMapCtrl(MapControl mapCtrl) {
156
        this.mapCtrl = mapCtrl;
157
        b_isMapControlAvailable = (mapCtrl!=null);
158
    }
159
    
160
    /**
161
     * You can use it to extract information from
162
     * the MapContext that will receive the new layer.
163
     * 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
            }
171
            else {
172
                    return this.mapCtrl.getMapContext();
173
            }
174
        
175
    }
176

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

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

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

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

    
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
                    dataStore.dispose(); 
221
                    dataStore = null;
222
                    preparedLayer =
223
                        application.prepareOpenLayer(layer,
224
                            new PrepareContextView_v1() {
225

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

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

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

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

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

    
281
                } finally {
282
                    if (preparedLayer != null && preparedLayer != layer) {
283
                        preparedLayer.dispose();
284
                        preparedLayer = null;
285
                    }
286
                    if (layer != null) {
287
                        layer.dispose();
288
                        layer = null;
289
                    }
290
                }
291
            }
292
        });
293
        task.start();
294

    
295
    }
296
    
297
    /**
298
     * 
299
     * @param mapControl
300
     * @param layerName
301
     * @param parameters
302
     * @deprecated Use {@link #doAddLayer(String, DataStoreParameters)}
303
     * in combination with {@link #setMapCtrl(MapControl)} if you need
304
     * to set the MapControl. Note that MapControl is automatically initialized
305
     * when creating the panel from the AddLayer extension.
306
     */
307
    protected void doAddLayer(final MapControl mapControl,
308
        final String layerName, final DataStoreParameters parameters) {
309
            this.setMapCtrl(mapControl);
310
            doAddLayer(layerName, parameters);
311
    }
312
    
313
    /**
314
     * This method is called for example when user changes
315
     * tab in add layer dialog (new tab's settings are valid?)
316
     * 
317
     * @return whether current wizard settings are enough
318
     * (for example, to enable an Accept button in a container)
319
     */
320
    public boolean areSettingsValid() {
321
        return true;
322
    }
323
    
324
}