Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / gui / layerproperties / LabelClassProperties.java @ 41670

History | View | Annotate | Download (29.5 KB)

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

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridBagConstraints;
48
import java.awt.GridBagLayout;
49
import java.awt.GridLayout;
50
import java.awt.event.ActionEvent;
51
import java.awt.event.ActionListener;
52

    
53
import javax.swing.AbstractCellEditor;
54
import javax.swing.BorderFactory;
55
import javax.swing.ButtonGroup;
56
import javax.swing.JButton;
57
import javax.swing.JCheckBox;
58
import javax.swing.JComponent;
59
import javax.swing.JLabel;
60
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
62
import javax.swing.JRadioButton;
63
import javax.swing.JScrollPane;
64
import javax.swing.JTable;
65
import javax.swing.JTextField;
66
import javax.swing.table.DefaultTableModel;
67
import javax.swing.table.TableCellEditor;
68
import javax.swing.table.TableColumnModel;
69

    
70
import org.gvsig.andami.ui.mdiManager.IWindow;
71
import org.gvsig.andami.ui.mdiManager.WindowInfo;
72
import org.gvsig.app.ApplicationLocator;
73
import org.gvsig.app.gui.labeling.LabelClassEditor;
74
import org.gvsig.app.gui.styling.SingleStyleSelectorFilter;
75
import org.gvsig.app.gui.styling.StylePreviewer;
76
import org.gvsig.app.gui.styling.StyleSelector;
77
import org.gvsig.fmap.dal.exception.DataException;
78
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
79
import org.gvsig.fmap.dal.feature.FeatureStore;
80
import org.gvsig.fmap.dal.feature.FeatureType;
81
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
82
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
83
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.ILabelStyle;
84
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IStyle;
85
import org.gvsig.gui.beans.AcceptCancelPanel;
86
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
87
import org.gvsig.gui.beans.swing.JBlank;
88
import org.gvsig.i18n.Messages;
89
import org.gvsig.labeling.lang.LabelClassUtils;
90
import org.slf4j.Logger;
91
import org.slf4j.LoggerFactory;
92

    
93

    
94
/**
95
 *
96
 * LabelClassProperties.java
97
 *
98
 *
99
 * @author jaume dominguez faus - jaume.dominguez@iver.es May 27, 2008
100
 *
101
 */
102
public class LabelClassProperties extends JPanel implements LabelClassEditor, IWindow,
103
ActionListener {
104
        
105
        private static final Logger logger =
106
                        LoggerFactory.getLogger(LabelClassProperties.class);
107
        
108
        private static final String TEST_SQL = "TEST_SQL";
109
        private static final String DELETE_FIELD = "DELETE_FIELD";
110
        private static final long serialVersionUID = 6528513396536811057L;
111
        private static final String ADD_FIELD = "ADD_FIELD";
112
        private static final String SELECT_STYLE = "SELECT_STYLE";
113
        private static final String CLEAR_STYLE = "CLEAR_STYLE";
114
        private static final String VERIFY = "VERIFY";
115
        private JTextField txtName;
116
        private ILabelClass lc, clonedClass;
117
        private JTable tblExpressions;
118
        private JButton btnRemoveField;
119
        private JButton btnAddField;
120
        private JButton btnVerify;
121
        private JComponent styPreviewer;
122
        private JButton btnSelStyle;
123
        private JRadioButton rdBtnFilteredFeatures;
124
        private JRadioButton rdBtnAllFeatures;
125
        private JTextField txtSQL;
126
        private JCheckBox chkLabelFeatures;
127
        private LabelTextStylePanel textStyle;
128
        private JPanel sqlPnl;
129
        private LabelClassPreview labelPreview;
130
        
131
        /*
132
        private String[] fieldNames;
133
        private int[] fieldTypes;
134
        */
135
        private FeatureStore featureStore = null;
136
        private JButton btnDontUseStyle;
137
        private boolean accepted = true;
138
        private AcceptCancelPanel acceptCancelPanel;
139

    
140

    
141

    
142
        /**
143
         * <p>
144
         * Creates a new instance of the dialog that configures all the properties
145
         * of a LabelClass.
146
         * </p>
147
         * @param strategy
148
         * @param asWindow
149
         */
150
        public LabelClassProperties(FeatureStore fsto) {
151

    
152
                featureStore = fsto;
153
                initialize();
154
        }
155

    
156
        private void initialize() {
157
                setPreferredSize(new Dimension(810, 580));
158
                setLayout(new BorderLayout(1, 1));
159

    
160
                txtName = new JTextField(40);
161
                JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 2));
162
                northPanel.add(new JLabel(Messages.getText("name") + ":"));
163
                northPanel.add(txtName);
