Statistics
| Revision:

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

History | View | Annotate | Download (32.2 KB)

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

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

    
72
import org.gvsig.gui.beans.AcceptCancelPanel;
73
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
74
import org.gvsig.gui.beans.swing.JBlank;
75
import org.gvsig.gui.beans.swing.JButton;
76

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

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

    
160

    
161

    
162
        private class MethodItem {
163
                private String name;
164
                private ILabelingMethod method;
165

    
166
                private MethodItem(String name, ILabelingMethod method) {
167
                        this.name = name;
168
                        this.method = method;
169
                }
170

    
171
                public String toString() {
172
                        return name;
173
                }
174
        }
175

    
176
        private class ExprEditorPanel extends JPanel implements IWindow {
177
                private ActionListener action = new ActionListener() {
178
                        public void actionPerformed(ActionEvent e) {
179
                                if ("OK".equals(e.getActionCommand())) {
180
                                        if (validateExpr())
181
                                                PluginServices.getMDIManager().closeWindow(
182
                                                                ExprEditorPanel.this);
183
                                        else
184
                                                JOptionPane.showMessageDialog(ExprEditorPanel.this,
185
                                                                PluginServices.getText(this, "syntax_error"),
186
                                                                PluginServices.getText(this, "error"),
187
                                                                JOptionPane.ERROR_MESSAGE);
188
                                } else {
189
                                        getTxtExpression().setText(lastExtression);
190
                                        PluginServices.getMDIManager().closeWindow(
191
                                                        ExprEditorPanel.this);
192
                                }
193
                        }
194

    
195

    
196
                };
197
                private JTextField txtExpression;
198
                private String lastExtression;
199

    
200
                public ExprEditorPanel(String currentExpression) {
201
                        super();
202
                        initialize();
203
                        lastExtression = currentExpression;
204
                        getTxtExpression().setText(currentExpression);
205

    
206
                }
207

    
208
                private void initialize() {
209
                        GridBagLayoutPanel content = new GridBagLayoutPanel();
210
                        content.addComponent(new JLabel(" "+PluginServices.getText(this, "label_expression_help")));
211
                        content.addComponent(
212
                                        " "+PluginServices.getText(this, "expression")+":",
213
                                        getTxtExpression());
214
                        setLayout(new BorderLayout(5, 5));
215
                        add(content, BorderLayout.CENTER);
216
                        add(new AcceptCancelPanel(action, action), BorderLayout.SOUTH);
217
                        setSize(new Dimension(300, 80));
218
                }
219

    
220
                private JTextField getTxtExpression() {
221
                        if (txtExpression == null) {
222
                                txtExpression = new JTextField(25);
223
                        }
224
                        return txtExpression;
225
                }
226

    
227
                public String getExpression() {
228
                        return getTxtExpression().getText();
229
                }
230

    
231
                public WindowInfo getWindowInfo() {
232
                        WindowInfo viewInfo = new WindowInfo(WindowInfo.MODALDIALOG);
233
                        viewInfo.setWidth(getWidth());
234
                        viewInfo.setHeight(getHeight());
235
                        viewInfo.setTitle(PluginServices.getText(this,"label_expression_editor"));
236
                        return viewInfo;
237
                }
238

    
239
                private boolean validateExpr() {
240
                        // TODO : implement it
241
                        return true;
242
                }
243
        }
244

    
245
        public LabelingManager() {
246
                super();
247
                initialize();
248
        }
249

    
250
        private void initialize() {
251
                setLayout(new BorderLayout());
252
                getUserDefinedContent();
253
                getTableDefinedContent();
254
                content = new JPanel(new BorderLayout());
255
                content.setBorder(BorderFactory.createEtchedBorder());
256
                add(getPnlNorth(), BorderLayout.NORTH);
257
                add(content, BorderLayout.SOUTH);
258
        }
259

    
260
        private JPanel getPnlNorth() {
261
                if (pnlNorth == null) {
262
                        pnlNorth = new JPanel(new BorderLayout(5,5));
263
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
264

    
265
                        aux.add(getChkApplyLabels());
266
                        pnlNorth.add(aux, BorderLayout.NORTH);
267
                        aux = new JPanel(new FlowLayout(FlowLayout.LEFT));
268
                        aux.add(new JLabel(PluginServices.getText(this, "general")+":"));
269
                        aux.add(getCmbMode());
270
                        pnlNorth.add(aux, BorderLayout.CENTER);
271

    
272
                }
273
                return pnlNorth;
274
        }
