Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / thememanager / legendmanager / panels / FPanelLegendLabels.java @ 6996

History | View | Annotate | Download (16.3 KB)

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

    
47
import java.awt.Font;
48
import java.awt.event.ActionEvent;
49
import java.sql.Types;
50
import java.util.ArrayList;
51

    
52
import javax.swing.ButtonGroup;
53
import javax.swing.DefaultComboBoxModel;
54
import javax.swing.JPanel;
55

    
56
import com.iver.andami.PluginServices;
57
import com.iver.cit.gvsig.ProjectExtension;
58
import com.iver.cit.gvsig.fmap.DriverException;
59
import com.iver.cit.gvsig.fmap.core.FShape;
60
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
61
import com.iver.cit.gvsig.fmap.layers.FLayer;
62
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
63
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
65
import com.iver.cit.gvsig.fmap.rendering.Legend;
66
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
67
import com.iver.cit.gvsig.gui.dialogs.FontChooser;
68
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
69

    
70
import javax.swing.JComboBox;
71
import javax.swing.JLabel;
72
import javax.swing.JCheckBox;
73

    
74
/**
75
 * @author fjp
76
 *
77
 */
78
public class FPanelLegendLabels extends JPanel implements ILegendPanel {
79
        private static final double FONT_SCALE_FACTOR = 1.4;
80

    
81

    
82
        private static final Font DEFAULT_FONT =
83
                  new Font("SansSerif", Font.PLAIN, 9);
84

    
85

    
86
        // private TOC m_TOC;
87
        private VectorialLegend m_Renderer;
88
        private ClassifiableVectorial m_lyr;
89
        private FSymbol m_FSymbol;
90

    
91
        private ColorChooserPanel m_colorChooser = new ColorChooserPanel();
92

    
93

    
94
        //private javax.swing.JLabel jLabel = null;
95
        private javax.swing.JCheckBox jCheckBox = null;
96
        private javax.swing.JLabel jLabel1 = null;
97
        private javax.swing.JComboBox jComboBoxTextField = null;
98
        private javax.swing.JLabel jLabel2 = null;
99
        private javax.swing.JComboBox jComboBoxHeightField = null;
100
        private javax.swing.JPanel jPanel = null;
101
        private javax.swing.JTextField jTextField = null;
102
        private javax.swing.JRadioButton jRadioButton = null;
103
        private javax.swing.JRadioButton jRadioButton1 = null;
104
        private FPreviewSymbol fPreviewSymbol = null;
105
        private javax.swing.JLabel jLabel3 = null;
106
        private javax.swing.JButton jButton = null;
107

    
108
        private Font labelFont = DEFAULT_FONT;
109
        //private JPanel parent;
110
        private javax.swing.JLabel jLabel4 = null;
111

    
112

    
113
    private JComboBox jComboBoxRotationField = null;
114

    
115

    
116
    private JLabel jLabel = null;
117

    
118

    
119
    private JCheckBox jCheckBoxTextOnly = null;
120
        /**
121
         * This is the default constructor
122
         */
123
        public FPanelLegendLabels(JPanel parent) {
124
                super();
125
                //this.parent = parent;
126
                initialize();
127
        }
128
        /**
129
         * This method initializes this
130
         *
131
         * @return void
132
         */
133
        private  void initialize() {
134
                jLabel = new JLabel();
135
                jLabel.setBounds(30, 110, 138, 18);
136
                jLabel.setText(PluginServices.getText(this, "rotation_field"));
137
                this.setLayout(null);
138
                this.setSize(454, 230);
139
                this.add(getJCheckBoxTextOnly(), null);
140
                this.add(jLabel, null);
141
                this.add(getJComboBoxRotationField(), null);
142
                this.add(getJCheckBox(), null);
143
                this.add(getJLabel1(), null);
144
                this.add(getJComboBoxTextField(), null);
145
                this.add(getJLabel2(), null);
146
                this.add(getJComboBoxHeightField(), null);
147
                this.add(getJPanel(), null);
148
                this.add(getFPreviewSymbol(), null);
149
                this.add(getJLabel3(), null);
150
                this.add(getJButton(), null);
151

    
152
                // m_colorChooser = new ColorChooserPanel();
153
                // m_colorChooser.setColor(Color.RED);
154
                m_colorChooser.setAlpha(255);
155
                m_colorChooser.setBounds(312, 115, 99, 34);
156
                m_colorChooser.addActionListener(new java.awt.event.ActionListener() {
157
                        public void actionPerformed(ActionEvent e) {
158
                                updateControls();
159
                        }
160
                });
161
                this.add(getJLabel4(), null);
162
                this.add(m_colorChooser);
163

    
164
        }
165
        public void setLayer(FLayer lyr, Legend r)
166
        {
167
                m_lyr = (ClassifiableVectorial) lyr;
168
                m_Renderer = (VectorialLegend)r;
169

    
170
                fillFieldNames();
171
                getJComboBoxTextField().getModel().setSelectedItem(m_Renderer.getLabelField());
172
        getJComboBoxHeightField().getModel().setSelectedItem(m_Renderer.getLabelHeightField());
173
        getJComboBoxRotationField().getModel().setSelectedItem(m_Renderer.getLabelRotationField());
174
                setFSymbol(r.getDefaultSymbol());
175

    
176
                if (m_Renderer.getLabelField() != null)
177
                        getJCheckBox().setSelected(true);
178
                else
179
                        getJCheckBox().setSelected(false);
180

    
181

    
182

    
183

    
184
        }
185
        public void setFSymbol(FSymbol sym)
186
        {
187
                if (sym == null)
188
                {
189
                        m_FSymbol = new FSymbol(FShape.MULTI);
190
                }
191
                else
192
                        m_FSymbol = sym;
193

    
194
                if (m_FSymbol.getFontColor()!=null){
195
                        m_colorChooser.setColor(m_FSymbol.getFontColor());
196
                }
197
                fPreviewSymbol.setSymbol(m_FSymbol);
198

    
199
                getJRadioButton1().setSelected(!m_FSymbol.isFontSizeInPixels());
200
        getJCheckBoxTextOnly().setSelected(!m_FSymbol.isShapeVisible());
201
        double size = m_FSymbol.getFontSize();
202
        if (!m_FSymbol.isFontSizeInPixels())
203
                // Scale the value of the font size to the value of the height
204
                // and avoid too much decimal numbers
205
                size = ((int) (100*size / FONT_SCALE_FACTOR))/100; // avoid too much decimals
206
                getJTextField().setText(String.valueOf(size));
207

    
208

    
209
        }
210

    
211
        private void updateControls() {
212
                getJComboBoxTextField().setEnabled(getJCheckBox().isSelected());
213
                getJComboBoxHeightField().setEnabled(getJCheckBox().isSelected());
214
        getJComboBoxRotationField().setEnabled(getJCheckBox().isSelected());
215
                getJRadioButton().setEnabled(getJCheckBox().isSelected());
216
                getJRadioButton1().setEnabled(getJCheckBox().isSelected());
217
                getJTextField().setEnabled(getJCheckBox().isSelected());
218
                if (getJCheckBox().isSelected())
219
                {
220
                        FSymbol sym = m_FSymbol;
221
                        sym.setFont(labelFont);
222
                        sym.setFontColor(m_colorChooser.getColor());
223
                        fPreviewSymbol.setSymbol(sym);
224
                }
225
        }
226
        public void updateValuesFromControls(VectorialLegend l) {
227
                if (getJCheckBox().isSelected())
228
                {
229
            m_FSymbol = l.getDefaultSymbol();
230
            m_FSymbol.setFont(labelFont);
231
                        l.setLabelField((String) getJComboBoxTextField().getSelectedItem());
232
                        l.setLabelHeightField((String) getJComboBoxHeightField().getSelectedItem());
233
            l.setLabelRotationField((String) getJComboBoxRotationField().getSelectedItem());
234
                        m_FSymbol.setFontColor(m_colorChooser.getColor());
235
                        //m_FSymbol.setSymbolType(m_Renderer.getShapeType());
236
                        if (!getJRadioButton().isSelected())
237
                        {
238
                                // meters
239
                                float sizeUnits = Float.parseFloat(getJTextField().getText());
240
                                /*
241
                                 * Java scales font size by defining the cell diagonal.
242
                                 * To obtain the right height value we need to apply a
243
                                 * factor to the diagonal to get a right value for the
244
                                 * height.
245
                                 */
246
                                sizeUnits *= FONT_SCALE_FACTOR;
247
                                m_FSymbol.setFontSize(sizeUnits); // Para que se pueda crear correctamente al recuperar del proyecto
248
                                m_FSymbol.setFontSizeInPixels(false);
249
                        }
250
                        else
251
                        {
252
                                // pixels
253
                                float sizePoints = Float.parseFloat(getJTextField().getText());
254
                                m_FSymbol.setFont(labelFont);
255
                                m_FSymbol.setFont(labelFont.deriveFont(sizePoints));
256
                                m_FSymbol.setFontSize(sizePoints);
257
                                m_FSymbol.setFontSizeInPixels(true);
258
                        }
259
            m_FSymbol.setShapeVisible(true);
260
            if (getJCheckBoxTextOnly().isSelected())
261
                m_FSymbol.setShapeVisible(false);
262

    
263
                        l.setDefaultSymbol(m_FSymbol);
264
            // System.out.println("Color punto 3 " + m_FSymbol.getColor().toString());
265
                }
266
                else
267
                {
268
                        // Para que no se dibujen las etiquetas, el campo labelField del Renderer
269
                        // ha de ser null
270
                        l.setLabelField(null);
271
                }
272
        }
273

    
274

    
275

    
276

    
277
        private void fillFieldNames() {
278

    
279
                SelectableDataSource rs;
280
                try {
281
                        // rs = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject().getDataSourceByLayer((FLayer) m_lyr);
282
                        rs = ((FLyrVect)m_lyr).getRecordset();
283
                        String[] nomFields;
284
            ArrayList nomNumericFields = new ArrayList();
285
                        nomFields = new String[rs.getFieldCount()];
286
                        for (int i = 0; i < rs.getFieldCount(); i++) {
287
                                nomFields[i] = rs.getFieldName(i).trim();
288
                if ((rs.getFieldType(i) == Types.FLOAT)
289
                   || (rs.getFieldType(i) == Types.BIGINT)
290
                   || (rs.getFieldType(i) == Types.DOUBLE)
291
                   || (rs.getFieldType(i) == Types.INTEGER))
292
                    nomNumericFields.add(nomFields[i]);
293
                        }
294

    
295
                        DefaultComboBoxModel cM_labelField = new DefaultComboBoxModel(nomFields);
296
                        getJComboBoxTextField().setModel(cM_labelField);
297
                        DefaultComboBoxModel cM_HeightField = new DefaultComboBoxModel(nomNumericFields.toArray(new String[0]));
298
                        getJComboBoxHeightField().setModel(cM_HeightField);
299
            DefaultComboBoxModel cM_RotationField = new DefaultComboBoxModel(nomNumericFields.toArray(new String[0]));
300
            getJComboBoxRotationField().setModel(cM_RotationField);
301

    
302
                } catch (DriverException e) {
303
                        e.printStackTrace();
304
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
305
                        e.printStackTrace();
306
                }
307
        }
308

    
309

    
310
        /**
311

312
         * This method initializes jCheckBox
313

314
         *
315

316
         * @return javax.swing.JCheckBox
317

318
         */
319
        private javax.swing.JCheckBox getJCheckBox() {
320
                if (jCheckBox == null) {
321
                        jCheckBox = new javax.swing.JCheckBox();
322
                        jCheckBox.setText(PluginServices.getText(this,"Habilitar_etiquetado"));
323
                        jCheckBox.setBounds(28, 22, 152, 23);
324
                        jCheckBox.addItemListener(new java.awt.event.ItemListener() {
325

    
326
                                public void itemStateChanged(java.awt.event.ItemEvent e) {
327
                                        updateControls();
328

    
329
                                }
330
                        });
331

    
332
                }
333
                return jCheckBox;
334
        }
335

    
336
        /**
337

338
         * This method initializes jLabel1
339

340
         *
341

342
         * @return javax.swing.JLabel
343

344
         */
345
        private javax.swing.JLabel getJLabel1() {
346
                if (jLabel1 == null) {
347
                        jLabel1 = new javax.swing.JLabel();
348
                        jLabel1.setText(PluginServices.getText(this,"Campo_de_etiquetado"));
349
                        jLabel1.setBounds(29, 53, 139, 21);
350
                }
351
                return jLabel1;
352
        }
353

    
354
        /**
355

356
         * This method initializes jComboBox
357

358
         *
359

360
         * @return javax.swing.JComboBox
361

362
         */
363
        private javax.swing.JComboBox getJComboBoxTextField() {
364
                if (jComboBoxTextField == null) {
365
                        jComboBoxTextField = new javax.swing.JComboBox();
366
                        jComboBoxTextField.setBounds(175, 52, 110, 19);
367
                        jComboBoxTextField.setEnabled(false);
368
                }
369
                return jComboBoxTextField;
370
        }
371

    
372
        /**
373

374
         * This method initializes jLabel2
375

376
         *
377

378
         * @return javax.swing.JLabel
379

380
         */
381
        private javax.swing.JLabel getJLabel2() {
382
                if (jLabel2 == null) {
383
                        jLabel2 = new javax.swing.JLabel();
384
                        jLabel2.setBounds(29, 81, 137, 21);
385
                        jLabel2.setText(PluginServices.getText(this,"campo_altura_texto"));
386
                        jLabel2.setVisible(true);
387
                }
388
                return jLabel2;
389
        }
390

    
391
        /**
392

393
         * This method initializes jComboBox1
394

395
         *
396

397
         * @return javax.swing.JComboBox
398

399
         */
400
        private javax.swing.JComboBox getJComboBoxHeightField() {
401
                if (jComboBoxHeightField == null) {
402
                        jComboBoxHeightField = new javax.swing.JComboBox();
403
                        jComboBoxHeightField.setBounds(175, 82, 110, 18);
404
                        jComboBoxHeightField.setEnabled(true);
405
                        jComboBoxHeightField.setVisible(true);
406
                }
407
                return jComboBoxHeightField;
408
        }
409

    
410
        /**
411

412
         * This method initializes jPanel
413

414
         *
415

416
         * @return javax.swing.JPanel
417

418
         */
419
        private javax.swing.JPanel getJPanel() {
420
                if (jPanel == null) {
421
                        jPanel = new javax.swing.JPanel();
422
                        jPanel.setLayout(null);
423
                        jPanel.setBounds(29, 136, 258, 84);
424
                        jPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, PluginServices.getText(this,"Altura_fija_de_texto"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
425
                        ButtonGroup jButtonGroup = new ButtonGroup();
426
                        jButtonGroup.add(getJRadioButton1());
427
                        jButtonGroup.add(getJRadioButton());
428
                        jPanel.add(getJRadioButton1(), null);
429
                        jPanel.add(getJRadioButton(), null);
430
                        jPanel.add(getJTextField(), null);
431
                }
432
                return jPanel;
433
        }
434

    
435
        /**
436

437
         * This method initializes jTextField
438

439
         *
440

441
         * @return javax.swing.JTextField
442

443
         */
444
        private javax.swing.JTextField getJTextField() {
445
                if (jTextField == null) {
446
                        jTextField = new javax.swing.JTextField();
447
                        jTextField.setBounds(26, 32, 76, 21);
448
                        jTextField.setEnabled(false);
449
                        jTextField.setText("10");
450
                }
451
                return jTextField;
452
        }
453

    
454
/**
455

456
         * This method initializes jRadioButton
457

458
         *
459

460
         * @return javax.swing.JRadioButton
461

462
         */
463
        private javax.swing.JRadioButton getJRadioButton() {
464
                if (jRadioButton == null) {
465
                        jRadioButton = new javax.swing.JRadioButton();
466
                        jRadioButton.setText(PluginServices.getText(this,"En_pixels"));
467
                        jRadioButton.setBounds(145, 44, 100, 23);
468
                        jRadioButton.setEnabled(false);
469
                        jRadioButton.setSelected(true);
470
                }
471
                return jRadioButton;
472
        }
473

    
474
        /**
475

476
         * This method initializes jRadioButton1
477

478
         *
479

480
         * @return javax.swing.JRadioButton
481

482
         */
483
        private javax.swing.JRadioButton getJRadioButton1() {
484
                if (jRadioButton1 == null) {
485
                        jRadioButton1 = new javax.swing.JRadioButton();
486
                        jRadioButton1.setText(PluginServices.getText(this,"En_metros"));
487
                        jRadioButton1.setBounds(145, 16, 100, 23);
488
                        jRadioButton1.setEnabled(false);
489
                }
490
                return jRadioButton1;
491
        }
492

    
493
        /**
494

495
         * This method initializes fPreviewSymbol
496

497
         *
498

499
         * @return com.iver.cit.opensig.gui.Panels.FPreviewSymbol
500

501
         */
502
        public FPreviewSymbol getFPreviewSymbol() {
503
                if (fPreviewSymbol == null) {
504
                        fPreviewSymbol = new FPreviewSymbol();
505
                        fPreviewSymbol.setBounds(311, 53, 134, 77);
506
                }
507
                return fPreviewSymbol;
508
        }
509

    
510
        /**
511

512
         * This method initializes jLabel3
513

514
         *
515

516
         * @return javax.swing.JLabel
517

518
         */
519
        private javax.swing.JLabel getJLabel3() {
520
                if (jLabel3 == null) {
521
                        jLabel3 = new javax.swing.JLabel();
522
                        jLabel3.setBounds(312, 28, 100, 18);
523
                        jLabel3.setText(PluginServices.getText(this,"Previsualizacion"));
524
                        jLabel3.setVisible(false);
525
                }
526
                return jLabel3;
527
        }
528

    
529
        /**
530

531
         * This method initializes jButton
532

533
         *
534

535
         * @return javax.swing.JButton
536

537
         */
538
        private javax.swing.JButton getJButton() {
539
                if (jButton == null) {
540
                        jButton = new javax.swing.JButton();
541
                        jButton.setBounds(314, 54, 111, 29);
542
                        jButton.setText(PluginServices.getText(this,"fuente")+"...");
543
                        jButton.addActionListener(new java.awt.event.ActionListener() {
544

    
545
                                public void actionPerformed(java.awt.event.ActionEvent e) {
546
                                        //JButton origen = (JButton) e.getSource();
547
                                        Font newFont;
548

    
549
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
550

    
551
                                if (newFont == null) {
552
                                    return;
553
                                }
554

    
555
                                labelFont = newFont;
556
                                updateControls();
557

    
558

    
559
                                }
560
                        });
561

    
562
                }
563
                return jButton;
564
        }
565

    
566
        /**
567

568
         * This method initializes jLabel4
569

570
         *
571

572
         * @return javax.swing.JLabel
573

574
         */
575
        private javax.swing.JLabel getJLabel4() {
576
                if (jLabel4 == null) {
577
                        jLabel4 = new javax.swing.JLabel();
578
                        jLabel4.setBounds(311, 88, 133, 23);
579
                        jLabel4.setText(PluginServices.getText(this,"color_texto"));
580
                }
581
                return jLabel4;
582
        }
583
        /* (non-Javadoc)
584
         * @see com.iver.cit.gvsig.gui.legendmanager.panels.ILegendPanel#getLegend()
585
         */
586
        public Legend getLegend() {
587
                updateValuesFromControls(m_Renderer);
588
                return m_Renderer;
589
        }
590
    /**
591
     * This method initializes jComboBox1
592
     *
593
     * @return javax.swing.JComboBox
594
     */
595
    private JComboBox getJComboBoxRotationField() {
596
            if (jComboBoxRotationField == null) {
597
                    jComboBoxRotationField = new JComboBox();
598
                    jComboBoxRotationField.setBounds(175, 111, 110, 19);
599
            }
600
            return jComboBoxRotationField;
601
    }
602
    /**
603
     * This method initializes jCheckBox1
604
     *
605
     * @return javax.swing.JCheckBox
606
     */
607
    private JCheckBox getJCheckBoxTextOnly() {
608
            if (jCheckBoxTextOnly == null) {
609
                    jCheckBoxTextOnly = new JCheckBox();
610
                    jCheckBoxTextOnly.setBounds(315, 171, 133, 22);
611
                    jCheckBoxTextOnly.setText(PluginServices.getText(this, "draw_text_only"));
612
            }
613
            return jCheckBoxTextOnly;
614
    }
615

    
616
        }  //  @jve:visual-info  decl-index=0 visual-constraint="10,10"