164
                add(northPanel, BorderLayout.NORTH);
165

    
166
                JPanel centerPanel = new JPanel(new BorderLayout(10, 10));
167
                textStyle = new LabelTextStylePanel();
168
                textStyle.addActionListener(this);
169
                centerPanel.add(textStyle, BorderLayout.NORTH);
170

    
171
                JPanel labelExpressionsPanel = new JPanel(new BorderLayout(1, 1));
172

    
173
                labelExpressionsPanel.add(new JBlank(10, 30), BorderLayout.WEST);
174
                labelExpressionsPanel.add(new JScrollPane(getTableFields()),
175
                                BorderLayout.CENTER);
176

    
177
                JPanel a = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
178
                a.add(new JLabel("<html><b>"+Messages.getText("text_fields")+"</b></html>"));
179
                centerPanel.add(labelExpressionsPanel, BorderLayout.CENTER);
180
                
181
                
182
                GridLayout gbl = new GridLayout(3, 1, 0, 6);
183
                JPanel auxp = new JPanel(gbl);
184
                auxp.add(getBtnVerify());
185
                auxp.add(getBtnAddField());
186
                auxp.add(getBtnRemoveField());
187
                auxp.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
188
                JPanel auxpp = new JPanel();
189
                auxpp.add(auxp);
190
                
191
                centerPanel.add(auxpp, BorderLayout.EAST);
192

    
193

    
194
                GridBagLayoutPanel aux = new GridBagLayoutPanel();
195
//                aux.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(
196
//                this, "features")));
197
                rdBtnAllFeatures = new JRadioButton(Messages.getText("all_features"));
198
                rdBtnFilteredFeatures = new JRadioButton(Messages.getText("filtered_features")
199
                                +" (SQL GDBMS)");
200

    
201
                rdBtnAllFeatures.addActionListener(this);
202
                rdBtnFilteredFeatures.addActionListener(this);
203

    
204
                ButtonGroup g = new ButtonGroup();
205
                g.add(rdBtnAllFeatures);
206
                g.add(rdBtnFilteredFeatures);
207
                aux.addComponent(chkLabelFeatures = new JCheckBox(
208
                                Messages.getText("label_features_in_this_class")));
209
                aux.addComponent("", rdBtnAllFeatures);
210
                aux.addComponent("", rdBtnFilteredFeatures);
211
                sqlPnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
212
                sqlPnl.add(new JLabel("   SQL: SELECT * FROM "));
213
                sqlPnl.add(new JLabel("<" + Messages.getText("layer_name")
214
                                + ">"));
215
                sqlPnl.add(new JLabel(" WHERE  "));
216
                sqlPnl.add(txtSQL = new JTextField(40));
217
                sqlPnl.add(new JLabel(";"));
218
                aux.addComponent("", sqlPnl);
219

    
220

    
221
                JPanel auxPanel = new JPanel(new BorderLayout(1,1));
222

    
223
                JPanel aux2 = new JPanel();
224
                auxPanel.setBorder(BorderFactory.createTitledBorder(
225
                                Messages.getText("background_style")));
226

    
227
                aux2.add(getStylePreviewer());
228

    
229
                gbl = new GridLayout(2, 1, 0, 6);
230
                JPanel aux3 = new JPanel(gbl);
231
                aux3.add(getBtnSelectStyle());
232
                aux3.add(getBtnDontUseStyle());
233
                aux3.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
234
                auxpp = new JPanel();
235
                auxpp.add(aux3);
236

    
237
                auxPanel.add(aux2,BorderLayout.CENTER);
238
                auxPanel.add(auxpp,BorderLayout.EAST);
239

    
240
                JPanel aux4 = new JPanel(new GridLayout(1,2));
241
                aux4.add(auxPanel);
242
                aux2 = new JPanel(new BorderLayout());
243
                aux2.setBorder(BorderFactory.createTitledBorder(Messages.getText("preview")));
244
                aux2.add(labelPreview = new LabelClassPreview(), BorderLayout.CENTER);
245
                aux4.add(aux2);
246

    
247
                GridBagLayoutPanel aux5 = new GridBagLayoutPanel();
248
                aux5.addComponent("", aux);
249
                aux5.addComponent(new JBlank(3, 5));
250
                aux5.addComponent("", aux4);
251
                centerPanel.add(aux5, BorderLayout.SOUTH);
252
                add(centerPanel, BorderLayout.CENTER);
253
                this.acceptCancelPanel = new AcceptCancelPanel(this, this);
254
                add(this.acceptCancelPanel, BorderLayout.SOUTH);
255

    
256
                chkLabelFeatures.addActionListener(this);
257
                txtName.addActionListener(this);
258

    
259
        }
260
        
