Statistics
| Revision:

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

History | View | Annotate | Download (35.4 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.project.documents.view.legend.gui;
46

    
47
import java.awt.BorderLayout;
48
import java.awt.Component;
49
import java.awt.Dimension;
50
import java.awt.FlowLayout;
51
import java.awt.Font;
52
import java.awt.GridLayout;
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55
import java.sql.Types;
56
import java.util.ArrayList;
57
import java.util.Hashtable;
58
import java.util.Iterator;
59

    
60
import javax.swing.BorderFactory;
61
import javax.swing.ButtonGroup;
62
import javax.swing.JCheckBox;
63
import javax.swing.JComponent;
64
import javax.swing.JLabel;
65
import javax.swing.JOptionPane;
66
import javax.swing.JPanel;
67
import javax.swing.JRadioButton;
68
import javax.swing.JTextField;
69

    
70
import org.gvsig.gui.beans.AcceptCancelPanel;
71
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
72
import org.gvsig.gui.beans.swing.JButton;
73

    
74
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
75
import com.iver.andami.PluginServices;
76
import com.iver.andami.messages.NotificationManager;
77
import com.iver.andami.ui.mdiManager.IWindow;
78
import com.iver.andami.ui.mdiManager.WindowInfo;
79
import com.iver.cit.gvsig.fmap.core.FShape;
80
import com.iver.cit.gvsig.fmap.core.ILabelable;
81
import com.iver.cit.gvsig.fmap.core.styles.ILabelStyle;
82
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
83
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
84
import com.iver.cit.gvsig.fmap.layers.FLayer;
85
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
86
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling;
87
import com.iver.cit.gvsig.fmap.rendering.styling.GeneralLabelingStrategy;
88
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingMethod;
89
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
90
import com.iver.cit.gvsig.fmap.rendering.styling.IPlacementConstraints;
91
import com.iver.cit.gvsig.fmap.rendering.styling.IZoomConstraints;
92
import com.iver.cit.gvsig.fmap.rendering.styling.LabelClass;
93
import com.iver.cit.gvsig.fmap.rendering.styling.LabelingFactory;
94
import com.iver.cit.gvsig.gui.JUnitsComboBox;
95
import com.iver.cit.gvsig.gui.styling.SelectorFilter;
96
import com.iver.cit.gvsig.gui.styling.StyleSelector;
97
import com.iver.cit.gvsig.gui.styling.SymbolPreviewer;
98
import com.iver.cit.gvsig.gui.styling.SymbolSelector;
99
import com.iver.cit.gvsig.gui.utils.FontChooser;
100
import com.iver.cit.gvsig.project.Project;
101
import com.iver.utiles.swing.JComboBox;
102

    
103
/**
104
 *
105
 * @author jaume dominguez faus - jaume.dominguez@iver.es
106
 *
107
 */
108
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
109
        private static final long serialVersionUID = 856162295985695717L;
110
        private static final String NO_FIELD_ITEM = "-- " +
111
                        PluginServices.getText(LabelingManager.class, "none") + " --";
112
        private static int newClassSuffix = 0;
113
        private static Hashtable<String, Class> methods = new Hashtable<String, Class>();
114
        private static Hashtable<String, Class> strategies = new Hashtable<String, Class>();
115
        private JCheckBox chkApplyLabels;
116
        private JComboBox cmbMethod;
117
        private GridBagLayoutPanel classesPanel;
118
        private JComboBox cmbClasses;
119
        private JCheckBox chkLabel;
120
        private JButton btnRenameClass;
121
        private JButton btnSQLQuery;
122
        private JButton btnDelClass;
123
        private JButton btnAddClass;
124
        private JComboBox cmbExpressions;
125
        private JButton btnEditExpression;
126
        private SymbolPreviewer previewer;
127
        private JButton btnFont;
128
        private Font labelFont;
129
        private JButton btnPlacement;
130
        private JButton btnLabelStyles;
131
        private JCheckBox chkTextOnly;
132
        private ILabelable layer;
133
        private JButton btnVisualization;
134
        private ArrayList<String> expressions;
135
        private JPanel content;
136
        private IPlacementConstraints placementConstraints;
137
        private IZoomConstraints zoomConstraints;
138
        private JButton btnSymbol;
139
        private GridBagLayoutPanel userDefinedContent;
140
        private JPanel tableDefinedContent;
141
        private JPanel pnlNorth;
142
        private JComboBox cmbStrategy;
143
        private JComboBox cmbRotationField;
144
        private JComboBox cmbHeightField;
145
        private JComboBox cmbTextField;
146
        private String[] fieldNames;
147
        private String[] numericFieldNames;
148
        private JUnitsComboBox cmbUnits;
149
        private JRadioButton rdBtnHeightField;
150
        private JRadioButton rdBtnFixedHeight;
151
        private JTextField txtHeightField;
152
        private boolean noEvent;
153

    
154

    
155

    
156
        private class MethodItem {
157
                private String name;
158
                private ILabelingMethod method;
159

    
160
                private MethodItem(String name, ILabelingMethod method) {
161
                        this.name = name;
162
                        this.method = method;
163
                }
164

    
165
                public String toString() {
166
                        return name;
167
                }
168
        }
169

    
170
        private class LabelingStrategyItem {
171
                private String name;
172
                private ILabelingStrategy strategy;
173

    
174
                private LabelingStrategyItem(String name, ILabelingStrategy strategy) {
175
                        this.name = name;
176
                        this.strategy = strategy;
177
                }
178

    
179
                public String toString() {
180
                        return name;
181
                }
182

    
183
                public boolean equals(Object obj) {
184
                        if (obj instanceof ILabelingStrategy) {
185
                                ILabelingStrategy strategy = (ILabelingStrategy) obj;
186
                                return this.strategy.getClass().equals(strategy.getClass());
187

    
188
                        }
189
                        return super.equals(obj);
190
                }
191

    
192
        }
193

    
194
        private class ExprEditorPanel extends JPanel implements IWindow {
195
                private static final long serialVersionUID = -3867224882591879900L;
196
                private ActionListener action = new ActionListener() {
197
                        public void actionPerformed(ActionEvent e) {
198
                                if ("OK".equals(e.getActionCommand())) {
199
                                        if (validateExpr())
200
                                                PluginServices.getMDIManager().closeWindow(
201
                                                                ExprEditorPanel.this);
202
                                        else
203
                                                JOptionPane.showMessageDialog(ExprEditorPanel.this,
204
                                                                PluginServices.getText(this, "syntax_error"),
205
                                                                PluginServices.getText(this, "error"),
206
                                                                JOptionPane.ERROR_MESSAGE);
207
                                } else {
208
                                        getTxtExpression().setText(lastExtression);
209
                                        PluginServices.getMDIManager().closeWindow(
210
                                                        ExprEditorPanel.this);
211
                                }
212
                        }
213

    
214

    
215
                };
216
                private JTextField txtExpression;
217
                private String lastExtression;
218

    
219
                public ExprEditorPanel(String currentExpression) {
220
                        super();
221
                        initialize();
222
                        lastExtression = currentExpression;
223
                        getTxtExpression().setText(currentExpression);
224

    
225
                }
226

    
227
                private void initialize() {
228
                        GridBagLayoutPanel content = new GridBagLayoutPanel();
229
                        content.addComponent(new JLabel(" "+PluginServices.getText(this, "label_expression_help")));
230
                        content.addComponent(
231
                                        " "+PluginServices.getText(this, "expression")+":",
232
                                        getTxtExpression());
233
                        setLayout(new BorderLayout(5, 5));
234

    
235
                        add(content, BorderLayout.CENTER);
236
                        add(new AcceptCancelPanel(action, action), BorderLayout.SOUTH);
237
                        setSize(new Dimension(300, 80));
238
                }
239

    
240
                private JTextField getTxtExpression() {
241
                        if (txtExpression == null) {
242
                                txtExpression = new JTextField(25);
243
                        }
244
                        return txtExpression;
245
                }
246

    
247
                public String getExpression() {
248
                        return getTxtExpression().getText();
249
                }
250

    
251
                public WindowInfo getWindowInfo() {
252
                        WindowInfo viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
253
                        viewInfo.setWidth(getWidth());
254
                        viewInfo.setHeight(getHeight());
255
                        viewInfo.setTitle(PluginServices.getText(this,"label_expression_editor"));
256
                        return viewInfo;
257
                }
258

    
259
                private boolean validateExpr() {
260
                        // TODO : implement it
261
                        return true;
262
                }
263
        }
264

    
265
        public LabelingManager() {
266
                super();
267
                initialize();
268
        }
269

    
270
        private void initialize() {
271
                setLayout(new BorderLayout());
272
                getUserDefinedContent();
273
                getTableDefinedContent();
274
                content = new JPanel(new BorderLayout());
275
                content.setBorder(BorderFactory.createEtchedBorder());
276
                add(getPnlNorth(), BorderLayout.NORTH);
277
                add(content, BorderLayout.SOUTH);
278
//                if (!strategies.keySet().isEmpty())  {
279
//                        getCmbStrategy().setSelectedIndex(0);
280
//                        actionPerformed(new ActionEvent(getCmbStrategy(),321, null));
281
//                }
282

    
283
        }
284

    
285
        private JPanel getPnlNorth() {
286
                if (pnlNorth == null) {
287
                        pnlNorth = new JPanel(new BorderLayout(5,5));
288
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
289

    
290
                        aux.add(getChkApplyLabels());
291
                        pnlNorth.add(aux, BorderLayout.NORTH);
292
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
293
                        aux.add(new JLabel(PluginServices.getText(this, "general")+":"));
294
                        aux.add(getCmbStrategy());
295
                        pnlNorth.add(aux, BorderLayout.CENTER);
296

    
297
                }
298
                return pnlNorth;
299
        }
300

    
301
        private JComboBox getCmbStrategy() {
302
                if (cmbStrategy == null) {
303
                        Iterator<String> it = strategies.keySet().iterator();
304
                        final ArrayList<LabelingStrategyItem> aux = new ArrayList<LabelingStrategyItem>();
305
                        while (it.hasNext()) {
306
                                String name = it.next();
307

    
308
                                Class strategyClass = strategies.get(name);
309
                                ILabelingStrategy strategy;
310
                                try {
311
                                        strategy = (ILabelingStrategy) strategyClass.newInstance();
312
                                        LabelingStrategyItem newItem = new LabelingStrategyItem(name, strategy);
313

    
314
                                        aux.add(newItem);
315

    
316
                                } catch (InstantiationException e) {
317
                                        NotificationManager.addError("Trying to instantiate an interface" +
318
                                                        " or abstract class + "+strategyClass.getName(), e);
319
                                } catch (IllegalAccessException e) {
320
                                        NotificationManager.addError("IllegalAccessException: does " +
321
                                                        strategyClass.getName()        + " class have an anonymous" +
322
                                                                        " constructor?", e);
323
                                }
324

    
325
                        }
326
                        final LabelingStrategyItem items[] = aux.toArray(new LabelingStrategyItem[aux.size()]);
327
                        
328
                        cmbStrategy = new JComboBox(items) {
329
                                
330
                                @Override
331
                                public void setSelectedItem(Object anObject) {
332
                                        if (anObject instanceof LabelingStrategyItem) {
333
                                                LabelingStrategyItem o = (LabelingStrategyItem) anObject;
334
                                                for (int i = 0; i < items.length; i++) {
335
                                                        if (items[i].strategy.getClass().equals(o.strategy.getClass())) {
336
                                                                super.setSelectedItem(items[i]);
337
                                                                return;
338
                                                        }
339
                                                }
340
                                                
341
                                        }
342
                                }
343
                                @Override
344
                                public Object getItemAt(int index) {
345
                                        return items[index];
346
                                }
347
                                
348
                                @Override
349
                                public int getItemCount() {
350
                                        return items.length;
351
                                }
352
                        };
353
                        cmbStrategy.setName("CMBMODE");
354
                        cmbStrategy.addActionListener(this);
355

    
356
                }
357

    
358
                return cmbStrategy;
359
        }
360

    
361
        private JPanel getTableDefinedContent() {
362
                if (tableDefinedContent == null) {
363
                        tableDefinedContent = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
364
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
365
                        aux.addComponent(PluginServices.getText(this, "text_field") + ":", getCmbTextField());
366
                        aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
367
                        aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
368
                        aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
369
                        aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
370
                        tableDefinedContent.add(aux);
371

    
372

    
373
                        ButtonGroup group = new ButtonGroup();
374
                        group.add(getRdBtnFixedHeight());
375
                        group.add(getRdBtnHeightField());
376

    
377
                        getRdBtnHeightField().setEnabled(true);
378
                }
379

    
380
                return tableDefinedContent;
381
        }
382

    
383
        private JTextField getTxtHeightField() {
384
                if (txtHeightField == null) {
385
                        txtHeightField = new JTextField(10);
386
                        txtHeightField.setText("10");
387
                        txtHeightField.setName("TXTHEIGHTFIELD");
388
                }
389

    
390
                return txtHeightField;
391
        }
392

    
393
        private JRadioButton getRdBtnFixedHeight() {
394
                if (rdBtnFixedHeight == null) {
395
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
396
                        rdBtnFixedHeight.addActionListener(this);
397
                        rdBtnFixedHeight.setSelected(false);
398
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
399
                }
400

    
401
                return rdBtnFixedHeight;
402
        }
403

    
404
        private JRadioButton getRdBtnHeightField() {
405
                if (rdBtnHeightField == null) {
406
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
407
                        rdBtnHeightField.addActionListener(this);
408
                        rdBtnHeightField.setSelected(true);
409
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
410
                }
411

    
412
                return rdBtnHeightField;
413
        }
414

    
415
        private JUnitsComboBox getCmbUnits() {
416
                if (cmbUnits == null) {
417
                        cmbUnits = new JUnitsComboBox();
418
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
419
                        cmbUnits.setName("CMBUNITS");
420
                }
421

    
422
                return cmbUnits;
423
        }
424

    
425
        private JComboBox getCmbRotationField() {
426
                if (cmbRotationField == null) {
427
                        cmbRotationField = new JComboBox();
428
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
429
                        cmbRotationField.addActionListener(this);
430
                        cmbRotationField.setName("CMBROTATIONFIELD");
431
                }
432
                return cmbRotationField;
433
        }
434

    
435
        private JComboBox getCmbHeightField() {
436
                if (cmbHeightField == null) {
437
                        cmbHeightField = new JComboBox();
438
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
439
                        cmbHeightField.setName("CMBHEIGHTFIELD");
440
                        cmbHeightField.addActionListener(this);
441
                }
442
                return cmbHeightField;
443
        }
444

    
445
        private JComboBox getCmbTextField() {
446
                if (cmbTextField == null) {
447
                        cmbTextField = new JComboBox();
448
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
449
                        cmbTextField.setName("CMBTEXTFIELD");
450
                        cmbTextField.addActionListener(this);
451
                }
452
                return cmbTextField;
453
        }
454

    
455
        private JPanel getUserDefinedContent() {
456
                if (userDefinedContent == null) {
457
                        userDefinedContent = new GridBagLayoutPanel();
458
                        userDefinedContent.addComponent(getChkApplyLabels());
459
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
460
                        aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
461
                        aux.add(getCmbMethod());
462
                        aux.setPreferredSize(new Dimension(530, 40));
463
                        userDefinedContent.addComponent(aux);
464

    
465

    
466
                        classesPanel = new GridBagLayoutPanel();
467
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
468
                        aux.add(new JLabel(PluginServices.getText(this, "class")+":"));
469
                        aux.add(getCmbClasses());
470
                        aux.add(getChkLabelFeatures());
471
                        aux.setPreferredSize(new Dimension(530, 40));
472
                        classesPanel.addComponent(aux);
473

    
474
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
475
                        aux.add(getBtnAddClass());
476
                        aux.add(getBtnDelClass());
477
                        aux.add(getBtnRenameClass());
478
                        aux.add(getBtnSQLQuery());
479
                        aux.setPreferredSize(new Dimension(530, 30));
480
                        classesPanel.addComponent(aux);
481
                        classesPanel.setBorder(BorderFactory.createTitledBorder(
482
                                        null, PluginServices.getText(this, "classes")));
483
                        userDefinedContent.addComponent(classesPanel);
484

    
485

    
486
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
487
                        aux.setBorder(BorderFactory.createTitledBorder(
488
                                        null, PluginServices.getText(this, "text_string")));
489
                        aux.add(new JLabel(PluginServices.getText(this, "expression")+":"));
490
                        aux.add(getCmbExpressions());
491
                        aux.add(getBtnEditExpression());
492
                        aux.setPreferredSize(new Dimension(542, 60));
493
                        userDefinedContent.addComponent(aux);
494

    
495

    
496
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
497
                        aux.setBorder(BorderFactory.createTitledBorder(
498
                                        null, PluginServices.getText(this, "text_symbol")));
499
                        aux.add(getSymbolPreviewer());
500
                        JPanel aux2 = new JPanel();
501
                        aux2.add(getBtnFont());
502
                        aux2.add(getBtnSymbol());
503

    
504
                        aux2.setLayout(new GridLayout(aux2.getComponentCount(), 1));
505
                        aux.add(aux2);
506
                        aux.setPreferredSize(new Dimension(542, 100));
507
                        userDefinedContent.addComponent(aux);
508

    
509

    
510
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
511
                        aux.setBorder(BorderFactory.createTitledBorder(
512
                                        null, PluginServices.getText(this, "options")));
513
                        aux.add(getBtnVisualization());
514
                        aux.add(getBtnPlacement());
515
                        aux.add(getBtnLabelStyles());
516
                        aux.setPreferredSize(new Dimension(542, 60));
517
                        userDefinedContent.addComponent(aux);
518
                }
519
                return userDefinedContent;
520
        }
521

    
522
        private Component getBtnSymbol() {
523
                if (btnSymbol == null) {
524
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
525
                        btnSymbol.setName("BTNSYMBOL");
526
                        btnSymbol.addActionListener(this);
527
                }
528

    
529
                return btnSymbol;
530
        }
531

    
532
        private JCheckBox getChkApplyLabels() {
533
                if (chkApplyLabels == null) {
534
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labeling"));
535
                        chkApplyLabels.setName("CHKAPPLYLABELS");
536
                        chkApplyLabels.addActionListener(this);
537
                }
538
                return chkApplyLabels;
539
        }
540

    
541
        public static void addLabelingMethod(String name, Class iLabelingMethodClass) {
542
                methods.put(name, iLabelingMethodClass);
543
        }
544

    
545
        public static void addLabelingStrategy(String name, Class iLabelingStrategyClass) {
546
                strategies.put(name, iLabelingStrategyClass);
547
        }
548

    
549
        private JButton getBtnVisualization() {
550
                if (btnVisualization == null) {
551
                        btnVisualization = new JButton(
552
                                        PluginServices.getText(this, "visualization")+"...");
553
                        btnVisualization.setName("BTNVISUALIZATION");
554
                        btnVisualization.addActionListener(this);
555
                }
556
                return btnVisualization;
557
        }
558

    
559
        private JButton getBtnLabelStyles() {
560
                if (btnLabelStyles == null) {
561
                        btnLabelStyles = new JButton(
562
                                        PluginServices.getText(this, "label_styles")+"...");
563
                        btnLabelStyles.setName("BTNLABELSTYLES");
564
                        btnLabelStyles.addActionListener(this);
565
                }
566
                return btnLabelStyles;
567
        }
568

    
569
        private JButton getBtnPlacement() {
570
                if (btnPlacement == null) {
571
                        btnPlacement = new JButton(
572
                                        PluginServices.getText(this, "placement")+"...");
573
                        btnPlacement.setName("BTNPLACEMENT");
574
                        btnPlacement.addActionListener(this);
575
                }
576
                return btnPlacement;
577
        }
578

    
579
        private JButton getBtnFont() {
580
                if (btnFont == null) {
581
                        btnFont = new JButton(
582
                                        PluginServices.getText(this,"fuente")+"...");
583
                        btnFont.setName("BTNFONT");
584
                        btnFont.addActionListener(this);
585
                }
586
                return btnFont;
587
        }
588

    
589
        private SymbolPreviewer getSymbolPreviewer() {
590
                if (previewer == null) {
591
                        previewer = new SymbolPreviewer();
592
                        previewer.setPreferredSize(new Dimension(420, 65));
593
                }
594
                return previewer;
595
        }
596

    
597
        private JComboBox getCmbMethod() {
598
                if (cmbMethod == null) {
599
                        Iterator<String> it = methods.keySet().iterator();
600
                        ArrayList<MethodItem> aux = new ArrayList<MethodItem>();
601
                        while (it.hasNext()) {
602
                                String name = it.next();
603

    
604
                                Class methodClass = (Class) methods.get(name);
605
                            ILabelingMethod method;
606
                                try {
607
                                        method = (ILabelingMethod) methodClass.newInstance();
608
                                        MethodItem newItem = new MethodItem(name, method);
609
                                        aux.add(newItem);
610

    
611
                                } catch (InstantiationException e) {
612
                                        NotificationManager.addError("Trying to instantiate an interface" +
613
                                                        " or abstract class + "+methodClass.getName(), e);
614
                                } catch (IllegalAccessException e) {
615
                                        NotificationManager.addError("IllegalAccessException: does " +
616
                                                        methodClass.getName()        + " class have an anonymous" +
617
                                                                        " constructor?", e);
618
                                }
619

    
620
                        }
621
                        cmbMethod = new JComboBox(aux.toArray());
622
                        cmbMethod.setSize(new Dimension(300, 22));
623
                        cmbMethod.setName("CMBMETHOD");
624
                        cmbMethod.addActionListener(this);
625
                }
626
                return cmbMethod;
627
        }
628

    
629
        private JButton getBtnEditExpression() {
630
                if (btnEditExpression == null) {
631
                        btnEditExpression = new JButton(
632
                                        PluginServices.getText(this, "edit_expression")+"...");
633
                        btnEditExpression.setName("BTNEDITEXPRESSION");
634
                        btnEditExpression.addActionListener(this);
635
                }
636
                return btnEditExpression;
637
        }
638

    
639
        private JComboBox getCmbExpressions() {
640
                if (cmbExpressions == null) {
641
                        cmbExpressions = new JComboBox();
642
                        cmbExpressions.setPreferredSize(new Dimension(150, 20));
643
                        cmbExpressions.setName("CMBEXPRESSIONS");
644
                        cmbExpressions.addActionListener(this);
645
                }
646
                return cmbExpressions;
647

    
648
        }
649

    
650
        private JButton getBtnSQLQuery() {
651
                if (btnSQLQuery == null) {
652
                        btnSQLQuery = new JButton(PluginServices.getText(this, "SQL_query"));
653
                        btnSQLQuery.setName("BTNSQLQUERY");
654
                        btnSQLQuery.addActionListener(this);
655
                }
656
                return btnSQLQuery;
657
        }
658

    
659
        private JButton getBtnRenameClass() {
660
                if (btnRenameClass == null) {
661
                        btnRenameClass = new JButton(PluginServices.getText(this, "remane_class"));
662
                        btnRenameClass.setName("BTNRENAMECLASS");
663
                        btnRenameClass.addActionListener(this);
664
                }
665
                return btnRenameClass;
666
        }
667

    
668
        private JButton getBtnDelClass() {
669
                if (btnDelClass == null) {
670
                        btnDelClass = new JButton(PluginServices.getText(this, "delete_class"));
671
                        btnDelClass.setName("BTNDELCLASS");
672
                        btnDelClass.addActionListener(this);
673
                }
674
                return btnDelClass;
675
        }
676

    
677
        private JButton getBtnAddClass() {
678
                if (btnAddClass == null) {
679
                        btnAddClass = new JButton(PluginServices.getText(this, "add_class"));
680
                        btnAddClass.setName("BTNADDCLASS");
681
                        btnAddClass.addActionListener(this);
682
                }
683
                return btnAddClass;
684
        }
685

    
686
        private JCheckBox getChkLabelFeatures() {
687
                if (chkLabel == null) {
688
                        chkLabel = new JCheckBox();
689
                        chkLabel.setText(PluginServices.getText(this, "label_features_in_this_class"));
690
                        chkLabel.setName("CHKLABEL");
691
                        chkLabel.addActionListener(this);
692
                }
693
                return chkLabel;
694
        }
695

    
696
        private JComboBox getCmbClasses() {
697
                if (cmbClasses == null) {
698
                        cmbClasses = new JComboBox();
699
                        cmbClasses.setPreferredSize(new Dimension(150, 20));
700
                        cmbClasses.setName("CMBCLASSES");
701
                        cmbClasses.addActionListener(this);
702
                }
703
                return cmbClasses;
704
        }
705

    
706
        private void setComponentEnabled(Component c, boolean b) {
707
                if (c instanceof JComponent) {
708
                        JComponent c1 = (JComponent) c;
709
                        for (int i = 0; i < c1.getComponentCount(); i++) {
710
                                setComponentEnabled(c1.getComponent(i), b);
711
                        }
712
                }
713
                c.setEnabled(b);
714
        }
715

    
716

    
717
        public void setModel(FLayer layer) throws IllegalArgumentException {
718
                if (layer instanceof ILabelable) {
719
                        // to allow the labeling of non-FLyrVect layers
720
                        if (layer instanceof FLyrVect) {
721
                                FLyrVect lv = (FLyrVect) layer;
722
                                try {
723
                                        fieldNames = lv.getRecordset().getFieldNames();
724

    
725
                                        // detect the numeric fields
726
                                        ArrayList<String> l = new ArrayList<String>();
727
                                        for (int i = 0; i < fieldNames.length; i++) {
728
                                                switch (lv.getRecordset().getFieldType(i)) {
729
                                                case Types.DECIMAL:
730
                                                case Types.NUMERIC:
731
                                                case Types.FLOAT:
732
                                                case Types.DOUBLE:
733
                                                case Types.INTEGER:
734
                                                case Types.REAL:
735
                                                case Types.SMALLINT:
736
                                                case Types.TINYINT:
737
                                                case Types.BIGINT:
738
                                                        l.add(fieldNames[i]);
739
                                                        break;
740
                                                }
741
                                        }
742
                                        numericFieldNames = l.toArray(new String[l.size()]);
743
                                } catch (ReadDriverException e) {
744
                                        NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
745
                                }
746

    
747
                        }
748

    
749

    
750
                        this.layer = (ILabelable) layer;
751

    
752
                        // get the labeling strategy
753
                        ILabelingStrategy strategy = this.layer.getLabelingStrategy();
754
                        if (strategy == null) {
755
                                // set a new (simplest) one
756
                                try {
757
                                        strategy = LabelingFactory.createDefaultStrategy(layer);
758
                                } catch (ReadDriverException e) {
759
                                        strategy = null;
760
                                }
761
                        } else {
762
                                setMethod(strategy.getLabelingMethod());
763
                                placementConstraints = strategy.getPlacementConstraints();
764
                        }
765
                        setComponentEnabled(this, true);
766
                        refreshControls();
767

    
768
//                        String mode = (this.layer.getLabelingStrategy() instanceof AttrInTableLabeling)
769
//                                ? ATTRS_IN_TABLE_ITEM : USER_DEFINED_ITEM;
770
//                        getCmbStrategy().setSelectedItem(mode);
771

    
772
                        if (strategy!=null)
773
                                getCmbStrategy().setSelectedItem(new LabelingStrategyItem("", strategy));
774
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
775
                        evt.setSource(chkApplyLabels);
776
                        actionPerformed(evt);
777

    
778
                } else {
779
                        setComponentEnabled(this, false);
780
                }
781
        }
782

    
783
        private void setMethod(ILabelingMethod labelingMethod) {
784
                ArrayList<ILabelingMethod> aux = new ArrayList<ILabelingMethod>();
785
                for (int i = 0; i < getCmbMethod().getItemCount(); i++) {
786
                        aux.add(((MethodItem) getCmbMethod().getItemAt(i)).method);
787
                }
788
                int i = aux.indexOf(labelingMethod);
789
                if (i == -1)
790
                        i = 0; // index of the simplest method: EquallyLabeled (must be installed)
791
                getCmbMethod().setSelectedIndex(i);
792

    
793

    
794
        }
795

    
796
        private ILabelingMethod getMethod() {
797
                return ((MethodItem) getCmbMethod().getSelectedItem()).method;
798
        }
799

    
800
        private void refreshControls() {
801
                if (layer == null) return;
802

    
803
                // enables labeling
804
                getChkApplyLabels().setSelected(layer.isLabeled());
805

    
806
                // fires an event from the method's combobox
807
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
808

    
809
                // expressions combobox
810
                refreshCmbExpressions();
811

    
812
                // When the attributes are in the table -----
813
                //      field with the text
814
                refreshCmbTextField();
815

    
816
                //      field with the rotation
817
                refreshCmbRotationField();
818

    
819
                //      field with the text height or the text size
820
                refreshTextHeight();
821

    
822
                //                the text size unit name
823
                refreshCmbUnits();
824

    
825
        }
826

    
827

    
828
        private void refreshCmbUnits() {
829
                getCmbUnits().setSelectedUnitIndex(Project.getDefaultDistanceUnits());
830

    
831
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
832
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
833
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
834
                }
835
        }
