Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / LabelingManager.java @ 12805

History | View | Annotate | Download (34.9 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.cit.gvsig.project.documents.layout.Attributes;
102
import com.iver.utiles.swing.JComboBox;
103

    
104
/**
105
 *
106
 * @author jaume dominguez faus - jaume.dominguez@iver.es
107
 *
108
 */
109
public class LabelingManager extends AbstractThemeManagerPage implements ActionListener {
110
        private static final String NO_FIELD_ITEM = "-- " +
111
                        PluginServices.getText(LabelingManager.class, "none") + " --";
112
        private static int newClassSuffix = 0;
113
        private static Hashtable methods = new Hashtable();
114
        private static Hashtable strategies = new Hashtable();
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 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 ActionListener action = new ActionListener() {
196
                        public void actionPerformed(ActionEvent e) {
197
                                if ("OK".equals(e.getActionCommand())) {
198
                                        if (validateExpr())
199
                                                PluginServices.getMDIManager().closeWindow(
200
                                                                ExprEditorPanel.this);
201
                                        else
202
                                                JOptionPane.showMessageDialog(ExprEditorPanel.this,
203
                                                                PluginServices.getText(this, "syntax_error"),
204
                                                                PluginServices.getText(this, "error"),
205
                                                                JOptionPane.ERROR_MESSAGE);
206
                                } else {
207
                                        getTxtExpression().setText(lastExtression);
208
                                        PluginServices.getMDIManager().closeWindow(
209
                                                        ExprEditorPanel.this);
210
                                }
211
                        }
212

    
213

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

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

    
224
                }
225

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

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

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

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

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

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

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

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

    
282
        }
283

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

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

    
296
                }
297
                return pnlNorth;
298
        }
299

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

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

    
313
                                        aux.add(newItem);
314

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

    
324
                        }
325
                        cmbStrategy = new JComboBox(aux.toArray());
326
                        cmbStrategy.setName("CMBMODE");
327
                        cmbStrategy.addActionListener(this);
328

    
329
                }
330

    
331
                return cmbStrategy;
332
        }
333

    
334
        private JPanel getTableDefinedContent() {
335
                if (tableDefinedContent == null) {
336
                        tableDefinedContent = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
337
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
338
                        aux.addComponent(PluginServices.getText(this, "text_field") + ":", getCmbTextField());
339
                        aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
340
                        aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
341
                        aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
342
                        aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
343
                        tableDefinedContent.add(aux);
344

    
345

    
346
                        ButtonGroup group = new ButtonGroup();
347
                        group.add(getRdBtnFixedHeight());
348
                        group.add(getRdBtnHeightField());
349

    
350
                        getRdBtnHeightField().setEnabled(true);
351
                }
352

    
353
                return tableDefinedContent;
354
        }
355

    
356
        private JTextField getTxtHeightField() {
357
                if (txtHeightField == null) {
358
                        txtHeightField = new JTextField(10);
359
                        txtHeightField.setText("10");
360
                        txtHeightField.setName("TXTHEIGHTFIELD");
361
                }
362

    
363
                return txtHeightField;
364
        }
365

    
366
        private JRadioButton getRdBtnFixedHeight() {
367
                if (rdBtnFixedHeight == null) {
368
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
369
                        rdBtnFixedHeight.addActionListener(this);
370
                        rdBtnFixedHeight.setSelected(false);
371
                        rdBtnFixedHeight.setName("RDFIXEDHEIGHT");
372
                }
373

    
374
                return rdBtnFixedHeight;
375
        }
376

    
377
        private JRadioButton getRdBtnHeightField() {
378
                if (rdBtnHeightField == null) {
379
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
380
                        rdBtnHeightField.addActionListener(this);
381
                        rdBtnHeightField.setSelected(true);
382
                        rdBtnHeightField.setName("RDHEIGHTFIELD");
383
                }
384

    
385
                return rdBtnHeightField;
386
        }
387

    
388
        private JUnitsComboBox getCmbUnits() {
389
                if (cmbUnits == null) {
390
                        cmbUnits = new JUnitsComboBox();
391
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
392
                        cmbUnits.setName("CMBUNITS");
393
                }
394

    
395
                return cmbUnits;
396
        }