261
        public void setAcceptCancelVisible(boolean visible) {
262
            this.acceptCancelPanel.setVisible(visible);
263
        }
264

    
265
        private JComponent getStylePreviewer() {
266

    
267
                if (styPreviewer == null) {
268
                        styPreviewer = new StylePreviewer();
269
                        styPreviewer.setPreferredSize(new java.awt.Dimension(90,90));
270
                        styPreviewer.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
271
                        ((StylePreviewer) styPreviewer).setShowOutline(true);
272
                }
273
                return styPreviewer;
274

    
275
        }
276

    
277
        private JButton getBtnSelectStyle() {
278
                if (btnSelStyle == null) {
279
                        btnSelStyle = new JButton(Messages.getText("select"));
280
                        btnSelStyle.setActionCommand(SELECT_STYLE);
281
                        btnSelStyle.addActionListener(this);
282
                }
283

    
284
                return btnSelStyle;
285
        }
286

    
287

    
288
        private JButton getBtnRemoveField() {
289
                if (btnRemoveField == null) {
290
                        btnRemoveField = new JButton(Messages.getText("remove"));
291
                        btnRemoveField.setActionCommand(DELETE_FIELD);
292
                        btnRemoveField.addActionListener(this);
293
                }
294

    
295
                return btnRemoveField;
296
        }
297

    
298
        private JButton getBtnAddField() {
299
                if (btnAddField == null) {
300
                        btnAddField = new JButton(Messages.getText("add"));
301
                        btnAddField.setActionCommand(ADD_FIELD);
302
                        btnAddField.addActionListener(this);
303
                }
304

    
305
                return btnAddField;
306
        }
307

    
308
        private JButton getBtnVerify() {
309
                if(btnVerify == null) {
310
                        btnVerify = new JButton(Messages.getText("verify"));
311
                        btnVerify.setActionCommand(VERIFY);
312
                        btnVerify.addActionListener(this);
313
                }
314
                return btnVerify;
315
        }
316

    
317
        private JButton getBtnDontUseStyle() {
318
                if (btnDontUseStyle == null) {
319
                        btnDontUseStyle = new JButton(Messages.getText("no_style"));
320
                        btnDontUseStyle.setActionCommand(CLEAR_STYLE);
321
                        btnDontUseStyle.addActionListener(this);
322
                }
323

    
324
                return btnDontUseStyle;
325
        }
326

    
327
        private JTable getTableFields() {
328
                if (tblExpressions == null) {
329
                        tblExpressions = new JTable();
330
                        tblExpressions.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
331
                }
332

    
333
                return tblExpressions;
334
        }
335

    
336

    
337
        private void setComponentEnabled(Component c, boolean b) {
338
                if (c instanceof JComponent) {
339
                        JComponent c1 = (JComponent) c;
340
                        for (int i = 0; i < c1.getComponentCount(); i++) {
341
                                setComponentEnabled(c1.getComponent(i), b);
342
                        }
343
                }
344
                c.setEnabled(b);
345
        }
346

    
347

    
348

    
349
        public void setLabelClass(ILabelClass labelClass) {
350
                if (labelClass == null)
351
                        return;
352

    
353
                lc = labelClass;
354
                clonedClass = (ILabelClass) LabelClassUtils.clone(lc);
355

    
356
                labelPreview.setLabelClass(clonedClass);
357
                boolean clo_use_sql = LabelClassUtils.isUseSqlQuery(clonedClass); 
358
                if (!clo_use_sql) {
359
                        rdBtnAllFeatures.setSelected(true);
360
                        txtSQL.setText("");
361
                } else {
362
                        rdBtnFilteredFeatures.setSelected(true);
363
                        txtSQL.setText(clonedClass.getSQLQuery());
364
                }
365

    
366
                chkLabelFeatures.setSelected(clonedClass.isVisible());
367
                rdBtnFilteredFeatures.setSelected(clo_use_sql);
368
                rdBtnAllFeatures.setSelected(!clo_use_sql);
369
                ((StylePreviewer) styPreviewer).setStyle(clonedClass.getLabelStyle());
370
                txtName.setText(clonedClass.getName());
371
                
372
                // String expr = lc.getStringLabelExpression();
373

    
374
                /*
375
                String EOExpr = LabelExpressionParser.tokenFor(LabelExpressionParser.EOEXPR);
376
                if (expr == null)
377
                        expr = EOExpr;
378
                expr = expr.trim();
379
                if (!expr.endsWith(EOExpr)) {
380
                        throw new Error("Invalid expression. Missing EOExpr token ("+EOExpr+").\n"+expr);
381
                }
382
                expr = expr.substring(0, expr.length()-1);
383
                */
384

    
385
                // expr = expr.trim();
386

    
387
                ITextSymbol lcTextSymbol = lc.getTextSymbol();
388
                ITextSymbol clonedSymbol = null;
389
                
390
                try {
391
                        clonedSymbol = (ITextSymbol) lcTextSymbol.clone();
392
                } catch (CloneNotSupportedException e) {
393
                        logger.error("While clonin.", e);
394
                }
395

    
396
                textStyle.setModel(
397
                                clonedSymbol, //lc.getTextSymbol(),
398
                                lc.getUnit(),
399
                                lc.getReferenceSystem());
400

    
401
                getTableFields().setRowHeight(22);
402
                getTableFields().setModel(new FieldTableExpressions(lc.getLabelExpressions()));
403
                TableColumnModel colMod = getTableFields().getColumnModel();
404
                colMod.getColumn(0).setPreferredWidth(100);
405
                colMod.getColumn(0).setWidth(100);
406
                colMod.getColumn(1).setResizable(true);
407
                colMod.getColumn(1).setPreferredWidth(513);
408
                
409
                FeatureAttributeDescriptor[] atts = null;
410
                
411
                try {
412
                        atts = featureStore.getDefaultFeatureType().getAttributeDescriptors();
413
                } catch (DataException e) {
414
                        logger.error("While getting attributes.", e);
415
                }
416
                
417
                getTableFields().setDefaultEditor(
418
                                Object.class,
419
                                new DefaultEditor(atts));
420
                repaint();
421

    
422
                actionPerformed(new ActionEvent(this, 0, null));
423
        }