275

    
276
        private JComboBox getCmbMode() {
277
                if (cmbMode == null) {
278
                        cmbMode = new JComboBox(new String[] {
279
                                        USER_DEFINED_ITEM,
280
                                        ATTRS_IN_TABLE_ITEM,
281
                        });
282
                        cmbMode.addActionListener(this);
283
                }
284

    
285
                return cmbMode;
286
        }
287

    
288
        private JPanel getTableDefinedContent() {
289
                if (tableDefinedContent == null) {
290
                        tableDefinedContent = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
291
                        GridBagLayoutPanel aux = new GridBagLayoutPanel();
292
                        aux.addComponent(PluginServices.getText(this, "text_field") + ":", getCmbTextField());
293
                        aux.addComponent(getRdBtnHeightField(), getCmbHeightField());
294
                        aux.addComponent(getRdBtnFixedHeight(), getTxtHeightField());
295
                        aux.addComponent(PluginServices.getText(this, "rotation_height") + ":", getCmbRotationField());
296
                        aux.addComponent(PluginServices.getText(this, "units") + ":", getCmbUnits());
297
                        tableDefinedContent.add(aux);
298

    
299

    
300
                        ButtonGroup group = new ButtonGroup();
301
                        group.add(getRdBtnFixedHeight());
302
                        group.add(getRdBtnHeightField());
303

    
304
                        getRdBtnHeightField().setEnabled(true);
305
                }
306

    
307
                return tableDefinedContent;
308
        }
309

    
310
        private JTextField getTxtHeightField() {
311
                if (txtHeightField == null) {
312
                        txtHeightField = new JTextField(10);
313
                        txtHeightField.setText("10");
314
                }
315

    
316
                return txtHeightField;
317
        }
318

    
319
        private JRadioButton getRdBtnFixedHeight() {
320
                if (rdBtnFixedHeight == null) {
321
                        rdBtnFixedHeight = new JRadioButton(PluginServices.getText(this, "fixed_height") + ":");
322
                        rdBtnFixedHeight.addActionListener(this);
323
                        rdBtnFixedHeight.setSelected(false);
324
                }
325

    
326
                return rdBtnFixedHeight;
327
        }
328

    
329
        private JRadioButton getRdBtnHeightField() {
330
                if (rdBtnHeightField == null) {
331
                        rdBtnHeightField = new JRadioButton(PluginServices.getText(this, "text_height_field") + ":");
332
                        rdBtnHeightField.addActionListener(this);
333
                        rdBtnHeightField.setSelected(true);
334
                }
335

    
336
                return rdBtnHeightField;
337
        }
338

    
339
        private JComboBox getCmbUnits() {
340
                if (cmbUnits == null) {
341
                        String[] units = new String[Attributes.NAMES.length+1];
342
                        for (int i = 0; i < Attributes.NAMES.length; i++) {
343
                                units[i] = PluginServices.getText(this, Attributes.NAMES[i]);
344
                        }
345
                        units[units.length-1] = PluginServices.getText(this, "pixels");
346
                        cmbUnits = new JComboBox(units);
347
                        cmbUnits.setSelectedIndex(Project.getDefaultDistanceUnits());
348
                }
349

    
350
                return cmbUnits;
351
        }
352

    
353
        private JComboBox getCmbRotationField() {
354
                if (cmbRotationField == null) {
355
                        cmbRotationField = new JComboBox();
356
                        cmbRotationField.setPreferredSize(new Dimension(200, 20));
357
                        cmbRotationField.addActionListener(this);
358
                }
359
                return cmbRotationField;
360
        }
361

    
362
        private JComboBox getCmbHeightField() {
363
                if (cmbHeightField == null) {
364
                        cmbHeightField = new JComboBox();
365
                        cmbHeightField.setPreferredSize(new Dimension(200, 20));
366
                        cmbHeightField.addActionListener(this);
367
                }
368
                return cmbHeightField;
369
        }
370

    
371
        private JComboBox getCmbTextField() {
372
                if (cmbTextField == null) {
373
                        cmbTextField = new JComboBox();
374
                        cmbTextField.setPreferredSize(new Dimension(200, 20));
375
                        cmbTextField.addActionListener(this);
376
                }
377
                return cmbTextField;
378
        }
379

    
380
        private JPanel getUserDefinedContent() {
381
                if (userDefinedContent == null) {
382
                        userDefinedContent = new GridBagLayoutPanel();
383
                        userDefinedContent.addComponent(getChkApplyLabels());
384
                        JPanel aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
385
                        aux.add(new JLabel(PluginServices.getText(this, "method")+":"));
386
                        aux.add(getCmbMethod());
387
                        aux.setPreferredSize(new Dimension(530, 40));
388
                        userDefinedContent.addComponent(aux);
389

    
390

    
391
                        classesPanel = new GridBagLayoutPanel();
392
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5,0));
393
                        aux.add(new JLabel(PluginServices.getText(this, "class")+":"));
