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

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(mapContext);
137
                } catch (MapControlCreationException ex) {
138
                    logger.warn("Can't create a MapControl.", ex);
139
                    throw  new RuntimeException(ex);
140
                }
141
                    mapCtrl=mc;
142
            }
143
            return mapCtrl;
144
    }
145

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

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

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

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

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

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

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

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

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

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

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

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