Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2061 / applications / appgvSIG / src / org / gvsig / app / project / documents / view / legend / gui / AttrInTableLabeling.java @ 39431

History | View | Annotate | Download (16.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.app.project.documents.view.legend.gui;
42

    
43
import java.awt.Dimension;
44
import java.awt.FlowLayout;
45
import java.awt.Font;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.util.ArrayList;
49
import java.util.Iterator;
50

    
51
import javax.swing.BorderFactory;
52
import javax.swing.ButtonGroup;
53
import javax.swing.JButton;
54
import javax.swing.JPanel;
55
import javax.swing.JRadioButton;
56
import javax.swing.JTextField;
57

    
58
import org.gvsig.andami.PluginServices;
59
import org.gvsig.andami.messages.NotificationManager;
60
import org.gvsig.app.ApplicationLocator;
61
import org.gvsig.app.gui.JComboBoxUnits;
62
import org.gvsig.app.gui.panels.ColorChooserPanel;
63
import org.gvsig.app.gui.styling.JComboBoxUnitsReferenceSystem;
64
import org.gvsig.app.gui.utils.FontChooser;
65
import org.gvsig.fmap.dal.DataTypes;
66
import org.gvsig.fmap.dal.exception.DataException;
67
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
68
import org.gvsig.fmap.dal.feature.FeatureType;
69
import org.gvsig.fmap.mapcontext.MapContextLocator;
70
import org.gvsig.fmap.mapcontext.layers.FLayer;
71
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
72
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
73
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
74
import org.gvsig.gui.beans.swing.JBlank;
75
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;
76
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.styling.IAttrInTableLabelingStrategy;
77
import org.gvsig.utils.swing.JComboBox;
78

    
79

    
80
public class AttrInTableLabeling extends JPanel implements  ILabelingStrategyPanel{
81
        private static final long serialVersionUID = 8229927418031917075L;
82
        private static final String NO_FIELD_ITEM = "-- " +
83
                        PluginServices.getText(LabelingManager.class, "none") + " --";
84
        private String[] fieldNames;
85
        private String[] numericFieldNames;
86
        private String[] integerFieldNames;
87

    
88
        private JRadioButton rdBtnFixedHeight;
89
        private JRadioButton rdBtnHeightField;
90
        private JRadioButton rdBtnFixedColor;
91
        private JRadioButton rdBtnColorField;
92
        private JComboBox cmbTextField;
93
        private JComboBox cmbHeightField;
94
        private JComboBox cmbRotationField;
95
        private JComboBoxUnits cmbUnits;
96
        private JComboBoxUnitsReferenceSystem cmbReferenceSystem;
97
        private JTextField txtHeightField;
98
        private FLyrVect layer;
99

    
100
        private ColorChooserPanel colorChooser;
101
        private JComboBox cmbColorField;
102
        private JButton chooseFontBut;
103
        private Font labelFont;
104

    
105
        public AttrInTableLabeling() {
106
                labelFont =
107
                                MapContextLocator.getSymbolManager()
108
                                                .getSymbolPreferences()
109
                                                .getDefaultSymbolFont();
110
                initialize();
111
        }
112

    
113
        private void initialize() {
114
                setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
115
                GridBagLayoutPanel panel = new GridBagLayoutPanel();
116

    
117
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
118
                aux.addComponent(PluginServices.getText(this, "_Field_to_use_in_label") + ":", getCmbTextField());
119
                aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
120
                aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
121
                aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
122
                aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
123
                aux.addComponent(PluginServices.getText(this,""),getCmbReferenceSystem());
124
                panel.add(aux);
125

    
126
                aux = new GridBagLayoutPanel();
127
                aux.addComponent(getChooseFontBut(),new JBlank(20,20));
128
                GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
129
                aux2.setBorder(BorderFactory.createTitledBorder(null,PluginServices.getText(this,"color")));
130
                aux2.addComponent(getRdBtnFixedColor(),colorChooser = new ColorChooserPanel(true));
131
                aux2.addComponent(getRdBtnColorField(),getCmbColorField());
132
                aux.addComponent(aux2);
133

    
134
                panel.add(new JBlank(20,20));
135
                panel.add(aux);
136

    
137

    
138
                add(panel);
139

    
140

    
141
                ButtonGroup group = new ButtonGroup();
142
                group.add(getRdBtnFixedHeight());
143
                group.add(getRdBtnHeightField());
144

    
145
                ButtonGroup colorGroup = new ButtonGroup();
146
                colorGroup.add(getRdBtnFixedColor());
147
                colorGroup.add(getRdBtnColorField());
148

    
149
//                getRdBtnHeightField().setEnabled(true);
150
        }
151

    
152
        private JButton getChooseFontBut() {
153
                if(chooseFontBut == null){
154
                        chooseFontBut = new JButton(PluginServices.getText(this,"font"));
155
                        chooseFontBut.setPreferredSize(new Dimension(80,10));
156
                        chooseFontBut.addActionListener(new ActionListener(){
157

    
158
                                public void actionPerformed(ActionEvent e) {
159
                                        Font newFont;
160

    
161
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
162
                                        if (newFont == null) {
163
                                                return;
164
                                        }
165

    
166
                                        labelFont = newFont;
167
                                }
168

    
169
                        });
170
                }
171
                return chooseFontBut;
172
        }
173

    
174
        private JRadioButton getRdBtnFixedHeight() {
175
                if (rdBtnFixedHeight == null) {
176
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
177
                        rdBtnFixedHeight.setSelected(true);
178
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
179
                }
180

    
181
                return rdBtnFixedHeight;
182
        }
183

    
184
        private JRadioButton getRdBtnHeightField() {
185
                if (rdBtnHeightField == null) {
186
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
187
                        rdBtnHeightField.setSelected(false);
188
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
189
                }
190

    
191
                return rdBtnHeightField;
192
        }
193

    
194
        private JRadioButton getRdBtnFixedColor() {
195
                if (rdBtnFixedColor == null) {
196
                        rdBtnFixedColor = new JRadioButton(PluginServices.getText(this, "_Fixed_color") + ":");
197
                        rdBtnFixedColor.setSelected(true);
198
                        rdBtnFixedColor.setName("RDFIXEDCOLOR");
199
                }
200

    
201
                return rdBtnFixedColor;
202
        }
203

    
204
        private JRadioButton getRdBtnColorField() {
205
                if (rdBtnColorField == null) {
206
                        rdBtnColorField = new JRadioButton(PluginServices.getText(this, "color_field") + ":");
207
                        rdBtnColorField.setSelected(false);
208
                        rdBtnColorField.setName("RDCOLORFIELD");
209
                }
210

    
211
                return rdBtnColorField;
212
        }
213

    
214
        private JComboBoxUnits getCmbUnits() {
215
                if (cmbUnits == null) {
216
                        cmbUnits = new JComboBoxUnits();
217
                        cmbUnits.setSelectedIndex(
218
                                        ApplicationLocator.getManager().getPreferences().getInt("DefaultDistanceUnits", 0)
219
                        );
220
                        cmbUnits.setName("CMBUNITS");
221
                }
222

    
223
                return cmbUnits;
224
        }
225

    
226
        private JComboBoxUnitsReferenceSystem getCmbReferenceSystem(){
227
                if(cmbReferenceSystem == null){
228
                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem();
229
                        cmbReferenceSystem.setName("CMBREFSYST");
230
                }
231
                return cmbReferenceSystem;
232
        }
233

    
234
        private JComboBox getCmbColorField() {
235
                if (cmbColorField == null) {
236
                        cmbColorField = new JComboBox();
237
                        cmbColorField.setName("CMBCOLOR");
238
                }
239

    
240
                return cmbColorField;
241
        }
242

    
243
        private void refreshControls() {
244
                // When the attributes are in the table -----
245
                //      field with the text
246
                refreshCmbTextField();
247

    
248
                //      field with the rotation
249
                refreshCmbRotationField();
250

    
251
                //      field with the text height or the text size
252
                refreshTextHeight();
253

    
254
                //                the text size unit name
255
                refreshCmbUnits();
256

    
257
                refreshCmbRefSystem();
258
                //                the font for the text
259
                refreshFont();
260
                //                the color for the font
261
                refreshColorFont();
262
        }
263

    
264
        private JComboBox getCmbRotationField() {
265
                if (cmbRotationField == null) {
266
                        cmbRotationField = new JComboBox();
267
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
268
                        cmbRotationField.setName("CMBROTATIONFIELD");
269
                }
270
                return cmbRotationField;
271
        }
272

    
273
        private JComboBox getCmbHeightField() {
274
                if (cmbHeightField == null) {
275
                        cmbHeightField = new JComboBox();
276
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
277
                        cmbHeightField.setName("CMBHEIGHTFIELD");
278
                }
279
                return cmbHeightField;
280
        }
281

    
282
        private JComboBox getCmbTextField() {
283
                if (cmbTextField == null) {
284
                        cmbTextField = new JComboBox();
285
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
286
                        cmbTextField.setName("CMBTEXTFIELD");
287
                }
288
                return cmbTextField;
289
        }
290

    
291

    
292
        private JTextField getTxtHeightField() {
293
                if (txtHeightField == null) {
294
                        txtHeightField = new JTextField(10);
295
                        txtHeightField.setText("10");
296
                        txtHeightField.setName("TXTHEIGHTFIELD");
297
                }
298

    
299
                return txtHeightField;
300
        }
301

    
302
        public void actionPerformed(ActionEvent e) {
303
                // TODO Auto-generated method stub
304
                throw new Error("Not yet implemented!");
305

    
306
        }
307
        private ColorChooserPanel getColorChooser() {
308
                if (colorChooser == null){
309
                        colorChooser = new ColorChooserPanel(true);
310
                }
311
                return colorChooser;
312
        }
313

    
314
        public ILabelingStrategy getLabelingStrategy() {
315
                // user selected to define each label attributes from values
316
                // contained in the table for each feature row.
317

    
318
                double fixedSize;
319
                try {
320
                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
321
                } catch (Exception e) {
322
                        fixedSize = 10;
323
                }
324
                AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
325
                strategy.setLayer(layer);
326

    
327
                if(getCmbHeightField().getItemCount() > 0 && !rdBtnFixedHeight.isSelected()) {
328
                        strategy.setHeightField(
329
                                (String) getCmbHeightField().getSelectedItem());
330
                }
331
                if(getCmbRotationField().getItemCount() > 0) {
332
                        if(!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM)) {
333
                                strategy.setRotationField(
334
                                                (String) getCmbRotationField().getSelectedItem());
335
                        } else {
336
                                strategy.setRotationField(null);
337
                        }
338
                }
339

    
340
                if(getCmbTextField().getItemCount() > 0) {
341
                        strategy.setTextField(
342
                                (String) getCmbTextField().getSelectedItem());
343
                }
344

    
345
                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
346
                strategy.setFixedSize(fixedSize);
347

    
348
                if(getCmbUnits().getItemCount() > 0) {
349
                        strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
350
                }
351
                if(getCmbReferenceSystem().getItemCount() > 0) {
352
                        strategy.setReferenceSystem(getCmbReferenceSystem().getSelectedIndex());
353
                }
354

    
355
                strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
356
                strategy.setFixedColor(getColorChooser().getColor());
357

    
358
                if(getCmbColorField().getItemCount() > 0 && !rdBtnFixedColor.isSelected()) {
359
                        strategy.setColorField((String) getCmbColorField().getSelectedItem());
360
                }
361

    
362

    
363
                strategy.setFont(labelFont);
364
                return strategy;
365
        }
366

    
367
        public void setModel(FLayer layer, ILabelingStrategy str) {
368
                this.layer = (FLyrVect) layer;
369
                // to allow the labeling of non-FLyrVect layers
370
                if (layer instanceof FLyrVect) {
371
                        FLyrVect lv = (FLyrVect) layer;
372
                        try {
373
                                FeatureType featureType = lv.getFeatureStore()
374
                                                .getDefaultFeatureType();
375
                                fieldNames = new String[featureType.size()];
376
                                Iterator<FeatureAttributeDescriptor> iterator = featureType
377
                                                .iterator();
378
                                ArrayList<String> l = new ArrayList<String>();
379
                                ArrayList<String> lColors = new ArrayList<String>();
380
                                String name;
381
                                FeatureAttributeDescriptor descriptor;
382
                                while (iterator.hasNext()) {
383
                                        descriptor = iterator.next();
384

    
385
                                        name = descriptor.getName();
386
                                        fieldNames[descriptor.getIndex()] = name;
387
                                        switch (descriptor.getType()) {
388
//                                        case DataTypes.DECIMAL:
389
//                                        case DataTypes.NUMERIC:
390
                                        case DataTypes.FLOAT:
391
//                                        case DataTypes.REAL:
392
                                        case DataTypes.DOUBLE:
393
                                                l.add(name);
394
                                                break;
395
                                        case DataTypes.INT:
396
//                                        case DataTypes.SMALLINT:
397
//                                        case DataTypes.TINYINT:
398
                                        case DataTypes.LONG:
399
                                                lColors.add(name);
400
                                                l.add(name);
401
                                                break;
402
                                        }
403
                                }
404
                                numericFieldNames = l.toArray(new String[l.size()]);
405
                                integerFieldNames = lColors.toArray(new String[lColors.size()]);
406
                        } catch (DataException e) {
407
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
408
                        }
409
                        refreshControls();
410
                }
411
        }
412

    
413
        private void refreshColorFont(){
414

    
415
                getCmbColorField().removeAllItems();
416

    
417
                boolean enabled = integerFieldNames.length>0;
418
                getCmbColorField().setEnabled(enabled);
419
                getRdBtnColorField().setEnabled(enabled);
420

    
421
                if (!enabled) {
422
                        getRdBtnFixedColor().setSelected(true);
423
                }
424

    
425
                for (int i = 0; i < integerFieldNames.length; i++) {
426
                        getCmbColorField().addItem(integerFieldNames[i]);
427
                }
428

    
429
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
430
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
431
                        getRdBtnFixedColor().setSelected(aux.usesFixedColor());
432
                        getRdBtnColorField().setSelected(!aux.usesFixedColor());
433

    
434
                        String item = aux.getColorField();
435
                        getCmbColorField().setSelectedItem(item);
436
                        getColorChooser().setColor(aux.getFixedColor());
437
                }
438
        }
