Statistics
| Revision:

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

History | View | Annotate | Download (11.6 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.Font;
33
import java.awt.GridLayout;
34
import java.awt.Window;
35
import java.awt.event.ActionEvent;
36

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

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

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

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

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

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

    
77
    private PrepareContext prepareContext;
78

    
79
    public void initWizard() {
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
176
            JFrame frame = new JFrame();
177

    
178
            AddLayerDialog addLayerDlg = new AddLayerDialog();
179

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

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

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

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

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

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

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

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

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

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

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

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

    
349

    
350
}