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

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

    
29
import javax.swing.JPanel;
30

    
31
import org.cresques.cts.IProjection;
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.fmap.dal.DALLocator;
38
import org.gvsig.fmap.dal.DataManager;
39
import org.gvsig.fmap.dal.DataStore;
40
import org.gvsig.fmap.dal.DataStoreParameters;
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.i18n.I18nManager;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
public abstract class WizardPanel extends JPanel {
54

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

    
63
    public void addWizardListener(WizardListener listener) {
64
        listenerSupport.addWizardListener(listener);
65
    }
66

    
67
    public void callError(Exception descripcion) {
68
        listenerSupport.callError(descripcion);
69
    }
70

    
71
    public void removeWizardListener(WizardListener listener) {
72
        listenerSupport.removeWizardListener(listener);
73
    }
74

    
75
    public void callStateChanged(boolean finishable) {
76
        listenerSupport.callStateChanged(finishable);
77
    }
78

    
79
    protected void setTabName(String name) {
80
        tabName = name;
81
    }
82

    
83
    public String getTabName() {
84
        return tabName;
85
    }
86

    
87
    abstract public void initWizard();
88

    
89
    /**
90
     * @deprecated use {@link #executeWizard()} instead.
91
     */
92
    abstract public void execute();
93

    
94
    /**
95
     * Executes the wizard and returns anything created in the process.
96
     * 
97
     * @return anything created in the process
98
     */
99
    public Object executeWizard() {
100
        execute();
101
        return null;
102
    }
103

    
104
    abstract public void close();
105

    
106
    abstract public DataStoreParameters[] getParameters();
107

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

    
147
    /**
148
     * Sets the MapControl that will receive the new layer
149
     * @param mapCtrl
150
     *            The mapCtrl to set.
151
     */
152
    public void setMapCtrl(MapControl mapCtrl) {
153
        this.mapCtrl = mapCtrl;
154
        b_isMapControlAvailable = (mapCtrl!=null);
155
    }
156
    
157
    /**
158
     * You can use it to extract information from
159
     * the MapContext that will receive the new layer.
160
     * For example, projection to use, or visible extent.
161
     * 
162
     * @return Returns the MapContext.
163
     */
164
    public MapContext getMapContext() {
165
            if (this.mapContext!=null || this.mapCtrl==null) {
166
                    return this.mapContext;
167
            }
168
            else {
169
                    return this.mapCtrl.getMapContext();
170
            }
171
        
172
    }
173

    
174
    /**
175
     * Sets the MapContext that will receive the new layer
176
     * @param mapContext
177
     *            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.
185
     * The MapControl may not be available in some circumstances, for instance
186
     * when adding layers to a 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, parameters.toString());
207
        Thread task = new Thread(new Runnable() {
208

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

    
221
                                public Window getOwnerWindow() {
222
                                    return null;
223
                                }
224

    
225
                                public MapControl getMapControl() {
226
                                    return mapControl;
227
                                }
228

    
229
                                                                public IProjection getViewProjection() {
230
                                                                        return mapContext.getProjection();
231
                                                                }
232

    
233
                                                                public MapContext getMapContext() {
234
                                                                        return mapContext;
235
                                                                }
236

    
237
                                                                public boolean isMapControlAvailable() {
238
                                                                        return b_isMapControlAvail;
239
                                                                }
240
                            });
241
                    if (preparedLayer != null) {
242
                        mapContext.getLayers().addLayer(preparedLayer);
243
                    }
244
                } catch (Exception e) {
245
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
246
                    MessagePanel.showMessage(
247
                            i18nManager.getTranslation("_Probems_loading_the_layer"), 
248
                            i18nManager.getTranslation("_Cant_load_the_layer"), 
249
                            e
250
                    );
251
                    logger.warn("Can't load layer '"+layerName+"'.",e);
252
                    return;
253
                } finally {
254
                    if (preparedLayer != null && preparedLayer != layer) {
255
                        preparedLayer.dispose();
256
                        preparedLayer = null;
257
                    }
258
                    if (layer != null) {
259
                        layer.dispose();
260
                        layer = null;
261
                    }
262
                }
263
            }
264
        });
265
        task.start();
266

    
267
    }
268
    
269
    /**
270
     * 
271
     * @param mapControl
272
     * @param layerName
273
     * @param parameters
274
     * @deprecated Use {@link #doAddLayer(String, DataStoreParameters)}
275
     * in combination with {@link #setMapCtrl(MapControl)} if you need
276
     * to set the MapControl. Note that MapControl is automatically initialized
277
     * when creating the panel from the AddLayer extension.
278
     */
279
    protected void doAddLayer(final MapControl mapControl,
280
        final String layerName, final DataStoreParameters parameters) {
281
            this.setMapCtrl(mapControl);
282
            doAddLayer(layerName, parameters);
283
    }
284
    
285
    /**
286
     * This method is called for example when user changes
287
     * tab in add layer dialog (new tab's settings are valid?)
288
     * 
289
     * @return whether current wizard settings are enough
290
     * (for example, to enable an Accept button in a container)
291
     */
292
    public boolean areSettingsValid() {
293
        return true;
294
    }
295
}