Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / AttrInTableLabeling.java @ 24759

History | View | Annotate | Download (16 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 com.iver.cit.gvsig.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.fmap.dal.DataTypes;
59
import org.gvsig.fmap.dal.exception.ReadException;
60
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
61
import org.gvsig.fmap.dal.feature.FeatureType;
62
import org.gvsig.fmap.mapcontext.layers.FLayer;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
64
import org.gvsig.fmap.mapcontext.rendering.legend.styling.AttrInTableLabelingStrategy;
65
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingStrategy;
66
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
67
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
68
import org.gvsig.gui.beans.swing.JBlank;
69
import org.gvsig.gui.beans.swing.JComboBoxFonts;
70

    
71
import com.iver.andami.PluginServices;
72
import com.iver.andami.messages.NotificationManager;
73
import com.iver.cit.gvsig.gui.JComboBoxUnits;
74
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
75
import com.iver.cit.gvsig.gui.utils.FontChooser;
76
import com.iver.cit.gvsig.project.Project;
77
import com.iver.utiles.swing.JComboBox;
78

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

    
87
        private JRadioButton rdBtnFixedHeight;
88
        private JRadioButton rdBtnHeightField;
89
        private JRadioButton rdBtnFixedColor;
90
        private JRadioButton rdBtnColorField;
91
        private JComboBox cmbTextField;
92
        private JComboBox cmbHeightField;
93
        private JComboBox cmbRotationField;
94
        private JComboBoxUnits cmbUnits;
95
        private JTextField txtHeightField;
96
        private FLyrVect layer;
97
        private JComboBoxFonts cmbFonts;
98
        private ColorChooserPanel colorChooser;
99
        private JComboBox cmbColorField;
100
        private JButton chooseFontBut;
101
        private Font labelFont = SymbologyFactory.DefaultTextFont;
102

    
103
        public AttrInTableLabeling() {
104
                initialize();
105
        }
106

    
107
        private void initialize() {
108
                setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
109
                GridBagLayoutPanel panel = new GridBagLayoutPanel();
110

    
111
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
112
                aux.addComponent(PluginServices.getText(this, "field_to_be_labeled") + ":", getCmbTextField());
113
                aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
114
                aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
115
                aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
116
                aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
117
                panel.add(aux);
118

    
119
                aux = new GridBagLayoutPanel();
120
                aux.addComponent(getChooseFontBut(),new JBlank(20,20));
121
                GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
122
                aux2.setBorder(BorderFactory.createTitledBorder(null,PluginServices.getText(this,"color")));
123
                aux2.addComponent(getRdBtnFixedColor(),colorChooser = new ColorChooserPanel(true));
124
                aux2.addComponent(getRdBtnColorField(),getCmbColorField());
125
                aux.addComponent(aux2);
126

    
127
                panel.add(new JBlank(20,20));
128
                panel.add(aux);
129

    
130

    
131
                add(panel);
132

    
133

    
134
                ButtonGroup group = new ButtonGroup();
135
                group.add(getRdBtnFixedHeight());
136
                group.add(getRdBtnHeightField());
137

    
138
                ButtonGroup colorGroup = new ButtonGroup();
139
                colorGroup.add(getRdBtnFixedColor());
140
                colorGroup.add(getRdBtnColorField());
141

    
142
                getRdBtnHeightField().setEnabled(true);
143
        }
144

    
145
        private JButton getChooseFontBut() {
146
                if(chooseFontBut == null){
147
                        chooseFontBut = new JButton(PluginServices.getText(this,"font"));
148
                        chooseFontBut.setPreferredSize(new Dimension(80,10));
149
                        chooseFontBut.addActionListener(new ActionListener(){
150

    
151
                                public void actionPerformed(ActionEvent e) {
152
                                        Font newFont;
153

    
154
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
155
                                        if (newFont == null) {
156
                                                return;
157
                                        }
158

    
159
                                        labelFont = newFont;
160
                                }
161

    
162
                        });
163
                }
164
                return chooseFontBut;
165
        }
166

    
167
        private JRadioButton getRdBtnFixedHeight() {
168
                if (rdBtnFixedHeight == null) {
169
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
170
                        rdBtnFixedHeight.setSelected(false);
171
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
172
                }
173

    
174
                return rdBtnFixedHeight;
175
        }
176

    
177
        private JRadioButton getRdBtnHeightField() {
178
                if (rdBtnHeightField == null) {
179
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
180
                        rdBtnHeightField.setSelected(true);
181
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
182
                }
183

    
184
                return rdBtnHeightField;
185
        }
186

    
187
        private JRadioButton getRdBtnFixedColor() {
188
                if (rdBtnFixedColor == null) {
189
                        rdBtnFixedColor = new JRadioButton(PluginServices.getText(this, "fixed_color") + ":");
190
                        rdBtnFixedColor.setSelected(false);
191
                        rdBtnFixedColor.setName("RDFIXEDCOLOR");
192
                }
193

    
194
                return rdBtnFixedColor;
195
        }
196

    
197
        private JRadioButton getRdBtnColorField() {
198
                if (rdBtnColorField == null) {
199
                        rdBtnColorField = new JRadioButton(PluginServices.getText(this, "color_field") + ":");
200
                        rdBtnColorField.setSelected(true);
201
                        rdBtnColorField.setName("RDCOLORFIELD");
202
                }
203

    
204
                return rdBtnColorField;
205
        }
206

    
207
        private JComboBoxUnits getCmbUnits() {
208
                if (cmbUnits == null) {
209
                        cmbUnits = new JComboBoxUnits();
210
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
211
                        cmbUnits.setName("CMBUNITS");
212
                }
213

    
214
                return cmbUnits;
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
                //                the font for the text
241
                refreshFont();
242
//
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

    
291
        public ILabelingStrategy getLabelingStrategy() {
292
                // user selected to define each label attributes from values
293
                // contained in the table for each feature row.
294

    
295
                double fixedSize;
296
                try {
297
                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
298
                } catch (Exception e) {
299
                        fixedSize = 10;
300
                }
301
                AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
302
                strategy.setLayer(layer);
303

    
304
                if(getCmbHeightField().getItemCount() > 0) {
305
                        strategy.setHeightField(
306
                                (String) getCmbHeightField().getSelectedItem());
307
                }
308
                if(getCmbRotationField().getItemCount() > 0) {
309
                        if(!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM)) {
310
                                strategy.setRotationField(
311
                                                (String) getCmbRotationField().getSelectedItem());
312
                        } else {
313
                                strategy.setRotationField(null);
314
                        }
315
                }
316

    
317
                if(getCmbTextField().getItemCount() > 0) {
318
                        strategy.setTextField(
319
                                (String) getCmbTextField().getSelectedItem());
320
                }
321

    
322
                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
323
                strategy.setFixedSize(fixedSize);
324

    
325
                if(getCmbUnits().getItemCount() > 0) {
326
                        strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
327
                }
328

    
329
                strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
330
                strategy.setFixedColor(colorChooser.getColor());
331

    
332
                if(getCmbColorField().getItemCount() > 0) {
333
                        strategy.setColorField((String) getCmbColorField().getSelectedItem());
334
                }
335

    
336

    
337
                strategy.setFont(labelFont);
338
                return strategy;
339
        }
340

    
341
        public void setModel(FLayer layer, ILabelingStrategy str) {
342
                this.layer = (FLyrVect) layer;
343
                // to allow the labeling of non-FLyrVect layers
344
                if (layer instanceof FLyrVect) {
345
                        FLyrVect lv = (FLyrVect) layer;
346
                        try {
347
                                FeatureType featureType=(FeatureType)lv.getFeatureStore().getFeatureTypes().get(0);
348
                                fieldNames = featureType.getFields();
349

    
350
                                Iterator iterator=featureType.iterator();
351
                                ArrayList<String> l = new ArrayList<String>();
352
                                ArrayList<String> lColors = new ArrayList<String>();
353
                                while (iterator.hasNext()) {
354
                                        FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next();
355
                                        if (descriptor.getDataType() == DataTypes.DOUBLE ||
356
                                                        descriptor.getDataType() == DataTypes.FLOAT ||
357
                                                        descriptor.getDataType() == DataTypes.LONG)/*||
358
                                                        descriptor.getDataType().equals(FeatureAttributeDescriptor.TYPE_INT)*/){
359
                                                l.add(descriptor.getName());
360
                                        }
361
                                        if (descriptor.getDataType() == DataTypes.INT) {
362
                                                l.add(descriptor.getName());
363
                                                lColors.add(descriptor.getName());
364
                                        }
365

    
366

    
367
                                // detect the numeric fields
368

    
369
//                                for (int i = 0; i < fieldNames.length; i++) {
370
//                                        switch (lv.getRecordset().getFieldType(i)) {
371
//                                        case Types.DECIMAL:
372
//                                        case Types.NUMERIC:
373
//                                        case Types.FLOAT:
374
//                                        case Types.DOUBLE:
375
//                                        case Types.INTEGER:
376
//                                        case Types.REAL:
377
//                                        case Types.SMALLINT:
378
//                                        case Types.TINYINT:
379
//                                        case Types.BIGINT:
380
//                                                l.add(fieldNames[i]);
381
//                                                break;
382
//                                        }
383
                                }
384
                                numericFieldNames = l.toArray(new String[l.size()]);
385
                                integerFieldNames = lColors.toArray(new String[lColors.size()]);
386
                        } catch (ReadException e) {
387
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
388
                        }
389
                        refreshControls();
390
                }
391
        }
