Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerAddLayerWizardPanel.java @ 30754

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

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 IVER T.I. S.A.   {{Task}}
26
 */
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing;
32

    
33
import java.awt.BorderLayout;
34
import java.awt.Window;
35
import java.awt.event.ActionEvent;
36

    
37
import javax.swing.JFrame;
38

    
39
import org.cresques.cts.IProjection;
40
import org.gvsig.andami.messages.NotificationManager;
41
import org.gvsig.app.AppGvSigLocator;
42
import org.gvsig.app.AppGvSigManager;
43
import org.gvsig.app.PrepareContext;
44
import org.gvsig.app.PrepareContextView;
45
import org.gvsig.app.addlayer.AddLayerDialog;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.dal.DataTypes;
48
import org.gvsig.fmap.dal.exception.DataException;
49
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
50
import org.gvsig.fmap.mapcontext.MapContext;
51
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
52
import org.gvsig.fmap.mapcontext.layers.FLayer;
53
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
54
import org.gvsig.fmap.mapcontext.layers.operations.SingleLayer;
55
import org.gvsig.fmap.mapcontrol.MapControl;
56
import org.gvsig.tools.dynobject.DynField;
57
import org.gvsig.tools.dynobject.DynObject;
58
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61

    
62

    
63
/**
64
 * @author jmvivo
65
 *
66
 */
67
public class FilesystemExplorerAddLayerWizardPanel extends
68
                FilesystemExplorerWizardPanel {
69

    
70
        /**
71
         *
72
         */
73
        private static final long serialVersionUID = -5054057255129168139L;
74
        private PrepareContext prepareContext;
75

    
76
        private static Logger logger = LoggerFactory
77
                        .getLogger(FilesystemExplorerAddLayerWizardPanel.class);
78

    
79
        /*
80
         * (non-Javadoc)
81
         *
82
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
83
         */
84
        @Override
85
        public void execute() {
86
                if (this.getMapCtrl() == null){
87
                        throw new IllegalArgumentException("MapControl need");
88
                }
89
                MapContext mapContext = this.getMapCtrl().getMapContext();
90
                FLayer layer;
91
                String layerName;
92
                LayerFactory layerFactor = LayerFactory.getInstance();
93

    
94
                AppGvSigManager appGvSigMan = AppGvSigLocator.getAppGvSigManager();
95

    
96
                for (DataStoreParameters params : this.getParameters()) {
97
                        IProjection proj = null;
98

    
99
                        proj = this.getMapCtrl().getProjection();
100

    
101
                        // Buscamos por el parametro de la proyeccion
102
                        // que sean obligatorios y est?n a null
103
                        // y le ponemos la proyeccion de la vista
104
                        DynField[] fields = params.getDynClass().getDynFields();
105
                        for (DynField field : fields) {
106
                                if (field.getType() == DataTypes.SRS && field.isMandatory()) {
107
                                        if (params.getDynValue(field.getName()) == null) {
108
                                                params.setDynValue(field.getName(), proj);
109
                                        }
110
                                }
111
                        }
112

    
113

    
114

    
115

    
116
                        //FIXME: el nombre deberia sugerirlo los parametros?
117
                        layerName = ((FilesystemStoreParameters) params).getFile().getName();
118

    
119
                        try {
120

    
121
                                layer = layerFactor.createLayer(layerName,
122
                                                params);
123
                        } catch (LoadLayerException e) {
124
                                NotificationManager.addError(e);
125
                                return;
126
                        }
127
                        try {
128
                                layer = appGvSigMan.prepareOpenLayer(layer,
129
                                                (PrepareContextView) this.getPrepareDataStoreContext());
130
                        } catch (Exception e) {
131
                                NotificationManager.addError(e);
132
                                if (layer instanceof SingleLayer) {
133
                                        try {
134
                                                ((SingleLayer) layer).getDataStore().dispose();
135
                                        } catch (DataException e1) {
136
                                                logger
137
                                                                .error(
138
                                                                                "Exeption disposing dataStore after prepareOpenLayer exception",
139
                                                                                e1);
140
                                        }
141
                                }
142
                                return;
143
                        }
144
                        if (layer == null) {
145
                                continue;
146
                        }
147

    
148
                        mapContext.getLayers().addLayer(layer);
149
                }
150

    
151

    
152
        }
153

    
154
        @Override
155
        public void actionPerformed(ActionEvent e) {
156

    
157
                String command = e.getActionCommand();
158
                FilesystemStoreListModel model = (FilesystemStoreListModel) getFileList()
159
                                .getModel();
160

    
161
                if (command == EDIT_COMMAND) {
162
                        DynObject dynObject = model.getDynObjectAt(getFileList()
163
                                        .getSelectedIndex());
164

    
165

    
166
                        DynObjectEditor editor = new DynObjectEditor(dynObject,
167
                                        DynObjectEditor.SHOW_ALL, null, true, true);
168
                        editor.editObject(true);
169
                        this.loadParamsList(dynObject);
170

    
171
                } else {
172
                        super.actionPerformed(e);
173
                }
174
        }
175

    
176
        public static void main(String[] args) {
177
                try {
178
                        new DefaultLibrariesInitializer().fullInitialize();
179

    
180
                        JFrame frame = new JFrame();
181

    
182
                        AddLayerDialog addLayerDlg = new AddLayerDialog();
183

    
184
                        FilesystemExplorerAddLayerWizardPanel wizardPanel = new FilesystemExplorerAddLayerWizardPanel();
185
                        addLayerDlg.addWizardTab("File", wizardPanel);
186
                        wizardPanel.initWizard();
187

    
188
                        frame.setLayout(new BorderLayout());
189
                        frame.add(addLayerDlg);
190

    
191
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
192
                        frame.setBounds(10, 10, 400, 400);
193
                        frame.setVisible(true);
194

    
195
                } catch (Exception e) {
196
                        e.printStackTrace();
197
                        System.exit(-1);
198
                        return;
199
                }
200
        }
201

    
202
        @Override
203
        protected PrepareContext getPrepareDataStoreContext() {
204
                if (this.prepareContext == null) {
205
                        this.prepareContext = new PrepareContextView() {
206
                                public Window getOwnerWindow() {
207
                                        return null;
208
                                }
209

    
210
                                public MapControl getMapControl() {
211
                                        return FilesystemExplorerAddLayerWizardPanel.this
212
                                                        .getMapCtrl();
213
                                }
214

    
215
                        };
216
                }
217
                return this.prepareContext;
218
        }
219

    
220

    
221
}