Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / thememanager / legendmanager / panels / FPanelLegendLabels.java @ 6998

History | View | Annotate | Download (16.2 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
        // Scale the value of the font size to the value of the height
203
        // and avoid too much decimal numbers
204
        size = ((int) (100*size / FONT_SCALE_FACTOR))/100;
205
                getJTextField().setText(String.valueOf(size));
206

    
207

    
208
        }
209

    
210
        private void updateControls() {
211
                getJComboBoxTextField().setEnabled(getJCheckBox().isSelected());
212
                getJComboBoxHeightField().setEnabled(getJCheckBox().isSelected());
213
        getJComboBoxRotationField().setEnabled(getJCheckBox().isSelected());
214
                getJRadioButton().setEnabled(getJCheckBox().isSelected());
215
                getJRadioButton1().setEnabled(getJCheckBox().isSelected());
216
                getJTextField().setEnabled(getJCheckBox().isSelected());
217
                if (getJCheckBox().isSelected())
218
                {
219
                        FSymbol sym = m_FSymbol;
220
                        sym.setFont(labelFont);
221
                        sym.setFontColor(m_colorChooser.getColor());
222
                        fPreviewSymbol.setSymbol(sym);
223
                }
224
        }
225
        public void updateValuesFromControls(VectorialLegend l) {
226
                if (getJCheckBox().isSelected())
227
                {
228
            m_FSymbol = l.getDefaultSymbol();
229
            m_FSymbol.setFont(labelFont);
230
                        l.setLabelField((String) getJComboBoxTextField().getSelectedItem());
231
                        l.setLabelHeightField((String) getJComboBoxHeightField().getSelectedItem());
232
            l.setLabelRotationField((String) getJComboBoxRotationField().getSelectedItem());
233
                        m_FSymbol.setFontColor(m_colorChooser.getColor());
234
                        //m_FSymbol.setSymbolType(m_Renderer.getShapeType());
235
                        float fontSize = Float.parseFloat(getJTextField().getText());
236
                        fontSize *= FONT_SCALE_FACTOR;
237
                        if (!getJRadioButton().isSelected())
238
                        {
239
                                // meters
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
                                m_FSymbol.setFontSize(fontSize); // Para que se pueda crear correctamente al recuperar del proyecto
247
                                m_FSymbol.setFontSizeInPixels(false);
248
                        }
249
                        else
250
                        {
251
                                // pixels
252
                                m_FSymbol.setFont(labelFont);
253
                                m_FSymbol.setFont(labelFont.deriveFont(fontSize));
254
                                m_FSymbol.setFontSize(fontSize);
255
                                m_FSymbol.setFontSizeInPixels(true);
256
                        }
257
            m_FSymbol.setShapeVisible(true);
258
            if (getJCheckBoxTextOnly().isSelected())
259
                m_FSymbol.setShapeVisible(false);
260

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

    
272

    
273

    
274

    
275
        private void fillFieldNames() {
276

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

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

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

    
307

    
308
        /**
309

310
         * This method initializes jCheckBox
311

312
         *
313

314
         * @return javax.swing.JCheckBox
315

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

    
324
                                public void itemStateChanged(java.awt.event.ItemEvent e) {
325
                                        updateControls();
326

    
327
                                }
328
                        });
329

    
330
                }
331
                return jCheckBox;
332
        }
333

    
334
        /**
335

336
         * This method initializes jLabel1
337

338
         *
339

340
         * @return javax.swing.JLabel
341

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

    
352
        /**
353

354
         * This method initializes jComboBox
355

356
         *
357

358
         * @return javax.swing.JComboBox
359

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

    
370
        /**
371

372
         * This method initializes jLabel2
373

374
         *
375

376
         * @return javax.swing.JLabel
377

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

    
389
        /**
390

391
         * This method initializes jComboBox1
392

393
         *
394

395
         * @return javax.swing.JComboBox
396

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

    
408
        /**
409

410
         * This method initializes jPanel
411

412
         *
413

414
         * @return javax.swing.JPanel
415

416
         */
417
        private javax.swing.JPanel getJPanel() {
418
                if (jPanel == null) {
419
                        jPanel = new javax.swing.JPanel();
420
                        jPanel.setLayout(null);
421
                        jPanel.setBounds(29, 136, 258, 84);
422
                        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));
423
                        ButtonGroup jButtonGroup = new ButtonGroup();
424
                        jButtonGroup.add(getJRadioButton1());
425
                        jButtonGroup.add(getJRadioButton());
426
                        jPanel.add(getJRadioButton1(), null);
427
                        jPanel.add(getJRadioButton(), null);
428
                        jPanel.add(getJTextField(), null);
429
                }
430
                return jPanel;
431
        }
432

    
433
        /**
434

435
         * This method initializes jTextField
436

437
         *
438

439
         * @return javax.swing.JTextField
440

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

    
452
/**
453

454
         * This method initializes jRadioButton
455

456
         *
457

458
         * @return javax.swing.JRadioButton
459

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

    
472
        /**
473

474
         * This method initializes jRadioButton1
475

476
         *
477

478
         * @return javax.swing.JRadioButton
479

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

    
491
        /**
492

493
         * This method initializes fPreviewSymbol
494

495
         *
496

497
         * @return com.iver.cit.opensig.gui.Panels.FPreviewSymbol
498

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

    
508
        /**
509

510
         * This method initializes jLabel3
511

512
         *
513

514
         * @return javax.swing.JLabel
515

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

    
527
        /**
528

529
         * This method initializes jButton
530

531
         *
532

533
         * @return javax.swing.JButton
534

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

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

    
547
                                        newFont = FontChooser.showDialog("Choose Font", labelFont);
548

    
549
                                if (newFont == null) {
550
                                    return;
551
                                }
552

    
553
                                labelFont = newFont;
554
                                updateControls();
555

    
556

    
557
                                }
558
                        });
559

    
560
                }
561
                return jButton;
562
        }
563

    
564
        /**
565

566
         * This method initializes jLabel4
567

568
         *
569

570
         * @return javax.swing.JLabel
571

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

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