397

    
398
        private JComboBox getCmbRotationField() {
399
                if (cmbRotationField == null) {
400
                        cmbRotationField = new JComboBox();
401
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
402
                        cmbRotationField.addActionListener(this);
403
                        cmbRotationField.setName("CMBROTATIONFIELD");
404
                }
405
                return cmbRotationField;
406
        }
407

    
408
        private JComboBox getCmbHeightField() {
409
                if (cmbHeightField == null) {
410
                        cmbHeightField = new JComboBox();
411
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
412
                        cmbHeightField.setName("CMBHEIGHTFIELD");
413
                        cmbHeightField.addActionListener(this);
414
                }
415
                return cmbHeightField;
416
        }
417

    
418
        private JComboBox getCmbTextField() {
419
                if (cmbTextField == null) {
420
                        cmbTextField = new JComboBox();
421
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
422
                        cmbTextField.setName("CMBTEXTFIELD");
423
                        cmbTextField.addActionListener(this);
424
                }
425
                return cmbTextField;
426
        }
427

    
428
        private JPanel getUserDefinedContent() {
429
                if (userDefinedContent == null) {
430
                        userDefinedContent = new GridBagLayoutPanel();
431
                        userDefinedContent.addComponent(getChkApplyLabels());
432
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
433
                        aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
434
                        aux.add(getCmbMethod());
435
                        aux.setPreferredSize(new Dimension(530, 40));
436
                        userDefinedContent.addComponent(aux);
437

    
438

    
439
                        classesPanel = new GridBagLayoutPanel();
440
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
441
                        aux.add(new JLabel(PluginServices.getText(this, "class")+":"));
442
                        aux.add(getCmbClasses());
443
                        aux.add(getChkLabelFeatures());
444
                        aux.setPreferredSize(new Dimension(530, 40));
445
                        classesPanel.addComponent(aux);
446

    
447
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
448
                        aux.add(getBtnAddClass());
449
                        aux.add(getBtnDelClass());
450
                        aux.add(getBtnRenameClass());
451
                        aux.add(getBtnSQLQuery());
452
                        aux.setPreferredSize(new Dimension(530, 30));
453
                        classesPanel.addComponent(aux);
454
                        classesPanel.setBorder(BorderFactory.createTitledBorder(
455
                                        null, PluginServices.getText(this, "classes")));
456
                        userDefinedContent.addComponent(classesPanel);
457

    
458

    
459
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
460
                        aux.setBorder(BorderFactory.createTitledBorder(
461
                                        null, PluginServices.getText(this, "text_string")));
462
                        aux.add(new JLabel(PluginServices.getText(this, "expression")+":"));
463
                        aux.add(getCmbExpressions());
464
                        aux.add(getBtnEditExpression());
465
                        aux.setPreferredSize(new Dimension(542, 60));
466
                        userDefinedContent.addComponent(aux);
467

    
468

    
469
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
470
                        aux.setBorder(BorderFactory.createTitledBorder(
471
                                        null, PluginServices.getText(this, "text_symbol")));
472
                        aux.add(getSymbolPreviewer());
473
                        JPanel aux2 = new JPanel();
474
                        aux2.add(getBtnFont());
475
                        aux2.add(getBtnSymbol());
476

    
477
                        aux2.setLayout(new GridLayout(aux2.getComponentCount(), 1));
478
                        aux.add(aux2);
479
                        aux.setPreferredSize(new Dimension(542, 100));
480
                        userDefinedContent.addComponent(aux);
481

    
482

    
483
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
484
                        aux.setBorder(BorderFactory.createTitledBorder(
485
                                        null, PluginServices.getText(this, "options")));
486
                        aux.add(getBtnVisualization());
487
                        aux.add(getBtnPlacement());
488
                        aux.add(getBtnLabelStyles());
489
                        aux.setPreferredSize(new Dimension(542, 60));
490
                        userDefinedContent.addComponent(aux);
491
                }
492
                return userDefinedContent;
493
        }
494

    
495
        private Component getBtnSymbol() {
496
                if (btnSymbol == null) {
497
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
498
                        btnSymbol.setName("BTNSYMBOL");
499
                        btnSymbol.addActionListener(this);
500
                }
501

    
502
                return btnSymbol;
503
        }