424

    
425
        public JComponent asJComponent() {
426
            return this;
427
        }
428

    
429
        public void showDialog() {
430
            ApplicationLocator.getManager().getUIManager().addWindow(this);
431
        }
432

    
433
        private class DefaultEditor extends AbstractCellEditor implements
434
                        TableCellEditor, ActionListener {
435

    
436
                JPanel editor;
437
                LabelExpressionEditorPanel dialog;
438
                private JTextField text;
439
                private JButton button;
440
                protected static final String EDIT = "edit";
441

    
442
                public DefaultEditor(FeatureAttributeDescriptor[] atts) {
443
                        editor = new JPanel();
444
                        editor.setLayout(new GridBagLayout());
445
                        GridBagConstraints cons = new GridBagConstraints();
446
//                        cons.anchor = GridBagConstraints.FIRST_LINE_START;
447
                        cons.fill = cons.BOTH;
448
                        cons.weightx=1.0;
449
                        cons.weighty =1.0;
450
                        text = new JTextField();
451
                        editor.add(text, cons);
452

    
453
                        GridBagConstraints cons1 = new GridBagConstraints();
454
//                        cons.anchor = GridBagConstraints.FIRST_LINE_END;
455
                        cons1.fill = cons1.VERTICAL;
456
                        cons1.weighty =1.0;
457
//                        cons.gridheight = GridBagConstraints.REMAINDER;
458
                        button = new JButton("...");
459
                        button.setActionCommand(DefaultEditor.EDIT);
460
                        button.addActionListener(this);
461
                        editor.add(button,cons1);
462

    
463
                        editor.updateUI();
464

    
465
                        // Set up the dialog that the button brings up.
466
                        dialog = new LabelExpressionEditorPanel(atts);
467
                }
468

    
469
                public void actionPerformed(ActionEvent e) {
470
                        if (EDIT.equals(e.getActionCommand())) {
471

    
472
                                dialog.setValue(text.getText());
473
                                ApplicationLocator.getManager().getUIManager().addWindow(dialog);
474
                                //fireEditingStopped(); // Make the renderer reappear.
475
                                text.setText(dialog.getValue());
476
                        }
477
                }
478

    
479
                // Implement the one CellEditor method that AbstractCellEditor doesn't.
480
                public Object getCellEditorValue() {
481
                        return text.getText();
482
                }
483

    
484
                // Implement the one method defined by TableCellEditor.
485
                public Component getTableCellEditorComponent(JTable table,
486
                                Object value, boolean isSelected, int row, int column) {
487
                        text.setText((String) value);
488
                        return editor;
489
                }
490
        }
491

    
492

    
493

    
494

    
495
        public WindowInfo getWindowInfo() {
496
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG
497
                                | WindowInfo.RESIZABLE);
498
                wi.setTitle(Messages.getText("label_class_properties"));
499
                wi.setWidth(getPreferredSize().width + 8);
500
                wi.setHeight(getPreferredSize().height);
501
                return wi;
502
        }
503

    
504
        public Object getWindowProfile() {
505
                return WindowInfo.DIALOG_PROFILE;
506
        }