836

    
837
        private void refreshTextHeight() {
838
                getCmbHeightField().removeAllItems();
839
                for (int i = 0; i < numericFieldNames.length; i++) {
840
                        getCmbHeightField().addItem(numericFieldNames[i]);
841
                }
842

    
843
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
844
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
845
                        try {
846
                                getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
847
                                getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
848
                                getRdBtnHeightField().setSelected(!aux.usesFixedSize());
849

    
850
                                String item = aux.getHeightField();
851
                                getCmbHeightField().setSelectedItem(item);
852

    
853
                        } catch (ReadDriverException e) {
854
                                // should never happen
855
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_height_field"), e);
856
                        }
857
                }
858
        }
859

    
860
        private void refreshCmbRotationField() {
861
                getCmbRotationField().removeAllItems();
862
                getCmbRotationField().addItem(NO_FIELD_ITEM);
863
                for (int i = 0; i < numericFieldNames.length; i++) {
864
                        getCmbRotationField().addItem(numericFieldNames[i]);
865
                }
866

    
867
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
868
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
869
                        try {
870
                                String item = aux.getRotationField();
871
                                getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
872
                        } catch (ReadDriverException e) {
873
                                // should never happen
874
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_rotation_field"), e);
875
                        }
876
                }
877
        }
878

    
879
        private void refreshCmbTextField() {
880
                getCmbTextField().removeAllItems();
881
                for (int i = 0; i < fieldNames.length; i++) {
882
                        getCmbTextField().addItem(fieldNames[i]);
883
                }
884

    
885
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
886
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
887
                        try {
888
                                String item = aux.getTextField();
889
                                getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
890
                        } catch (ReadDriverException e) {
891
                                // should never happen
892
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_field"), e);
893
                        }
894
                }
