Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / WizardPanel.java @ 38519

History | View | Annotate | Download (5.78 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.gui;
23

    
24
import java.awt.Window;
25

    
26
import javax.swing.JPanel;
27

    
28
import org.cresques.cts.IProjection;
29
import org.gvsig.andami.messages.NotificationManager;
30
import org.gvsig.app.ApplicationLocator;
31
import org.gvsig.app.ApplicationManager;
32
import org.gvsig.app.gui.wizards.WizardListener;
33
import org.gvsig.app.gui.wizards.WizardListenerSupport;
34
import org.gvsig.app.prepareAction.PrepareContextView;
35
import org.gvsig.fmap.dal.DataStoreParameters;
36
import org.gvsig.fmap.mapcontext.MapContext;
37
import org.gvsig.fmap.mapcontext.MapContextLocator;
38
import org.gvsig.fmap.mapcontext.MapContextManager;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontrol.MapControl;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
public abstract class WizardPanel extends JPanel {
45

    
46
        private static final Logger logger = LoggerFactory.getLogger(WizardPanel.class);
47
    private static final long serialVersionUID = 5317749209668850863L;
48
    private String tabName = "TabName";
49
    private MapControl mapCtrl = null;
50
    private WizardListenerSupport listenerSupport = new WizardListenerSupport();
51

    
52
    public void addWizardListener(WizardListener listener) {
53
        listenerSupport.addWizardListener(listener);
54
    }
55

    
56
    public void callError(Exception descripcion) {
57
        listenerSupport.callError(descripcion);
58
    }
59

    
60
    public void removeWizardListener(WizardListener listener) {
61
        listenerSupport.removeWizardListener(listener);
62
    }
63

    
64
    public void callStateChanged(boolean finishable) {
65
        listenerSupport.callStateChanged(finishable);
66
    }
67

    
68
    protected void setTabName(String name) {
69
        tabName = name;
70
    }
71

    
72
    public String getTabName() {
73
        return tabName;
74
    }
75

    
76
    abstract public void initWizard();
77

    
78
    /**
79
     * @deprecated use {@link #executeWizard()} instead.
80
     */
81
    abstract public void execute();
82

    
83
    /**
84
     * Executes the wizard and returns anything created in the process.
85
     * 
86
     * @return anything created in the process
87
     */
88
    public Object executeWizard() {
89
        execute();
90
        return null;
91
    }
92

    
93
    abstract public void close();
94

    
95
    abstract public DataStoreParameters[] getParameters();
96

    
97
    /**
98
     * You can use it to extract information from
99
     * the mapControl that will receive the new layer.
100
     * For example, projection to use, or visible extent.
101
     * 
102
     * @return Returns the mapCtrl.
103
     */
104
    public MapControl getMapCtrl() {
105
        return mapCtrl;
106
    }
107

    
108
    /**
109
     * @param mapCtrl
110
     *            The mapCtrl to set.
111
     */
112
    public void setMapCtrl(MapControl mapCtrl) {
113
        this.mapCtrl = mapCtrl;
114
    }
115

    
116
    protected void doAddLayer(final MapControl mapControl,
117
        final String layerName, final DataStoreParameters parameters) {
118
        final ApplicationManager application = ApplicationLocator.getManager();
119
        final MapContextManager manager =
120
            MapContextLocator.getMapContextManager();
121
        final MapContext mapContext = mapControl.getMapContext();
122

    
123
        logger.info("addLayer('{}','{}')", layerName, parameters.toString());
124
        Thread task = new Thread(new Runnable() {
125

    
126
            public void run() {
127
                FLayer layer = null;
128
                FLayer preparedLayer = null;
129
                try {
130
                    layer = manager.createLayer(layerName, parameters);
131
                    preparedLayer =
132
                        application.prepareOpenLayer(layer,
133
                            new PrepareContextView() {
134

    
135
                                public Window getOwnerWindow() {
136
                                    return null;
137
                                }
138

    
139
                                public MapControl getMapControl() {
140
                                    return mapControl;
141
                                }
142

    
143
                                                                public IProjection getViewProjection() {
144
                                                                        return mapControl.getProjection();
145
                                                                }
146
                            });
147
                    if (preparedLayer != null) {
148
                        mapContext.getLayers().addLayer(preparedLayer);
149
                    }
150
                } catch (Exception e) {
151
                    NotificationManager.addError(e);
152
                    return;
153
                } finally {
154
                    if (preparedLayer != null && preparedLayer != layer) {
155
                        preparedLayer.dispose();
156
                        preparedLayer = null;
157
                    }
158
                    if (layer != null) {
159
                        layer.dispose();
160
                        layer = null;
161
                    }
162
                }
163
            }
164
        });
165
        task.start();
166
    }
167
    
168
    /**
169
     * This method is called for example when user changes
170
     * tab in add layer dialog (new tab's settings are valid?)
171
     * 
172
     * @return whether current wizard settings are enough
173
     * (for example, to enable an Accept button in a container)
174
     */
175
    public boolean areSettingsValid() {
176
        return true;
177
    }
178
}