507

    
508

    
509
        /*
510
         * Return a hashtable with example values.
511
         * Only for verification purposes.
512
        private Hashtable<String, Value> getValuesTable(String[] fNames, int[] fTypes) {
513
                
514
                Hashtable<String, Value> parser_symbol_table = new Hashtable<String, Value>();
515

516
                Random rand = new Random();
517
                byte b = 0;
518
            short s = (short)rand.nextInt(Short.MAX_VALUE);
519
            int i = rand.nextInt();
520
                long l = rand.nextLong();
521
                boolean bool = rand.nextBoolean();
522
                float f = rand.nextFloat();
523
                double d = rand.nextDouble();
524

525
                for (int j = 0; j < fNames.length; j++) {
526
                        Value value=null;
527

528
                        int type = fTypes[j];
529
            switch (type) {
530
            case Types.BIGINT:
531
                value = ValueFactory.createValue(l);
532
                break;
533
            case Types.BIT:
534
            case Types.BOOLEAN:
535
                value = ValueFactory.createValue(bool);
536
                break;
537
            case Types.CHAR:
538
            case Types.VARCHAR:
539
            case Types.LONGVARCHAR:
540
                value = ValueFactory.createValue("");
541
                break;
542
            case Types.DATE:
543
                Date auxDate = new Date();
544
                    if (auxDate != null){
545
                    value = ValueFactory.createValue(auxDate);
546
                    }
547
                break;
548
            case Types.DECIMAL:
549
            case Types.NUMERIC:
550
            case Types.FLOAT:
551
            case Types.DOUBLE:
552
                value = ValueFactory.createValue(d);
553
                break;
554
            case Types.INTEGER:
555
                value = ValueFactory.createValue(i);
556
                break;
557
            case Types.REAL:
558
                value = ValueFactory.createValue(f);
559
                break;
560
            case Types.SMALLINT:
561
                value = ValueFactory.createValue(s);
562
                break;
563
            case Types.TINYINT:
564
                value = ValueFactory.createValue(b);
565
                break;
566
            case Types.BINARY:
567
            case Types.VARBINARY:
568
            case Types.LONGVARBINARY:
569
                byte[] auxByteArray = {b}; //new byte[1];
570
                value = ValueFactory.createValue(auxByteArray);
571
                break;
572
            case Types.TIMESTAMP:
573
                           value = ValueFactory.createValue(new Timestamp(l));
574
                break;
575
            case Types.TIME:
576
                    value = ValueFactory.createValue(new Time(l));
577
                break;
578
            default:
579
                    value = ValueFactory.createValue("");
580
                    break;
581
        }
582
                        parser_symbol_table.put(fNames[j], value);
583
                }
584
                return parser_symbol_table;
585
        }
586
                 */
587

    
588

    
589
        /*
590
        private boolean verifyExpresion(String expresion, Hashtable<String, Value> symbols){
591
                LabelExpressionParser parser;
592
                try {
593
                        parser = new LabelExpressionParser(new StringReader(expresion),symbols);
594
                        parser.LabelExpression();
595
                        Expression expr = ((Expression)parser.getStack().pop());
596
                        expr.evaluate();
597
                } catch (ExpressionException e2) {
598
                        return false;
599
                } catch (Exception e3) {
600
                        return false;
601
                }
602
                return true;
603
        }
604
        */
605

    
606
        private boolean verifyExpressions(){
607
                
608
                String[] expressions = ((FieldTableExpressions) getTableFields().getModel()).getExpression();
609
                if(expressions.length == 1 && expressions[0].equals("")){
610
                        return true;
611
                }
612
                boolean result = true;
613
                String message = "";
614
                for (int i = 0; i < expressions.length; i++) {
615
                        String expresion = expressions[i];
616
                        
617
                        if( !LabelClassUtils.validExpression(expresion, featureStore, false) ){
618
                                result = false;
619
                                if(message.compareTo("")!=0){
620
                                        message = message +        " , " + (i+1);
621
                                } else {
622
                                        message = " " + Messages.getText("at_fields")+" "+(i+1);
623
                                }
624
                        }
625
                }
626
                message = message +".";
627
                if(!result){
628
                        JOptionPane.showMessageDialog(
629
                                        ApplicationLocator.getManager().getRootComponent(),
630
                                        Messages.getText("malformed_or_empty_expression") + message,
631
                                        Messages.getText("verify"),
632
                                        JOptionPane.ERROR_MESSAGE);
633
                } else {
634
                        JOptionPane.showMessageDialog(
635
                                        ApplicationLocator.getManager().getRootComponent(),
636
                                        Messages.getText("_Expressions_are_correct"),
637
                                        Messages.getText("verify"),
638
                                        JOptionPane.INFORMATION_MESSAGE);
639
                }
640
                return result;
641
        }
642

    
643
        public boolean isAccepted(){
644
                return accepted;
645
        }
