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 / app / project / documents / view / legend / gui / AttrInTableLabeling.java @ 40558

History | View | Annotate | Download (16.3 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.app.project.documents.view.legend.gui;
25

    
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.awt.Font;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.util.ArrayList;
32
import java.util.Iterator;
33

    
34
import javax.swing.BorderFactory;
35
import javax.swing.ButtonGroup;
36
import javax.swing.JButton;
37
import javax.swing.JPanel;
38
import javax.swing.JRadioButton;
39
import javax.swing.JTextField;
40

    
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.andami.messages.NotificationManager;
43
import org.gvsig.app.ApplicationLocator;
44
import org.gvsig.app.gui.JComboBoxUnits;
45
import org.gvsig.app.gui.panels.ColorChooserPanel;
46
import org.gvsig.app.gui.styling.JComboBoxUnitsReferenceSystem;
47
import org.gvsig.app.gui.utils.FontChooser;
48
import org.gvsig.fmap.dal.DataTypes;
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
51
import org.gvsig.fmap.dal.feature.FeatureType;
52
import org.gvsig.fmap.mapcontext.MapContextLocator;
53
import org.gvsig.fmap.mapcontext.layers.FLayer;
54
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
55
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
56
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
57
import org.gvsig.gui.beans.swing.JBlank;
58
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;
59
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.IAttrInTableLabelingStrategy;
60
import org.gvsig.utils.swing.JComboBox;
61

    
62

    
63
public class AttrInTableLabeling extends JPanel implements  ILabelingStrategyPanel{
64
        private static final long serialVersionUID = 8229927418031917075L;
65
        private static final String NO_FIELD_ITEM = "-- " +
66
                        PluginServices.getText(LabelingManager.class, "none") + " --";
67
        private String[] fieldNames;
68
        private String[] numericFieldNames;
69
        private String[] integerFieldNames;
70

    
71
        private JRadioButton rdBtnFixedHeight;
72
        private JRadioButton rdBtnHeightField;
73
        private JRadioButton rdBtnFixedColor;
74
        private JRadioButton rdBtnColorField;
75
        private JComboBox cmbTextField;
76
        private JComboBox cmbHeightField;
77
        private JComboBox cmbRotationField;
78
        private JComboBoxUnits cmbUnits;
79
        private JComboBoxUnitsReferenceSystem cmbReferenceSystem;
80
        private JTextField txtHeightField;
81
        private FLyrVect layer;
82

    
83
        private ColorChooserPanel colorChooser;
84
        private JComboBox cmbColorField;
85
        private JButton chooseFontBut;
86
        private Font labelFont;
87

    
88
        public AttrInTableLabeling() {
89
                labelFont =
90
                                MapContextLocator.getSymbolManager()
91
                                                .getSymbolPreferences()
92
                                                .getDefaultSymbolFont();
93
                initialize();
94
        }
95

    
96
        private void initialize() {
97
                setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
98
                GridBagLayoutPanel panel = new GridBagLayoutPanel();
99

    
100
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
101
                aux.addComponent(PluginServices.getText(this, "_Field_to_use_in_label") + ":", getCmbTextField());
102
                aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
103
                aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
104
                aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
105
                aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
106
                aux.addComponent(PluginServices.getText(this,""),getCmbReferenceSystem());
107
                panel.add(aux);
108

    
109
                aux = new GridBagLayoutPanel();
110
                aux.addComponent(getChooseFontBut(),new JBlank(20,20));
111
                GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
112
                aux2.setBorder(BorderFactory.createTitledBorder(null,PluginServices.getText(this,"color")));
113
                aux2.addComponent(getRdBtnFixedColor(),colorChooser = new ColorChooserPanel(true));
114
                aux2.addComponent(getRdBtnColorField(),getCmbColorField());
115
                aux.addComponent(aux2);
116

    
117
                panel.add(new JBlank(20,20));
118
                panel.add(aux);
119

    
120

    
121
                add(panel);
122

    
123

    
124
                ButtonGroup group = new ButtonGroup();
125
                group.add(getRdBtnFixedHeight());
126
                group.add(getRdBtnHeightField());
127

    
128
                ButtonGroup colorGroup = new ButtonGroup();
129
                colorGroup.add(getRdBtnFixedColor());
130
                colorGroup.add(getRdBtnColorField());
131

    
132
//                getRdBtnHeightField().setEnabled(true);
133
        }
134

    
135
        private JButton getChooseFontBut() {
136
                if(chooseFontBut == null){
137
                        chooseFontBut = new JButton(PluginServices.getText(this,"font"));
138
                        chooseFontBut.setPreferredSize(new Dimension(80,10));
139
                        chooseFontBut.addActionListener(new ActionListener(){
140

    
141
                                public void actionPerformed(ActionEvent e) {
142
                                        Font newFont;
143

    
144
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
145
                                        if (newFont == null) {
146
                                                return;
147
                                        }
148

    
149
                                        labelFont = newFont;
150
                                }
151

    
152
                        });
153
                }
154
                return chooseFontBut;
155
        }
156

    
157
        private JRadioButton getRdBtnFixedHeight() {
158
                if (rdBtnFixedHeight == null) {
159
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
160
                        rdBtnFixedHeight.setSelected(true);
161
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
162
                }
163

    
164
                return rdBtnFixedHeight;
165
        }
166

    
167
        private JRadioButton getRdBtnHeightField() {
168
                if (rdBtnHeightField == null) {
169
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
170
                        rdBtnHeightField.setSelected(false);
171
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
172
                }
173

    
174
                return rdBtnHeightField;
175
        }
176

    
177
        private JRadioButton getRdBtnFixedColor() {
178
                if (rdBtnFixedColor == null) {
179
                        rdBtnFixedColor = new JRadioButton(PluginServices.getText(this, "_Fixed_color") + ":");
180
                        rdBtnFixedColor.setSelected(true);
181
                        rdBtnFixedColor.setName("RDFIXEDCOLOR");
182
                }
183

    
184
                return rdBtnFixedColor;
185
        }
186

    
187
        private JRadioButton getRdBtnColorField() {
188
                if (rdBtnColorField == null) {
189
                        rdBtnColorField = new JRadioButton(PluginServices.getText(this, "color_field") + ":");
190
                        rdBtnColorField.setSelected(false);
191
                        rdBtnColorField.setName("RDCOLORFIELD");
192
                }
193

    
194
                return rdBtnColorField;
195
        }
196

    
197
        private JComboBoxUnits getCmbUnits() {
198
                if (cmbUnits == null) {
199
                        cmbUnits = new JComboBoxUnits();
200
                        cmbUnits.setSelectedIndex(
201
                                        ApplicationLocator.getManager().getPreferences().getInt("DefaultDistanceUnits", 0)
202
                        );
203
                        cmbUnits.setName("CMBUNITS");
204
                }
205

    
206
                return cmbUnits;
207
        }
208

    
209
        private JComboBoxUnitsReferenceSystem getCmbReferenceSystem(){
210
                if(cmbReferenceSystem == null){
211
                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem();
212
                        cmbReferenceSystem.setName("CMBREFSYST");
213
                }
214
                return cmbReferenceSystem;
215
        }
216

    
217
        private JComboBox getCmbColorField() {
218
                if (cmbColorField == null) {
219
                        cmbColorField = new JComboBox();
220
                        cmbColorField.setName("CMBCOLOR");
221
                }
222

    
223
                return cmbColorField;
224
        }
225

    
226
        private void refreshControls() {
227
                // When the attributes are in the table -----
228
                //      field with the text
229
                refreshCmbTextField();
230

    
231
                //      field with the rotation
232
                refreshCmbRotationField();
233

    
234
                //      field with the text height or the text size
235
                refreshTextHeight();
236

    
237
                //                the text size unit name
238
                refreshCmbUnits();
239

    
240
                refreshCmbRefSystem();
241
                //                the font for the text
242
                refreshFont();
243
                //                the color for the font
244
                refreshColorFont();
245
        }
246

    
247
        private JComboBox getCmbRotationField() {
248
                if (cmbRotationField == null) {
249
                        cmbRotationField = new JComboBox();
250
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
251
                        cmbRotationField.setName("CMBROTATIONFIELD");
252
                }
253
                return cmbRotationField;
254
        }
255

    
256
        private JComboBox getCmbHeightField() {
257
                if (cmbHeightField == null) {
258
                        cmbHeightField = new JComboBox();
259
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
260
                        cmbHeightField.setName("CMBHEIGHTFIELD");
261
                }
262
                return cmbHeightField;
263
        }
264

    
265
        private JComboBox getCmbTextField() {
266
                if (cmbTextField == null) {
267
                        cmbTextField = new JComboBox();
268
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
269
                        cmbTextField.setName("CMBTEXTFIELD");
270
                }
271
                return cmbTextField;
272
        }
273

    
274

    
275
        private JTextField getTxtHeightField() {
276
                if (txtHeightField == null) {
277
                        txtHeightField = new JTextField(10);
278
                        txtHeightField.setText("10");
279
                        txtHeightField.setName("TXTHEIGHTFIELD");
280
                }
281

    
282
                return txtHeightField;
283
        }
284

    
285
        public void actionPerformed(ActionEvent e) {
286
                // TODO Auto-generated method stub
287
                throw new Error("Not yet implemented!");
288

    
289
        }
290
        private ColorChooserPanel getColorChooser() {
291
                if (colorChooser == null){
292
                        colorChooser = new ColorChooserPanel(true);
293
                }
294
                return colorChooser;
295
        }
296

    
297
        public ILabelingStrategy getLabelingStrategy() {
298
                // user selected to define each label attributes from values
299
                // contained in the table for each feature row.
300

    
301
                double fixedSize;
302
                try {
303
                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
304
                } catch (Exception e) {
305
                        fixedSize = 10;
306
                }
307
                AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
308
                strategy.setLayer(layer);
309

    
310
                if(getCmbHeightField().getItemCount() > 0 && !rdBtnFixedHeight.isSelected()) {
311
                        strategy.setHeightField(
312
                                (String) getCmbHeightField().getSelectedItem());
313
                }
314
                if(getCmbRotationField().getItemCount() > 0) {
315
                        if(!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM)) {
316
                                strategy.setRotationField(
317
                                                (String) getCmbRotationField().getSelectedItem());
318
                        } else {
319
                                strategy.setRotationField(null);
320
                        }
321
                }
322

    
323
                if(getCmbTextField().getItemCount() > 0) {
324
                        strategy.setTextField(
325
                                (String) getCmbTextField().getSelectedItem());
326
                }
327

    
328
                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
329
                strategy.setFixedSize(fixedSize);
330

    
331
                if(getCmbUnits().getItemCount() > 0) {
332
                        strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
333
                }
334
                if(getCmbReferenceSystem().getItemCount() > 0) {
335
                        strategy.setReferenceSystem(getCmbReferenceSystem().getSelectedIndex());
336
                }
337

    
338
                strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
339
                strategy.setFixedColor(getColorChooser().getColor());
340

    
341
                if(getCmbColorField().getItemCount() > 0 && !rdBtnFixedColor.isSelected()) {
342
                        strategy.setColorField((String) getCmbColorField().getSelectedItem());
343
                }
344

    
345

    
346
                strategy.setFont(labelFont);
347
                return strategy;
348
        }
349

    
350
        public void setModel(FLayer layer, ILabelingStrategy str) {
351
                this.layer = (FLyrVect) layer;
352
                // to allow the labeling of non-FLyrVect layers
353
                if (layer instanceof FLyrVect) {
354
                        FLyrVect lv = (FLyrVect) layer;
355
                        try {
356
                                FeatureType featureType = lv.getFeatureStore()
357
                                                .getDefaultFeatureType();
358
                                fieldNames = new String[featureType.size()];
359
                                Iterator<FeatureAttributeDescriptor> iterator = featureType
360
                                                .iterator();
361
                                ArrayList<String> l = new ArrayList<String>();
362
                                ArrayList<String> lColors = new ArrayList<String>();
363
                                String name;
364
                                FeatureAttributeDescriptor descriptor;
365
                                while (iterator.hasNext()) {
366
                                        descriptor = iterator.next();
367

    
368
                                        name = descriptor.getName();
369
                                        fieldNames[descriptor.getIndex()] = name;
370
                                        switch (descriptor.getType()) {
371
//                                        case DataTypes.DECIMAL:
372
//                                        case DataTypes.NUMERIC:
373
                                        case DataTypes.FLOAT:
374
//                                        case DataTypes.REAL:
375
                                        case DataTypes.DOUBLE:
376
                                                l.add(name);
377
                                                break;
378
                                        case DataTypes.INT:
379
//                                        case DataTypes.SMALLINT:
380
//                                        case DataTypes.TINYINT:
381
                                        case DataTypes.LONG:
382
                                                lColors.add(name);
383
                                                l.add(name);
384
                                                break;
385
                                        }
386
                                }
387
                                numericFieldNames = l.toArray(new String[l.size()]);
388
                                integerFieldNames = lColors.toArray(new String[lColors.size()]);
389
                        } catch (DataException e) {
390
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
391
                        }
392
                        refreshControls();
393
                }
394
        }
395

    
396
        private void refreshColorFont(){
397

    
398
                getCmbColorField().removeAllItems();
399

    
400
                boolean enabled = integerFieldNames.length>0;
401
                getCmbColorField().setEnabled(enabled);
402
                getRdBtnColorField().setEnabled(enabled);
403

    
404
                if (!enabled) {
405
                        getRdBtnFixedColor().setSelected(true);
406
                }
407

    
408
                for (int i = 0; i < integerFieldNames.length; i++) {
409
                        getCmbColorField().addItem(integerFieldNames[i]);
410
                }
411

    
412
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
413
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
414
                        getRdBtnFixedColor().setSelected(aux.usesFixedColor());
415
                        getRdBtnColorField().setSelected(!aux.usesFixedColor());
416

    
417
                        String item = aux.getColorField();
418
                        getCmbColorField().setSelectedItem(item);
419
                        getColorChooser().setColor(aux.getFixedColor());
420
                }
421
        }