394
                        aux.add(getCmbClasses());
395
                        aux.add(getChkLabelFeatures());
396
                        aux.setPreferredSize(new Dimension(530, 40));
397
                        classesPanel.addComponent(aux);
398

    
399
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
400
                        aux.add(getBtnAddClass());
401
                        aux.add(getBtnDelClass());
402
                        aux.add(getBtnRenameClass());
403
                        aux.add(getBtnSQLQuery());
404
                        aux.setPreferredSize(new Dimension(530, 30));
405
                        classesPanel.addComponent(aux);
406
                        classesPanel.setBorder(BorderFactory.createTitledBorder(
407
                                        null, PluginServices.getText(this, "classes")));
408
                        userDefinedContent.addComponent(classesPanel);
409

    
410

    
411
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
412
                        aux.setBorder(BorderFactory.createTitledBorder(
413
                                        null, PluginServices.getText(this, "text_string")));
414
                        aux.add(new JLabel(PluginServices.getText(this, "expression")+":"));
415
                        aux.add(getCmbExpressions());
416
                        aux.add(getBtnEditExpression());
417
                        aux.setPreferredSize(new Dimension(542, 60));
418
                        userDefinedContent.addComponent(aux);
419

    
420

    
421
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
422
                        aux.setBorder(BorderFactory.createTitledBorder(
423
                                        null, PluginServices.getText(this, "text_symbol")));
424
                        aux.add(getSymbolPreviewer());
425
                        JPanel aux2 = new JPanel();
426
                        aux2.add(getBtnFont());
427
                        aux2.add(getBtnSymbol());
428

    
429
                        aux2.setLayout(new GridLayout(aux2.getComponentCount(), 1));
430
                        aux.add(aux2);
431
                        aux.setPreferredSize(new Dimension(542, 100));
432
                        userDefinedContent.addComponent(aux);
433

    
434

    
435
                        aux = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
436
                        aux.setBorder(BorderFactory.createTitledBorder(
437
                                        null, PluginServices.getText(this, "options")));
438
                        aux.add(getBtnVisualization());
439
                        aux.add(getBtnPlacement());
440
                        aux.add(getBtnLabelStyles());
441
                        aux.setPreferredSize(new Dimension(542, 60));
442
                        userDefinedContent.addComponent(aux);
443
                }
444
                return userDefinedContent;
445
        }
446

    
447
        private Component getBtnSymbol() {
448
                if (btnSymbol == null) {
449
                        btnSymbol = new JButton(PluginServices.getText(this, "symbol"));
450
                        btnSymbol.addActionListener(this);
451
                }
452

    
453
                return btnSymbol;
454
        }
455

    
456
        private JCheckBox getChkApplyLabels() {
457
                if (chkApplyLabels == null) {
458
                        chkApplyLabels = new JCheckBox(PluginServices.getText(this, "enable_labelling"));
459
                        chkApplyLabels.addActionListener(this);
460
                }
461
                return chkApplyLabels;
462
        }
463

    
464
        public static void addLabelingMethod(String name, Class iLabelingMethodClass) {
465
                methods.put(name, iLabelingMethodClass);
466
        }
467

    
468
        private JButton getBtnVisualization() {
469
                if (btnVisualization == null) {
470
                        btnVisualization = new JButton(
471
                                        PluginServices.getText(this, "visualization")+"...");
472
                        btnVisualization.addActionListener(this);
473
                }
474
                return btnVisualization;
475
        }
476

    
477
        private JButton getBtnLabelStyles() {
478
                if (btnLabelStyles == null) {
479
                        btnLabelStyles = new JButton(
480
                                        PluginServices.getText(this, "label_styles")+"...");
481
                        btnLabelStyles.addActionListener(this);
482
                }
483
                return btnLabelStyles;
484
        }
485

    
486
        private JButton getBtnPlacement() {
487
                if (btnPlacement == null) {
488
                        btnPlacement = new JButton(
489
                                        PluginServices.getText(this, "placement")+"...");
490
                        btnPlacement.addActionListener(this);
491
                }
492
                return btnPlacement;
493
        }
494

    
495
        private JButton getBtnFont() {
496
                if (btnFont == null) {
497
                        btnFont = new JButton(
498
                                        PluginServices.getText(this,"fuente")+"...");
499
                        btnFont.addActionListener(this);
500
                }
501
                return btnFont;
502
        }