504

    
505
        private JCheckBox getChkApplyLabels() {
506
                if (chkApplyLabels == null) {
507
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labelling"));
508
                        chkApplyLabels.setName("CHKAPPLYLABELS");
509
                        chkApplyLabels.addActionListener(this);
510
                }
511
                return chkApplyLabels;
512
        }
513

    
514
        public static void addLabelingMethod(String name, Class iLabelingMethodClass) {
515
                methods.put(name, iLabelingMethodClass);
516
        }
517

    
518
        public static void addLabelingStrategy(String name, Class iLabelingStrategyClass) {
519
                strategies.put(name, iLabelingStrategyClass);
520
        }
521

    
522
        private JButton getBtnVisualization() {
523
                if (btnVisualization == null) {
524
                        btnVisualization = new JButton(
525
                                        PluginServices.getText(this, "visualization")+"...");
526
                        btnVisualization.setName("BTNVISUALIZATION");
527
                        btnVisualization.addActionListener(this);
528
                }
529
                return btnVisualization;
530
        }
531

    
532
        private JButton getBtnLabelStyles() {
533
                if (btnLabelStyles == null) {
534
                        btnLabelStyles = new JButton(
535
                                        PluginServices.getText(this, "label_styles")+"...");
536
                        btnLabelStyles.setName("BTNLABELSTYLES");
537
                        btnLabelStyles.addActionListener(this);
538
                }
539
                return btnLabelStyles;
540
        }
541

    
542
        private JButton getBtnPlacement() {
543
                if (btnPlacement == null) {
544
                        btnPlacement = new JButton(
545
                                        PluginServices.getText(this, "placement")+"...");
546
                        btnPlacement.setName("BTNPLACEMENT");
547
                        btnPlacement.addActionListener(this);
548
                }
549
                return btnPlacement;
550
        }
551

    
552
        private JButton getBtnFont() {
553
                if (btnFont == null) {
554
                        btnFont = new JButton(
555
                                        PluginServices.getText(this,"fuente")+"...");
556
                        btnFont.setName("BTNFONT");
557
                        btnFont.addActionListener(this);
558
                }
559
                return btnFont;
560
        }
561

    
562
        private SymbolPreviewer getSymbolPreviewer() {
563
                if (previewer == null) {
564
                        previewer = new SymbolPreviewer();
565
                        previewer.setPreferredSize(new Dimension(420, 65));
566
                }
567
                return previewer;
568
        }
569

    
570
        private JComboBox getCmbMethod() {
571
                if (cmbMethod == null) {
572
                        Iterator it = methods.keySet().iterator();
573
                        ArrayList aux = new ArrayList();
574
                        while (it.hasNext()) {
575
                                String name = (String) it.next();
576

    
577
                                Class methodClass = (Class) methods.get(name);
578
                            ILabelingMethod method;
579
                                try {
580
                                        method = (ILabelingMethod) methodClass.newInstance();
581
                                        MethodItem newItem = new MethodItem(name, method);
582
                                        aux.add(newItem);
583

    
584
                                } catch (InstantiationException e) {
585
                                        NotificationManager.addError("Trying to instantiate an interface" +
586
                                                        " or abstract class + "+methodClass.getName(), e);
587
                                } catch (IllegalAccessException e) {
588
                                        NotificationManager.addError("IllegalAccessException: does " +
589
                                                        methodClass.getName()        + " class have an anonymous" +
590
                                                                        " constructor?", e);
591
                                }
592

    
593
                        }
594
                        cmbMethod = new JComboBox(aux.toArray());
595
                        cmbMethod.setSize(new Dimension(300, 22));
596
                        cmbMethod.setName("CMBMETHOD");
597
                        cmbMethod.addActionListener(this);
598
                }
599
                return cmbMethod;
600
        }
601

    
602
        private JButton getBtnEditExpression() {
603
                if (btnEditExpression == null) {
604
                        btnEditExpression = new JButton(
605
                                        PluginServices.getText(this, "edit_expression")+"...");
606
                        btnEditExpression.setName("BTNEDITEXPRESSION");
607
                        btnEditExpression.addActionListener(this);
608
                }
609
                return btnEditExpression;
610
        }
