Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / AttrInTableLabeling.java @ 23395

History | View | Annotate | Download (15.4 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
import com.iver.cit.gvsig.gui.styling.SymbolEditor;
74
import com.iver.cit.gvsig.gui.utils.FontChooser;
75
import com.iver.cit.gvsig.project.Project;
76
import com.iver.utiles.swing.JComboBox;
77
78
public class AttrInTableLabeling extends JPanel implements  ILabelingStrategyPanel{
79
        private static final long serialVersionUID = 8229927418031917075L;
80
        private static final String NO_FIELD_ITEM = "-- " +
81
        PluginServices.getText(LabelingManager.class, "none") + " --";
82
        private String[] fieldNames;
83
        private String[] numericFieldNames;
84
        private String[] integerFieldNames;
85
86
        private JRadioButton rdBtnFixedHeight;
87
        private JRadioButton rdBtnHeightField;
88
        private JRadioButton rdBtnFixedColor;
89
        private JRadioButton rdBtnColorField;
90
        private JComboBox cmbTextField;
91
        private JComboBox cmbHeightField;
92
        private JComboBox cmbRotationField;
93
        private JComboBoxUnits cmbUnits;
94
        private JTextField txtHeightField;
95
        private FLyrVect layer;
96
97
        private ColorChooserPanel colorChooser;
98
        private JComboBox cmbColorField;
99
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
146
        private JButton getChooseFontBut() {
147
                if(chooseFontBut == null){
148
                        chooseFontBut = new JButton(PluginServices.getText(this,"font"));
149
                        chooseFontBut.setPreferredSize(new Dimension(80,10));
150
                        chooseFontBut.addActionListener(new ActionListener(){
151
152
                                public void actionPerformed(ActionEvent e) {
153
                                        Font newFont;
154
155
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
156
                                        if (newFont == null) {
157
                                                return;
158
                                        }
159
160
                                        labelFont = newFont;
161
                                }
162
163
                        });
164
                }
165
                return chooseFontBut;
166
        }
167
168
        private JRadioButton getRdBtnFixedHeight() {
169
                if (rdBtnFixedHeight == null) {
170
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
171
                        rdBtnFixedHeight.setSelected(false);
172
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
173
                }
174
175
                return rdBtnFixedHeight;
176
        }
177
178
        private JRadioButton getRdBtnHeightField() {
179
                if (rdBtnHeightField == null) {
180
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
181
                        rdBtnHeightField.setSelected(true);
182
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
183
                }
184
185
                return rdBtnHeightField;
186
        }
187
188
        private JRadioButton getRdBtnFixedColor() {
189
                if (rdBtnFixedColor == null) {
190
                        rdBtnFixedColor = new JRadioButton(PluginServices.getText(this, "fixed_color") + ":");
191
                        rdBtnFixedColor.setSelected(false);
192
                        rdBtnFixedColor.setName("RDFIXEDCOLOR");
193
                }
194
195
                return rdBtnFixedColor;
196
        }
197
198
        private JRadioButton getRdBtnColorField() {
199
                if (rdBtnColorField == null) {
200
                        rdBtnColorField = new JRadioButton(PluginServices.getText(this, "color_field") + ":");
201
                        rdBtnColorField.setSelected(true);
202
                        rdBtnColorField.setName("RDCOLORFIELD");
203
                }
204
205
                return rdBtnColorField;
206
        }
207
208
        private JComboBoxUnits getCmbUnits() {
209
                if (cmbUnits == null) {
210
                        cmbUnits = new JComboBoxUnits();
211
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
212
                        cmbUnits.setName("CMBUNITS");
213
                }
214
215
                return cmbUnits;
216
        }
217
218
        private JComboBox getCmbColorField() {
219
                if (cmbColorField == null) {
220
                        cmbColorField = new JComboBox();
221
                        cmbColorField.setName("CMBCOLOR");
222
                }
223
224
                return cmbColorField;
225
        }
226
227
        private void refreshControls() {
228
                // When the attributes are in the table -----
229
                //      field with the text
230
                refreshCmbTextField();
231
232
                //      field with the rotation
233
                refreshCmbRotationField();
234
235
                //      field with the text height or the text size
236
                refreshTextHeight();
237
238
                //                the text size unit name
239
                refreshCmbUnits();
240
241
                //                the font for the text
242
                refreshFont();
243
//
244
                //                the color for the font
245
                refreshColorFont();
246
        }
247
248
        private JComboBox getCmbRotationField() {
249
                if (cmbRotationField == null) {
250
                        cmbRotationField = new JComboBox();
251
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
252
                        cmbRotationField.setName("CMBROTATIONFIELD");
253
                }
254
                return cmbRotationField;
255
        }
256
257
        private JComboBox getCmbHeightField() {
258
                if (cmbHeightField == null) {
259
                        cmbHeightField = new JComboBox();
260
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
261
                        cmbHeightField.setName("CMBHEIGHTFIELD");
262
                }
263
                return cmbHeightField;
264
        }
265
266
        private JComboBox getCmbTextField() {
267
                if (cmbTextField == null) {
268
                        cmbTextField = new JComboBox();
269
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
270
                        cmbTextField.setName("CMBTEXTFIELD");
271
                }
272
                return cmbTextField;
273
        }
274
275
276
        private JTextField getTxtHeightField() {
277
                if (txtHeightField == null) {
278
                        txtHeightField = new JTextField(10);
279
                        txtHeightField.setText("10");
280
                        txtHeightField.setName("TXTHEIGHTFIELD");
281
                }
282
283
                return txtHeightField;
284
        }
285
286
        public void actionPerformed(ActionEvent e) {
287
288
        }
289
290
        public ILabelingStrategy getLabelingStrategy() {
291
                // user selected to define each label attributes from values
292
                // contained in the table for each feature row.
293
294
                double fixedSize;
295
                try {
296
                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
297
                } catch (Exception e) {
298
                        fixedSize = 10;
299
                }
300
                AttrInTableLabelingStrategy strategy = new AttrInTableLabelingStrategy();
301
                strategy.setLayer(layer);
302
303
                if(getCmbHeightField().getItemCount() > 0)
304
                        strategy.setHeightField(
305
                                (String) getCmbHeightField().getSelectedItem());
306
                if(getCmbRotationField().getItemCount() > 0)
307
                        if(!getCmbRotationField().getSelectedItem().equals(NO_FIELD_ITEM))
308
                                strategy.setRotationField(
309
                                                (String) getCmbRotationField().getSelectedItem());
310
                        else
311
                                strategy.setRotationField(null);
312
313
                if(getCmbTextField().getItemCount() > 0)
314
                        strategy.setTextField(
315
                                (String) getCmbTextField().getSelectedItem());
316
317
                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
318
                strategy.setFixedSize(fixedSize);
319
320
                if(getCmbUnits().getItemCount() > 0)
321
                        strategy.setUnit(getCmbUnits().getSelectedUnitIndex());
322
323
                strategy.setUsesFixedColor(getRdBtnFixedColor().isSelected());
324
                strategy.setFixedColor(colorChooser.getColor());
325
326
                if(getCmbColorField().getItemCount() > 0)
327
                        strategy.setColorField((String) getCmbColorField().getSelectedItem());
328
329
330
                strategy.setFont(labelFont);
331
                return strategy;
332
        }
333
334
        public void setModel(FLayer layer, ILabelingStrategy str) {
335
                this.layer = (FLyrVect) layer;
336
                // to allow the labeling of non-FLyrVect layers
337
                if (layer instanceof FLyrVect) {
338
                        FLyrVect lv = (FLyrVect) layer;
339
                        try {
340
                                fieldNames = lv.getRecordset().getFieldNames();
341
342
                                // detect the numeric fields
343
                                ArrayList<String> l = new ArrayList<String>();
344
                                ArrayList<String> lColors = new ArrayList<String>();
345
346
                                for (int i = 0; i < fieldNames.length; i++) {
347
                                        switch (lv.getRecordset().getFieldType(i)) {
348
                                        case Types.DECIMAL:
349
                                        case Types.NUMERIC:
350
                                        case Types.FLOAT:
351
                                        case Types.REAL:
352
                                        case Types.DOUBLE:
353
                                                l.add(fieldNames[i]);
354
                                                break;
355
                                        case Types.INTEGER:
356
                                        case Types.SMALLINT:
357
                                        case Types.TINYINT:
358
                                        case Types.BIGINT:
359
                                                lColors.add(fieldNames[i]);
360
                                                l.add(fieldNames[i]);
361
                                                break;
362
                                        }
363
                                }
364
                                numericFieldNames = l.toArray(new String[l.size()]);
365
                                integerFieldNames = lColors.toArray(new String[lColors.size()]);
366
                        } catch (ReadDriverException e) {
367
                                NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
368
                        }
369
370
371
                        refreshControls();
372
                }
373
        }
374
375
376
        private void refreshColorFont(){
377
378
                getCmbColorField().removeAllItems();
379
380
                boolean enabled = integerFieldNames.length>0;
381
                getCmbColorField().setEnabled(enabled);
382
                getRdBtnColorField().setEnabled(enabled);
383
384
                if (!enabled) {
385
                        getRdBtnFixedColor().setSelected(true);
386
                }
387
388
                for (int i = 0; i < integerFieldNames.length; i++) {
389
                        getCmbColorField().addItem(integerFieldNames[i]);
390
                }
391
392
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
393
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
394
                        try {
395
396
                                getRdBtnFixedColor().setSelected(aux.usesFixedColor());
397
                                getRdBtnColorField().setSelected(!aux.usesFixedColor());
398
399
                                String item = aux.getColorField();
400
                                getCmbColorField().setSelectedItem(item);
401
                                colorChooser.setColor(aux.getFixedColor());
402
403
                        } catch (ReadDriverException e) {
404
                                // should never happen
405
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_color_field"), e);
406
                        }
407
                }
408
        }
409
410
        private void refreshFont(){
411
412
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
413
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
414
                        labelFont = aux.getFont();
415
                }
416
        }