503

    
504
        private SymbolPreviewer getSymbolPreviewer() {
505
                if (previewer == null) {
506
                        previewer = new SymbolPreviewer();
507
                        previewer.setPreferredSize(new Dimension(420, 65));
508
                }
509
                return previewer;
510
        }
511

    
512
        private JComboBox getCmbMethod() {
513
                if (cmbMethod == null) {
514
                        Iterator it = methods.keySet().iterator();
515
                        ArrayList aux = new ArrayList();
516
                        while (it.hasNext()) {
517
                                String name = (String) it.next();
518

    
519
                                Class methodClass = (Class) methods.get(name);
520
                            ILabelingMethod method;
521
                                try {
522
                                        method = (ILabelingMethod) methodClass.newInstance();
523
                                        MethodItem newItem = new MethodItem(name, method);
524
                                        aux.add(newItem);
525

    
526
                                } catch (InstantiationException e) {
527
                                        NotificationManager.addError("Trying to instantiate an interface" +
528
                                                        " or abstract class + "+methodClass.getName(), e);
529
                                } catch (IllegalAccessException e) {
530
                                        NotificationManager.addError("IllegalAccessException: does " +
531
                                                        methodClass.getName()        + " class have an anonymous" +
532
                                                                        " constructor?", e);
533
                                }
534

    
535
                        }
536
                        cmbMethod = new JComboBox(aux.toArray());
537
                        cmbMethod.setSize(new Dimension(300, 22));
538
                        cmbMethod.addActionListener(this);
539
                }
540
                return cmbMethod;
541
        }
542

    
543
        private JButton getBtnEditExpression() {
544
                if (btnEditExpression == null) {
545
                        btnEditExpression = new JButton(
546
                                        PluginServices.getText(this, "edit_expression")+"...");
547
                        btnEditExpression.addActionListener(this);
548
                }
549
                return btnEditExpression;
550
        }
551

    
552
        private JComboBox getCmbExpressions() {
553
                if (cmbExpressions == null) {
554
                        cmbExpressions = new JComboBox();
555
                        cmbExpressions.setPreferredSize(new Dimension(150, 20));
556
                        cmbExpressions.addActionListener(this);
557
                }
558
                return cmbExpressions;
559

    
560
        }
561

    
562
        private JButton getBtnSQLQuery() {
563
                if (btnSQLQuery == null) {
564
                        btnSQLQuery = new JButton(PluginServices.getText(this, "SQL_query"));
565
                        btnSQLQuery.addActionListener(this);
566
                }
567
                return btnSQLQuery;
568
        }
569

    
570
        private JButton getBtnRenameClass() {
571
                if (btnRenameClass == null) {
572
                        btnRenameClass = new JButton(PluginServices.getText(this, "remane_class"));
573
                        btnRenameClass.addActionListener(this);
574
                }
575
                return btnRenameClass;
576
        }
577

    
578
        private JButton getBtnDelClass() {
579
                if (btnDelClass == null) {
580
                        btnDelClass = new JButton(PluginServices.getText(this, "delete_class"));
581
                        btnDelClass.addActionListener(this);
582
                }
583
                return btnDelClass;
584
        }
585

    
586
        private JButton getBtnAddClass() {
587
                if (btnAddClass == null) {
588
                        btnAddClass = new JButton(PluginServices.getText(this, "add_class"));
589
                        btnAddClass.addActionListener(this);
590
                }
591
                return btnAddClass;
592
        }
593

    
594
        private JCheckBox getChkLabelFeatures() {
595
                if (chkLabel == null) {
596
                        chkLabel = new JCheckBox();
597
                        chkLabel.setText(PluginServices.getText(this, "label_features_in_this_class"));
598
                        chkLabel.addActionListener(this);
599
                }
600
                return chkLabel;
601
        }
602

    
603
        private JComboBox getCmbClasses() {
604
                if (cmbClasses == null) {
605
                        cmbClasses = new JComboBox();
606
                        cmbClasses.setPreferredSize(new Dimension(150, 20));
607
                        cmbClasses.addActionListener(this);
608
                }
609
                return cmbClasses;
610
        }
611

    
612
        private void setComponentEnabled(Component c, boolean b) {
613
                if (c instanceof JComponent) {
614
                        JComponent c1 = (JComponent) c;
615
                        for (int i = 0; i < c1.getComponentCount(); i++) {
616
                                setComponentEnabled(c1.getComponent(i), b);
617
                        }
618
                }
619
                c.setEnabled(b);
620
        }