611

    
612
        private JComboBox getCmbExpressions() {
613
                if (cmbExpressions == null) {
614
                        cmbExpressions = new JComboBox();
615
                        cmbExpressions.setPreferredSize(new Dimension(150, 20));
616
                        cmbExpressions.setName("CMBEXPRESSIONS");
617
                        cmbExpressions.addActionListener(this);
618
                }
619
                return cmbExpressions;
620

    
621
        }
622

    
623
        private JButton getBtnSQLQuery() {
624
                if (btnSQLQuery == null) {
625
                        btnSQLQuery = new JButton(PluginServices.getText(this, "SQL_query"));
626
                        btnSQLQuery.setName("BTNSQLQUERY");
627
                        btnSQLQuery.addActionListener(this);
628
                }
629
                return btnSQLQuery;
630
        }
631

    
632
        private JButton getBtnRenameClass() {
633
                if (btnRenameClass == null) {
634
                        btnRenameClass = new JButton(PluginServices.getText(this, "remane_class"));
635
                        btnRenameClass.setName("BTNRENAMECLASS");
636
                        btnRenameClass.addActionListener(this);
637
                }
638
                return btnRenameClass;
639
        }
640

    
641
        private JButton getBtnDelClass() {
642
                if (btnDelClass == null) {
643
                        btnDelClass = new JButton(PluginServices.getText(this, "delete_class"));
644
                        btnDelClass.setName("BTNDELCLASS");
645
                        btnDelClass.addActionListener(this);
646
                }
647
                return btnDelClass;
648
        }
649

    
650
        private JButton getBtnAddClass() {
651
                if (btnAddClass == null) {
652
                        btnAddClass = new JButton(PluginServices.getText(this, "add_class"));
653
                        btnAddClass.setName("BTNADDCLASS");
654
                        btnAddClass.addActionListener(this);
655
                }
656
                return btnAddClass;
657
        }
658

    
659
        private JCheckBox getChkLabelFeatures() {
660
                if (chkLabel == null) {
661
                        chkLabel = new JCheckBox();
662
                        chkLabel.setText(PluginServices.getText(this, "label_features_in_this_class"));
663
                        chkLabel.setName("CHKLABEL");
664
                        chkLabel.addActionListener(this);
665
                }
666
                return chkLabel;
667
        }
668

    
669
        private JComboBox getCmbClasses() {
670
                if (cmbClasses == null) {
671
                        cmbClasses = new JComboBox();
672
                        cmbClasses.setPreferredSize(new Dimension(150, 20));
673
                        cmbClasses.setName("CMBCLASSES");
674
                        cmbClasses.addActionListener(this);
675
                }
676
                return cmbClasses;
677
        }
678

    
679
        private void setComponentEnabled(Component c, boolean b) {
680
                if (c instanceof JComponent) {
681
                        JComponent c1 = (JComponent) c;
682
                        for (int i = 0; i < c1.getComponentCount(); i++) {
683
                                setComponentEnabled(c1.getComponent(i), b);
684
                        }
685
                }
686
                c.setEnabled(b);
687
        }
688

    
689

    
690
        public void setModel(FLayer layer) throws IllegalArgumentException {
691
                if (layer instanceof ILabelable) {
692
                        // to allow the labeling of non-FLyrVect layers
693
                        if (layer instanceof FLyrVect) {
694
                                FLyrVect lv = (FLyrVect) layer;
695
                                try {
696
                                        fieldNames = lv.getRecordset().getFieldNames();
697

    
698
                                        // detect the numeric fields
699
                                        ArrayList l = new ArrayList();
700
                                        for (int i = 0; i < fieldNames.length; i++) {
701
                                                switch (lv.getRecordset().getFieldType(i)) {
702
                                                case Types.DECIMAL:
703
                                                case Types.NUMERIC:
704
                                                case Types.FLOAT:
705
                                                case Types.DOUBLE:
706
                                                case Types.INTEGER:
707
                                                case Types.REAL:
708
                                                case Types.SMALLINT:
709
                                                case Types.TINYINT:
710
                                                case Types.BIGINT:
711
                                                        l.add(fieldNames[i]);
712
                                                        break;
713
                                                }
714
                                        }
715
                                        numericFieldNames = (String[]) l.toArray(new String[l.size()]);
716
                                } catch (ReadDriverException e) {
717
                                        NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
718
                                }
719

    
720
                        }
721

    
722

    
723
                        this.layer = (ILabelable) layer;
724

    
725
                        // get the labeling strategy
726
                        ILabelingStrategy strategy = this.layer.getLabelingStrategy();
727
                        if (strategy == null) {
728
                                // set a new (simplest) one
729
                                try {
730
                                        strategy = LabelingFactory.createDefaultStrategy(layer);
731
                                } catch (ReadDriverException e) {
732
                                        strategy = null;
733
                                }
734
                        } else {
735
                                setMethod(strategy.getLabelingMethod());
736
                                placementConstraints = strategy.getPlacementConstraints();
737
                        }
738
                        setComponentEnabled(this, true);
739
                        refreshControls();
740

    
741
//                        String mode = (this.layer.getLabelingStrategy() instanceof AttrInTableLabeling)
742
//                                ? ATTRS_IN_TABLE_ITEM : USER_DEFINED_ITEM;
743
//                        getCmbStrategy().setSelectedItem(mode);
744

    
745
                        if (this.layer.getLabelingStrategy()!=null)
746
                                getCmbStrategy().setSelectedItem(this.layer.getLabelingStrategy());
747
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
748
                        evt.setSource(chkApplyLabels);
749
                        actionPerformed(evt);
750

    
751
                } else {
752
                        setComponentEnabled(this, false);
753
                }
754
        }