422

    
423
        private void refreshFont(){
424

    
425
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
426
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
427
                        labelFont = aux.getFont();
428
                }
429
        }
430

    
431
        private void refreshCmbUnits() {
432

    
433
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
434
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
435
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
436
                }
437
        }
438

    
439
        private void refreshCmbRefSystem() {
440

    
441
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
442
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
443
                        getCmbReferenceSystem().setSelectedIndex(aux.getReferenceSystem());
444
                }
445
        }
446

    
447
        private void refreshTextHeight() {
448
                getCmbHeightField().removeAllItems();
449

    
450
                /*
451
                boolean enabled = numericFieldNames.length>0;
452
            // getCmbHeightField().setEnabled(enabled);
453
            // getRdBtnHeightField().setEnabled(enabled);
454

455
                if (!enabled) {
456
                        getRdBtnFixedHeight().setSelected(true);
457
                }
458
                */
459

    
460
                for (int i = 0; i < numericFieldNames.length; i++) {
461
                        getCmbHeightField().addItem(numericFieldNames[i]);
462
                }
463

    
464
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
465
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
466
                        getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
467
                        getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
468
                        getRdBtnHeightField().setSelected(!aux.usesFixedSize());
469

    
470
                        String item = aux.getHeightField();
471
                        getCmbHeightField().setSelectedItem(item);
472

    
473
                }
