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

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

    
28
import javax.swing.JPanel;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.andami.messages.NotificationManager;
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;
37
import org.gvsig.app.prepareAction.PrepareContextView_v1;
38
import org.gvsig.fmap.dal.DataStoreParameters;
39
import org.gvsig.fmap.mapcontext.MapContext;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.MapContextManager;
42
import org.gvsig.fmap.mapcontext.layers.FLayer;
43
import org.gvsig.fmap.mapcontrol.MapControl;
44
import org.gvsig.tools.ToolsLocator;
45
import org.gvsig.tools.i18n.I18nManager;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
public abstract class WizardPanel extends JPanel {
50

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

    
59
    public void addWizardListener(WizardListener listener) {
60
        listenerSupport.addWizardListener(listener);
61
    }
62

    
63
    public void callError(Exception descripcion) {
64
        listenerSupport.callError(descripcion);
65
    }
66

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

    
71
    public void callStateChanged(boolean finishable) {
72
        listenerSupport.callStateChanged(finishable);
73
    }
74

    
75
    protected void setTabName(String name) {
76
        tabName = name;
77
    }
78

    
79
    public String getTabName() {
80
        return tabName;
81
    }
82

    
83
    abstract public void initWizard();
84

    
85
    /**
86
     * @deprecated use {@link #executeWizard()} instead.
87
     */
88
    abstract public void execute();
89

    
90
    /**
91
     * Executes the wizard and returns anything created in the process.
92
     * 
93
     * @return anything created in the process
94
     */
95
    public Object executeWizard() {
96
        execute();
97
        return null;
98
    }
99

    
100
    abstract public void close();
101

    
102
    abstract public DataStoreParameters[] getParameters();
103

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

    
137
    /**
138
     * Sets the MapControl that will receive the new layer
139
     * @param mapCtrl
140
     *            The mapCtrl to set.
141
     */
142
    public void setMapCtrl(MapControl mapCtrl) {
143
        this.mapCtrl = mapCtrl;
144
        b_isMapControlAvailable = (mapCtrl!=null);
145
    }
146
    
147
    /**
148
     * You can use it to extract information from
149
     * the MapContext that will receive the new layer.
150
     * For example, projection to use, or visible extent.
151
     * 
152
     * @return Returns the MapContext.
153
     */
154
    public MapContext getMapContext() {
155
            if (this.mapContext!=null || this.mapCtrl==null) {
156
                    return this.mapContext;
157
            }
158
            else {
159
                    return this.mapCtrl.getMapContext();
160
            }
161
        
162
    }
163

    
164
    /**
165
     * Sets the MapContext that will receive the new layer
166
     * @param mapContext
167
     *            The mapContext to set.
168
     */
169
    public void setMapContext(MapContext mapContext) {
170
        this.mapContext = mapContext;
171
    }
172

    
173
    /**
174
     * Checks whether the MapControl is available.
175
     * The MapControl may not be available in some circumstances, for instance
176
     * when adding layers to a MapContext not associated with a View.
177
     * 
178
     * A MapContext should always be available on the {@link #getMapContext()}
179
     * method.
180
     * 
181
     * @return true if the MapControl is available, false otherwise
182
     */
183
    public boolean isMapControlAvailable() {
184
            return b_isMapControlAvailable ;
185
    }
186

    
187
    protected void doAddLayer(
188
            final String layerName, final DataStoreParameters parameters) {
189
            final boolean b_isMapControlAvail = this.isMapControlAvailable();
190
            final MapControl mapControl = this.getMapCtrl();
191
            final MapContext mapContext = this.getMapContext();
192
            final ApplicationManager application = ApplicationLocator.getManager();
193
        final MapContextManager manager =
194
            MapContextLocator.getMapContextManager();
195

    
196
        logger.info("addLayer('{}','{}')", layerName, parameters.toString());
197
        Thread task = new Thread(new Runnable() {
198

    
199
            public void run() {
200
                FLayer layer = null;
201
                FLayer preparedLayer = null;
202
                try {
203
                    layer = manager.createLayer(layerName, parameters);
204
                    preparedLayer =
205
                        application.prepareOpenLayer(layer,
206
                            new PrepareContextView_v1() {
207

    
208
                                public Window getOwnerWindow() {
209
                                    return null;
210
                                }
211

    
212
                                public MapControl getMapControl() {
213
                                    return mapControl;
214
                                }
215

    
216
                                                                public IProjection getViewProjection() {
217
                                                                        return mapContext.getProjection();
218
                                                                }
219

    
220
                                                                public MapContext getMapContext() {
221
                                                                        return mapContext;
222
                                                                }
223

    
224
                                                                public boolean isMapControlAvailable() {
225
                                                                        return b_isMapControlAvail;
226
                                                                }
227
                            });
228
                    if (preparedLayer != null) {
229
                        mapContext.getLayers().addLayer(preparedLayer);
230
                    }
231
                } catch (Exception e) {
232
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
233
                    MessagePanel.showMessage(
234
                            i18nManager.getTranslation("_Probems_loading_the_layer"), 
235
                            i18nManager.getTranslation("_Cant_load_the_layer"), 
236
                            e
237
                    );
238
                    logger.warn("Can't load layer '"+layerName+"'.",e);
239
                    return;
240
                } finally {
241
                    if (preparedLayer != null && preparedLayer != layer) {
242
                        preparedLayer.dispose();
243
                        preparedLayer = null;
244
                    }
245
                    if (layer != null) {
246
                        layer.dispose();
247
                        layer = null;
248
                    }
249
                }
250
            }
251
        });
252
        task.start();
253

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