439

    
440
        private void refreshFont(){
441

    
442
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
443
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
444
                        labelFont = aux.getFont();
445
                }
446
        }
447

    
448
        private void refreshCmbUnits() {
449

    
450
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
451
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
452
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
453
                }
454
        }
455

    
456
        private void refreshCmbRefSystem() {
457

    
458
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
459
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
460
                        getCmbReferenceSystem().setSelectedIndex(aux.getReferenceSystem());
461
                }
462
        }
463

    
464
        private void refreshTextHeight() {
465
                getCmbHeightField().removeAllItems();
466

    
467
                /*
468
                boolean enabled = numericFieldNames.length>0;
469
            // getCmbHeightField().setEnabled(enabled);
470
            // getRdBtnHeightField().setEnabled(enabled);
471

472
                if (!enabled) {
473
                        getRdBtnFixedHeight().setSelected(true);
474
                }
475
                */
476

    
477
                for (int i = 0; i < numericFieldNames.length; i++) {
478
                        getCmbHeightField().addItem(numericFieldNames[i]);
479
                }
480

    
481
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
482
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
483
                        getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
484
                        getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
485
                        getRdBtnHeightField().setSelected(!aux.usesFixedSize());
486

    
487
                        String item = aux.getHeightField();
488
                        getCmbHeightField().setSelectedItem(item);
489

    
490
                }