474

    
475
        }
476

    
477
        private void refreshCmbRotationField() {
478
                getCmbRotationField().removeAllItems();
479
                getCmbRotationField().addItem(NO_FIELD_ITEM);
480
                for (int i = 0; i < numericFieldNames.length; i++) {
481
                        getCmbRotationField().addItem(numericFieldNames[i]);
482
                }
483

    
484
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
485
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
486
                        String item = aux.getRotationField();
487
                        getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
488
                }
489
        }
490

    
491
        private void refreshCmbTextField() {
492
                getCmbTextField().removeAllItems();
493
                for (int i = 0; i < fieldNames.length; i++) {
494
                        getCmbTextField().addItem(fieldNames[i]);
495
                }
496

    
497
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
498
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
499
                        String item = aux.getTextField();
500
                        getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
501
                }
502
        }
503

    
504
        public String getLabelingStrategyName() {
505
                return PluginServices.getText(this, "label_attributes_defined_in_table");
506
        }
507

    
508
        public Class getLabelingStrategyClass() {
509
                return AttrInTableLabelingStrategy.class;
510
        }
511

    
512
        public void setEnabled(boolean enabled) {
513
                super.setEnabled(enabled);
514
                getChooseFontBut().setEnabled(enabled);
515
                getCmbColorField().setEnabled(enabled);
516
                getCmbHeightField().setEnabled(enabled);
517
                getCmbReferenceSystem().setEnabled(enabled);
518
                getCmbRotationField().setEnabled(enabled);
519
                getCmbTextField().setEnabled(enabled);
520
                getCmbUnits().setEnabled(enabled);
521
                getColorChooser().setEnabled(enabled);
522
                getRdBtnColorField().setEnabled(enabled);
523
                getRdBtnFixedColor().setEnabled(enabled);
524
                getRdBtnFixedHeight().setEnabled(enabled);
525
                getRdBtnHeightField().setEnabled(enabled);
526
                getTxtHeightField().setEnabled(enabled);
527

    
528
        }
529
}