Statistics
| Revision:

root / org.gvsig.gpe / library / trunk / org.gvsig.gpe / org.gvsig.gpe.exportto / org.gvsig.gpe.exportto.kml / src / main / java / org / gvsig / gpe / exportto / kml / swing / panels / KMLPanelView.java @ 876

History | View | Annotate | Download (9.14 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.gpe.exportto.kml.swing.panels;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.FlowLayout;
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.Insets;
31
import java.util.ArrayList;
32

    
33
import javax.swing.JCheckBox;
34
import javax.swing.JComboBox;
35
import javax.swing.JComponent;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38

    
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
//import org.gvsig.exportto.swing.ExporttoSwingLocator;
43
//import org.gvsig.exportto.swing.ExporttoSwingManager;
44
//import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException;
45
//import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel;
46
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandler;
47
import org.gvsig.gpe.lib.spi.GPEProviderLocator;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.i18n.I18nManager;
50

    
51

    
52
/**
53
 * 
54
 * This addditional panel lets user choose KML
55
 * version and other options.
56
 * 
57
 * @author gvSIG Team
58
 * @version $Id$
59
 * 
60
 */
61
public class KMLPanelView extends JPanel {
62

    
63
    private static final long serialVersionUID = -3278172717881233447L;
64

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

    
67
    private boolean offerLabels = false;
68
    private boolean offerReproj = true;
69
    
70
    private JCheckBox labelsChk = null;
71
    private JCheckBox reproChk = null;
72
    private JCheckBox balloonsChk = null;
73
    
74
    private JComboBox versionCombo = null;
75
    I18nManager i18n = ToolsLocator.getI18nManager();
76
    
77
    public KMLPanelView() {
78
        initializePanel();
79
    }
80

    
81
    /**
82
     * Initializer
83
     */
84
    protected void initializePanel() {
85
        setLayout(new BorderLayout());
86
        add(createPanel(), BorderLayout.CENTER);
87
    }
88

    
89
    private JPanel createPanel() {
90
        
91
        JPanel jpanel1 = new JPanel();
92
        jpanel1.setLayout(new BorderLayout());
93
        
94
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
95
        JPanel topPanel = new JPanel(fl);
96
        // ====================================================
97
        JPanel gridPanel = new JPanel();
98
        gridPanel.setLayout(new GridBagLayout());
99
        GridBagConstraints gbc = new GridBagConstraints();
100
        
101
        gbc.anchor = GridBagConstraints.WEST;
102
        gbc.insets = new Insets(3, 3, 3, 3);
103
        
104
        gbc.gridx = 0;
105
        gbc.gridy = 0;
106
        gbc.gridwidth = 3;
107
        gridPanel.add(new JLabel(i18n.getTranslation("kml_Choose_KML_version")), gbc);
108
        gbc.gridx = 3;
109
        gbc.gridwidth = 3;
110
        gridPanel.add(getVersionsCombo(), gbc);
111
        // =====================
112
        gbc.gridx = 0;
113
        gbc.gridy = 1;
114
        gbc.gridwidth = 1;
115
        gridPanel.add(getBalloonsCheckbox(), gbc);
116
        gbc.gridx = 1;
117
        gbc.gridwidth = 5;
118
        gridPanel.add(new JLabel(i18n.getTranslation(
119
                "kml_Show_attributes_in_balloon")), gbc);
120
        // =====================
121
        gbc.gridx = 0;
122
        gbc.gridy = 2;
123
        gbc.gridwidth = 1;
124
        getLabelsCheckbox(offerLabels);
125
        //gridPanel.add(getLabelsCheckbox(offerLabels), gbc);
126
        gbc.gridx = 1;
127
        gbc.gridwidth = 5;
128
        JLabel lbl_lbl = new JLabel(i18n.getTranslation("" +
129
                        "kml_Use_labels_Label_point_will_be_added_to_non_points"));
130
        lbl_lbl.setEnabled(offerLabels);
131
        //gridPanel.add(lbl_lbl, gbc);
132
        // ====================================================
133
        gbc.gridx = 0;
134
        gbc.gridy = 2;
135
        gbc.gridwidth = 1;
136
        gridPanel.add(getReproCheckbox(offerReproj), gbc);
137
        gbc.gridx = 1;
138
        gbc.gridwidth = 5;
139
        JLabel rep_lbl = new JLabel(i18n.getTranslation(
140
            "kml_Force_to_EPSG_4326"));
141
        rep_lbl.setEnabled(offerReproj);
142
        gridPanel.add(rep_lbl, gbc);
143
        // ====================================================
144
        topPanel.add(gridPanel);
145
        jpanel1.add(topPanel, BorderLayout.NORTH);
146
        return jpanel1;
147
    }
148
    
149
//    public String getMimeType() {
150
//        IGPEWriterHandler item = (IGPEWriterHandler) this.versionCombo.getItemAt(0);
151
//        String longn = item.getFormat();
152
//        return longn;
153
//    }
154

    
155
    public String getMimeType() {
156
        Object obj = this.getVersionsCombo().getSelectedItem();
157
        if (obj instanceof FormatListItem) {
158
            return ((FormatListItem) obj).getLongFormat();
159
        } else {
160
            return null;
161
        }
162
    }
163
    public boolean getUseLabels() {
164
        return this.labelsChk.isSelected();
165
    }
166

    
167
    public boolean getAttsAsBalloon() {
168
        return this.balloonsChk.isSelected();
169
    }
170

    
171
    public boolean getReprojectTo4326() {
172
        return this.reproChk.isSelected();
173
    }
174

    
175
    private JCheckBox getLabelsCheckbox(boolean enabled) {
176
        
177
        if (labelsChk == null) {
178
            labelsChk = new JCheckBox("");
179
            labelsChk.setSelected(false);
180
            labelsChk.setEnabled(enabled);
181
        }
182
        return labelsChk;
183
    }
184
    
185
    private JCheckBox getReproCheckbox(boolean enabled) {
186
        
187
        if (reproChk == null) {
188
            reproChk = new JCheckBox("");
189
            reproChk.setSelected(true);
190
            reproChk.setEnabled(enabled);
191
        }
192
        return reproChk;
193
    }
194
    
195
    private JCheckBox getBalloonsCheckbox() {
196
        
197
        if (balloonsChk == null) {
198
            balloonsChk = new JCheckBox("");
199
            balloonsChk.setSelected(false);
200
        }
201
        return balloonsChk;
202
    }
203
    
204

    
205
    private JComboBox getVersionsCombo() {
206
        
207
        if (versionCombo == null) {
208
            versionCombo = new JComboBox();
209
            ArrayList list = GPEProviderLocator.getGPEProviderManager().getWriterHandlerByFormat("kml");
210
            IGPEWriterHandler item = null;
211
            String longn = null;
212
            String shortn = null;
213
            for (int i=0; i<list.size(); i++) {
214
                item = (IGPEWriterHandler) list.get(i);
215
                longn = item.getFormat();
216
                shortn = getShortWithVersion(longn);
217
                if (longn != null && shortn != null) {
218
                    versionCombo.addItem(new FormatListItem(longn, shortn));
219
                }
220
            }
221
        }
222
        return versionCombo;
223
    }
224

    
225
    public String getPanelTitle() {
226
        return i18n.getTranslation("kml_KML_options");
227
    }
228

    
229
    public boolean isValidPanel() {
230
        return getLongFormat() != null;
231
    }
232
    
233
    
234
    /**
235
     * Returns null if not valid format
236
     */
237
    private String getShortWithVersion(String fmt) {
238
        // Example: "text/xml; subtype=gml/2.1.2" => "gml"
239
        if (fmt == null) {
240
            return null;
241
        }
242
        String[] parts = fmt.split(";");
243
        String aux = "";
244
        if (parts.length > 1) {
245
            aux = parts[1].trim();
246
        } else {
247
            return null;
248
        }
249
        parts = aux.split("=");
250
        if (parts.length > 1) {
251
            aux = parts[1].trim();
252
            // Example: aux = "gml/2.1.2"
253
            aux = aux.replaceAll("/", " ");
254
            return aux.toUpperCase();
255
        } else {
256
            return null;
257
        }
258
    }    
259

    
260
    public void enterPanel() {
261
        
262
    }
263

    
264
    public JComponent asJComponent() {
265
        return this;
266
    }
267
    
268
    public class FormatListItem {
269
        
270
        private String longName = "";
271
        private String shortName = "";
272
        
273
        public FormatListItem(String longname, String shortname) {
274
            longName = longname;
275
            shortName = shortname;
276
        }
277
        
278
        public String toString() {
279
            return shortName;
280
        }
281
        
282
        public String getLongFormat() {
283
            return longName;
284
        }
285
    }
286
    
287
    // ==================================================
288
    // ==================================================
289
    
290
    public String getLongFormat() {
291
        Object obj = this.getVersionsCombo().getSelectedItem();
292
        if (obj instanceof FormatListItem) {
293
            return ((FormatListItem) obj).getLongFormat();
294
        } else {
295
            return null;
296
        }
297
    }
298
    
299
    public boolean useLabels() {
300
        return this.getLabelsCheckbox(false).isSelected();
301
    }
302

    
303
    public boolean useBalloons() {
304
        return this.getBalloonsCheckbox().isSelected();
305
    }
306
    
307
    public boolean mustReprojectToEpsg4326() {
308
        return this.getReproCheckbox(true).isSelected();
309
    }
310
    
311
    
312

    
313
}