Statistics
| Revision:

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

History | View | Annotate | Download (5.1 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.gvsig.andami.messages.NotificationManager;
29
import org.gvsig.app.ApplicationLocator;
30
import org.gvsig.app.ApplicationManager;
31
import org.gvsig.app.gui.wizards.WizardListener;
32
import org.gvsig.app.gui.wizards.WizardListenerSupport;
33
import org.gvsig.app.prepareAction.PrepareContextView;
34
import org.gvsig.fmap.dal.DataStoreParameters;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.MapContextLocator;
37
import org.gvsig.fmap.mapcontext.MapContextManager;
38
import org.gvsig.fmap.mapcontext.layers.FLayer;
39
import org.gvsig.fmap.mapcontrol.MapControl;
40

    
41
public abstract class WizardPanel extends JPanel {
42

    
43
    private static final long serialVersionUID = 5317749209668850863L;
44
    private String tabName = "TabName";
45
    private MapControl mapCtrl = null;
46
    private WizardListenerSupport listenerSupport = new WizardListenerSupport();
47

    
48
    public void addWizardListener(WizardListener listener) {
49
        listenerSupport.addWizardListener(listener);
50
    }
51

    
52
    public void callError(Exception descripcion) {
53
        listenerSupport.callError(descripcion);
54
    }
55

    
56
    public void removeWizardListener(WizardListener listener) {
57
        listenerSupport.removeWizardListener(listener);
58
    }
59

    
60
    public void callStateChanged(boolean finishable) {
61
        listenerSupport.callStateChanged(finishable);
62
    }
63

    
64
    protected void setTabName(String name) {
65
        tabName = name;
66
    }
67

    
68
    public String getTabName() {
69
        return tabName;
70
    }
71

    
72
    abstract public void initWizard();
73

    
74
    /**
75
     * @deprecated use {@link #executeWizard()} instead.
76
     */
77
    abstract public void execute();
78

    
79
    /**
80
     * Executes the wizard and returns anything created in the process.
81
     * 
82
     * @return anything created in the process
83
     */
84
    public Object executeWizard() {
85
        execute();
86
        return null;
87
    }
88

    
89
    abstract public void close();
90

    
91
    abstract public DataStoreParameters[] getParameters();
92

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

    
104
    /**
105
     * @param mapCtrl
106
     *            The mapCtrl to set.
107
     */
108
    public void setMapCtrl(MapControl mapCtrl) {
109
        this.mapCtrl = mapCtrl;
110
    }
111

    
112
    protected void doAddLayer(final MapControl mapControl,
113
        final String layerName, final DataStoreParameters parameters) {
114
        final ApplicationManager application = ApplicationLocator.getManager();
115
        final MapContextManager manager =
116
            MapContextLocator.getMapContextManager();
117
        final MapContext mapContext = mapControl.getMapContext();
118

    
119
        Thread task = new Thread(new Runnable() {
120

    
121
            public void run() {
122
                FLayer layer = null;
123
                FLayer preparedLayer = null;
124
                try {
125
                    layer = manager.createLayer(layerName, parameters);
126
                    preparedLayer =
127
                        application.prepareOpenLayer(layer,
128
                            new PrepareContextView() {
129

    
130
                                public Window getOwnerWindow() {
131
                                    return null;
132
                                }
133

    
134
                                public MapControl getMapControl() {
135
                                    return mapControl;
136
                                }
137
                            });
138
                    if (preparedLayer != null) {
139
                        mapContext.getLayers().addLayer(preparedLayer);
140
                    }
141
                } catch (Exception e) {
142
                    NotificationManager.addError(e);
143
                    return;
144
                } finally {
145
                    if (preparedLayer != null && preparedLayer != layer) {
146
                        preparedLayer.dispose();
147
                        preparedLayer = null;
148
                    }
149
                    if (layer != null) {
150
                        layer.dispose();
151
                        layer = null;
152
                    }
153
                }
154
            }
155
        });
156
        task.start();
157
    }
158
}