646
        
647
        public void doCancel() {
648
            accepted = false;
649
            setLabelClass(lc);        
650
        }
651

    
652
        public void doAccept() {
653
            if (rdBtnFilteredFeatures.isSelected()) {
654
                String sqlstr = txtSQL.getText();
655
                if (!validVisibleSQL(sqlstr, featureStore)) {
656
                    JOptionPane.showMessageDialog(
657
                            ApplicationLocator.getManager().getRootComponent(),
658
                            Messages.getText("error_validating_filter_query")
659
                            + ":\n\n'" + sqlstr + "'",
660
                            Messages.getText("filtered_features"),
661
                            JOptionPane.ERROR_MESSAGE);
662
                    return;
663
                }
664
            }
665
            clonedClass.setUnit(textStyle.getUnit());
666
            clonedClass.setReferenceSystem(textStyle.getReferenceSystem());
667
            clonedClass.setTextSymbol(textStyle.getTextSymbol());
668
            lc = clonedClass;
669
            accepted = true;
670
        }
671

    
672
        public void actionPerformed(ActionEvent e) {
673
                setComponentEnabled(sqlPnl, rdBtnFilteredFeatures.isSelected());
674
                
675
                applySettings();
676
                
677
                if ("OK".equals(e.getActionCommand())) {
678
                        doAccept();
679
                        try {
680
                                ApplicationLocator.getManager().getUIManager().closeWindow(this);
681
                        } catch (Exception ex) {
682
                                // this only happens when running this as stand-alone app
683
                                // from main method;
684
                                logger.warn("While closing window.", ex);
685
                        }
686
                } else if ("CANCEL".equals(e.getActionCommand())) {
687
                        doCancel();
688
                        try {
689
                                ApplicationLocator.getManager().getUIManager().closeWindow(this);
690
                        } catch (Exception ex) {
691
                                // this only happens when running this as stand-alone app
692
                                // from main method;
693
                                logger.warn("While closing window.", ex);
694
                        }
695
                } else if (CLEAR_STYLE.equals(e.getActionCommand())) {
696
                        clonedClass.setLabelStyle(null);
697
//                        styPreviewer.setStyle(null);
698
                        ((StylePreviewer) styPreviewer).setStyle(null);
699
                } else if (TEST_SQL.equals(e.getActionCommand())) {
700
                        logger.info("TEST_SQL = " + TEST_SQL);
701
                } else if (DELETE_FIELD.equals(e.getActionCommand())) {
702
                        int[] rowInd = getTableFields().getSelectedRows();
703
                        for (int i = rowInd.length-1; i >= 0 ; i--) {
704
                                delField(rowInd[i]);
705
                        }
706
                        clonedClass.setLabelExpressions(((FieldTableExpressions) getTableFields().getModel()).getExpression());
707
                        setLabelClass(clonedClass);
708
                } else if (VERIFY.equals(e.getActionCommand())) {
709
                        verifyExpressions();
710
                } else if (ADD_FIELD.equals(e.getActionCommand())) {
711
                        addField();
712
                } else if (SELECT_STYLE.equals(e.getActionCommand())) {
713

    
714
                        IStyle myStyle = ((StylePreviewer)styPreviewer).getStyle();
715
                        
716
                        FeatureType fty = null;
717
                        
718
                        try {
719
                                fty = featureStore.getDefaultFeatureType();
720
                        } catch (DataException e1) {
721
                                logger.error("While getting feat type.", e1);
722
                        }
723
                        
724
                        int geotype = fty.getDefaultGeometryAttribute().getGeomType().getType();
725
                        
726
                        SingleStyleSelectorFilter filter =
727
                                        new SingleStyleSelectorFilter(ILabelStyle.class);
728
                        StyleSelector stySel = null;
729
                        
730
                        try {
731
                                stySel = new StyleSelector(myStyle, geotype, filter);
732
                        } catch (IllegalArgumentException iae) {
733
                                JOptionPane.showMessageDialog(this,
734
                                                Messages.getText("_Option_not_available_for_this_geometry_type"),
735
                                                getWindowInfo().getTitle(),
736
                                                JOptionPane.WARNING_MESSAGE);
737
                                return;
738
                        }
739
                        
740
                        ApplicationLocator.getManager().getUIManager().addWindow(stySel);
741
                        ILabelStyle sty = (ILabelStyle) stySel.getSelectedObject();
742

    
743
                        if (sty != null) {
744
                                // gather the style and apply to the class
745
                                clonedClass.setLabelStyle(sty);
746
                                clonedClass.setUnit(stySel.getUnit());
747
                                clonedClass.setReferenceSystem(stySel.getReferenceSystem());
748
                                setLabelClass(clonedClass);
749
                        }
750

    
751
//                        styPreviewer.setStyle(sty);
752
                        ((StylePreviewer) styPreviewer).setStyle(sty);
753

    
754
                }else if (e.getSource().equals(rdBtnAllFeatures) || e.getSource().equals(rdBtnFilteredFeatures)){
755
                        clonedClass.setUseSqlQuery(rdBtnFilteredFeatures.isSelected());
756
                }
757

    
758
                repaint();
759
        }