621

    
622

    
623
        public void setModel(FLayer layer) throws IllegalArgumentException {
624
                if (layer instanceof ILabelable) {
625
                        // to allow the labeling of non-FLyrVect layers
626
                        if (layer instanceof FLyrVect) {
627
                                FLyrVect lv = (FLyrVect) layer;
628
                                try {
629
                                        fieldNames = lv.getRecordset().getFieldNames();
630

    
631
                                        // detect the numeric fields
632
                                        ArrayList l = new ArrayList();
633
                                        for (int i = 0; i < fieldNames.length; i++) {
634
                                                switch (lv.getRecordset().getFieldType(i)) {
635
                                                case Types.DECIMAL:
636
                                                case Types.NUMERIC:
637
                                                case Types.FLOAT:
638
                                                case Types.DOUBLE:
639
                                                case Types.INTEGER:
640
                                                case Types.REAL:
641
                                                case Types.SMALLINT:
642
                                                case Types.TINYINT:
643
                                                case Types.BIGINT:
644
                                                        l.add(fieldNames[i]);
645
                                                        break;
646
                                                }
647
                                        }
648
                                        numericFieldNames = (String[]) l.toArray(new String[l.size()]);
649
                                } catch (ReadDriverException e) {
650
                                        NotificationManager.addError(PluginServices.getText(this, "accessing_file_structure"), e);
651
                                }
652

    
653
                        }
654

    
655

    
656
                        this.layer = (ILabelable) layer;
657

    
658
                        // get the labeling strategy
659
                        ILabelingStrategy strategy = this.layer.getLabelingStrategy();
660
                        if (strategy == null) {
661
                                // set a new (simplest) one
662
                                try {
663
                                        strategy = LabelingFactory.createDefaultStrategy(layer);
664
                                } catch (ReadDriverException e) {
665
                                        strategy = null;
666
                                }
667
                        }
668
                        if (strategy != null)
669
                                setMethod(strategy.getLabelingMethod());
670
                        setComponentEnabled(this, true);
671
                        refreshControls();
672

    
673
                        String mode = (this.layer.getLabelingStrategy() instanceof AttrInTableLabeling)
674
                                ? ATTRS_IN_TABLE_ITEM : USER_DEFINED_ITEM;
675
                        getCmbMode().setSelectedItem(mode);
676

    
677
                        ActionEvent evt = new ActionEvent(chkApplyLabels, 0, null);
678
                        evt.setSource(chkApplyLabels);
679
                        actionPerformed(evt);
680

    
681
                } else {
682
                        setComponentEnabled(this, false);
683
                }
684
        }
685

    
686
        private void setMethod(ILabelingMethod labelingMethod) {
687
                ArrayList aux = new ArrayList();
688
                for (int i = 0; i < getCmbMethod().getItemCount(); i++) {
689
                        aux.add(((MethodItem) getCmbMethod().getItemAt(i)).method);
690
                }
691
                int i = aux.indexOf(labelingMethod);
692
                if (i == -1)
693
                        i = 0; // index of the simplest method: EquallyLabeled (must be installed)
694
                getCmbMethod().setSelectedIndex(i);
695

    
696

    
697
        }
698

    
699
        private ILabelingMethod getMethod() {
700
                return ((MethodItem) getCmbMethod().getSelectedItem()).method;
701
        }
702

    
703
        private void refreshControls() {
704
                if (layer == null) return;
705

    
706
                // enables labeling
707
                getChkApplyLabels().setSelected(layer.isLabeled());
708

    
709
                // fires an event from the method's combobox
710
                actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
711

    
712
                // expressions combobox
713
                refreshCmbExpressions();
714

    
715
                // When the attributes are in the table -----
716
                //      field with the text
717
                refreshCmbTextField();
718

    
719
                //      field with the rotation
720
                refreshCmbRotationField();
721

    
722
                //      field with the text height or the text size
723
                refreshTextHeight();
724

    
725
                //                the text size unit name
726
                refreshCmbUnits();
727

    
728
        }
729

    
730

    
731
        private void refreshCmbUnits() {
732
                getCmbUnits().setSelectedIndex(Project.getDefaultDistanceUnits());
733

    
734
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
735
                        double[] aux2 = Attributes.CHANGE;
736

    
737
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
738
                        if (aux.getUnitFactor() < 0) {
739
                                // size in pixels
740
                                getCmbUnits().setSelectedIndex(aux2.length); // pixels
741
                                return;
742
                        }
743

    
744
                        for (int i = 0; i < aux2.length; i++) {
745
                                if (aux2[i] == aux.getUnitFactor()*100) {
746
                                        getCmbUnits().setSelectedIndex(i);
747
                                        return;
748
                                };
749
                        }
750

    
751
                }
