Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2052 / applications / appgvSIG / src / org / gvsig / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerAddLayerWizardPanel.java @ 38815

History | View | Annotate | Download (10.5 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
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing;
28

    
29
import java.awt.BorderLayout;
30
import java.awt.Color;
31
import java.awt.Component;
32
import java.awt.GridLayout;
33
import java.awt.Window;
34
import java.awt.event.ActionEvent;
35

    
36
import javax.swing.JFrame;
37
import javax.swing.JLabel;
38
import javax.swing.JList;
39
import javax.swing.JPanel;
40
import javax.swing.ListCellRenderer;
41
import javax.swing.ListModel;
42
import javax.swing.UIDefaults;
43

    
44
import org.cresques.cts.IProjection;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
import org.gvsig.andami.messages.Messages;
49
import org.gvsig.app.addlayer.AddLayerDialog;
50
import org.gvsig.app.prepareAction.PrepareContext;
51
import org.gvsig.app.prepareAction.PrepareContextView;
52
import org.gvsig.fmap.dal.DataStoreParameters;
53
import org.gvsig.fmap.dal.DataTypes;
54
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
55
import org.gvsig.fmap.mapcontrol.MapControl;
56
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
57
import org.gvsig.tools.dynobject.DynField;
58
import org.gvsig.tools.dynobject.DynObject;
59
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
60
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
61
import org.gvsig.tools.service.ServiceException;
62

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

    
70
    private static final long serialVersionUID = -5054057255129168139L;
71

    
72
    private static final Logger LOG = LoggerFactory
73
        .getLogger(FilesystemExplorerAddLayerWizardPanel.class);
74

    
75
    private PrepareContext prepareContext;
76

    
77
    public void initWizard() {
78

    
79
        super.initWizard();
80
        
81
        this.getFileList().setCellRenderer(
82
            new TwoColumnFileListCellRenderer(
83
                this.getMapCtrl().getProjection().getAbrev()));
84
        
85
    }
86

    
87
    
88
    
89
    @Override
90
    public void execute() {
91
        if (this.getMapCtrl() == null) {
92
            throw new IllegalArgumentException("MapControl need");
93
        }
94
        String layerName;
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.CRS && field.isMandatory()) {
107
                    if (params.getDynValue(field.getName()) == null) {
108
                        params.setDynValue(field.getName(), proj);
109
                    }
110
                }
111
            }
112

    
113
            layerName =
114
                ((FilesystemStoreParameters) params).getFile().getName();
115

    
116
            this.doAddLayer(this.getMapCtrl(), layerName, params);
117
        }
118
    }
119

    
120
    @Override
121
    public void actionPerformed(ActionEvent e) {
122

    
123
        String command = e.getActionCommand();
124
        FilesystemStoreListModel model =
125
            (FilesystemStoreListModel) getFileList().getModel();
126

    
127
        if (command == EDIT_COMMAND) {
128
            DynObject dynObject =
129
                model.getDynObjectAt(getFileList().getSelectedIndex());
130

    
131
            // For store parameters with a CRS field, add the current view
132
            // CRS as default value when null.
133
            try {
134
                IProjection projection =
135
                    (IProjection) dynObject.getDynValue("CRS");
136
                if (projection == null) {
137
                    projection = getDefaultProjection();
138
                    dynObject.setDynValue("CRS", projection);
139
                }
140
            } catch (DynFieldNotFoundException e1) {
141
                LOG.debug("Loading a store whose parameters "
142
                    + "don't have a CRS field");
143
            }
144

    
145
            try {
146
                DynObjectEditor editor = new DynObjectEditor(dynObject);
147
                editor.editObject(true);
148
            } catch (ServiceException ex) {
149
                LOG.error(
150
                    "Error creating a Swing component for the DynObject: "
151
                        + dynObject, ex);
152
            }
153

    
154
            // force repaint
155
            ListModel lm = this.getFileList().getModel();
156
            if (lm instanceof FilesystemStoreListModel) {
157
                FilesystemStoreListModel mm = (FilesystemStoreListModel) lm;
158
                mm.forceUpdate(getFileList().getSelectedIndex());
159
            }
160
            
161

    
162
            
163
        } else {
164
            super.actionPerformed(e);
165
        }
166
    }