895
        }
896

    
897

    
898
        private void refreshCmbExpressions() {
899

    
900
                ArrayList<String> exp = getExpressions();
901
                noEvent = true;
902
                getCmbExpressions().removeAllItems();
903
                for (int i = 0; i < exp.size(); i++)
904
                        getCmbExpressions().addItem(exp.get(i));
905
                LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
906
                noEvent = false;
907
                if (lc != null) {
908
                        int index = exp.indexOf(lc.getLabelExpression());
909
                        getCmbExpressions().setSelectedIndex(index < 0? 0: index);
910
                }
911

    
912

    
913
        }
914

    
915
        private ArrayList<String> getExpressions() {
916
                if (expressions == null) {
917
                        expressions = new ArrayList<String>();
918
                        try {
919
                                for (int i = 0; i < fieldNames.length; i++) {
920
                                        expressions.add("[ "+fieldNames[i]+" ]");
921
                                }
922
                        } catch (Exception e) {
923
                                NotificationManager.addError(
924
                                                PluginServices.getText(
925
                                                                this, "could_not_retreive_layer_names"), e);
926
                        }
927
                }
928
                return expressions;
929
        }
930

    
931
        public void actionPerformed(ActionEvent e) {
932
                if (noEvent) return;
933
                JComponent c = (JComponent)e.getSource();
934

    
935
                if (c.equals(chkApplyLabels)) {
936
                        boolean b = chkApplyLabels.isSelected();
937
                        // enables/disables all components
938
                        for (int i = 0; i < content.getComponentCount(); i++) {
939
                                Component c1 = content.getComponent(i);
940
                                if (!c1.equals(c))
941
                                        setComponentEnabled(c1, b);
942
                        }
943
//                        if (b) actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
944

    
945
                } else if (c.equals(btnAddClass)) {
946
                        LabelClass newClass = new LabelClass();
947
                        newClass.setName(PluginServices.getText(this, "labeling")+String.valueOf(++newClassSuffix));
948
                        getMethod().addLabelClass(newClass);
949
                        refreshCmbClasses();
950
                        getCmbClasses().setSelectedItem(newClass);
951
                } else if (c.equals(btnDelClass)) {
952
                        LabelClass clazz = getActiveClass();
953
                        getMethod().deleteLabelClass(clazz);
954
                        refreshCmbClasses();
955
                } else if (c.equals(btnEditExpression)) {
956
                        ExprEditorPanel ep = new ExprEditorPanel((String) getCmbExpressions().
957
                                        getSelectedItem());
958
                        PluginServices.getMDIManager().addWindow( ep );
959
                        getCmbExpressions().addItem(ep.getExpression());
960
                        getCmbExpressions().setSelectedItem(ep.getExpression());
961
                } else if (c.equals(btnFont)) {
962
                        Font newFont = FontChooser.showDialog(PluginServices.getText(this, "font"), labelFont);
963
                if (newFont != null) {
964
                    labelFont = newFont;
965
                }
966

    
967
                } else if (c.equals(btnLabelStyles)) {
968
                        LabelClass clazz = getActiveClass();
969
                        // here open symbol selector
970

    
971
                        StyleSelector stySel = new StyleSelector(
972
                                        clazz.getLabelStyle(),
973
                                        FShape.TEXT );
974
                        PluginServices.getMDIManager().addWindow(stySel);
975
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
976
                        if (sty != null)
977
                                // gather the symbol and apply to the class
978
                                clazz.setLabelStyle(sty);
979
                } else if (c.equals(btnPlacement)) {
980

    
981
                        try {
982
                                PlacementProperties pp = new PlacementProperties(
983
                                                (FLyrVect) layer,
984
                                                getPlacementConstraints());
985
                                PluginServices.getMDIManager().addWindow(pp);
986
                                placementConstraints = pp.getPlacementConstraints();
987
                        } catch (ClassCastException ccEx) {
988
                                NotificationManager.addError("Placement constraints not prepared for:"
989
                                                +layer.getClass().getName(),
990
                                                ccEx);
991
                        } catch (ReadDriverException dEx) {
992
                                NotificationManager.addWarning("Should be unreachable code", dEx);
993
                                NotificationManager.addError(PluginServices.getText(this, "usupported_layer_type"), dEx);
994
                        }
995

    
996
                } else if (c.equals(btnRenameClass)) {
997
                        LabelClass clazz = getActiveClass();
998
                        String newName = JOptionPane.showInputDialog(
999
                                        PluginServices.getText(this, "enter_new_name"));
1000
                        if (newName != null)
1001
                                getMethod().renameLabelClass(clazz, newName);
1002
                        refreshCmbClasses();
1003
                } else if (c.equals(btnSQLQuery)) {
1004

    
1005
                } else if (c.equals(btnVisualization)) {
1006

    
1007
                } else if (c.equals(chkLabel)) {
1008
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
1009
                        if (lc == null)
1010
                                lc = getMethod().getDefaultLabelClass();
1011
                        lc.setVisible(chkLabel.isSelected());
1012
                } else if (c.equals(chkTextOnly)) {
1013

    
1014
                } else if (c.equals(cmbClasses)) {
1015
                        // refresh expressions
1016
                        LabelClass lc = (LabelClass) cmbClasses.getSelectedItem();
1017
                        if (lc != null && lc.getLabelExpression() != null && !getExpressions().contains(lc.getLabelExpression())) {
1018
                                getExpressions().add(0, lc.getLabelExpression());
1019
                        }
1020
                        refreshCmbExpressions();
1021
                } else if (c.equals(cmbExpressions)) {
1022
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
1023
                        if (lc == null)
1024
                                lc = getMethod().getDefaultLabelClass();
1025
                        lc.setLabelExpression((String) cmbExpressions.getSelectedItem());
1026
                } else if (c.equals(cmbMethod)) {
1027
                        // disable components in class panel
1028
                        // multiple class or not enables or disables the class panel
1029
                        setComponentEnabled(classesPanel, getMethod().allowsMultipleClass());
1030
                        refreshCmbClasses();
1031
                } else if (c.equals(btnSymbol)) {
1032
                        LabelClass clazz = getActiveClass();
1033
                        // here open symbol selector
1034

    
1035
                        ISymbolSelector symSel = SymbolSelector.createSymbolSelector(clazz.getLabelSymbol(),
1036
                                        FShape.TEXT,
1037
                                        new SelectorFilter() {
1038
                                                public boolean accepts(Object obj) {
1039
                                                        if (obj instanceof ISymbol) {
1040
                                                                ISymbol sym = (ISymbol) obj;
1041
                                                                return sym.getSymbolType() == FShape.TEXT;
1042
                                                        }
1043
                                                        return false;
1044
                                                }
1045
                                        });
1046
                        PluginServices.getMDIManager().addWindow(symSel);
1047
                        ISymbol sym = (ISymbol) symSel.getSelectedObject();
1048
                        if (sym != null)
1049
                                // gather the symbol and apply to the class
1050
                                clazz.setSymbol((ITextSymbol) symSel.getSelectedObject());
1051
                } else if (c.equals(cmbStrategy)) {
1052
                        LabelingStrategyItem it = (LabelingStrategyItem) cmbStrategy.getSelectedItem();
1053
                        if (it!=null) {
1054
                                remove(content);
1055
                                ILabelingStrategy selectedItem = it.strategy;
1056
                                if (selectedItem.getClass().equals(GeneralLabelingStrategy.class)) {
1057

    
1058
                                        content.remove(getTableDefinedContent());
1059
                                        content.add(getUserDefinedContent());
1060
                                } else {
1061

    
1062
                                        content.remove(getUserDefinedContent());
1063
                                        content.add(getTableDefinedContent());
1064
                                }
1065
                                add(content, BorderLayout.CENTER);
1066
                                actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
1067
                                revalidate();
1068
                                paintImmediately(getBounds());
1069
                        }
1070
                }
1071
        }