417
418
        private void refreshCmbUnits() {
419
                getCmbUnits().setSelectedUnitIndex(Project.getDefaultDistanceUnits());
420
421
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
422
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
423
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
424
                }
425
        }
426
427
428
        private void refreshTextHeight() {
429
                getCmbHeightField().removeAllItems();
430
431
                boolean enabled = numericFieldNames.length>0;
432
                getCmbHeightField().setEnabled(enabled);
433
                getRdBtnHeightField().setEnabled(enabled);
434
435
                if (!enabled) {
436
                        getRdBtnFixedHeight().setSelected(true);
437
                }
438
439
                for (int i = 0; i < numericFieldNames.length; i++) {
440
                        getCmbHeightField().addItem(numericFieldNames[i]);
441
                }
442
443
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
444
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
445
                        try {
446
                                getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
447
                                getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
448
                                getRdBtnHeightField().setSelected(!aux.usesFixedSize());
449
450
                                String item = aux.getHeightField();
451
                                getCmbHeightField().setSelectedItem(item);
452
453
                        } catch (ReadDriverException e) {
454
                                // should never happen
455
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_height_field"), e);
456
                        }
457
                }
458
459
        }
460
461
        private void refreshCmbRotationField() {
462
                getCmbRotationField().removeAllItems();
463
                getCmbRotationField().addItem(NO_FIELD_ITEM);
464
                for (int i = 0; i < numericFieldNames.length; i++) {
465
                        getCmbRotationField().addItem(numericFieldNames[i]);
466
                }
467
468
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
469
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
470
                        try {
471
                                String item = aux.getRotationField();
472
                                getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
473
                        } catch (ReadDriverException e) {
474
                                // should never happen
475
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_rotation_field"), e);
476
                        }
477
                }
478
        }
479
480
        private void refreshCmbTextField() {
481
                getCmbTextField().removeAllItems();
482
                for (int i = 0; i < fieldNames.length; i++) {
483
                        getCmbTextField().addItem(fieldNames[i]);
484
                }
485
486
                if (layer.getLabelingStrategy() instanceof AttrInTableLabelingStrategy) {
487
                        AttrInTableLabelingStrategy aux = (AttrInTableLabelingStrategy) layer.getLabelingStrategy();
488
                        try {
489
                                String item = aux.getTextField();
490
                                getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
491
                        } catch (ReadDriverException e) {
492
                                // should never happen
493
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_field"), e);
494
                        }
495
                }
496
        }
497
498
        public String getLabelingStrategyName() {
499
                return PluginServices.getText(this, "label_attributes_defined_in_table");
500
        }
501
502
        public Class getLabelingStrategyClass() {
503
                return AttrInTableLabeling.class;
504
        }
505
506
}