752
        }
753

    
754
        private void refreshTextHeight() {
755
                getCmbHeightField().removeAllItems();
756
                for (int i = 0; i < numericFieldNames.length; i++) {
757
                        getCmbHeightField().addItem(numericFieldNames[i]);
758
                }
759

    
760
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
761
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
762
                        try {
763
                                getTxtHeightField().setText(String.valueOf(aux.getFixedSize()));
764
                                getRdBtnFixedHeight().setSelected(aux.usesFixedSize());
765
                                getRdBtnHeightField().setSelected(!aux.usesFixedSize());
766

    
767
                                String item = aux.getHeightField();
768
                                getCmbHeightField().setSelectedItem(item);
769

    
770
                        } catch (ReadDriverException e) {
771
                                // should never happen
772
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_height_field"), e);
773
                        }
774
                }
775
        }
776

    
777
        private void refreshCmbRotationField() {
778
                getCmbRotationField().removeAllItems();
779
                getCmbRotationField().addItem(NO_FIELD_ITEM);
780
                for (int i = 0; i < numericFieldNames.length; i++) {
781
                        getCmbRotationField().addItem(numericFieldNames[i]);
782
                }
783

    
784
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
785
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
786
                        try {
787
                                String item = aux.getRotationField();
788
                                getCmbRotationField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
789
                        } catch (ReadDriverException e) {
790
                                // should never happen
791
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_rotation_field"), e);
792
                        }
793
                }
794
        }
795

    
796
        private void refreshCmbTextField() {
797
                getCmbTextField().removeAllItems();
798
                for (int i = 0; i < fieldNames.length; i++) {
799
                        getCmbTextField().addItem(fieldNames[i]);
800
                }
801

    
802
                if (layer.getLabelingStrategy() instanceof AttrInTableLabeling) {
803
                        AttrInTableLabeling aux = (AttrInTableLabeling) layer.getLabelingStrategy();
804
                        try {
805
                                String item = aux.getTextField();
806
                                getCmbTextField().setSelectedItem(item != null? item : NO_FIELD_ITEM);
807
                        } catch (ReadDriverException e) {
808
                                // should never happen
809
                                NotificationManager.addWarning(PluginServices.getText(this, "could_not_restore_text_field"), e);
810
                        }
811
                }
812
        }
813

    
814

    
815
        private void refreshCmbExpressions() {
816
                ArrayList exp = getExpressions();
817
                getCmbExpressions().removeAllItems();
818
                for (int i = 0; i < exp.size(); i++)
819
                        getCmbExpressions().addItem(exp.get(i));
820
                LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
821
                if (lc != null)
822
                        getCmbExpressions().setSelectedItem(lc.getLabelExpression());
823
        }
824

    
825
        private ArrayList getExpressions() {
826
                if (expressions == null) {
827
                        expressions = new ArrayList();
828
                        try {
829
                                String[] n =  ((FLyrVect) layer).getRecordset().getFieldNames();
830
                                for (int i = 0; i < n.length; i++) {
831
                                        expressions.add("[ "+n[i]+" ]");
832
                                }
833
                        } catch (Exception e) {
834
                                NotificationManager.addError(
835
                                                PluginServices.getText(
836
                                                                this, "could_not_retreive_layer_names"), e);
837
                        }
838
                }
839
                return expressions;
840
        }
