Statistics
| Revision:

root / org.gvsig.chart / trunk / org.gvsig.chart / org.gvsig.chart.app / org.gvsig.chart.app.legendplugin / src / main / java / org / gvsig / project / documents / view / legend / gui / ChartLegendPanelPie.java @ 571

History | View | Annotate | Download (34.2 KB)

1
package org.gvsig.project.documents.view.legend.gui;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Dimension;
6
import java.awt.FlowLayout;
7
import java.awt.Font;
8
import java.awt.event.ActionEvent;
9
import java.awt.event.ActionListener;
10
import java.sql.Types;
11
import java.util.ArrayList;
12
import java.util.Iterator;
13
import java.util.Random;
14

    
15
import javax.swing.BorderFactory;
16
import javax.swing.DefaultListModel;
17
import javax.swing.ImageIcon;
18
import javax.swing.JButton;
19
import javax.swing.JCheckBox;
20
import javax.swing.JLabel;
21
import javax.swing.JList;
22
import javax.swing.JPanel;
23
import javax.swing.JScrollPane;
24
import javax.swing.border.TitledBorder;
25
import javax.swing.event.ChangeEvent;
26
import javax.swing.event.ChangeListener;
27

    
28
import org.gvsig.andami.IconThemeHelper;
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.messages.NotificationManager;
31
import org.gvsig.app.gui.panels.ColorChooserPanel;
32
import org.gvsig.app.gui.styling.JComboBoxColorScheme;
33
import org.gvsig.app.project.documents.view.legend.gui.ILegendPanel;
34
import org.gvsig.app.project.documents.view.legend.gui.JSymbolPreviewButton;
35
import org.gvsig.app.project.documents.view.legend.gui.MultipleAttributes;
36
import org.gvsig.chart.ChartLocator;
37
import org.gvsig.chart.ChartManager;
38
import org.gvsig.chart.ChartProperties;
39
import org.gvsig.chart.ChartService;
40
import org.gvsig.chart.legend.IChartLegend;
41
import org.gvsig.chart.legend.ILinkedChartLegend;
42
import org.gvsig.chart.legend.IPieChartLegend;
43
import org.gvsig.chart.legend.symbols.IChartSymbol;
44
import org.gvsig.fmap.dal.exception.DataException;
45
import org.gvsig.fmap.dal.exception.ReadException;
46
import org.gvsig.fmap.dal.feature.Feature;
47
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
48
import org.gvsig.fmap.dal.feature.FeatureSet;
49
import org.gvsig.fmap.dal.feature.FeatureStore;
50
import org.gvsig.fmap.dal.feature.FeatureType;
51
import org.gvsig.fmap.mapcontext.MapContextLocator;
52
import org.gvsig.fmap.mapcontext.layers.FLayer;
53
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
54
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
55
import org.gvsig.gui.beans.swing.JNumberSpinner;
56
import org.gvsig.project.documents.view.legend.PieChartLegend;
57
import org.gvsig.tools.dataTypes.DataTypes;
58
import org.gvsig.tools.dispose.DisposableIterator;
59
import org.gvsig.tools.service.ServiceException;
60

    
61
public class ChartLegendPanelPie extends JPanel implements ILegendPanel {
62

    
63
    /**
64
     *
65
     */
66
    private static final long serialVersionUID = -5009857246030623420L;
67
    // private PreviewBarSymbol previewPanel = null;
68
    private FLyrVect layer;
69
    private int shapeType;
70
    private IChartLegend auxLegend; // @jve:decl-index=0:
71
    private IChartLegend theLegend;
72
    private JPanel centerPanel = null; // @jve:decl-index=0:visual-constraint="10,10"
73
    private JScrollPane jScrollPane = null;
74
    private JList jFieldList = null;
75
    private Random rand = new Random(System.currentTimeMillis());
76
    private JPanel buttons = null;
77
    private JButton addButton = null;
78
    private JButton removeButton = null;
79
    private JButton addAllButton = null;
80
    private JButton removeAllButton = null;
81
    private JPanel pCentral = null;
82
    private FieldsTable fieldsTable = null; // @jve:decl-index=0:visual-constraint="746,10"
83
    private JPanel northPanel = null;
84
    private JPanel southPanel = null;
85

    
86
    private JButton bSize = null;
87
    private JPanel symbolPanel = null;
88
    private JLabel lBackgroundSymbol = null;
89

    
90
    private JSymbolPreviewButton bSymbolBackground = null;
91
    private JComboBoxColorScheme colorCombo = null;
92
    private JLabel lColorSchema = null;
93

    
94
    private JCheckBox ckOnlySelection = null;
95
    private JPanel eastPanel;
96
    private JPanel outlinePanel;
97
    private JLabel jLabel;
98
    private JCheckBox jCheckBox;
99
    private JCheckBox jShowAxis;
100
    private JPanel jPanel;
101
    private JLabel jLabel1;
102
    private JPanel jPanel1;
103
    private JNumberSpinner comboWidth;
104
    private JPanel dimensionPanel;
105
    private JCheckBox ck3D;
106
    private ColorChooserPanel colorButton;
107

    
108
    /**
109
     * This is the default constructor
110
     */
111
    public ChartLegendPanelPie() {
112
        super();
113
        initialize();
114
    }
115

    
116
    /**
117
     * This method initializes this
118
     *
119
     * @return void
120
     */
121
    private void initialize() {
122
        this.setPreferredSize(new Dimension(732, 354));
123
        this.setLayout(new BorderLayout());
124
        this.add(getCenterPanel(), BorderLayout.CENTER);
125
        this.add(getSouthPanel(), BorderLayout.SOUTH);
126
        this.add(getEastPanel(), BorderLayout.EAST);
127

    
128
    }
129

    
130
    private JPanel getEastPanel() {
131
        if (eastPanel == null) {
132
            eastPanel = new JPanel();
133
            eastPanel.setLayout(new BorderLayout());
134
            eastPanel.setPreferredSize(new Dimension(150, 52));
135
            // eastPanel.add(getPreviewChart(), BorderLayout.NORTH);
136
            eastPanel.add(getOutlinePanel(), BorderLayout.CENTER);
137
            eastPanel.add(getDimensionPanel(), BorderLayout.SOUTH);
138
        }
139
        return eastPanel;
140
    }
141

    
142
    private JPanel getDimensionPanel() {
143
        if (dimensionPanel == null) {
144
            dimensionPanel = new JPanel();
145
            dimensionPanel.setLayout(new BorderLayout());
146
            dimensionPanel.setBorder(BorderFactory.createTitledBorder(null,
147
                    PluginServices.getText(this, "dimension"),
148
                    TitledBorder.DEFAULT_JUSTIFICATION,
149
                    TitledBorder.DEFAULT_POSITION, new Font("Dialog",
150
                            Font.BOLD, 12), new Color(51, 51, 51)));
151
            dimensionPanel.setPreferredSize(new Dimension(150, 50));
152
            dimensionPanel.add(getCk3D(), BorderLayout.NORTH);
153
        }
154
        return dimensionPanel;
155
    }
156

    
157
    private JCheckBox getCk3D() {
158
        if (ck3D == null) {
159
            ck3D = new JCheckBox();
160
            ck3D.setEnabled(false);
161
            ck3D.setText(PluginServices.getText(this, "display_3d"));
162
        }
163
        return ck3D;
164
    }
165

    
166
    private JPanel getOutlinePanel() {
167
        if (outlinePanel == null) {
168
            jLabel = new JLabel();
169
            jLabel.setText(PluginServices.getText(this, "color"));
170
            outlinePanel = new JPanel();
171
            outlinePanel.setLayout(new BorderLayout());
172
            outlinePanel.setBorder(BorderFactory.createTitledBorder(null,
173
                    PluginServices.getText(this, "background_color"),
174
                    TitledBorder.DEFAULT_JUSTIFICATION,
175
                    TitledBorder.DEFAULT_POSITION, new Font("Dialog",
176
                            Font.BOLD, 12), new Color(51, 51, 51)));
177
            outlinePanel.setPreferredSize(new Dimension(150, 115));
178
            outlinePanel.add(getJCheckBox(), BorderLayout.NORTH);
179
            outlinePanel.add(getJPanel(), BorderLayout.CENTER);
180
            //outlinePanel.add(getJPanel1(), BorderLayout.SOUTH);
181
        }
182
        return outlinePanel;
183
    }
184

    
185
    private JPanel getJPanel() {
186
        if (jPanel == null) {
187
            jPanel = new JPanel();
188
            jPanel.setLayout(new FlowLayout());
189
            jPanel.add(jLabel, null);
190
            jPanel.add(getColorButton(), null);
191
        }
192
        return jPanel;
193
    }
194

    
195
    private ColorChooserPanel getColorButton() {
196
        if (colorButton == null) {
197
            colorButton = new ColorChooserPanel(true);
198
            colorButton.setColor(Color.black);
199
            colorButton.addActionListener(new ActionListener() {
200
                public void actionPerformed(ActionEvent e) {
201
                    auxLegend.setOutlineColor(colorButton.getColor());
202
                }
203
            });
204
        }
205
        return colorButton;
206
    }
207

    
208
    /**
209
     * This method initializes jPanel1
210
     *
211
     * @return javax.swing.JPanel
212
     */
213
    private JPanel getJPanel1() {
214
        if (jPanel1 == null) {
215
            jLabel1 = new JLabel();
216
            jLabel1.setText(PluginServices.getText(this, "width"));
217
            jPanel1 = new JPanel();
218
            jPanel1.setLayout(new FlowLayout());
219
            jPanel1.add(jLabel1, null);
220
            jPanel1.add(getComboWidth(), null);
221
        }
222
        return jPanel1;
223
    }
224

    
225
    private JNumberSpinner getComboWidth() {
226
        if (comboWidth == null) {
227
            comboWidth = new JNumberSpinner(String.valueOf(1), 3, 0, Integer.MAX_VALUE, 1);
228

    
229
            comboWidth.addActionListener(new ActionListener() {
230
                public void actionPerformed(ActionEvent e) {
231
                    auxLegend.setOutlineWidth(comboWidth.getInteger());
232
                }
233

    
234
            });
235
        }
236
        return comboWidth;
237
    }
238

    
239
    private JCheckBox getShowAxis() {
240
        if (jShowAxis == null) {
241
            jShowAxis = new JCheckBox();
242
            jShowAxis.setEnabled(false);
243
            jShowAxis.setText(PluginServices.getText(this, "show_axis"));
244
        }
245
        return jShowAxis;
246
    }
247

    
248
    private JCheckBox getJCheckBox() {
249
        if (jCheckBox == null) {
250
            jCheckBox = new JCheckBox();
251
            jCheckBox.setText(PluginServices.getText(this, "Show"));
252
            jCheckBox.addChangeListener(new ChangeListener() {
253
                public void stateChanged(ChangeEvent e) {
254
                    if(auxLegend!=null){
255
                        auxLegend.setOutlineShow(jCheckBox.isSelected());
256
                    }
257
                }
258

    
259
            });
260
        }
261
        return jCheckBox;
262
    }
263

    
264
    private JPanel getSouthPanel() {
265
        if (southPanel == null) {
266
            southPanel = new JPanel();
267
            southPanel.setLayout(new FlowLayout());
268
            southPanel.setPreferredSize(new Dimension(241, 90));
269
            southPanel.add(getSymbolPanel(), null);
270
            southPanel.add(getNorthPanel(), null);
271
            southPanel.add(getBSize(), null);
272
            southPanel.add(getCkOnlySelection(), null);
273
        }
274
        return southPanel;
275
    }
276

    
277
    private JPanel getSymbolPanel() {
278
        if (symbolPanel == null) {
279
            lBackgroundSymbol = new JLabel();
280
            lBackgroundSymbol.setText(PluginServices.getText(this,
281
                    "background_symbol"));
282
            lBackgroundSymbol.setText(PluginServices.getText(this,
283
                    "show_axis"));
284
            symbolPanel = new JPanel();
285
            symbolPanel.setLayout(new FlowLayout());
286
            symbolPanel.setPreferredSize(new Dimension(241, 40));
287
//                        symbolPanel.add(lBackgroundSymbol, null);
288
//                        symbolPanel.add(getBackgroundSymbol(), null);
289
            symbolPanel.add(getShowAxis(), null);
290

    
291
        }
292
        return symbolPanel;
293
    }
294

    
295
    private JSymbolPreviewButton getBackgroundSymbol() {
296
        if (bSymbolBackground == null) {
297
            bSymbolBackground = new JSymbolPreviewButton(shapeType);
298
            bSymbolBackground.setPreferredSize(new Dimension(110, 20));
299
            bSymbolBackground.addActionListener(new ActionListener() {
300
                public void actionPerformed(ActionEvent e) {
301
                    // getPreviewChart().setBackgroundSymbol(bSymbolBackground.getSymbol());
302
                    // getPreviewChart().repaint();
303
                }
304
            });
305
        }
306
        return bSymbolBackground;
307
    }
308

    
309
    private JButton getBSize() {
310
        if (bSize == null) {
311
            bSize = new JButton();
312
            bSize.setText(PluginServices.getText(this, "size"));
313
            bSize.setPreferredSize(new Dimension(100, 30));
314
            bSize.setMaximumSize(new Dimension(100, 30));
315
            bSize.addActionListener(new ActionListener() {
316
                public void actionPerformed(ActionEvent e) {
317
                    PluginServices.getMDIManager().addCentredWindow(
318
                            new SizePanel(layer, (IChartLegend) getLegend()));
319
                }
320

    
321
            });
322
        }
323
        return bSize;
324
    }
325

    
326
    /**
327
     * This method initializes centerPanel
328
     *
329
     * @return javax.swing.JPanel
330
     */
331
    private JPanel getCenterPanel() {
332
        if (centerPanel == null) {
333
            centerPanel = new JPanel();
334
            centerPanel.setLayout(new BorderLayout());
335
            centerPanel.setPreferredSize(new Dimension(350, 300));
336
            centerPanel.add(getJScrollPane(), BorderLayout.WEST);
337
            centerPanel.add(getButtons(), BorderLayout.CENTER);
338
            centerPanel.add(getFieldsTable(), java.awt.BorderLayout.EAST);
339
        }
340
        return centerPanel;
341
    }
342

    
343
    private JPanel getPCentral() {
344
        if (pCentral == null) {
345
            pCentral = new JPanel();
346
            pCentral.setLayout(new FlowLayout());
347
            pCentral.setPreferredSize(new Dimension(50, 185));
348
            pCentral.add(getRemoveAllButton(), null);
349
            pCentral.add(getAddAllButton(), null);
350
            pCentral.add(getRemoveButton(), null);
351
            pCentral.add(getAddButton(), null);
352
        }
353
        return pCentral;
354
    }
355

    
356
    private JButton getAddButton() {
357
        if (addButton == null) {
358
            addButton = new JButton();
359
            addButton.setText(">");
360
            addButton.setPreferredSize(new Dimension(50, 40));
361
            addButton.addActionListener(new java.awt.event.ActionListener() {
362
                public void actionPerformed(java.awt.event.ActionEvent e) {
363
                    Object[] fields = getJFieldList().getSelectedValues();
364
                    Color[] colorScheme = colorCombo.getSelectedColors();
365

    
366
                    Color[] colors = new Color[colorScheme.length];
367
                    for (int i = 0; i < colorScheme.length; i++) {
368
                        colors[i] = colorScheme[i];
369
                    }
370
                    for (int i = 0; i < fields.length; i++) {
371
                        getFieldsTable().addTableRecord(
372
                                colorScheme[rand.nextInt(colorScheme.length)],
373
                                (String) fields[i], (String) fields[i]);
374
                        ((DefaultListModel) getJFieldList().getModel())
375
                                .removeElement(fields[i]);
376
                    }
377
                }
378
            });
379
        }
380
        return addButton;
381
    }
382

    
383
    /**
384
     * This method initializes northPanel
385
     *
386
     * @return javax.swing.JPanel
387
     */
388
    private JPanel getNorthPanel() {
389
        if (northPanel == null) {
390
            lColorSchema = new JLabel();
391
            lColorSchema.setText(PluginServices.getText(this, "color_schema"));
392
            northPanel = new JPanel();
393
            northPanel.setLayout(new FlowLayout());
394
            northPanel.add(lColorSchema, null);
395
            northPanel.add(getColorCombo());
396
        }
397
        return northPanel;
398
    }
399

    
400
    private JComboBoxColorScheme getColorCombo() {
401
        if (colorCombo == null) {
402
            colorCombo = new JComboBoxColorScheme(false);
403
        }
404
        return colorCombo;
405
    }
406

    
407
    /**
408
     * This method initializes removeButton
409
     *
410
     * @return javax.swing.JButton
411
     */
412
    private JButton getRemoveButton() {
413
        if (removeButton == null) {
414
            removeButton = new JButton();
415
            removeButton.setPreferredSize(new Dimension(50, 40));
416
            removeButton.setText("<");
417
            removeButton.addActionListener(new java.awt.event.ActionListener() {
418
                public void actionPerformed(java.awt.event.ActionEvent e) {
419
                    int[] index = getFieldsTable().getSelectedRows();
420
                    String[] fields = new String[index.length];
421
                    for (int i = 0; i < index.length; i++) {
422
                        fields[i] = (String) getFieldsTable().getFieldValue(
423
                                index[i], 1);
424
                    }
425
                    for (int i = 0; i < fields.length; i++) {
426
                        ((DefaultListModel) getJFieldList().getModel())
427
                                .addElement(fields[i]);
428
                    }
429
                    getFieldsTable().removeSelectedRows();
430
                }
431
            });
432
        }
433
        return removeButton;
434
    }
435

    
436
    /**
437
     * This method initializes addAllButton
438
     *
439
     * @return javax.swing.JButton
440
     */
441
    private JButton getAddAllButton() {
442
        if (addAllButton == null) {
443
            addAllButton = new JButton();
444
            addAllButton.setPreferredSize(new Dimension(50, 40));
445
            addAllButton.setText(">>");
446
            addAllButton.addActionListener(new java.awt.event.ActionListener() {
447
                public void actionPerformed(java.awt.event.ActionEvent e) {
448
                    Object[] fields = ((DefaultListModel) getJFieldList()
449
                            .getModel()).toArray();
450

    
451
                    Color[] colorScheme = colorCombo.getSelectedColors();
452

    
453
                    Color[] colors = new Color[colorScheme.length];
454
                    for (int i = 0; i < colorScheme.length; i++) {
455
                        colors[i] = colorScheme[i];
456
                    }
457
                    for (int i = 0; i < fields.length; i++) {
458
                        getFieldsTable().addTableRecord(
459
                                colorScheme[rand.nextInt(colorScheme.length)],
460
                                (String) fields[i], (String) fields[i]);
461
                        ((DefaultListModel) getJFieldList().getModel())
462
                                .removeElement(fields[i]);
463
                    }
464
                }
465
            });
466
        }
467
        return addAllButton;
468
    }
469

    
470
    /**
471
     * This method initializes removeAllButton
472
     *
473
     * @return javax.swing.JButton
474
     */
475
    private JButton getRemoveAllButton() {
476
        if (removeAllButton == null) {
477
            removeAllButton = new JButton();
478
            removeAllButton.setPreferredSize(new Dimension(50, 40));
479
            removeAllButton.setText("<<");
480
            removeAllButton
481
                    .addActionListener(new java.awt.event.ActionListener() {
482
                        public void actionPerformed(java.awt.event.ActionEvent e) {
483
                            fillFieldNames();
484
                            getFieldsTable().removeAllItems();
485
                        }
486
                    });
487
        }
488
        return removeAllButton;
489
    }
490

    
491
    private void fillFieldNames() {
492
        FeatureStore rs;
493
        ((DefaultListModel) getJFieldList().getModel()).removeAllElements();
494
        try {
495
            rs = ((FLyrVect) layer).getFeatureStore();
496
            ArrayList<String> names = new ArrayList<String>();
497
            Iterator iterator = rs.getDefaultFeatureType().iterator();
498
            DefaultListModel cM = new DefaultListModel();
499
            while (iterator.hasNext()) {
500
                FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator
501
                        .next();
502
                if (descriptor.getDataType().isNumeric()) {
503
                    names.add(descriptor.getName());
504
                    cM.addElement(descriptor.getName());
505
                }
506
            }
507

    
508
            getJFieldList().setModel(cM);
509

    
510
        } catch (DataException e) {
511
            NotificationManager.addError(
512
                    PluginServices.getText(this, "recovering_recordset"), e);
513
        }
514

    
515
    }
516

    
517
    private JPanel getButtons() {
518
        if (buttons == null) {
519
            FlowLayout flowLayout = new FlowLayout();
520
            flowLayout.setHgap(5);
521
            flowLayout.setAlignment(FlowLayout.CENTER);
522
            flowLayout.setVgap(1);
523
            buttons = new JPanel();
524
            buttons.setPreferredSize(new Dimension(60, 50));
525
            buttons.setLayout(flowLayout);
526
            buttons.add(getPCentral(), null);
527
        }
528
        return buttons;
529
    }
530

    
531
    private FieldsTable getFieldsTable() {
532
        if (fieldsTable == null) {
533
            fieldsTable = new FieldsTable();
534
            fieldsTable.setPreferredSize(new Dimension(250, 300));
535
            fieldsTable.addColorChange(new ColorChange() {
536
                public void actionChange() {
537
                    // actualizePreviewSymbol();
538
                }
539

    
540
            });
541
        }
542
        return fieldsTable;
543
    }
544

    
545
    private JScrollPane getJScrollPane() {
546
        if (jScrollPane == null) {
547
            jScrollPane = new JScrollPane();
548
            jScrollPane.setBorder(BorderFactory.createTitledBorder(null,
549
                    PluginServices.getText(this, "fields"),
550
                    TitledBorder.DEFAULT_JUSTIFICATION,
551
                    TitledBorder.DEFAULT_POSITION, new Font("Dialog",
552
                            Font.BOLD, 12), new Color(51, 51, 51)));
553
            jScrollPane.setPreferredSize(new Dimension(150, 150));
554
            jScrollPane.setViewportView(getJFieldList());
555
        }
556
        return jScrollPane;
557
    }
558

    
559
    /**
560
     * This method initializes jFieldList
561
     *
562
     * @return javax.swing.JList
563
     */
564
    private JList getJFieldList() {
565
        if (jFieldList == null) {
566
            DefaultListModel dlm = new DefaultListModel();
567
            jFieldList = new JList();
568
            jFieldList.setSize(new Dimension(150, 150));
569
            jFieldList.setModel(dlm);
570
        }
571
        return jFieldList;
572
    }
573

    
574
    public String getDescription() {
575
        return PluginServices.getText(this, "draw_bar_chart_for_each_feature");
576
    }
577

    
578
    public ImageIcon getIcon() {
579
        return IconThemeHelper.getImageIcon("legend-pie-chart");
580
    }
581

    
582
    /*
583
         * (non-Javadoc)
584
         *
585
         * @see
586
         * org.gvsig.app.project.documents.view.legend.gui.ILegendPanel#getLegend()
587
     */
588
    public ILegend getLegend() {
589
        if (auxLegend == null) {
590
            auxLegend = (IChartLegend) MapContextLocator.getMapContextManager()
591
                    .createLegend(IChartLegend.PIECHART_LEGEND_NAME);
592
        }
593
        auxLegend.setOutlineShow(getJCheckBox().isSelected());
594
        auxLegend.setOutlineColor(getColorButton().getColor());
595
        auxLegend.setOutlineWidth(getComboWidth().getInteger());
596
        auxLegend.setIs3D(getCk3D().isSelected());
597

    
598
        if (getFieldsTable() != null) {
599
            int rowCount = getFieldsTable().getRowCount();
600
            if (rowCount > 0) {
601
                String[] fields = new String[rowCount];
602
                String[] labels = new String[rowCount];
603
                Color[] colors = new Color[rowCount];
604
                int[] types = new int[rowCount];
605
                for (int i = 0; i < fields.length; i++) {
606
                    colors[i] = ((JPanel) getFieldsTable().getFieldValue(i, 0))
607
                            .getBackground();
608
                    fields[i] = (String) getFieldsTable().getFieldValue(i, 1);
609
                    labels[i] = (String) getFieldsTable().getFieldValue(i, 2);
610
                    //No es necesario para nada, pero si no lo inicializamos, peta el clonado de la leyenda (en AbstractClassifiedVectorLegend)
611
                    types[i] = DataTypes.STRING;
612
                }
613
                auxLegend.setClassifyingFieldNames(fields);
614
                //No es necesario para nada, pero si no lo inicializamos, peta el clonado de la leyenda (en AbstractClassifiedVectorLegend)
615
                auxLegend.setClassifyingFieldTypes(types);
616
                auxLegend.setFields(fields);
617
                auxLegend.setLabels(labels);
618
                auxLegend.setColors(colors);
619

    
620
                if (auxLegend.getSizeOption() == IChartSymbol.SIZEFIELD) {
621
                    int classifyingCount = fields.length + 2;
622
                    if (auxLegend.getFieldNormalize().equals("---")) {
623
                        classifyingCount = fields.length + 1;
624
                    }
625
                    String[] classifyingFields = new String[classifyingCount];
626
                    for (int i = 0; i < fields.length; i++) {
627
                        classifyingFields[i] = fields[i];
628
                    }
629
                    if (!auxLegend.getFieldNormalize().equals("---")) {
630
                        classifyingFields[classifyingCount - 2] = auxLegend
631
                                .getFieldNormalize();
632
                    }
633
                    classifyingFields[classifyingCount - 1] = auxLegend.getFieldSize();
634

    
635
                    auxLegend.setClassifyingFieldNames(classifyingFields);
636
                } else {
637
                    if (fields != null && fields.length > 0) {
638
                        auxLegend.setClassifyingFieldNames(fields);
639
                    }
640
                }
641

    
642
                double min = Double.MAX_VALUE;
643
                double max = Double.MIN_VALUE;
644

    
645
                if (auxLegend.getSizeOption() == IChartSymbol.SIZEFIELD) {
646
                    try {
647
                        FeatureStore fs = layer.getFeatureStore();
648
                        FeatureType ft = fs.getDefaultFeatureType();
649
                        FeatureAttributeDescriptor[] atts = ft
650
                                .getAttributeDescriptors();
651
                        ArrayList<String> auxNames = new ArrayList<String>();
652
                        FeatureSet fset = fs.getFeatureSet();
653
                        DisposableIterator it = fset.fastIterator();
654
                        long rows = fs.getFeatureCount();
655
                        int index = ft.getAttributeDescriptor(auxLegend.getFieldSize())
656
                                .getIndex();
657
                        while (it.hasNext()) {
658
                            Feature f = (Feature) it.next();
659
                            double dob = f.getDouble(index);
660
                            if (dob < min) {
661
                                min = dob;
662
                            }
663
                            if (dob > max) {
664
                                max = dob;
665
                            }
666
                        }
667
                        it.dispose();
668
                        auxLegend.setMinFeature(min);
669
                        auxLegend.setMaxFeature(max);
670

    
671
                    } catch (DataException e) {
672
                        // TODO Auto-generated catch block
673
                        e.printStackTrace();
674
                    }
675

    
676
                } else if (auxLegend.getSizeOption() == IChartSymbol.SIZESUM) {
677
                    try {
678

    
679
                        FeatureStore fs = layer.getFeatureStore();
680
                        FeatureType ft = fs.getDefaultFeatureType();
681
                        FeatureAttributeDescriptor[] atts = ft
682
                                .getAttributeDescriptors();
683
                        ArrayList<String> auxNames = new ArrayList<String>();
684
                        FeatureSet fset = fs.getFeatureSet();
685
                        DisposableIterator it = fset.fastIterator();
686
                        long rows = fs.getFeatureCount();
687
                        String[] names = auxLegend.getClassifyingFieldNames();
688
                        int[] indexes = new int[names.length];
689
                        for (int i = 0; i < names.length; i++) {
690
                            indexes[i] = ft.getAttributeDescriptor(names[i]).getIndex();
691
                        }
692

    
693
                        while (it.hasNext()) {
694
                            Feature f = (Feature) it.next();
695
                            double dob = 0;
696
                            for (int i = 0; i < indexes.length; i++) {
697
                                dob += f.getDouble(indexes[i]);
698
                            }
699
                            if (dob < min) {
700
                                min = dob;
701
                            }
702
                            if (dob > max) {
703
                                max = dob;
704
                            }
705
                        }
706
                        it.dispose();
707
                        auxLegend.setMinFeature(min);
708
                        auxLegend.setMaxFeature(max);
709

    
710
                    } catch (DataException e) {
711
                        // TODO Auto-generated catch block
712
                        e.printStackTrace();
713
                    }
714

    
715
                }// ////////////////////////////////////////////
716

    
717
                auxLegend.useDefaultSymbol(true);
718

    
719
                // TODO: DEVOLVER LA LEYENDA CORRECTA!!!
720
                // TODO: ESTO HAY QUE HACERLO BIEN. LA IDEA ES GENERAR CORRECTAMENTE
721
                // EL CHARTDATASET, PERO NO CON LOS REQUIREDFIELDS.
722
                // Ahora los campos son como las filas con las que trabajabamos
723
                // antes.
724
                IChartSymbol symbol = (IChartSymbol) MapContextLocator
725
                        .getSymbolManager().createSymbol(IChartSymbol.SYMBOL_NAME);
726

    
727
                ChartManager cm = ChartLocator.getServiceManager().getChartManager();
728
                ChartProperties props;
729
                try {
730
                    props = cm.createProperties("PieChart");
731
                    if (auxLegend.is3D()) {
732
                        props.setChartDimension(true);
733
                        props.setChartRenderer("PieChartRenderer3DBasic");
734
                    } else {
735
                        props.setChartDimension(false);
736
                        props.setChartRenderer("PieChartRendererBasic");
737
                    }
738

    
739
                    auxLegend.setColors(colors);
740

    
741
                    props.setHasPlotOutline(auxLegend.isOutlineShow());
742
                    props.setIsLegendVisible(false);
743
                    props.setPlotBackgroundColor(auxLegend.getOutlineColor());
744
                    if (getJCheckBox().isSelected()) {
745
                        float alpha = colorButton.getAlpha() / 100;
746
                        if (alpha > 1) {
747
                            props.setPlotBackgroundAlpha(1.0f);
748
                        } else {
749
                            props.setPlotBackgroundAlpha(alpha);
750
                        }
751
                    } else {
752
                        props.setPlotBackgroundAlpha(0.0f);
753
                    }
754
                    props.setChartName("");
755
                    props.setChartDimension(auxLegend.is3D());
756

    
757
                    ChartService cs = cm.createChart(props);
758
                    cs.setIsAChartSymbol(true);
759

    
760
                    // Fijamos los colores dentro del ChartSymbol para hacerlo solo una vez
761
                    if (cs.getRenderer() != null) {
762
                        for (int i = 0; i < fields.length; i++) {
763
                            cs.getRenderer().setSeriesPaint(i, auxLegend.getColors()[i]);
764
                        }
765
                    }
766

    
767
                    // TODO: REVISAR QU? PASA CUANDO HAY SELECCION. EL SIMBOLO QUE DEVUELVE NO ES CORRECTO
768
                    symbol.setChart(cs);
769
                    auxLegend.setDefaultSymbol(symbol);
770

    
771
                } catch (ServiceException e) {
772
                    // TODO Auto-generated catch block
773
                    e.printStackTrace();
774
                }
775

    
776
            }
777
        }
778
        this.theLegend = auxLegend;
779
        return theLegend;
780
    }
781

    
782
    public Class getLegendClass() {
783
        return PieChartLegend.class;
784
    }
785

    
786
    public JPanel getPanel() {
787
        return this;
788
    }
789

    
790
    public Class getParentClass() {
791
        return MultipleAttributes.class;
792
    }
793

    
794
    public String getTitle() {
795
        return PluginServices.getText(this, "pie_legend");
796
    }
797

    
798
    public boolean isSuitableFor(FLayer layer) {
799
        return (layer instanceof FLyrVect);
800
    }
801

    
802
    public void setData(FLayer layer, ILegend legend) {
803
        this.layer = (FLyrVect) layer;
804
        try {
805
            shapeType = this.layer.getShapeType();
806
        } catch (ReadException e) {
807
            e.printStackTrace();
808
        }
809
        //getBackgroundSymbol().setShapeType(shapeType);
810

    
811
        fieldsTable = getFieldsTable();
812

    
813
        fieldsTable.setSize(new Dimension(235, 129));
814
        fillFieldNames();
815

    
816
        fieldsTable.removeAllItems();
817

    
818
        if (legend instanceof IPieChartLegend) {
819
            IPieChartLegend pieChartLegend = (IPieChartLegend) legend;
820
            // TODO: PROVISIONAL. CREAMOS SIEMPRE LA LEYENDA PORQUE NO LA PODEMOS CLONAR TODAVIA.
821
            // auxLegend = (IChartLegend) legend.cloneLegend();
822

    
823
            getJCheckBox().setSelected(pieChartLegend.isOutlineShow());
824
            getColorButton().setColor(pieChartLegend.getOutlineColor());
825
            getComboWidth().setInteger(pieChartLegend.getOutlineWidth());
826

    
827
            getCk3D().setSelected(pieChartLegend.is3D());
828
            getCkOnlySelection().setSelected(pieChartLegend.isOnlySelection());
829
            String[] fields;
830
            String[] labels;
831

    
832
            fields = pieChartLegend.getClassifyingFieldNames();
833
            labels = pieChartLegend.getFieldLabels();
834
            if (fields != null) {
835
                if (labels != null && labels.length != fields.length) {
836
                    String[] aux = new String[fields.length];
837
                    for (int i = 0; i < fields.length; i++) {
838
                        aux[i] = fields[i];
839
                    }
840
                    labels = aux;
841
                }
842
                Color[] colors = new Color[fields.length];
843
                for (int i = 0; i < fields.length; i++) {
844
                    colors[i] = ((IPieChartLegend) legend).getSymbolByValue(fields[i]).getColor();
845
                }
846

    
847
                if (colors != null) {
848
                    getFieldsTable().fillTableFromSymbolList(colors, fields, labels);
849
                    // getPreviewChart().setSections(fields, colors);
850
                }
851
                for (int i = 0; i < fields.length; i++) {
852
                    ((DefaultListModel) getJFieldList().getModel()).removeElement(fields[i]);
853
                }
854
            }
855
        }
856
    }
857

    
858
    private boolean isNumericField(int fieldType) {
859
        switch (fieldType) {
860
            case Types.BIGINT:
861
            case Types.DECIMAL:
862
            case Types.DOUBLE:
863
            case Types.FLOAT:
864
            case Types.INTEGER:
865
            case Types.NUMERIC:
866
            case Types.REAL:
867
            case Types.SMALLINT:
868
            case Types.TINYINT:
869
                return true;
870
            default:
871
                return false;
872
        }
873

    
874
    }
875

    
876
    /**
877
     * This method initializes ckOnlySelection
878
     *
879
     * @return javax.swing.JCheckBox
880
     */
881
    private JCheckBox getCkOnlySelection() {
882
        if (ckOnlySelection == null) {
883
            ckOnlySelection = new JCheckBox();
884
            ckOnlySelection.setText(PluginServices.getText(this,
885
                    "only_selection"));
886
        }
887
        return ckOnlySelection;
888
    }
889

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