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

History | View | Annotate | Download (10.8 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
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32

    
33
import javax.swing.JLabel;
34
import javax.swing.JList;
35
import javax.swing.JPanel;
36
import javax.swing.ListCellRenderer;
37
import javax.swing.ListModel;
38
import javax.swing.UIDefaults;
39
import org.apache.commons.lang3.StringUtils;
40

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

    
45
import org.gvsig.andami.messages.Messages;
46
import org.gvsig.app.ApplicationLocator;
47
import org.gvsig.app.prepareAction.PrepareContext;
48
import org.gvsig.app.prepareAction.PrepareContextView;
49
import org.gvsig.fmap.dal.DataStore;
50
import org.gvsig.fmap.dal.DataStoreParameters;
51
import org.gvsig.fmap.dal.DataTypes;
52
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
53
import org.gvsig.fmap.mapcontrol.MapControl;
54
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
55
import org.gvsig.tools.ToolsLocator;
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.i18n.I18nManager;
60
import org.gvsig.tools.service.ServiceException;
61
import org.gvsig.tools.swing.api.ToolsSwingLocator;
62
import org.gvsig.tools.swing.api.windowmanager.Dialog;
63
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
64
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
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
    @Override
81
    public void initWizard() {
82

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

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

    
95
        for (DataStoreParameters params : this.getParameters()) {
96
            IProjection 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 = (FilesystemStoreListModel) getFileList().getModel();
122

    
123
        if (EDIT_COMMAND.equalsIgnoreCase(command)) {
124
            showPropertiesDialog(model.getDynObjectAt(getFileList().getSelectedIndex()));
125
        } else {
126
            super.actionPerformed(e);
127
        }
128
    }
129

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

    
145
        FilesystemExplorerPropertiesPanelManager manager = ApplicationLocator.getFilesystemExplorerPropertiesPanelManager();
146
        manager.showPropertiesDialog(parameters);
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
     * @return
158
     */
159
    private IProjection getDefaultProjection() {
160
        return getMapCtrl().getViewPort().getProjection();
161
    }
162

    
163
    @Override
164
    protected PrepareContext getPrepareDataStoreContext() {
165
        if (this.prepareContext == null) {
166
            this.prepareContext = new PrepareContextView() {
167

    
168
                @Override
169
                public Window getOwnerWindow() {
170
                    return null;
171
                }
172

    
173
                @Override
174
                public MapControl getMapControl() {
175
                    return FilesystemExplorerAddLayerWizardPanel.this
176
                            .getMapCtrl();
177
                }
178

    
179
                @Override
180
                public IProjection getViewProjection() {
181
                    return getMapControl().getProjection();
182
                }
183

    
184
            };
185
        }
186
        return this.prepareContext;
187
    }
188

    
189
    // =================
190
    private class TwoColumnFileListCellRenderer extends JPanel implements ListCellRenderer {
191

    
192
        private JLabel fileName = null;
193
        private JLabel crsName = null;
194

    
195
        private Color selBC = null;
196
        private Color selFC = null;
197
        private Color unselBC = null;
198
        private Color unselFC = null;
199

    
200
        private MapControl mapCtrl = null;
201

    
202
        public TwoColumnFileListCellRenderer(MapControl mct) {
203
            // 1 col for name, one for crs
204
            setLayout(new GridLayout(0, 2));
205
            fileName = new JLabel();
206
            crsName = new JLabel();
207

    
208
            Font fnt = crsName.getFont();
209
            fnt = fnt.deriveFont((float) (fnt.getSize() - 1));
210
            crsName.setFont(fnt);
211

    
212
            add(fileName);
213
            add(crsName);
214
            mapCtrl = mct;
215

    
216
            UIDefaults defaults = javax.swing.UIManager.getDefaults();
217
            selBC = defaults.getColor("List.selectionBackground");
218
            selFC = defaults.getColor("List.selectionForeground");
219
            unselBC = this.getBackground();
220
            unselFC = this.getForeground();
221
        }
222

    
223
        @Override
224
        public Component getListCellRendererComponent(
225
                JList list, Object _value,
226
                int index, boolean isSelected, boolean cellHasFocus) {
227

    
228
            if (isSelected) {
229
                setBackground(selBC);
230
                fileName.setForeground(selFC);
231
                crsName.setForeground(selFC);
232
            } else {
233
                setBackground(unselBC);
234
                fileName.setForeground(unselFC);
235
                crsName.setForeground(unselFC);
236
            }
237

    
238
            // ===========================
239
            fileName.setText(_value == null
240
                    ? "?" : fixString(_value.toString()));
241

    
242
            ListModel lm = list.getModel();
243
            String unkown_tag = Messages.get("_Unknown_CRS_so_assumed");
244
            String view_crs = getViewCRS();
245

    
246
            if (lm instanceof FilesystemStoreListModel) {
247

    
248
                FilesystemStoreListModel fsslm = (FilesystemStoreListModel) lm;
249
                DynObject dyno = fsslm.getDynObjectAt(index);
250

    
251
                try {
252
                    Object crs_obj = dyno.getDynValue(DataStore.METADATA_CRS);
253
                    if (crs_obj == null || crs_obj.toString().compareToIgnoreCase("null") == 0) {
254
                        crsName.setText(unkown_tag + " " + view_crs);
255
                    } else {
256
                        if (crs_obj instanceof IProjection) {
257
                            IProjection ipr = (IProjection) crs_obj;
258
                            if (ipr.getAbrev().compareToIgnoreCase(view_crs) == 0) {
259
                                // CRS set but same as view's
260
                                crsName.setText(view_crs);
261
                            } else {
262
                                // CRS set and not same as view's
263
                                crsName.setText(ipr.getAbrev()
264
                                        + " ("
265
                                        + Messages.get("_reprojected_on_the_fly")
266
                                        + ")");
267
                            }
268
                        } else {
269
                            // CRS parameter class not expected
270
                            crsName.setText(unkown_tag + " " + view_crs);
271
                        }
272

    
273
                    }
274
                } catch (Exception ex) {
275
                    // CRS parameter not found
276
                    crsName.setText(unkown_tag + " " + view_crs);
277
                }
278

    
279
            } else {
280
                // list model not of expected class
281
                crsName.setText(unkown_tag + " " + view_crs);
282
            }
283

    
284
            return this;
285
        }
286

    
287
        /**
288
         * @param string
289
         * @return
290
         */
291
        private String fixString(String str) {
292

    
293
            int len = str.length();
294
            String resp = str;
295
            if (len < 32) {
296
                // return str;
297
            } else {
298
                int cut = len - 14;
299
                resp = resp.substring(0, 14)
300
                        + "..." + resp.substring(cut);
301
            }
302
            return resp + "    ";
303
        }
304

    
305
        private String getViewCRS() {
306
            if (this.mapCtrl == null) {
307
                return "CRS?";
308
            } else {
309
                return mapCtrl.getProjection().getAbrev();
310
            }
311
        }
312

    
313
    }
314

    
315
}