Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerAddLayerWizardPanel.java @ 35341

History | View | Annotate | Download (5.67 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.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
import org.gvsig.app.addlayer.AddLayerDialog;
44
import org.gvsig.app.prepareAction.PrepareContext;
45
import org.gvsig.app.prepareAction.PrepareContextView;
46
import org.gvsig.fmap.dal.DataStoreParameters;
47
import org.gvsig.fmap.dal.DataTypes;
48
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
51
import org.gvsig.tools.dynobject.DynField;
52
import org.gvsig.tools.dynobject.DynObject;
53
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
54
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
55
import org.gvsig.tools.service.ServiceException;
56

    
57

    
58
/**
59
 * @author jmvivo
60
 *
61
 */
62
public class FilesystemExplorerAddLayerWizardPanel extends
63
                FilesystemExplorerWizardPanel {
64

    
65
        /**
66
         *
67
         */
68
        private static final long serialVersionUID = -5054057255129168139L;
69

    
70
    private static final Logger LOG = LoggerFactory
71
        .getLogger(FilesystemExplorerAddLayerWizardPanel.class);
72

    
73
        private PrepareContext prepareContext;
74

    
75
        /*
76
         * (non-Javadoc)
77
         *
78
         * @see com.iver.cit.gvsig.gui.WizardPanel#execute()
79
         */
80
        @Override
81
        public void execute() {
82
                if (this.getMapCtrl() == null){
83
                        throw new IllegalArgumentException("MapControl need");
84
                }
85
                String layerName;
86

    
87
                for (DataStoreParameters params : this.getParameters()) {
88
                        IProjection proj = null;
89

    
90
                        proj = this.getMapCtrl().getProjection();
91

    
92
                        // Buscamos por el parametro de la proyeccion
93
                        // que sean obligatorios y est?n a null
94
                        // y le ponemos la proyeccion de la vista
95
                        DynField[] fields = params.getDynClass().getDynFields();
96
                        for (DynField field : fields) {
97
                                if (field.getType() == DataTypes.CRS && field.isMandatory()) {
98
                                        if (params.getDynValue(field.getName()) == null) {
99
                                                params.setDynValue(field.getName(), proj);
100
                                        }
101
                                }
102
                        }
103

    
104
                        layerName = ((FilesystemStoreParameters) params).getFile().getName();
105

    
106
                        this.doAddLayer(this.getMapCtrl(), layerName, params);
107
                }
108
        }
109
        
110
        @Override
111
        public void actionPerformed(ActionEvent e) {
112

    
113
                String command = e.getActionCommand();
114
                FilesystemStoreListModel model = (FilesystemStoreListModel) getFileList()
115
                                .getModel();
116

    
117
                if (command == EDIT_COMMAND) {
118
                        DynObject dynObject = model.getDynObjectAt(getFileList()
119
                                        .getSelectedIndex());
120

    
121
            // For store parameters with a CRS field, add the current view
122
            // CRS as default value when null.
123
            try {
124
                IProjection projection =
125
                    (IProjection) dynObject.getDynValue("CRS");
126
                if (projection == null) {
127
                    projection = getDefaultProjection();
128
                    dynObject.setDynValue("CRS", projection);
129
                }
130
            } catch (DynFieldNotFoundException e1) {
131
                LOG.debug("Loading a store whose parameters "
132
                    + "don't have a CRS field");
133
            }
134

    
135
            try {
136
                DynObjectEditor editor = new DynObjectEditor(dynObject);
137
                editor.editObject(true);
138
                this.loadParamsList(dynObject);
139
            } catch (ServiceException ex) {
140
                LOG.error(
141
                    "Error creating a Swing component for the DynObject: "
142
                        + dynObject, ex);
143
            }
144

    
145
                } else {
146
                        super.actionPerformed(e);
147
                }
148
        }
149

    
150
    /**
151
     * @return
152
     */
153
    private IProjection getDefaultProjection() {
154
        return getMapCtrl().getViewPort().getProjection();
155
    }
156

    
157
        public static void main(String[] args) {
158
                try {
159
                        new DefaultLibrariesInitializer().fullInitialize();
160

    
161
                        JFrame frame = new JFrame();
162

    
163
                        AddLayerDialog addLayerDlg = new AddLayerDialog();
164

    
165
                        FilesystemExplorerAddLayerWizardPanel wizardPanel = new FilesystemExplorerAddLayerWizardPanel();
166
                        addLayerDlg.addWizardTab("File", wizardPanel);
167
                        wizardPanel.initWizard();
168

    
169
                        frame.setLayout(new BorderLayout());
170
                        frame.add(addLayerDlg);
171

    
172
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
173
                        frame.setBounds(10, 10, 400, 400);
174
                        frame.setVisible(true);
175

    
176
                } catch (Exception e) {
177
                        e.printStackTrace();
178
                        System.exit(-1);
179
                        return;
180
                }
181
        }
182

    
183
        @Override
184
        protected PrepareContext getPrepareDataStoreContext() {
185
                if (this.prepareContext == null) {
186
                        this.prepareContext = new PrepareContextView() {
187
                                public Window getOwnerWindow() {
188
                                        return null;
189
                                }
190

    
191
                                public MapControl getMapControl() {
192
                                        return FilesystemExplorerAddLayerWizardPanel.this
193
                                                        .getMapCtrl();
194
                                }
195

    
196
                        };
197
                }
198
                return this.prepareContext;
199
        }
200

    
201

    
202
}