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

History | View | Annotate | Download (9.68 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.dal.serverexplorer.filesystem.swing;
24

    
25
import java.awt.Color;
26
import java.awt.Component;
27
import java.awt.Font;
28
import java.awt.GridLayout;
29
import java.awt.Window;
30

    
31
import javax.swing.JLabel;
32
import javax.swing.JList;
33
import javax.swing.JPanel;
34
import javax.swing.ListCellRenderer;
35
import javax.swing.ListModel;
36
import javax.swing.UIDefaults;
37

    
38
import org.cresques.cts.IProjection;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
import org.gvsig.andami.messages.Messages;
43
import org.gvsig.app.ApplicationLocator;
44
import org.gvsig.app.prepareAction.PrepareContext;
45
import org.gvsig.app.prepareAction.PrepareContextView;
46
import org.gvsig.fmap.dal.DataStore;
47
import org.gvsig.fmap.dal.DataStoreParameters;
48
import org.gvsig.fmap.dal.DataTypes;
49
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
50
import org.gvsig.fmap.mapcontrol.MapControl;
51
import org.gvsig.tools.dynobject.DynField;
52
import org.gvsig.tools.dynobject.DynObject;
53
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
54

    
55
/**
56
 * @author jmvivo
57
 *
58
 */
59
public class FilesystemExplorerAddLayerWizardPanel extends
60
        FilesystemExplorerWizardPanel {
61

    
62
    private static final long serialVersionUID = -5054057255129168139L;
63

    
64
    private static final Logger LOG = LoggerFactory
65
            .getLogger(FilesystemExplorerAddLayerWizardPanel.class);
66

    
67
    private PrepareContext prepareContext;
68

    
69
    @Override
70
    public void initWizard() {
71

    
72
        super.initWizard();
73
        this.getFileList().setCellRenderer(
74
                new TwoColumnFileListCellRenderer(this.getMapCtrl()));
75
    }
76

    
77
    @Override
78
    public void execute() {
79
        if (this.getMapCtrl() == null) {
80
            throw new IllegalArgumentException("MapControl need");
81
        }
82
        String layerName;
83

    
84
        for (DataStoreParameters params : this.getParameters()) {
85
            IProjection proj = this.getMapCtrl().getProjection();
86

    
87
            // Buscamos por el parametro de la proyeccion
88
            // que sean obligatorios y estén a null
89
            // y le ponemos la proyeccion de la vista
90
            DynField[] fields = params.getDynClass().getDynFields();
91
            for (DynField field : fields) {
92
                if (field.getType() == DataTypes.CRS && field.isMandatory()) {
93
                    if (params.getDynValue(field.getName()) == null) {
94
                        params.setDynValue(field.getName(), proj);
95
                    }
96
                }
97
            }
98

    
99
            layerName
100
                    = ((FilesystemStoreParameters) params).getFile().getName();
101

    
102
            this.doAddLayer(this.getMapCtrl(), layerName, params);
103
        }
104
    }
105

    
106
    protected void showPropertiesDialog(final DynObject parameters) {
107
        // For store parameters with a CRS field, add the current view
108
        // CRS as default value when null.
109
        try {
110
            IProjection projection = (IProjection) parameters.getDynValue("CRS");
111
            if (projection == null) {
112
                projection = getDefaultProjection();
113
                parameters.setDynValue("CRS", projection);
114
            }
115
        } catch (DynFieldNotFoundException e1) {
116
            LOG.info("Loading a store whose parameters don't have a CRS field");
117
        }
118

    
119
        FilesystemExplorerPropertiesPanelManager manager = ApplicationLocator.getFilesystemExplorerPropertiesPanelManager();
120
        FilesystemExplorerPropertiesPanel panel = manager.createPanel(parameters);   
121
        panel.setExcludeGeometryOptions(false);
122
        manager.showPropertiesDialog(parameters, panel);
123
        
124
        refreshFileList();
125
    }
126

    
127

    
128
    /**
129
     * @return
130
     */
131
    private IProjection getDefaultProjection() {
132
        return getMapCtrl().getViewPort().getProjection();
133
    }
134

    
135
    @Override
136
    protected PrepareContext getPrepareDataStoreContext() {
137
        if (this.prepareContext == null) {
138
            this.prepareContext = new PrepareContextView() {
139

    
140
                @Override
141
                public Window getOwnerWindow() {
142
                    return null;
143
                }
144

    
145
                @Override
146
                public MapControl getMapControl() {
147
                    return FilesystemExplorerAddLayerWizardPanel.this
148
                            .getMapCtrl();
149
                }
150

    
151
                @Override
152
                public IProjection getViewProjection() {
153
                    return getMapControl().getProjection();
154
                }
155

    
156
            };
157
        }
158
        return this.prepareContext;
159
    }
160

    
161
    // =================
162
    private class TwoColumnFileListCellRenderer extends JPanel implements ListCellRenderer {
163

    
164
        private JLabel fileName = null;
165
        private JLabel crsName = null;
166

    
167
        private Color selBC = null;
168
        private Color selFC = null;
169
        private Color unselBC = null;
170
        private Color unselFC = null;
171

    
172
        private MapControl mapCtrl = null;
173

    
174
        public TwoColumnFileListCellRenderer(MapControl mct) {
175
            // 1 col for name, one for crs
176
            setLayout(new GridLayout(0, 2));
177
            fileName = new JLabel();
178
            crsName = new JLabel();
179

    
180
            Font fnt = crsName.getFont();
181
            fnt = fnt.deriveFont((float) (fnt.getSize() - 1));
182
            crsName.setFont(fnt);
183

    
184
            add(fileName);
185
            add(crsName);
186
            mapCtrl = mct;
187

    
188
            UIDefaults defaults = javax.swing.UIManager.getDefaults();
189
            selBC = defaults.getColor("List.selectionBackground");
190
            selFC = defaults.getColor("List.selectionForeground");
191
            unselBC = this.getBackground();
192
            unselFC = this.getForeground();
193
        }
194

    
195
        @Override
196
        public Component getListCellRendererComponent(
197
                JList list, Object _value,
198
                int index, boolean isSelected, boolean cellHasFocus) {
199

    
200
            if (isSelected) {
201
                setBackground(selBC);
202
                fileName.setForeground(selFC);
203
                crsName.setForeground(selFC);
204
            } else {
205
                setBackground(unselBC);
206
                fileName.setForeground(unselFC);
207
                crsName.setForeground(unselFC);
208
            }
209

    
210
            // ===========================
211
            fileName.setText(_value == null
212
                    ? "?" : fixString(_value.toString()));
213

    
214
            ListModel lm = list.getModel();
215
            String unkown_tag = Messages.get("_Unknown_CRS_so_assumed");
216
            String view_crs = getViewCRS();
217

    
218
            if (lm instanceof FilesystemStoreListModel) {
219

    
220
                FilesystemStoreListModel fsslm = (FilesystemStoreListModel) lm;
221
                DynObject dyno = fsslm.getDynObjectAt(index);
222

    
223
                try {
224
                    Object crs_obj = dyno.getDynValue(DataStore.METADATA_CRS);
225
                    if (crs_obj == null || crs_obj.toString().compareToIgnoreCase("null") == 0) {
226
                        crsName.setText(unkown_tag + " " + view_crs);
227
                    } else {
228
                        if (crs_obj instanceof IProjection) {
229
                            IProjection ipr = (IProjection) crs_obj;
230
                            if (ipr.getAbrev().compareToIgnoreCase(view_crs) == 0) {
231
                                // CRS set but same as view's
232
                                crsName.setText(view_crs);
233
                            } else {
234
                                // CRS set and not same as view's
235
                                crsName.setText(ipr.getAbrev()
236
                                        + " ("
237
                                        + Messages.get("_reprojected_on_the_fly")
238
                                        + ")");
239
                            }
240
                        } else {
241
                            // CRS parameter class not expected
242
                            crsName.setText(unkown_tag + " " + view_crs);
243
                        }
244

    
245
                    }
246
                } catch (Exception ex) {
247
                    // CRS parameter not found
248
                    crsName.setText(unkown_tag + " " + view_crs);
249
                }
250

    
251
            } else {
252
                // list model not of expected class
253
                crsName.setText(unkown_tag + " " + view_crs);
254
            }
255

    
256
            return this;
257
        }
258

    
259
        /**
260
         * @param string
261
         * @return
262
         */
263
        private String fixString(String str) {
264

    
265
            int len = str.length();
266
            String resp = str;
267
            if (len < 32) {
268
                // return str;
269
            } else {
270
                int cut = len - 14;
271
                resp = resp.substring(0, 14)
272
                        + "..." + resp.substring(cut);
273
            }
274
            return resp + "    ";
275
        }
276

    
277
        private String getViewCRS() {
278
            if (this.mapCtrl == null) {
279
                return "CRS?";
280
            } else {
281
                return mapCtrl.getProjection().getAbrev();
282
            }
283
        }
284

    
285
    }
286

    
287
}