755

    
756
        private void setMethod(ILabelingMethod labelingMethod) {
757
                ArrayList aux = new ArrayList();
758
                for (int i = 0; i < getCmbMethod().getItemCount(); i++) {
759
                        aux.add(((MethodItem) getCmbMethod().getItemAt(i)).method);
760
                }
761
                int i = aux.indexOf(labelingMethod);
762
                if (i == -1)
763
                        i = 0; // index of the simplest method: EquallyLabeled (must be installed)
764
                getCmbMethod().setSelectedIndex(i);
765

    
766

    
767
        }
768

    
769
        private ILabelingMethod getMethod() {
770
                return ((MethodItem) getCmbMethod().getSelectedItem()).method;
771
        }
772

    
773
        private void refreshControls() {
774
                if (layer == null) return;
775

    
776
                // enables labeling
777
                getChkApplyLabels().setSelected(layer.isLabeled());
778

    
779
                // fires an event from the method's combobox
780
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
781

    
782
                // expressions combobox
783
                refreshCmbExpressions();
784

    
785
                // When the attributes are in the table -----
786
                //      field with the text
787
                refreshCmbTextField();
788

    
789
                //      field with the rotation
790
                refreshCmbRotationField();
791

    
792
                //      field with the text height or the text size
793
                refreshTextHeight();
794

    
795
                //                the text size unit name
796
                refreshCmbUnits();
797

    
798
        }
799

    
800

    
801
        private void refreshCmbUnits() {
802
                getCmbUnits().setSelectedUnitIndex(Project.getDefaultDistanceUnits());
803

    
804
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
805
                        double[] aux2 = Attributes.CHANGE;
806

    
807
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
808
                        /*if (aux.getUnitFactor() == 0) {
809
                                // size in pixels
810
                                getCmbUnits().setSelectedIndex(aux2.length); // pixels
811
                                return;
812
                        }
813

814
                        for (int i = 0; i < aux2.length; i++) {
815
                                if (aux2[i] == aux.getUnitFactor()*100) {
816
                                        getCmbUnits().setSelectedIndex(i);
817
                                        return;
818
                                };
819
                        }*/
820
                        getCmbUnits().setSelectedUnitIndex(aux.getUnit());
821
                }
822
        }
823

    
824
        private void refreshTextHeight() {
825
                getCmbHeightField().removeAllItems();
826
                for (int i = 0; i < numericFieldNames.length; i++) {
827
                        getCmbHeightField().addItem(numericFieldNames[i]);
828
                }
829

    
830
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
831
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
832
                        try {
833
                                getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
834
                                getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
835
                                getRdBtnHeightField().setSelected(!aux.usesFixedSize());
836

    
837
                                String item = aux.getHeightField();
838
                                getCmbHeightField().setSelectedItem(item);
839

    
840
                        } catch (ReadDriverException e) {
841
                                // should never happen
842
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_height_field"), e);
843
                        }
844
                }
845
        }