760

    
761
        private boolean validVisibleSQL(String sqlstr, FeatureStore fsto) {
762
            
763
            return LabelClassUtils.validExpression(sqlstr, fsto, true);
764
    }
765

    
766
    private void applySettings() {
767
            
768
                clonedClass.setVisible(chkLabelFeatures.isSelected());
769
                clonedClass.setName(txtName.getText());
770
                
771
                if (rdBtnFilteredFeatures.isSelected()) {
772
                    clonedClass.setSQLQuery(txtSQL.getText());
773
                } else {
774
                    clonedClass.setSQLQuery("");
775
                }
776
                
777
                JTable tableFields=getTableFields();
778
                TableCellEditor cellEditor=tableFields.getCellEditor();
779
                if(cellEditor != null){
780
                        int row = tableFields.getEditingRow();
781
                        int column = tableFields.getEditingColumn();
782
                        cellEditor.stopCellEditing();
783
                        Object value = cellEditor.getCellEditorValue();
784

    
785
                        if (value != null) {
786
                                ((FieldTableExpressions) tableFields.
787
                                                getModel()).setValueAt(value, row, column);
788
                        }
789
                }
790
                clonedClass.setLabelExpressions(
791
                                ((FieldTableExpressions) tableFields.
792
                                                getModel()).getExpression());
793
                clonedClass.setTextSymbol(textStyle.getTextSymbol());
794
        }
795

    
796
        private void addField() {
797
                addField("");
798
        }
799

    
800
        private void addField(String fieldExpr) {
801
                FieldTableExpressions m = ((FieldTableExpressions) getTableFields().getModel());
802
                m.addRow(new Object[] { m.getRowCount()+1, fieldExpr });
803
        }
804

    
805
        private void delField(int fieldIndex) {
806
                try {
807
                        ((FieldTableExpressions) getTableFields().getModel()).removeRow(fieldIndex);
808
                } catch (ArrayIndexOutOfBoundsException ex) {}
809
        }
810

    
811

    
812
        public ILabelClass getLabelClass() {
813
                return lc;
814
        }
815

    
816

    
817

    
818
        private static final Object[] TABLE_HEADERS = new String[] {
819
                
820
                Messages.getText("field_number"),
821
                Messages.getText("label_expression")
822
        };
823
        
824
        /*
825
        private static String fieldSeparator =
826
        LabelExpressionParser.tokenImage[LabelExpressionParser.EOFIELD].replaceAll("\"", "");
827
        */
828
        
829
        private class FieldTableExpressions extends DefaultTableModel {
830
                private static final long serialVersionUID = 2002427714889477770L;
831
                public FieldTableExpressions(String[] expressions) {
832
                        super();
833

    
834
                        if(expressions != null && expressions.length > 0){
835
                                Integer[] aux = new Integer[expressions.length];
836
                                for (int i = 0; i < aux.length; i++) {
837
                                        aux[i] = i+1;
838
                                }
839

    
840
                                Object[][] values = new Object[aux.length][2];
841
                                for (int i = 0; i < values.length; i++) {
842
                                        values[i][0] = aux[i];
843
                                        values[i][1] = expressions[i];
844
                                }
845
                                setDataVector(values, TABLE_HEADERS);
846
                        }
847
                        else{
848
                                Object[][] values = new Object[1][2];
849
                                values[0][0] = 1;
850
                                values[0][1] = "";
851
                                setDataVector(values,TABLE_HEADERS);
852
                        }
853

    
854
                }
855

    
856

    
857
                @Override
858
                public boolean isCellEditable(int row, int column) {
859
                        return column == 1;
860
                }
861

    
862
                public String[] getExpression() {
863

    
864
                        String[] expressions = new String[getRowCount()];
865

    
866
                        for (int i = 0; i < getRowCount(); i++) {
867
                                expressions[i] = ""+(String) getValueAt(i,1);
868
                        }
869
                        return expressions;
870
                }
871

    
872
        }