841

    
842
        public void actionPerformed(ActionEvent e) {
843
                JComponent c = (JComponent)e.getSource();
844
                if (c.equals(chkApplyLabels)) {
845
                        boolean b = chkApplyLabels.isSelected();
846
                        // enables/disables all components
847
                        for (int i = 0; i < content.getComponentCount(); i++) {
848
                                Component c1 = content.getComponent(i);
849
                                if (!c1.equals(c))
850
                                        setComponentEnabled(c1, b);
851
                        }
852
                        if (b) actionPerformed(new ActionEvent(getCmbMethod(), 0, null));
853

    
854
                } else if (c.equals(btnAddClass)) {
855
                        LabelClass newClass = new LabelClass();
856
                        newClass.setName(PluginServices.getText(this, "labeling")+String.valueOf(++newClassSuffix));
857
                        getMethod().addLabelClass(newClass);
858
                        refreshCmbClasses();
859
                        getCmbClasses().setSelectedItem(newClass);
860
                } else if (c.equals(btnDelClass)) {
861
                        LabelClass clazz = getActiveClass();
862
                        getMethod().deleteLabelClass(clazz);
863
                        refreshCmbClasses();
864
                } else if (c.equals(btnEditExpression)) {
865
                        ExprEditorPanel ep = new ExprEditorPanel((String) getCmbExpressions().
866
                                        getSelectedItem());
867
                        PluginServices.getMDIManager().addWindow( ep );
868
                        getCmbExpressions().addItem(ep.getExpression());
869
                        getCmbExpressions().setSelectedItem(ep.getExpression());
870
                } else if (c.equals(btnFont)) {
871
                        Font newFont = FontChooser.showDialog(PluginServices.getText(this, "font"), labelFont);
872
                if (newFont != null) {
873
                    labelFont = newFont;
874
                }
875

    
876
                } else if (c.equals(btnLabelStyles)) {
877
                        LabelClass clazz = getActiveClass();
878
                        // here open symbol selector
879

    
880
                        StyleSelector stySel = new StyleSelector(
881
                                        clazz.getLabelStyle(),
882
                                        FShape.TEXT );
883
                        PluginServices.getMDIManager().addWindow(stySel);
884
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
885
                        if (sty != null)
886
                                // gather the symbol and apply to the class
887
                                clazz.setLabelStyle(sty);
888
                } else if (c.equals(btnPlacement)) {
889

    
890
                        try {
891
                                PlacementProperties pp = new PlacementProperties(
892
                                                (FLyrVect) layer,
893
                                                getPlacementConstraints());
894
                                PluginServices.getMDIManager().addWindow(pp);
895
                                placementConstraints = pp.getPlacementConstraints();
896
                        } catch (ClassCastException ccEx) {
897
                                NotificationManager.addError("Placement constraints not prepared for:"
898
                                                +layer.getClass().getName(),
899
                                                ccEx);
900
                        } catch (ReadDriverException dEx) {
901
                                NotificationManager.addWarning("Should be unreachable code", dEx);
902
                                NotificationManager.addError(PluginServices.getText(this, "usupported_layer_type"), dEx);
903
                        }
904

    
905
                } else if (c.equals(btnRenameClass)) {
906
                        LabelClass clazz = getActiveClass();
907
                        String newName = JOptionPane.showInputDialog(
908
                                        PluginServices.getText(this, "enter_new_name"));
909
                        if (newName != null)
910
                                getMethod().renameLabelClass(clazz, newName);
911
                        refreshCmbClasses();
912
                } else if (c.equals(btnSQLQuery)) {
913

    
914
                } else if (c.equals(btnVisualization)) {
915

    
916
                } else if (c.equals(chkLabel)) {
917
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
918
                        if (lc == null)
919
                                lc = getMethod().getDefaultLabelClass();
920
                        lc.setVisible(chkLabel.isSelected());
921
                } else if (c.equals(chkTextOnly)) {
922

    
923
                } else if (c.equals(cmbClasses)) {
924
                        // refresh expressions
925
                        LabelClass lc = (LabelClass) cmbClasses.getSelectedItem();
926
                        if (lc != null && !getExpressions().contains(lc.getLabelExpression())) {
927
                                getExpressions().add(0, lc.getLabelExpression());
928
                        }
929
                        refreshCmbExpressions();
930
                } else if (c.equals(cmbExpressions)) {
931
                        LabelClass lc = (LabelClass) getCmbClasses().getSelectedItem();
932
                        if (lc == null)
933
                                lc = getMethod().getDefaultLabelClass();
934
                        lc.setLabelExpression((String) cmbExpressions.getSelectedItem());
935
                } else if (c.equals(cmbMethod)) {
936
                        // disable components in class panel
937
                        // multiple class or not enables or disables the class panel
938
                        setComponentEnabled(classesPanel, getMethod().allowsMultipleClass());
939
                        refreshCmbClasses();
940
                } else if (c.equals(btnSymbol)) {
941
                        LabelClass clazz = getActiveClass();
942
                        // here open symbol selector
943

    
944
                        SymbolSelector symSel = new SymbolSelector(
945
                                        clazz.getLabelSymbol(),
946
                                        FShape.TEXT,
947
                                        new SelectorFilter() {
948
                                                public boolean accepts(Object obj) {
949
                                                        if (obj instanceof ISymbol) {
950
                                                                ISymbol sym = (ISymbol) obj;
951
                                                                return sym.getSymbolType() == FShape.TEXT;
952
                                                        }
953
                                                        return false;
954
                                                }
955
                                        });
956
                        PluginServices.getMDIManager().addWindow(symSel);
957
                        ISymbol sym = (ISymbol) symSel.getSelectedObject();
958
                        if (sym != null)
959
                                // gather the symbol and apply to the class
960
                                clazz.setSymbol((ITextSymbol) symSel.getSelectedObject());
961
                } else if (c.equals(cmbMode)) {
962
                        remove(content);
963
                        if (cmbMode.getSelectedItem().equals(USER_DEFINED_ITEM)) {
964

    
965
                                content.remove(getTableDefinedContent());
966
                                content.add(getUserDefinedContent());
967
                        } else {
968

    
969
                                content.remove(getUserDefinedContent());
970
                                content.add(getTableDefinedContent());
971
                        }
972
                        add(content, BorderLayout.CENTER);
973
                        actionPerformed(new ActionEvent(chkApplyLabels, 0, null));
974
                        revalidate();
975
                        paintImmediately(getBounds());
976
                }
977
        }