491

    
492
        }
493

    
494
        private void refreshCmbRotationField() {
495
                getCmbRotationField().removeAllItems();
496
                getCmbRotationField().addItem(NO_FIELD_ITEM);
497
                for (int i = 0; i < numericFieldNames.length; i++) {
498
                        getCmbRotationField().addItem(numericFieldNames[i]);
499
                }
500

    
501
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
502
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
503
                        String item = aux.getRotationField();
504
                        getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
505
                }
506
        }
507

    
508
        private void refreshCmbTextField() {
509
                getCmbTextField().removeAllItems();
510
                for (int i = 0; i < fieldNames.length; i++) {
511
                        getCmbTextField().addItem(fieldNames[i]);
512
                }
513

    
514
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
515
                        IAttrInTableLabelingStrategy aux = (IAttrInTableLabelingStrategy) layer.getLabelingStrategy();
516
                        String item = aux.getTextField();
517
                        getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
518
                }
519
        }
520

    
521
        public String getLabelingStrategyName() {
522
                return PluginServices.getText(this, "label_attributes_defined_in_table");
523
        }
524

    
525
        public Class getLabelingStrategyClass() {
526
                return AttrInTableLabelingStrategy.class;
527
        }
528

    
529
        public void setEnabled(boolean enabled) {
530
                super.setEnabled(enabled);
531
                getChooseFontBut().setEnabled(enabled);
532
                getCmbColorField().setEnabled(enabled);
533
                getCmbHeightField().setEnabled(enabled);
534
                getCmbReferenceSystem().setEnabled(enabled);
535
                getCmbRotationField().setEnabled(enabled);
536
                getCmbTextField().setEnabled(enabled);
537
                getCmbUnits().setEnabled(enabled);
538
                getColorChooser().setEnabled(enabled);
539
                getRdBtnColorField().setEnabled(enabled);
540
                getRdBtnFixedColor().setEnabled(enabled);
541
                getRdBtnFixedHeight().setEnabled(enabled);
542
                getRdBtnHeightField().setEnabled(enabled);
543
                getTxtHeightField().setEnabled(enabled);
544

    
545
        }
546
}