392
        private void refreshCmbFont(){
393

    
394
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
395
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
396
                        cmbFonts.setFont(aux.getFont());
397
                }
398
        }
399

    
400
        private void refreshColorFont(){
401

    
402
                getCmbColorField().removeAllItems();
403

    
404
                boolean enabled = integerFieldNames.length>0;
405
                getCmbColorField().setEnabled(enabled);
406
                getRdBtnColorField().setEnabled(enabled);
407

    
408
                if (!enabled) {
409
                        getRdBtnFixedColor().setSelected(true);
410
                }
411

    
412
                for (int i = 0; i < integerFieldNames.length; i++) {
413
                        getCmbColorField().addItem(integerFieldNames[i]);
414
                }
415

    
416
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
417
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
418
                        try {
419

    
420
                                getRdBtnFixedColor().setSelected(aux.usesFixedColor());
421
                                getRdBtnColorField().setSelected(!aux.usesFixedColor());
422

    
423
                                String item = aux.getColorField();
424
                                getCmbColorField().setSelectedItem(item);
425
                                colorChooser.setColor(aux.getFixedColor());
426

    
427
                        } catch (ReadException e) {
428
                                // should never happen
429
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_color_field"), e);
430
                        }
431
                }
432
        }
433

    
434
        private void refreshFont(){
435

    
436
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
437
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
438
                        labelFont = aux.getFont();
439
                }
