Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerAddLayerWizardPanel.java @ 38262

History | View | Annotate | Download (6.21 KB)

1 25936 jmvivo
/* 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
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing;
28
29
import java.awt.BorderLayout;
30 26919 jmvivo
import java.awt.Window;
31 26776 jmvivo
import java.awt.event.ActionEvent;
32 25936 jmvivo
33
import javax.swing.JFrame;
34
35
import org.cresques.cts.IProjection;
36 33662 cordinyana
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38
39 29596 jpiera
import org.gvsig.app.addlayer.AddLayerDialog;
40 31496 jjdelcerro
import org.gvsig.app.prepareAction.PrepareContext;
41
import org.gvsig.app.prepareAction.PrepareContextView;
42 25936 jmvivo
import org.gvsig.fmap.dal.DataStoreParameters;
43 27387 jmvivo
import org.gvsig.fmap.dal.DataTypes;
44 25936 jmvivo
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
45 26919 jmvivo
import org.gvsig.fmap.mapcontrol.MapControl;
46 35341 jpiera
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
47 27387 jmvivo
import org.gvsig.tools.dynobject.DynField;
48 26776 jmvivo
import org.gvsig.tools.dynobject.DynObject;
49 34017 cordinyana
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
50 30580 cordinyana
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
51 33662 cordinyana
import org.gvsig.tools.service.ServiceException;
52 25936 jmvivo
53
/**
54
 * @author jmvivo
55 36628 cordinyana
 *
56 25936 jmvivo
 */
57 26889 jmvivo
public class FilesystemExplorerAddLayerWizardPanel extends
58 36628 cordinyana
    FilesystemExplorerWizardPanel {
59 25936 jmvivo
60 36628 cordinyana
    private static final long serialVersionUID = -5054057255129168139L;
61 33662 cordinyana
62
    private static final Logger LOG = LoggerFactory
63
        .getLogger(FilesystemExplorerAddLayerWizardPanel.class);
64
65 36628 cordinyana
    private PrepareContext prepareContext;
66 25936 jmvivo
67 36628 cordinyana
    @Override
68
    public void execute() {
69
        if (this.getMapCtrl() == null) {
70
            throw new IllegalArgumentException("MapControl need");
71
        }
72
        String layerName;
73 25936 jmvivo
74 36628 cordinyana
        for (DataStoreParameters params : this.getParameters()) {
75
            IProjection proj = null;
76 27387 jmvivo
77 36628 cordinyana
            proj = this.getMapCtrl().getProjection();
78 25936 jmvivo
79 36628 cordinyana
            // Buscamos por el parametro de la proyeccion
80
            // que sean obligatorios y est?n a null
81
            // y le ponemos la proyeccion de la vista
82
            DynField[] fields = params.getDynClass().getDynFields();
83
            for (DynField field : fields) {
84
                if (field.getType() == DataTypes.CRS && field.isMandatory()) {
85
                    if (params.getDynValue(field.getName()) == null) {
86
                        params.setDynValue(field.getName(), proj);
87
                    }
88
                }
89
            }
90 27387 jmvivo
91 36628 cordinyana
            layerName =
92
                ((FilesystemStoreParameters) params).getFile().getName();
93 25936 jmvivo
94 36628 cordinyana
            this.doAddLayer(this.getMapCtrl(), layerName, params);
95
        }
96
    }
97 26776 jmvivo
98 36628 cordinyana
    @Override
99
    public void actionPerformed(ActionEvent e) {
100 26776 jmvivo
101 36628 cordinyana
        String command = e.getActionCommand();
102
        FilesystemStoreListModel model =
103
            (FilesystemStoreListModel) getFileList().getModel();
104 26776 jmvivo
105 36628 cordinyana
        if (command == EDIT_COMMAND) {
106
            DynObject dynObject =
107
                model.getDynObjectAt(getFileList().getSelectedIndex());
108
109 34017 cordinyana
            // For store parameters with a CRS field, add the current view
110
            // CRS as default value when null.
111
            try {
112
                IProjection projection =
113
                    (IProjection) dynObject.getDynValue("CRS");
114
                if (projection == null) {
115
                    projection = getDefaultProjection();
116
                    dynObject.setDynValue("CRS", projection);
117
                }
118
            } catch (DynFieldNotFoundException e1) {
119
                LOG.debug("Loading a store whose parameters "
120
                    + "don't have a CRS field");
121
            }
122 26776 jmvivo
123 33662 cordinyana
            try {
124
                DynObjectEditor editor = new DynObjectEditor(dynObject);
125
                editor.editObject(true);
126
            } catch (ServiceException ex) {
127
                LOG.error(
128
                    "Error creating a Swing component for the DynObject: "
129
                        + dynObject, ex);
130
            }
131 26776 jmvivo
132 36628 cordinyana
        } else {
133
            super.actionPerformed(e);
134
        }
135
    }
136 26776 jmvivo
137 34017 cordinyana
    /**
138
     * @return
139
     */
140
    private IProjection getDefaultProjection() {
141
        return getMapCtrl().getViewPort().getProjection();
142
    }
143
144 36628 cordinyana
    public static void main(String[] args) {
145
        try {
146
            new DefaultLibrariesInitializer().fullInitialize();
147 25936 jmvivo
148 36628 cordinyana
            JFrame frame = new JFrame();
149 25936 jmvivo
150 36628 cordinyana
            AddLayerDialog addLayerDlg = new AddLayerDialog();
151 25936 jmvivo
152 36628 cordinyana
            FilesystemExplorerAddLayerWizardPanel wizardPanel =
153
                new FilesystemExplorerAddLayerWizardPanel();
154
            addLayerDlg.addWizardTab("File", wizardPanel);
155
            wizardPanel.initWizard();
156 25936 jmvivo
157 36628 cordinyana
            frame.setLayout(new BorderLayout());
158
            frame.add(addLayerDlg);
159 25936 jmvivo
160 36628 cordinyana
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
161
            frame.setBounds(10, 10, 400, 400);
162
            frame.setVisible(true);
163 25936 jmvivo
164 36628 cordinyana
        } catch (Exception e) {
165
            e.printStackTrace();
166
            System.exit(-1);
167
            return;
168
        }
169
    }
170 25936 jmvivo
171 36628 cordinyana
    @Override
172
    protected PrepareContext getPrepareDataStoreContext() {
173
        if (this.prepareContext == null) {
174
            this.prepareContext = new PrepareContextView() {
175 26919 jmvivo
176 36628 cordinyana
                public Window getOwnerWindow() {
177
                    return null;
178
                }
179 26919 jmvivo
180 36628 cordinyana
                public MapControl getMapControl() {
181
                    return FilesystemExplorerAddLayerWizardPanel.this
182
                        .getMapCtrl();
183
                }
184 26919 jmvivo
185 36760 nbrodin
                                public IProjection getViewProjection() {
186
                                        return getMapControl().getProjection();
187
                                }
188
189 36628 cordinyana
            };
190
        }
191
        return this.prepareContext;
192
    }
193 26919 jmvivo
194 36628 cordinyana
}