978

    
979
        private void refreshCmbClasses() {
980
                // label classes
981
                getCmbClasses().removeAllItems();
982
                LabelClass[] lClasses = getMethod().getLabelClasses();
983
                for (int i = 0; i < lClasses.length; i++) {
984
                        getCmbClasses().addItem(lClasses[i]);
985
                }
986

    
987
                refreshCmbExpressions();
988
        }
989

    
990
        private LabelClass getActiveClass() {
991
                return (LabelClass) getCmbClasses().getSelectedItem();
992
        }
993

    
994
        public void acceptAction() {
995

    
996
        }
997

    
998
        public void cancelAction() {
999

    
1000
        }
1001

    
1002
        public void applyAction() {
1003
                if (layer != null) { // in other case the layer is not labelable
1004
                        layer.setIsLabeled(getChkApplyLabels().isSelected());
1005
                        if (getCmbMode().getSelectedItem().equals(USER_DEFINED_ITEM)) {
1006
                                // user selected an automated labeling strategy
1007
                                try {
1008
                                        layer.setLabelingStrategy(
1009
                                                        LabelingFactory.
1010
                                                        createStrategy((FLayer) layer,
1011
                                                                        getMethod(),
1012
                                                                        getPlacementConstraints(),
1013
                                                                        getZoomConstraints()));
1014
                                } catch (ReadDriverException e) {
1015
                                        NotificationManager.addError(
1016
                                                        PluginServices.getText(
1017
                                                                        this, "failed_creating_labeling_strategy"), e);
1018
                                }
1019
                        } else if (getCmbMode().getSelectedItem().equals(ATTRS_IN_TABLE_ITEM)) {
1020
                                // user selected to define each label attributes from values
1021
                                // contained in the table for each feature row.
1022

    
1023
                                double fixedSize;
1024
                                try {
1025
                                        fixedSize = Double.parseDouble(getTxtHeightField().getText());
1026
                                } catch (Exception e) {
1027
                                        fixedSize = 10;
1028
                                }
1029
                                AttrInTableLabeling strategy = new AttrInTableLabeling();
1030
                                strategy.setLayer((FLyrVect) layer);
1031
                                strategy.setHeightField(
1032
                                                (String) getCmbHeightField().getSelectedItem());
1033
                                strategy.setRotationField(
1034
                                                (String) getCmbRotationField().getSelectedItem());
1035
                                strategy.setTextField(
1036
                                                (String) getCmbTextField().getSelectedItem());
1037
                                strategy.setUsesFixedSize(getRdBtnFixedHeight().isSelected());
1038
                                strategy.setFixedSize(fixedSize);
1039
                                // add more attributes for AttrInTableLabeling (if you need) here
1040
                                double unitFactor;
1041
                                try {
1042
                                        unitFactor = Attributes.CHANGE[getCmbUnits().getSelectedIndex()]/100;
1043
                                } catch (ArrayIndexOutOfBoundsException aioobEx) { //jijiji
1044
                                        unitFactor = -1; // which represents size in pixel
1045
                                }
1046
                                strategy.setUnitFactor(unitFactor);
1047
                                layer.setLabelingStrategy(strategy);
1048
                        }
1049
                }
1050
        }
1051

    
1052
        private IZoomConstraints getZoomConstraints() {
1053
                return zoomConstraints;
1054
        }
1055

    
1056
        private IPlacementConstraints getPlacementConstraints() {
1057
                return placementConstraints;
1058
        }
1059

    
1060
        public String getName() {
1061
                return PluginServices.getText(this,"Etiquetados");
1062
        }
1063
}
1064