440
        }
441

    
442
        private void refreshCmbUnits() {
443
                getCmbUnits().setSelectedUnitIndex(Project.getDefaultDistanceUnits());
444

    
445
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
446
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
447
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
448
                }
449
        }
450

    
451

    
452
        private void refreshTextHeight() {
453
                getCmbHeightField().removeAllItems();
454

    
455
                boolean enabled = numericFieldNames.length>0;
456
                getCmbHeightField().setEnabled(enabled);
457
                getRdBtnHeightField().setEnabled(enabled);
458

    
459
                if (!enabled) {
460
                        getRdBtnFixedHeight().setSelected(true);
461
                }
462

    
463
                for (int i = 0; i < numericFieldNames.length; i++) {
464
                        getCmbHeightField().addItem(numericFieldNames[i]);
465
                }
466

    
467
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
468
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
469
                        try {
470
                                getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
471
                                getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
472
                                getRdBtnHeightField().setSelected(!aux.usesFixedSize());
473

    
474
                                String item = aux.getHeightField();
475
                                getCmbHeightField().setSelectedItem(item);
476

    
477
                        } catch (ReadException e) {
478
                                // should never happen
479
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_height_field"), e);
480
                        }
481
                }
482

    
483
        }
484

    
485
        private void refreshCmbRotationField() {
486
                getCmbRotationField().removeAllItems();
487
                getCmbRotationField().addItem(NO_FIELD_ITEM);
488
                for (int i = 0; i < numericFieldNames.length; i++) {
489
                        getCmbRotationField().addItem(numericFieldNames[i]);
490
                }
491

    
492
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
493
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
494
                        try {
495
                                String item = aux.getRotationField();
496
                                getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
497
                        } catch (ReadException e) {
498
                                // should never happen
499
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_rotation_field"), e);
500
                        }
501
                }
502
        }
503

    
504
        private void refreshCmbTextField() {
505
                getCmbTextField().removeAllItems();
506
                for (int i = 0; i < fieldNames.length; i++) {
507
                        getCmbTextField().addItem(fieldNames[i]);
508
                }
509

    
510
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
511
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
512
                        try {
513
                                String item = aux.getTextField();
514
                                getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
515
                        } catch (ReadException e) {
516
                                // should never happen
517
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_field"), e);
518
                        }
519
                }
520
        }
521

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

    
526
        public Class getLabelingStrategyClass() {
527
                return AttrInTableLabeling.class;
528
        }
529

    
530
}