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 / fmap / dal / serverexplorer / filesystem / swing / FilesystemExplorerAddLayerWizardPanel.java @ 40596

History | View | Annotate | Download (11.6 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.fmap.dal.serverexplorer.filesystem.swing;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Component;
29
import java.awt.Font;
30
import java.awt.GridLayout;
31
import java.awt.Window;
32
import java.awt.event.ActionEvent;
33

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

    
42
import org.cresques.cts.IProjection;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

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

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

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

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

    
74
    private PrepareContext prepareContext;
75

    
76
    public void initWizard() {
77

    
78
        super.initWizard();
79
        this.getFileList().setCellRenderer(
80
            new TwoColumnFileListCellRenderer(this.getMapCtrl()));
81
    }
82

    
83
    @Override
84
    public void execute() {
85
        if (this.getMapCtrl() == null) {
86
            throw new IllegalArgumentException("MapControl need");
87
        }
88
        String layerName;
89

    
90
        for (DataStoreParameters params : this.getParameters()) {
91
            IProjection proj = null;
92

    
93
            proj = this.getMapCtrl().getProjection();
94

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

    
107
            layerName =
108
                ((FilesystemStoreParameters) params).getFile().getName();
109

    
110
            this.doAddLayer(this.getMapCtrl(), layerName, params);
111
        }
112
    }
113

    
114
    @Override
115
    public void actionPerformed(ActionEvent e) {
116

    
117
        String command = e.getActionCommand();
118
        FilesystemStoreListModel model =
119
            (FilesystemStoreListModel) getFileList().getModel();
120

    
121
        if (command == EDIT_COMMAND) {
122
            DynObject dynObject =
123
                model.getDynObjectAt(getFileList().getSelectedIndex());
124

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

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

    
148
            // force repaint
149
            ListModel lm = this.getFileList().getModel();
150
            if (lm instanceof FilesystemStoreListModel) {
151
                FilesystemStoreListModel mm = (FilesystemStoreListModel) lm;
152
                mm.forceUpdate(getFileList().getSelectedIndex());
153
            }
154
            
155

    
156
            
157
        } else {
158
            super.actionPerformed(e);
159
        }
160
    }
161

    
162
    /**
163
     * @return
164
     */
165
    private IProjection getDefaultProjection() {
166
        return getMapCtrl().getViewPort().getProjection();
167
    }
168

    
169
    public static void main(String[] args) {
170
        try {
171
            new DefaultLibrariesInitializer().fullInitialize();
172

    
173
            JFrame frame = new JFrame();
174

    
175
            AddLayerDialog addLayerDlg = new AddLayerDialog();
176

    
177
            FilesystemExplorerAddLayerWizardPanel wizardPanel =
178
                new FilesystemExplorerAddLayerWizardPanel();
179
            addLayerDlg.addWizardTab("File", wizardPanel);
180
            wizardPanel.initWizard();
181

    
182
            frame.setLayout(new BorderLayout());
183
            frame.add(addLayerDlg);
184

    
185
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
186
            frame.setBounds(10, 10, 400, 400);
187
            frame.setVisible(true);
188

    
189
        } catch (Exception e) {
190
            e.printStackTrace();
191
            System.exit(-1);
192
            return;
193
        }
194
    }
195

    
196
    @Override
197
    protected PrepareContext getPrepareDataStoreContext() {
198
        if (this.prepareContext == null) {
199
            this.prepareContext = new PrepareContextView() {
200

    
201
                public Window getOwnerWindow() {
202
                    return null;
203
                }
204

    
205
                public MapControl getMapControl() {
206
                    return FilesystemExplorerAddLayerWizardPanel.this
207
                        .getMapCtrl();
208
                }
209

    
210
                                public IProjection getViewProjection() {
211
                                        return getMapControl().getProjection();
212
                                }
213

    
214
            };
215
        }
216
        return this.prepareContext;
217
    }
218
    
219
    // =================
220
    
221
    private class TwoColumnFileListCellRenderer extends JPanel implements ListCellRenderer {
222

    
223
        private JLabel fileName = null;
224
        private JLabel crsName = null;
225
        
226
        private Color selBC = null;
227
        private Color selFC = null;
228
        private Color unselBC = null;
229
        private Color unselFC = null;
230
        
231
        private MapControl mapCtrl = null;
232
        
233
        public TwoColumnFileListCellRenderer(MapControl mct) {
234
            // 1 col for name, one for crs
235
            setLayout(new GridLayout(0, 2));
236
            fileName = new JLabel();
237
            crsName = new JLabel();
238
            
239
            Font fnt = crsName.getFont();
240
            fnt = fnt.deriveFont((float) (fnt.getSize() - 1));
241
            crsName.setFont(fnt);
242
            
243
            add(fileName);
244
            add(crsName);
245
            mapCtrl = mct;
246
            
247
            UIDefaults defaults = javax.swing.UIManager.getDefaults();
248
            selBC = defaults.getColor("List.selectionBackground");
249
            selFC = defaults.getColor("List.selectionForeground");
250
            unselBC = this.getBackground();
251
            unselFC = this.getForeground();
252
        }
253

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

    
318
        /**
319
         * @param string
320
         * @return
321
         */
322
        private String fixString(String str) {
323
            
324
            int len = str.length();
325
            String resp = str;
326
            if (len < 32) {
327
                // return str;
328
            } else {
329
                int cut = len - 14;
330
                resp = resp.substring(0, 14) +
331
                    "..." + resp.substring(cut); 
332
            }
333
            return resp + "    ";
334
        }
335
        
336
        private String getViewCRS() {
337
            if (this.mapCtrl == null) {
338
                return "CRS?";
339
            } else {
340
                return mapCtrl.getProjection().getAbrev();
341
            }
342
        }
343
        
344
    }
345

    
346

    
347
}