846

    
847
        private void refreshCmbRotationField() {
848
                getCmbRotationField().removeAllItems();
849
                getCmbRotationField().addItem(NO_FIELD_ITEM);
850
                for (int i = 0; i < numericFieldNames.length; i++) {
851
                        getCmbRotationField().addItem(numericFieldNames[i]);
852
                }
853

    
854
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
855
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
856
                        try {
857
                                String item = aux.getRotationField();
858
                                getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
859
                        } catch (ReadDriverException e) {
860
                                // should never happen
861
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_rotation_field"), e);
862
                        }
863
                }
864
        }
865

    
866
        private void refreshCmbTextField() {
867
                getCmbTextField().removeAllItems();
868
                for (int i = 0; i < fieldNames.length; i++) {
869
                        getCmbTextField().addItem(fieldNames[i]);
870
                }
871

    
872
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
873
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
874
                        try {
875
                                String item = aux.getTextField();
876
                                getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
877
                        } catch (ReadDriverException e) {
878
                                // should never happen
879
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_field"), e);
880
                        }
881
                }
882
        }
883

    
884

    
885
        private void refreshCmbExpressions() {
886

    
887
                ArrayList exp = getExpressions();
888
                noEvent = true;
889
                getCmbExpressions().removeAllItems();
890
                for (int i = 0; i < exp.size(); i++)
891
                        getCmbExpressions().addItem(exp.get(i));
892
                LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
893
                noEvent = false;
894
                if (lc != null) {
895
                        int index = exp.indexOf(lc.getLabelExpression());
896
                        getCmbExpressions().setSelectedIndex(index < 0? 0: index);
897
                }
898

    
899

    
900
        }
901

    
902
        private ArrayList getExpressions() {
903
                if (expressions == null) {
904
                        expressions = new ArrayList();
905
                        try {
906
                                for (int i = 0; i < fieldNames.length; i++) {
907
                                        expressions.add("[ "+fieldNames[i]+" ]");
908
                                }
909
                        } catch (Exception e) {
910
                                NotificationManager.addError(
911
                                                PluginServices.getText(
912
                                                                this, "could_not_retreive_layer_names"), e);
913
                        }
914
                }
915
                return expressions;
916
        }