1072

    
1073
        private void refreshCmbClasses() {
1074
                // label classes
1075
                getCmbClasses().removeAllItems();
1076
                LabelClass[] lClasses = getMethod().getLabelClasses();
1077
                for (int i = 0; i < lClasses.length; i++) {
1078
                        getCmbClasses().addItem(lClasses[i]);
1079
                }
1080

    
1081
//                refreshCmbExpressions();
1082
        }
1083

    
1084
        private LabelClass getActiveClass() {
1085
                return (LabelClass) getCmbClasses().getSelectedItem();
1086
        }
1087

    
1088
        public void acceptAction() {
1089

    
1090
        }
1091

    
1092
        public void cancelAction() {
1093

    
1094
        }
1095

    
1096
        public void applyAction() {
1097
                if (layer != null) { // in other case the layer is not labelable
1098
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
1099
                        ILabelingStrategy selectedItem = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategy;
1100
                        if (selectedItem.getClass().equals(GeneralLabelingStrategy.class)) {
1101
                                // user selected an automated labeling strategy
1102
                                try {
1103
                                        layer.setLabelingStrategy(
1104
                                                        LabelingFactory.
1105
                                                        createStrategy((FLayer) layer,
1106
                                                                        getMethod(),
1107
                                                                        getPlacementConstraints(),
1108
                                                                        getZoomConstraints()));
1109
                                } catch (ReadDriverException e) {
1110
                                        NotificationManager.addError(
1111
                                                        PluginServices.getText(
1112
                                                                        this, "failed_creating_labeling_strategy"), e);
1113
                                }
1114
                        } else if (selectedItem.getClass().equals(AttrInTableLabeling.class)) {
1115
                                // user selected to define each label attributes from values
1116
                                // contained in the table for each feature row.
1117

    
1118
                                double fixedSize;
1119
                                try {
1120
                                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
1121
                                } catch (Exception e) {
1122
                                        fixedSize = 10;
1123
                                }
1124
                                AttrInTableLabeling strategy = new AttrInTableLabeling();
1125
                                strategy.setLayer((FLyrVect) layer);
1126
                                strategy.setHeightField(
1127
                                                (String) getCmbHeightField().getSelectedItem());
1128
                                strategy.setRotationField(
1129
                                                (String) getCmbRotationField().getSelectedItem());
1130
                                strategy.setTextField(
1131
                                                (String) getCmbTextField().getSelectedItem());
1132
                                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
1133
                                strategy.setFixedSize(fixedSize);
1134
                                // add more attributes for AttrInTableLabeling (if you need) here
1135

    
1136

    
1137
                                double unitFactor;
1138
                                /*try {
1139
                                        unitFactor = getCmbUnits().getUnitConversionFactor();
1140
                                } catch (ArrayIndexOutOfBoundsException aioobEx) { //jijiji
1141
                                        unitFactor = 0; // which represents size in pixel
1142
                                }
1143
                                strategy.setUnitFactor(unitFactor);
1144
                                */
1145
                                strategy.setUnit(getCmbUnits().getSelectedIndex());
1146
                                layer.setLabelingStrategy(strategy);
1147
                        }
1148
                }
1149
        }
1150

    
1151
        private IZoomConstraints getZoomConstraints() {
1152
                return zoomConstraints;
1153
        }
1154

    
1155
        private IPlacementConstraints getPlacementConstraints() {
1156
                return placementConstraints;
1157
        }
1158

    
1159
        public String getName() {
1160
                return PluginServices.getText(this,"Etiquetados");
1161
        }
1162
}