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

History | View | Annotate | Download (5.83 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.fmap.dal.DataStoreParameters;
38
import org.gvsig.fmap.mapcontext.MapContext;
39
import org.gvsig.fmap.mapcontext.MapContextLocator;
40
import org.gvsig.fmap.mapcontext.MapContextManager;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontrol.MapControl;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
public abstract class WizardPanel extends JPanel {
47

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

    
54
    public void addWizardListener(WizardListener listener) {
55
        listenerSupport.addWizardListener(listener);
56
    }
57

    
58
    public void callError(Exception descripcion) {
59
        listenerSupport.callError(descripcion);
60
    }
61

    
62
    public void removeWizardListener(WizardListener listener) {
63
        listenerSupport.removeWizardListener(listener);
64
    }
65

    
66
    public void callStateChanged(boolean finishable) {
67
        listenerSupport.callStateChanged(finishable);
68
    }
69

    
70
    protected void setTabName(String name) {
71
        tabName = name;
72
    }
73

    
74
    public String getTabName() {
75
        return tabName;
76
    }
77

    
78
    abstract public void initWizard();
79

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

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

    
95
    abstract public void close();
96

    
97
    abstract public DataStoreParameters[] getParameters();
98

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

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

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

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

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

    
137
                                public Window getOwnerWindow() {
138
                                    return null;
139
                                }
140

    
141
                                public MapControl getMapControl() {
142
                                    return mapControl;
143
                                }
144

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