917

    
918
        public void actionPerformed(ActionEvent e) {
919
                if (noEvent) return;
920
                JComponent c = (JComponent)e.getSource();
921
                
922
                if (c.equals(chkApplyLabels)) {
923
                        boolean b = chkApplyLabels.isSelected();
924
                        // enables/disables all components
925
                        for (int i = 0; i < content.getComponentCount(); i++) {
926
                                Component c1 = content.getComponent(i);
927
                                if (!c1.equals(c))
928
                                        setComponentEnabled(c1, b);
929
                        }
930
//                        if (b) actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
931

    
932
                } else if (c.equals(btnAddClass)) {
933
                        LabelClass newClass = new LabelClass();
934
                        newClass.setName(PluginServices.getText(this, "labeling")+String.valueOf(++newClassSuffix));
935
                        getMethod().addLabelClass(newClass);
936
                        refreshCmbClasses();
937
                        getCmbClasses().setSelectedItem(newClass);
938
                } else if (c.equals(btnDelClass)) {
939
                        LabelClass clazz = getActiveClass();
940
                        getMethod().deleteLabelClass(clazz);
941
                        refreshCmbClasses();
942
                } else if (c.equals(btnEditExpression)) {
943
                        ExprEditorPanel ep = new ExprEditorPanel((String) getCmbExpressions().
944
                                        getSelectedItem());
945
                        PluginServices.getMDIManager().addWindow( ep );
946
                        getCmbExpressions().addItem(ep.getExpression());
947
                        getCmbExpressions().setSelectedItem(ep.getExpression());
948
                } else if (c.equals(btnFont)) {
949
                        Font newFont = FontChooser.showDialog(PluginServices.getText(this, "font"), labelFont);
950
                if (newFont != null) {
951
                    labelFont = newFont;
952
                }
953

    
954
                } else if (c.equals(btnLabelStyles)) {
955
                        LabelClass clazz = getActiveClass();
956
                        // here open symbol selector
957

    
958
                        StyleSelector stySel = new StyleSelector(
959
                                        clazz.getLabelStyle(),
960
                                        FShape.TEXT );
961
                        PluginServices.getMDIManager().addWindow(stySel);
962
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
963
                        if (sty != null)
964
                                // gather the symbol and apply to the class
965
                                clazz.setLabelStyle(sty);
966
                } else if (c.equals(btnPlacement)) {
967

    
968
                        try {
969
                                PlacementProperties pp = new PlacementProperties(
970
                                                (FLyrVect) layer,
971
                                                getPlacementConstraints());
972
                                PluginServices.getMDIManager().addWindow(pp);
973
                                placementConstraints = pp.getPlacementConstraints();
974
                        } catch (ClassCastException ccEx) {
975
                                NotificationManager.addError("Placement constraints not prepared for:"
976
                                                +layer.getClass().getName(),
977
                                                ccEx);
978
                        } catch (ReadDriverException dEx) {
979
                                NotificationManager.addWarning("Should be unreachable code", dEx);
980
                                NotificationManager.addError(PluginServices.getText(this, "usupported_layer_type"), dEx);
981
                        }
982

    
983
                } else if (c.equals(btnRenameClass)) {
984
                        LabelClass clazz = getActiveClass();
985
                        String newName = JOptionPane.showInputDialog(
986
                                        PluginServices.getText(this, "enter_new_name"));
987
                        if (newName != null)
988
                                getMethod().renameLabelClass(clazz, newName);
989
                        refreshCmbClasses();
990
                } else if (c.equals(btnSQLQuery)) {
991

    
992
                } else if (c.equals(btnVisualization)) {
993

    
994
                } else if (c.equals(chkLabel)) {
995
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
996
                        if (lc == null)
997
                                lc = getMethod().getDefaultLabelClass();
998
                        lc.setVisible(chkLabel.isSelected());
999
                } else if (c.equals(chkTextOnly)) {
1000

    
1001
                } else if (c.equals(cmbClasses)) {
1002
                        // refresh expressions
1003
                        LabelClass lc = (LabelClass) cmbClasses.getSelectedItem();
1004
                        if (lc != null && lc.getLabelExpression() != null && !getExpressions().contains(lc.getLabelExpression())) {
1005
                                getExpressions().add(0, lc.getLabelExpression());
1006
                        }
1007
                        refreshCmbExpressions();
1008
                } else if (c.equals(cmbExpressions)) {
1009
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
1010
                        if (lc == null)
1011
                                lc = getMethod().getDefaultLabelClass();
1012
                        lc.setLabelExpression((String) cmbExpressions.getSelectedItem());
1013
                } else if (c.equals(cmbMethod)) {
1014
                        // disable components in class panel
1015
                        // multiple class or not enables or disables the class panel
1016
                        setComponentEnabled(classesPanel, getMethod().allowsMultipleClass());
1017
                        refreshCmbClasses();
1018
                } else if (c.equals(btnSymbol)) {
1019
                        LabelClass clazz = getActiveClass();
1020
                        // here open symbol selector
1021

    
1022
                        SymbolSelector symSel = new SymbolSelector(
1023
                                        clazz.getLabelSymbol(),
1024
                                        FShape.TEXT,
1025
                                        new SelectorFilter() {
1026
                                                public boolean accepts(Object obj) {
1027
                                                        if (obj instanceof ISymbol) {
1028
                                                                ISymbol sym = (ISymbol) obj;
1029
                                                                return sym.getSymbolType() == FShape.TEXT;
1030
                                                        }
1031
                                                        return false;
1032
                                                }
1033
                                        });
1034
                        PluginServices.getMDIManager().addWindow(symSel);
1035
                        ISymbol sym = (ISymbol) symSel.getSelectedObject();
1036
                        if (sym != null)
1037
                                // gather the symbol and apply to the class
1038
                                clazz.setSymbol((ITextSymbol) symSel.getSelectedObject());
1039
                } else if (c.equals(cmbStrategy)) {
1040
                        LabelingStrategyItem it = (LabelingStrategyItem) cmbStrategy.getSelectedItem();
1041
                        if (it!=null) {
1042
                                remove(content);
1043
                                ILabelingStrategy selectedItem = it.strategy;
1044
                                if (selectedItem.getClass().equals(GeneralLabelingStrategy.class)) {
1045

    
1046
                                        content.remove(getTableDefinedContent());
1047
                                        content.add(getUserDefinedContent());
1048
                                } else {
1049

    
1050
                                        content.remove(getUserDefinedContent());
1051
                                        content.add(getTableDefinedContent());
1052
                                }
1053
                                add(content, BorderLayout.CENTER);
1054
                                actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
1055
                                revalidate();
1056
                                paintImmediately(getBounds());
1057
                        }
1058
                }
1059
        }