873

    
874
//        public static void main(String[] args) {
875
//                JFrame f = new JFrame("Test LabelClassProperties panel");
876
//
877
//                String xmlString =
878
//                        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
879
//                        "<xml-tag xmlns=\"http://www.gvsig.gva.es\">\n" +
880
//                        "    <property key=\"className\" value=\"com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass\"/>\n" +
881
//                        "    <property key=\"isVisible\" value=\"false\"/>\n" +
882
//                        "    <property key=\"name\" value=\"Default\"/>\n" +
883
//                        "    <property key=\"labelExpression\" value=\"[TIPO] : lao39805502232 : Substring([MOTO], 2,2);\"/>\n" +
884
//                        "    <property key=\"unit\" value=\"-1\"/>\n" +
885
//                        "    <property key=\"referenceSystem\" value=\"0\"/>\n" +
886
//                        "    <property key=\"sqlQuery\" value=\"TIPO = 'C'\"/>\n" +
887
//                        "    <property key=\"priority\" value=\"0\"/>\n" +
888
//                        "    <xml-tag>\n" +
889
//                        "        <property key=\"className\" value=\"org.gvsig.symbology.fmap.styles.SimpleLabelStyle\"/>\n" +
890
//                        "        <property key=\"desc\" value=\"Placa Carrer VLC.style\"/>\n" +
891
//                        "        <property key=\"markerPointX\" value=\"0.0\"/>\n" +
892
//                        "        <property key=\"markerPointY\" value=\"0.0\"/>\n" +
893
//                        "        <property key=\"minXArray\" value=\"0.35 ,0.25\"/>\n" +
894
//                        "        <property key=\"minYArray\" value=\"0.15 ,0.5\"/>\n" +
895
//                        "        <property key=\"widthArray\" value=\"0.5 ,0.6\"/>\n" +
896
//                        "        <property key=\"heightArray\" value=\"0.27 ,0.37\"/>\n" +
897
//                        "        <property key=\"id\" value=\"labelStyle\"/>\n" +
898
//                        "        <xml-tag>\n" +
899
//                        "            <property key=\"className\" value=\"org.gvsig.symbology.fmap.styles.RemoteFileStyle\"/>\n" +
900
//                        "            <property key=\"source\" value=\"http://www.boomlapop.com/boomlapop.jpg\"/>\n" +
901
//                        "            <property key=\"desc\"/>\n" +
902
//                        "            <property key=\"id\" value=\"LabelStyle\"/>\n" +
903
//                        "        </xml-tag>\n" +
904
//                        "    </xml-tag>\n" +
905
//                        "    <xml-tag>\n" +
906
//                        "        <property key=\"className\" value=\"com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol\"/>\n" +
907
//                        "        <property key=\"desc\"/>\n" +
908
//                        "        <property key=\"isShapeVisible\" value=\"true\"/>\n" +
909
//                        "        <property key=\"font\" value=\"Arial\"/>\n" +
910
//                        "        <property key=\"fontStyle\" value=\"1\"/>\n" +
911
//                        "        <property key=\"size\" value=\"12\"/>\n" +
912
//                        "        <property key=\"text\" value=\"GeneralLabeling.sample_text\"/>\n" +
913
//                        "        <property key=\"textColor\" value=\"255,255,0,255\"/>\n" +
914
//                        "        <property key=\"unit\" value=\"-1\"/>\n" +
915
//                        "        <property key=\"referenceSystem\" value=\"0\"/>\n" +
916
//                        "        <property key=\"id\" value=\"TextSymbol\"/>\n" +
917
//                        "    </xml-tag>\n" +
918
//                        "</xml-tag>\n" +
919
//                        "";
920
//
921
//                LabelClass lc = null;
922
//                try {
923
//                        XMLEntity xml = new XMLEntity((XmlTag) XmlTag.unmarshal(
924
//                                        XMLEncodingUtils.getReader(new ByteArrayInputStream(xmlString.getBytes()))));
925
//                        lc = new LabelClass();
926
//                        lc.setXMLEntity(xml);
927
//                } catch (Exception e) {
928
//                        e.printStackTrace();
929
//                }
930
//                String[] names = new String[] { "Field1", "Field2", "Field3"        };
931
//                int[] types = new int[] { Types.VARCHAR, Types.INTEGER, Types.DOUBLE };
932
//                final LabelClassProperties lcp = new LabelClassProperties(
933
//                                names, types ) {
934
//                        private static final long serialVersionUID = -1843509415649666459L;
935
//
936
//                        @Override
937
//                        public void actionPerformed(ActionEvent e) {
938
//                                super.actionPerformed(e);
939
//                                if ("OK".equals(e.getActionCommand())) {
940
//                                        Sys tem.out.println(getLabelClass().getXMLEntity());
941
//                                }
942
//                        }
943
//                };
944
//                lcp.setLabelClass(lc);
945
//                f.setContentPane(lcp);
946
//                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
947
//                f.pack();
948
//                f.setVisible(true);
949
//
950
//        }
951

    
952
}