Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / AttrInTableLabeling.java @ 28460

History | View | Annotate | Download (17.1 KB)

1 23395 vcaballero
/* 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.Component;
44
import java.awt.Dimension;
45
import java.awt.FlowLayout;
46
import java.awt.Font;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.sql.Types;
50
import java.util.ArrayList;
51
52
import javax.swing.BorderFactory;
53
import javax.swing.ButtonGroup;
54
import javax.swing.JButton;
55
import javax.swing.JPanel;
56
import javax.swing.JRadioButton;
57
import javax.swing.JTextField;
58
59
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
60
import org.gvsig.gui.beans.swing.JBlank;
61
import org.gvsig.gui.beans.swing.JComboBoxFonts;
62
63
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.messages.NotificationManager;
66
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
69
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.AttrInTableLabelingStrategy;
70
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
71
import com.iver.cit.gvsig.gui.JComboBoxUnits;
72
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
73 24242 vcaballero
import com.iver.cit.gvsig.gui.styling.JComboBoxUnitsReferenceSystem;
74 23395 vcaballero
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
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 24242 vcaballero
        private JComboBoxUnitsReferenceSystem cmbReferenceSystem;
96 23395 vcaballero
        private JTextField txtHeightField;
97
        private FLyrVect layer;
98
99
        private ColorChooserPanel colorChooser;
100
        private JComboBox cmbColorField;
101
102
        private JButton chooseFontBut;
103
        private Font labelFont = SymbologyFactory.DefaultTextFont;
104
105
        public AttrInTableLabeling() {
106
                initialize();
107
        }
108
109
        private void initialize() {
110
                setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
111
                GridBagLayoutPanel panel = new GridBagLayoutPanel();
112
113
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
114
                aux.addComponent(PluginServices.getText(this, "field_to_be_labeled") + ":", getCmbTextField());
115
                aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
116
                aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
117
                aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
118
                aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
119 24242 vcaballero
                aux.addComponent(PluginServices.getText(this,""),getCmbReferenceSystem());
120 23395 vcaballero
                panel.add(aux);
121
122
                aux = new GridBagLayoutPanel();
123
                aux.addComponent(getChooseFontBut(),new JBlank(20,20));
124
                GridBagLayoutPanel aux2 = new GridBagLayoutPanel();
125
                aux2.setBorder(BorderFactory.createTitledBorder(null,PluginServices.getText(this,"color")));
126 25687 vcaballero
                aux2.addComponent(getRdBtnFixedColor(),getColorChooser());
127 23395 vcaballero
                aux2.addComponent(getRdBtnColorField(),getCmbColorField());
128
                aux.addComponent(aux2);
129
130
                panel.add(new JBlank(20,20));
131
                panel.add(aux);
132
133
134
                add(panel);
135
136
137
                ButtonGroup group = new ButtonGroup();
138
                group.add(getRdBtnFixedHeight());
139
                group.add(getRdBtnHeightField());
140
141
                ButtonGroup colorGroup = new ButtonGroup();
142
                colorGroup.add(getRdBtnFixedColor());
143
                colorGroup.add(getRdBtnColorField());
144
145 25687 vcaballero
//                getRdBtnHeightField().setEnabled(true);
146 23395 vcaballero
        }
147
148
149 25687 vcaballero
        private ColorChooserPanel getColorChooser() {
150
                if (colorChooser == null){
151
                        colorChooser = new ColorChooserPanel(true);
152
                }
153
                return colorChooser;
154
        }
155
156 23395 vcaballero
        private JButton getChooseFontBut() {
157
                if(chooseFontBut == null){
158
                        chooseFontBut = new JButton(PluginServices.getText(this,"font"));
159
                        chooseFontBut.setPreferredSize(new Dimension(80,10));
160
                        chooseFontBut.addActionListener(new ActionListener(){
161
162
                                public void actionPerformed(ActionEvent e) {
163
                                        Font newFont;
164
165
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
166
                                        if (newFont == null) {
167
                                                return;
168
                                        }
169
170
                                        labelFont = newFont;
171
                                }
172
173
                        });
174
                }
175
                return chooseFontBut;
176
        }
177
178
        private JRadioButton getRdBtnFixedHeight() {
179
                if (rdBtnFixedHeight == null) {
180
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
181
                        rdBtnFixedHeight.setSelected(false);
182
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
183
                }
184
185
                return rdBtnFixedHeight;
186
        }
187
188
        private JRadioButton getRdBtnHeightField() {
189
                if (rdBtnHeightField == null) {
190
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
191
                        rdBtnHeightField.setSelected(true);
192
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
193
                }
194
195
                return rdBtnHeightField;
196
        }
197
198
        private JRadioButton getRdBtnFixedColor() {
199
                if (rdBtnFixedColor == null) {
200
                        rdBtnFixedColor = new JRadioButton(PluginServices.getText(this, "fixed_color") + ":");
201
                        rdBtnFixedColor.setSelected(false);
202
                        rdBtnFixedColor.setName("RDFIXEDCOLOR");
203
                }
204
205
                return rdBtnFixedColor;
206
        }
207
208
        private JRadioButton getRdBtnColorField() {
209
                if (rdBtnColorField == null) {
210
                        rdBtnColorField = new JRadioButton(PluginServices.getText(this, "color_field") + ":");
211
                        rdBtnColorField.setSelected(true);
212
                        rdBtnColorField.setName("RDCOLORFIELD");
213
                }
214
215
                return rdBtnColorField;
216
        }
217
218
        private JComboBoxUnits getCmbUnits() {
219
                if (cmbUnits == null) {
220
                        cmbUnits = new JComboBoxUnits();
221
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
222
                        cmbUnits.setName("CMBUNITS");
223
                }
224
225
                return cmbUnits;
226
        }
227
228 24242 vcaballero
        private JComboBoxUnitsReferenceSystem getCmbReferenceSystem(){
229
                if(cmbReferenceSystem == null){
230
                        cmbReferenceSystem = new JComboBoxUnitsReferenceSystem();
231
                        cmbReferenceSystem.setName("CMBREFSYST");
232
                }
233
                return cmbReferenceSystem;
234
        }
235
236 23395 vcaballero
        private JComboBox getCmbColorField() {
237
                if (cmbColorField == null) {
238
                        cmbColorField = new JComboBox();
239
                        cmbColorField.setName("CMBCOLOR");
240
                }
241
242
                return cmbColorField;
243
        }
244
245
        private void refreshControls() {
246
                // When the attributes are in the table -----
247
                //      field with the text
248
                refreshCmbTextField();
249
250
                //      field with the rotation
251
                refreshCmbRotationField();
252
253
                //      field with the text height or the text size
254
                refreshTextHeight();
255
256
                //                the text size unit name
257
                refreshCmbUnits();
258
259 24242 vcaballero
                refreshCmbRefSystem();
260 23395 vcaballero
                //                the font for the text
261
                refreshFont();
262
                //                the color for the font
263
                refreshColorFont();
264
        }
265
266
        private JComboBox getCmbRotationField() {
267
                if (cmbRotationField == null) {
268
                        cmbRotationField = new JComboBox();
269
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
270
                        cmbRotationField.setName("CMBROTATIONFIELD");
271
                }
272
                return cmbRotationField;
273
        }
274
275
        private JComboBox getCmbHeightField() {
276
                if (cmbHeightField == null) {
277
                        cmbHeightField = new JComboBox();
278
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
279
                        cmbHeightField.setName("CMBHEIGHTFIELD");
280
                }
281
                return cmbHeightField;
282
        }
283
284
        private JComboBox getCmbTextField() {
285
                if (cmbTextField == null) {
286
                        cmbTextField = new JComboBox();
287
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
288
                        cmbTextField.setName("CMBTEXTFIELD");
289
                }
290
                return cmbTextField;
291
        }
292
293
294
        private JTextField getTxtHeightField() {
295
                if (txtHeightField == null) {
296
                        txtHeightField = new JTextField(10);
297
                        txtHeightField.setText("10");
298
                        txtHeightField.setName("TXTHEIGHTFIELD");
299
                }
300
301
                return txtHeightField;
302
        }
303
304
        public void actionPerformed(ActionEvent e) {
305
306
        }
307
308
        public ILabelingStrategy getLabelingStrategy() {
309
                // user selected to define each label attributes from values
310
                // contained in the table for each feature row.
311
312
                double fixedSize;
313
                try {
314
                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
315
                } catch (Exception e) {
316
                        fixedSize = 10;
317
                }
318
                AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
319
                strategy.setLayer(layer);
320
321 25730 vcaballero
                if(getCmbHeightField().getItemCount() > 0 && !rdBtnFixedHeight.isSelected())
322 23395 vcaballero
                        strategy.setHeightField(
323
                                (String) getCmbHeightField().getSelectedItem());
324
                if(getCmbRotationField().getItemCount() > 0)
325
                        if(!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM))
326
                                strategy.setRotationField(
327
                                                (String) getCmbRotationField().getSelectedItem());
328
                        else
329
                                strategy.setRotationField(null);
330
331
                if(getCmbTextField().getItemCount() > 0)
332
                        strategy.setTextField(
333
                                (String) getCmbTextField().getSelectedItem());
334
335
                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
336
                strategy.setFixedSize(fixedSize);
337
338
                if(getCmbUnits().getItemCount() > 0)
339
                        strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
340 24242 vcaballero
                if(getCmbReferenceSystem().getItemCount() > 0)
341
                        strategy.setReferenceSystem(getCmbReferenceSystem().getSelectedIndex());
342 23395 vcaballero
343
                strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
344 25687 vcaballero
                strategy.setFixedColor(getColorChooser().getColor());
345 23395 vcaballero
346 25730 vcaballero
                if(getCmbColorField().getItemCount() > 0 && !rdBtnFixedColor.isSelected())
347 23395 vcaballero
                        strategy.setColorField((String) getCmbColorField().getSelectedItem());
348
349
350
                strategy.setFont(labelFont);
351
                return strategy;
352
        }
353
354
        public void setModel(FLayer layer, ILabelingStrategy str) {
355
                this.layer = (FLyrVect) layer;
356
                // to allow the labeling of non-FLyrVect layers
357
                if (layer instanceof FLyrVect) {
358
                        FLyrVect lv = (FLyrVect) layer;
359
                        try {
360
                                fieldNames = lv.getRecordset().getFieldNames();
361
362
                                // detect the numeric fields
363
                                ArrayList<String> l = new ArrayList<String>();
364
                                ArrayList<String> lColors = new ArrayList<String>();
365
366
                                for (int i = 0; i < fieldNames.length; i++) {
367
                                        switch (lv.getRecordset().getFieldType(i)) {
368
                                        case Types.DECIMAL:
369
                                        case Types.NUMERIC:
370
                                        case Types.FLOAT:
371
                                        case Types.REAL:
372
                                        case Types.DOUBLE:
373
                                                l.add(fieldNames[i]);
374
                                                break;
375
                                        case Types.INTEGER:
376
                                        case Types.SMALLINT:
377
                                        case Types.TINYINT:
378
                                        case Types.BIGINT:
379
                                                lColors.add(fieldNames[i]);
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 (ReadDriverException e) {
387
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
388
                        }
389
390
391
                        refreshControls();
392
                }
393
        }
394
395
396
        private void refreshColorFont(){
397
398
                getCmbColorField().removeAllItems();
399
400
                boolean enabled = integerFieldNames.length>0;
401 25687 vcaballero
//                getCmbColorField().setEnabled(enabled);
402
//                getRdBtnColorField().setEnabled(enabled);
403 23395 vcaballero
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
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
414
                        try {
415
416
                                getRdBtnFixedColor().setSelected(aux.usesFixedColor());
417
                                getRdBtnColorField().setSelected(!aux.usesFixedColor());
418
419
                                String item = aux.getColorField();
420
                                getCmbColorField().setSelectedItem(item);
421 25687 vcaballero
                                getColorChooser().setColor(aux.getFixedColor());
422 23395 vcaballero
423
                        } catch (ReadDriverException e) {
424
                                // should never happen
425
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_color_field"), e);
426
                        }
427
                }
428
        }
429
430
        private void refreshFont(){
431
432
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
433
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
434
                        labelFont = aux.getFont();
435
                }
436
        }
437
438
        private void refreshCmbUnits() {
439
440
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
441
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
442
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
443
                }
444
        }
445
446 24242 vcaballero
        private void refreshCmbRefSystem() {
447 23395 vcaballero
448 24242 vcaballero
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
449
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
450
                        getCmbReferenceSystem().setSelectedIndex(aux.getReferenceSystem());
451
                }
452
        }
453
454 23395 vcaballero
        private void refreshTextHeight() {
455
                getCmbHeightField().removeAllItems();
456
457
                boolean enabled = numericFieldNames.length>0;
458 25687 vcaballero
//                getCmbHeightField().setEnabled(enabled);
459
//                getRdBtnHeightField().setEnabled(enabled);
460 23395 vcaballero
461
                if (!enabled) {
462
                        getRdBtnFixedHeight().setSelected(true);
463
                }
464
465
                for (int i = 0; i < numericFieldNames.length; i++) {
466
                        getCmbHeightField().addItem(numericFieldNames[i]);
467
                }
468
469
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
470
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
471
                        try {
472
                                getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
473
                                getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
474
                                getRdBtnHeightField().setSelected(!aux.usesFixedSize());
475
476
                                String item = aux.getHeightField();
477
                                getCmbHeightField().setSelectedItem(item);
478
479
                        } catch (ReadDriverException e) {
480
                                // should never happen
481
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_height_field"), e);
482
                        }
483
                }
484
485
        }
486
487
        private void refreshCmbRotationField() {
488
                getCmbRotationField().removeAllItems();
489
                getCmbRotationField().addItem(NO_FIELD_ITEM);
490
                for (int i = 0; i < numericFieldNames.length; i++) {
491
                        getCmbRotationField().addItem(numericFieldNames[i]);
492
                }
493
494
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
495
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
496
                        try {
497
                                String item = aux.getRotationField();
498
                                getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
499
                        } catch (ReadDriverException e) {
500
                                // should never happen
501
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_rotation_field"), e);
502
                        }
503
                }
504
        }
505
506
        private void refreshCmbTextField() {
507
                getCmbTextField().removeAllItems();
508
                for (int i = 0; i < fieldNames.length; i++) {
509
                        getCmbTextField().addItem(fieldNames[i]);
510
                }
511
512
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
513
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
514
                        try {
515
                                String item = aux.getTextField();
516
                                getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
517
                        } catch (ReadDriverException e) {
518
                                // should never happen
519
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_field"), e);
520
                        }
521
                }
522
        }
523
524
        public String getLabelingStrategyName() {
525
                return PluginServices.getText(this, "label_attributes_defined_in_table");
526
        }
527
528
        public Class getLabelingStrategyClass() {
529 24522 vcaballero
                return AttrInTableLabelingStrategy.class;
530 23395 vcaballero
        }
531
532 25687 vcaballero
        public void setEnabled(boolean enabled) {
533
                super.setEnabled(enabled);
534
                getChooseFontBut().setEnabled(enabled);
535
                getCmbColorField().setEnabled(enabled);
536
                getCmbHeightField().setEnabled(enabled);
537
                getCmbReferenceSystem().setEnabled(enabled);
538
                getCmbRotationField().setEnabled(enabled);
539
                getCmbTextField().setEnabled(enabled);
540
                getCmbUnits().setEnabled(enabled);
541
                getColorChooser().setEnabled(enabled);
542
                getRdBtnColorField().setEnabled(enabled);
543
                getRdBtnFixedColor().setEnabled(enabled);
544
                getRdBtnFixedHeight().setEnabled(enabled);
545
                getRdBtnHeightField().setEnabled(enabled);
546
                getTxtHeightField().setEnabled(enabled);
547
548
        }
549
550 23395 vcaballero
}