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 @ 40558

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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 IVER T.I. S.A.   {{Task}}
27
 */
28
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing;
29

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

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

    
46
import org.cresques.cts.IProjection;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

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

    
66
/**
67
 * @author jmvivo
68
 * 
69
 */
70
public class FilesystemExplorerAddLayerWizardPanel extends
71
    FilesystemExplorerWizardPanel {
72

    
73
    private static final long serialVersionUID = -5054057255129168139L;
74

    
75
    private static final Logger LOG = LoggerFactory
76
        .getLogger(FilesystemExplorerAddLayerWizardPanel.class);
77

    
78
    private PrepareContext prepareContext;
79

    
80
    public void initWizard() {
81

    
82
        super.initWizard();
83
        this.getFileList().setCellRenderer(
84
            new TwoColumnFileListCellRenderer(this.getMapCtrl()));
85
    }
86

    
87
    @Override
88
    public void execute() {
89
        if (this.getMapCtrl() == null) {
90
            throw new IllegalArgumentException("MapControl need");
91
        }
92
        String layerName;
93

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

    
97
            proj = this.getMapCtrl().getProjection();
98

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

    
111
            layerName =
112
                ((FilesystemStoreParameters) params).getFile().getName();
113

    
114
            this.doAddLayer(this.getMapCtrl(), layerName, params);
115
        }
116
    }
117

    
118
    @Override
119
    public void actionPerformed(ActionEvent e) {
120

    
121
        String command = e.getActionCommand();
122
        FilesystemStoreListModel model =
123
            (FilesystemStoreListModel) getFileList().getModel();
124

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

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

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

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

    
160
            
161
        } else {
162
            super.actionPerformed(e);
163
        }
164
    }
165

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

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

    
177
            JFrame frame = new JFrame();
178

    
179
            AddLayerDialog addLayerDlg = new AddLayerDialog();
180

    
181
            FilesystemExplorerAddLayerWizardPanel wizardPanel =
182
                new FilesystemExplorerAddLayerWizardPanel();
183
            addLayerDlg.addWizardTab("File", wizardPanel);
184
            wizardPanel.initWizard();
185

    
186
            frame.setLayout(new BorderLayout());
187
            frame.add(addLayerDlg);
188

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

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

    
200
    @Override
201
    protected PrepareContext getPrepareDataStoreContext() {
202
        if (this.prepareContext == null) {
203
            this.prepareContext = new PrepareContextView() {
204

    
205
                public Window getOwnerWindow() {
206
                    return null;
207
                }
208

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

    
214
                                public IProjection getViewProjection() {
215
                                        return getMapControl().getProjection();
216
                                }
217

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

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

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

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

    
350

    
351
}