167

    
168
    /**
169
     * @return
170
     */
171
    private IProjection getDefaultProjection() {
172
        return getMapCtrl().getViewPort().getProjection();
173
    }
174

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

    
179
            JFrame frame = new JFrame();
180

    
181
            AddLayerDialog addLayerDlg = new AddLayerDialog();
182

    
183
            FilesystemExplorerAddLayerWizardPanel wizardPanel =
184
                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

    
207
                public Window getOwnerWindow() {
208
                    return null;
209
                }
210

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

    
216
                                public IProjection getViewProjection() {
217
                                        return getMapControl().getProjection();
218
                                }
219

    
220
            };
221
        }
222
        return this.prepareContext;
223
    }
224
    
225
    // =================
226
    
227
    private class TwoColumnFileListCellRenderer extends JPanel implements ListCellRenderer {
228

    
229
        private JLabel fileName = null;
230
        private JLabel crsName = null;
231
        private String viewCrs = "?";
232
        
233
        private Color selBC = null;
234
        private Color selFC = null;
235
        private Color unselBC = null;
236
        private Color unselFC = null;
237
        
238
        public TwoColumnFileListCellRenderer(String view_crs) {
239
            // 1 col for name, one for crs
240
            setLayout(new GridLayout(0, 2));
241
            fileName = new JLabel();
242
            crsName = new JLabel();
243
            
244
            add(fileName);
245
            add(crsName);
246
            viewCrs = view_crs;
247
            
248
            UIDefaults defaults = javax.swing.UIManager.getDefaults();
249
            selBC = defaults.getColor("List.selectionBackground");
250
            selFC = defaults.getColor("List.selectionForeground");
251
            unselBC = this.getBackground();
252
            unselFC = this.getForeground();
253
        }
254

    
255
        public Component getListCellRendererComponent(
256
            JList list, Object _value,
257
            int index, boolean isSelected, boolean cellHasFocus) {
258
            
259
            if (isSelected) {
260
                setBackground(selBC);
261
                fileName.setForeground(selFC);
262
                crsName.setForeground(selFC);
263
            } else {
264
                setBackground(unselBC);
265
                fileName.setForeground(unselFC);
266
                crsName.setForeground(unselFC);
267
            }
268
            
269
            // ===========================
270
            
271
            fileName.setText(_value == null ? "?": _value.toString());
272
            
273
            ListModel lm = list.getModel();
274
            
275
            if (lm instanceof FilesystemStoreListModel) {
276
                
277
                FilesystemStoreListModel fsslm = (FilesystemStoreListModel) lm;
278
                DynObject dyno = fsslm.getDynObjectAt(index);
279
                
280
                try {
281
                    Object crs_obj = dyno.getDynValue("CRS");
282
                    if (crs_obj == null || crs_obj.toString().compareToIgnoreCase("null") == 0) {
283
                        crsName.setText(viewCrs);
284
                    } else {
285
                        if (crs_obj instanceof IProjection) {
286
                            IProjection ipr = (IProjection) crs_obj;
287
                            if (ipr.getAbrev().compareToIgnoreCase(viewCrs) == 0) {
288
                                // CRS set but same as view's
289
                                crsName.setText(viewCrs);
290
                            } else {
291
                                // CRS set and not same as view's
292
                                crsName.setText(ipr.getAbrev() +
293
                                    " (" +
294
                                    Messages.get("_reprojected_on_the_fly") +
295
                                    ")");
296
                            }
297
                        } else {
298
                            // CRS parameter class not expected
299
                            crsName.setText("CRS?");
300
                        }
301
                        
302
                    }
303
                } catch (Exception ex) {
304
                    // CRS parameter not found
305
                    crsName.setText("CRS?");
306
                }
307
                
308
            } else {
309
                // list model not of expected class
310
                crsName.setText("CRS?");
311
            }
312
            
313
            return this;
314
        }
315
        
316
    }
317

    
318

    
319
}