1060

    
1061
        private void refreshCmbClasses() {
1062
                // label classes
1063
                getCmbClasses().removeAllItems();
1064
                LabelClass[] lClasses = getMethod().getLabelClasses();
1065
                for (int i = 0; i < lClasses.length; i++) {
1066
                        getCmbClasses().addItem(lClasses[i]);
1067
                }
1068

    
1069
//                refreshCmbExpressions();
1070
        }
1071

    
1072
        private LabelClass getActiveClass() {
1073
                return (LabelClass) getCmbClasses().getSelectedItem();
1074
        }
1075

    
1076
        public void acceptAction() {
1077

    
1078
        }
1079

    
1080
        public void cancelAction() {
1081

    
1082
        }
1083

    
1084
        public void applyAction() {
1085
                if (layer != null) { // in other case the layer is not labelable
1086
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
1087
                        ILabelingStrategy selectedItem = ((LabelingStrategyItem) cmbStrategy.getSelectedItem()).strategy;
1088
                        if (selectedItem.getClass().equals(GeneralLabelingStrategy.class)) {
1089
                                // user selected an automated labeling strategy
1090
                                try {
1091
                                        layer.setLabelingStrategy(
1092
                                                        LabelingFactory.
1093
                                                        createStrategy((FLayer) layer,
1094
                                                                        getMethod(),
1095
                                                                        getPlacementConstraints(),
1096
                                                                        getZoomConstraints()));
1097
                                } catch (ReadDriverException e) {
1098
                                        NotificationManager.addError(
1099
                                                        PluginServices.getText(
1100
                                                                        this, "failed_creating_labeling_strategy"), e);
1101
                                }
1102
                        } else if (selectedItem.getClass().equals(AttrInTableLabeling.class)) {
1103
                                // user selected to define each label attributes from values
1104
                                // contained in the table for each feature row.
1105

    
1106
                                double fixedSize;
1107
                                try {
1108
                                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
1109
                                } catch (Exception e) {
1110
                                        fixedSize = 10;
1111
                                }
1112
                                AttrInTableLabeling strategy = new AttrInTableLabeling();
1113
                                strategy.setLayer((FLyrVect) layer);
1114
                                strategy.setHeightField(
1115
                                                (String) getCmbHeightField().getSelectedItem());
1116
                                strategy.setRotationField(
1117
                                                (String) getCmbRotationField().getSelectedItem());
1118
                                strategy.setTextField(
1119
                                                (String) getCmbTextField().getSelectedItem());
1120
                                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
1121
                                strategy.setFixedSize(fixedSize);
1122
                                // add more attributes for AttrInTableLabeling (if you need) here
1123

    
1124

    
1125
                                double unitFactor;
1126
                                /*try {
1127
                                        unitFactor = getCmbUnits().getUnitConversionFactor();
1128
                                } catch (ArrayIndexOutOfBoundsException aioobEx) { //jijiji
1129
                                        unitFactor = 0; // which represents size in pixel
1130
                                }
1131
                                strategy.setUnitFactor(unitFactor);
1132
                                */
1133
                                strategy.setUnit(getCmbUnits().getSelectedIndex());
1134
                                layer.setLabelingStrategy(strategy);
1135
                        }
1136
                }
1137
        }
1138

    
1139
        private IZoomConstraints getZoomConstraints() {
1140
                return zoomConstraints;
1141
        }
1142

    
1143
        private IPlacementConstraints getPlacementConstraints() {
1144
                return placementConstraints;
1145
        }
1146

    
1147
        public String getName() {
1148
                return PluginServices.getText(this,"Etiquetados");
1149
        }
1150
        
1151
        @Override
1152
        public boolean isSuitableFor(FLayer layer) {
1153
                return layer instanceof FLyrVect;
1154
        }
1155
}
1156