Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / com / iver / cit / gvsig / project / documents / view / legend / gui / ExpressionCreator.java @ 24989

History | View | Annotate | Download (22.6 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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 com.iver.cit.gvsig.project.documents.view.legend.gui;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.KeyEvent;
49
import java.awt.event.KeyListener;
50
import java.awt.event.MouseEvent;
51
import java.awt.event.MouseListener;
52
import java.awt.event.MouseMotionListener;
53
import java.io.StringReader;
54
import java.sql.Types;
55
import java.util.Hashtable;
56

    
57
import javax.swing.JButton;
58
import javax.swing.JList;
59
import javax.swing.JOptionPane;
60
import javax.swing.JPanel;
61
import javax.swing.JScrollPane;
62
import javax.swing.JTextArea;
63
import javax.swing.ListModel;
64

    
65
import org.gvsig.gui.beans.AcceptCancelPanel;
66
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
67
import org.gvsig.remoteClient.sld.filterEncoding.FilterTags;
68
import org.gvsig.symbology.fmap.labeling.parse.LabelExpressionParser;
69
import org.gvsig.symbology.fmap.labeling.parse.ParseException;
70
import org.gvsig.symbology.fmap.rendering.filter.operations.Expression;
71
import org.gvsig.symbology.fmap.rendering.filter.operations.ExpressionException;
72
import org.gvsig.symbology.fmap.rendering.filter.operations.OperationTags;
73
import org.gvsig.symbology.fmap.rendering.filter.operations.OperatorsFactory;
74
import org.gvsig.symbology.fmap.rendering.filter.operations.OperatorsUtils;
75
import org.gvsig.symbology.fmap.rendering.filter.operations.ReplaceOperator;
76

    
77
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
78
import com.hardcode.gdbms.engine.data.DataSource;
79
import com.hardcode.gdbms.engine.values.Value;
80
import com.iver.andami.PluginServices;
81
import com.iver.andami.messages.NotificationManager;
82
import com.iver.andami.ui.mdiManager.IWindow;
83
import com.iver.andami.ui.mdiManager.WindowInfo;
84
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
85
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
86
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
87

    
88

    
89

    
90
/**
91
 * Implements the panel which is used to create or modify an
92
 * specific filter expression contained in a VectorialFilterExpressionLegend.
93
 * @author Pepe Vidal Salvador - jose.vidal.salvador@iver.es
94
 *
95
 */
96
public class ExpressionCreator extends JPanel implements IWindow {
97

    
98

    
99
        private static final long serialVersionUID = 1L;
100
        private FLyrVect layer;
101
        private String[] fieldNames;
102
        private String[] operators;
103
        private int width = 750;
104
        private int height = 385;
105
        private DataSource dataSource;
106
        private Object[] fieldNamesExpression;
107
        private LabelExpressionParser parser;
108
        private ISymbol symbolForExpression;
109
        private String descriptionForExpression;
110
        private int shapeType;
111
        private JButton addExpressBut;
112
        private JButton addOperatorBut;
113
        private JButton butVerify;
114
        private JButton butClear;
115
        private AcceptCancelPanel acceptCancelPanel;
116

    
117
        private JTextArea expressionArea;
118
        private JList fieldNamesList;
119
        private JList operatorsList;
120
        private JTextArea fieldPatternText = new JTextArea();
121
        private JTextArea operatorPatternText = new JTextArea();
122
        private JSymbolPreviewButton symbol;
123
        private JTextArea description;
124

    
125
        private MyListener myBehavior = new MyListener();
126

    
127
        String expression="";
128
        /**
129
         * Constructor method
130
         * @param vect
131
         */
132
        public ExpressionCreator(FLyrVect vect) {
133
                super();
134
                this.layer = vect;
135
                this.setOperators(operators);
136
                getShapeType();
137
                fieldNamesList = getFieldNames();
138
                operatorsList = getFieldOperators();
139
                initialize();
140
        }
141
        /**
142
         * This method initializes this
143
         *
144
         */
145
        private void initialize() {
146

    
147
                GridBagLayoutPanel topPanel = new GridBagLayoutPanel();
148
                topPanel.add(getFieldsPanel());
149
                topPanel.add(getButtonsPanel());
150
                topPanel.add(getOperatorsPanel());
151

    
152
                expressionArea = new JTextArea();
153
                expressionArea.setColumns(60);
154
                expressionArea.setRows(5);
155
                JScrollPane expressionScroll = new JScrollPane(expressionArea);
156
                expressionScroll.setPreferredSize(new Dimension(this.width-125, (this.height)/5));
157

    
158
                GridBagLayoutPanel centPanel = new GridBagLayoutPanel();
159
                centPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
160
                                null, PluginServices.getText(this,"expression"),
161
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
162
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
163
                centPanel.add(expressionScroll, null);
164

    
165
                JPanel buttPanel = new JPanel();
166
                buttPanel.setPreferredSize(new Dimension (100, this.height/7));
167

    
168
                buttPanel.add(getButVerify());
169
                buttPanel.add(getButClear());
170

    
171
                centPanel.add(buttPanel);
172

    
173
                GridBagLayoutPanel symAndDescPanel = new GridBagLayoutPanel();
174
                symAndDescPanel.add(getSymbol());
175
                symAndDescPanel.add(getDescriptionPanel());
176

    
177
                JPanel bottomPanel = new JPanel();
178
                acceptCancelPanel = new AcceptCancelPanel(action, action);
179
                bottomPanel.add(acceptCancelPanel, BorderLayout.SOUTH);
180
                bottomPanel.setPreferredSize(new Dimension(200,35));
181

    
182
                this.add(topPanel);
183
                this.add(centPanel);
184
                this.add(symAndDescPanel);
185
                this.add(bottomPanel);
186
        }
187

    
188
        private JPanel getOperatorsPanel() {
189
                JScrollPane operatorsScroll = new JScrollPane(operatorsList);
190
                operatorsScroll.setPreferredSize(new Dimension(this.width/4, this.height/5));
191

    
192
                JPanel operatorsPanel = new JPanel();
193
                operatorsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
194
                                null, PluginServices.getText(this,"operators"),
195
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
196
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
197
                operatorsPanel.setPreferredSize(new Dimension((this.width)/3,(this.height+160)/3));
198

    
199
                operatorPatternText.setEnabled(false);
200
                JScrollPane patternOperators = new JScrollPane(operatorPatternText);
201
                patternOperators.setBorder(javax.swing.BorderFactory.createTitledBorder(
202
                                null, PluginServices.getText(this,"pattern"),
203
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
204
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
205
                patternOperators.setPreferredSize(new Dimension((this.width-50)/3, (this.height+40)/7));
206

    
207
                operatorsPanel.add(operatorsScroll, null);
208
                operatorsPanel.add(patternOperators, null);
209

    
210
                return operatorsPanel;
211
        }
212

    
213
        private JPanel getFieldsPanel() {
214

    
215
                JPanel fieldPanel = new JPanel();
216
                JScrollPane fieldsScroll = new JScrollPane(fieldNamesList);
217
                fieldsScroll.setPreferredSize(new Dimension(this.width/4, this.height/5));
218

    
219

    
220
                fieldPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
221
                                null, PluginServices.getText(this,"fields_exp"),
222
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
223
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
224
                fieldPanel.setPreferredSize(new Dimension((this.width)/3,(this.height+160)/3));
225

    
226

    
227
                fieldPatternText.setEnabled(false);
228
                JScrollPane patternFields = new JScrollPane(fieldPatternText);
229
                patternFields.setBorder(javax.swing.BorderFactory.createTitledBorder(
230
                                null, PluginServices.getText(this,"pattern"),
231
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
232
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
233
                patternFields.setPreferredSize(new Dimension((this.width-50)/3, (this.height+40)/7));
234

    
235
                fieldPanel.add(fieldsScroll, null);
236
                fieldPanel.add(patternFields,null);
237

    
238
                return fieldPanel;
239
        }
240

    
241
        private JPanel getButtonsPanel() {
242
                JPanel myPanelBut = new JPanel();
243

    
244
                addExpressBut = new JButton(PluginServices.getText(this, "add_expression"));
245
                addExpressBut.addActionListener(action);
246
                myPanelBut.add(addExpressBut);
247

    
248
                addOperatorBut = new JButton(PluginServices.getText(this, "add_operator"));
249
                addOperatorBut.addActionListener(action);
250

    
251
                myPanelBut.add(addOperatorBut);
252

    
253
                return myPanelBut;
254
        }
255

    
256
        private JButton getButClear() {
257
                if(butClear == null) {
258
                        butClear = new JButton();
259
                        butClear.setText(PluginServices.getText(this, "clear"));
260
                        butClear.setPreferredSize(new Dimension(80,20 ));
261
                        butClear.addActionListener(action);
262
                }
263
                return butClear;
264
        }
265

    
266
        private JButton getButVerify() {
267
                if(butVerify == null) {
268
                        butVerify = new JButton();
269
                        butVerify.setText(PluginServices.getText(this, "verify"));
270
                        butVerify.setPreferredSize(new Dimension(80,20 ));
271
                        butVerify.addActionListener(action);
272
                }
273
                return butVerify;
274
        }
275

    
276
        private JList getFieldNames() {
277

    
278
                try {
279
                        dataSource = this.layer.getRecordset();
280
                        dataSource.start();
281
                        this.fieldNames = dataSource.getFieldNames();
282
                        dataSource.stop();
283
                } catch (ReadDriverException e) {
284
                        NotificationManager.addError(PluginServices.getText(this,"error_trying_to_access_to_the_layer"),e);
285
                }
286

    
287
                JList myList = new JList(fieldNames);
288
                myList.addMouseListener(myBehavior);
289
                myList.addMouseMotionListener(myBehavior);
290
                myList.addKeyListener(myBehavior);
291

    
292
                return myList;
293
        }
294

    
295
        private JList getFieldOperators() {
296

    
297
                String[] myOperators = new String [OperatorsUtils.getOperatorNames().length];
298
                for (int i = 0; i < OperatorsUtils.getOperatorNames().length; i++) {
299
                        if(OperatorsUtils.getOperatorNames()[i].toString().compareTo(OperationTags.REPLACE_OP) != 0)
300
                                myOperators[i] = OperatorsUtils.getOperatorNames()[i].toString();
301
                }
302
                JList myFieldOperators = new JList(myOperators);
303
                myFieldOperators.addMouseListener(myBehavior);
304
                myFieldOperators.addMouseMotionListener(myBehavior);
305
                myFieldOperators.addKeyListener(myBehavior);
306

    
307
                return myFieldOperators;
308
        }
309

    
310
        private JScrollPane getDescriptionPanel() {
311
                if(description == null) {
312
                        description = new JTextArea();
313
                        description.setColumns(55);
314
                        description.setRows(3);
315

    
316
                        JScrollPane scroll = new JScrollPane(description);
317
                        scroll.setBorder(javax.swing.BorderFactory.createTitledBorder(
318
                                        null, PluginServices.getText(this,"description"),
319
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
320
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
321
                        return scroll;
322
                }
323
                return null;
324
        }
325

    
326
        private JPanel getSymbol() {
327
                if(symbol == null) {
328
                        JPanel panel = new JPanel();
329
                        symbol = new JSymbolPreviewButton(getShapeType());
330
                        symbol.setSymbol(SymbologyFactory.createDefaultSymbolByShapeType(shapeType));
331
                        symbol.setPreferredSize(new Dimension(100,35));
332
                        panel.setBorder(javax.swing.BorderFactory.createTitledBorder(
333
                                        null, PluginServices.getText(this,"symbol"),
334
                                        javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
335
                                        javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
336
                        panel.add(symbol);
337
                        return panel;
338
                }
339
                return null;
340
        }
341

    
342
        /**
343
         * Obtains the shapetype of the layer
344
         *
345
         * @return int shapetype of the layer
346
         */
347
        private int getShapeType() {
348

    
349
                try {
350
                        shapeType = layer.getShapeType();
351
                } catch (ReadDriverException e) {
352
                        NotificationManager.addError(PluginServices.getText(this,"error_trying_to_access_to_the_layer"),e);
353
                }
354
                return shapeType;
355
        }
356

    
357

    
358

    
359

    
360
        public WindowInfo getWindowInfo() {
361
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG | WindowInfo.RESIZABLE);
362
                wi.setTitle(PluginServices.getText(this, "expression_creator"));
363
                wi.setWidth(this.width);
364
                wi.setHeight(this.height);
365

    
366
                return wi;
367
        }
368
        
369
        public Object getWindowProfile() {
370
                return WindowInfo.DIALOG_PROFILE;
371
        }
372

    
373

    
374
        private ActionListener action = new ActionListener() {
375

    
376
                Object[] options = {PluginServices.getText(this, "yes"),
377
                                PluginServices.getText(this, "no"),
378
                                PluginServices.getText(this, "cancel")};
379

    
380
                public void actionPerformed(ActionEvent e) {
381
                        String actionCommand = e.getActionCommand();
382
                        if ("OK".equals(actionCommand)) {
383
                                if(verifyExpression() && symbol.getSymbol() != null) {
384
                                        setExpression(expressionArea.getText());
385
                                        setFieldNamesExpression(parser.getClassNames().toArray());
386
                                        setDescriptionForExpression(description.getText());
387
                                        setSymbolForExpression(symbol.getSymbol());
388
                                        PluginServices.getMDIManager().closeWindow(ExpressionCreator.this);
389
                                }
390
                                int answer = 0;
391

    
392
                                if(!verifyExpression()) {
393
                                        answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
394
                                                        PluginServices.getText(this, "malformed_or_empty_expression")+"\n"+
395
                                                        PluginServices.getText(this, "it_will_not_be_added"),
396
                                                        PluginServices.getText(this, "confirmation_dialog"),
397
                                                        JOptionPane.YES_NO_CANCEL_OPTION,
398
                                                        JOptionPane.QUESTION_MESSAGE,
399
                                                        null,
400
                                                        options, options[1]);
401

    
402
                                        if (answer==JOptionPane.OK_OPTION) {
403
                                                setExpression(null);
404
                                                PluginServices.getMDIManager().closeWindow(ExpressionCreator.this);
405
                                        }
406

    
407

    
408
                                }
409
                                else if(symbol.getSymbol() == null) {
410
                                        answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
411
                                                        PluginServices.getText(this, "no_symbol_especified")+"\n"+
412
                                                        PluginServices.getText(this,"a_default_symbol_will_be_used"),
413
                                                        PluginServices.getText(this, "confirmation_dialog"),
414
                                                        JOptionPane.YES_NO_CANCEL_OPTION,
415
                                                        JOptionPane.QUESTION_MESSAGE,
416
                                                        null,
417
                                                        options, options[1]);
418

    
419
                                        if (answer==JOptionPane.OK_OPTION) {
420
                                                setExpression(expressionArea.getText());
421
                                                setFieldNamesExpression(parser.getClassNames().toArray());
422
                                                setDescriptionForExpression(description.getText());
423
                                                setSymbolForExpression(SymbologyFactory.createDefaultSymbolByShapeType(shapeType));
424
                                                PluginServices.getMDIManager().closeWindow(ExpressionCreator.this);
425
                                        }
426

    
427
                                }
428
                        }
429
                        else if ("CANCEL".equals(actionCommand)){
430
                                setExpression(null);
431
                                PluginServices.getMDIManager().closeWindow(ExpressionCreator.this);
432
                        }
433
                        else if (e.getSource().equals(addExpressBut)) {
434
                                if(fieldNamesList.getSelectedValue() != null) {
435
                                        expressionArea.append(" ["+fieldNamesList.getSelectedValue().toString()+"]");
436
                                }
437
                        }
438
                        else if (e.getSource().equals(addOperatorBut)) {
439
                                if(operatorsList.getSelectedValue() != null) {
440
                                        expressionArea.append(" "+operatorsList.getSelectedValue().toString());
441
                                }
442
                        }
443
                        else if (e.getSource().equals(butClear)) {
444
                                expressionArea.setText("");
445
                                expression = "";
446
                        }
447
                        else if (e.getSource().equals(butVerify)) {
448

    
449
                                if(expressionArea.getText().compareTo("") == 0) {
450
                                        JOptionPane.showMessageDialog(butVerify, PluginServices.getText(this, "no_expression_defined")+"\n");
451
                                }
452
                                else {
453
                                        Expression exp = null;
454
                                        Hashtable<String, Value> symbols = getSymbolsTable(fieldNames);
455

    
456
                                        try {
457
                                                parser = new LabelExpressionParser(new StringReader(expressionArea.getText()+";"),symbols);
458
                                                parser.LabelExpression();
459
                                                exp = (Expression) parser.getStack().pop();
460
                                                if(exp.evaluate().toString().compareTo("true") == 0 ||
461
                                                                exp.evaluate().toString().compareTo("false") == 0)
462
                                                        JOptionPane.showMessageDialog(butVerify,PluginServices.getText(this, "correct_expression")+"\n");
463
                                                else
464
                                                        JOptionPane.showMessageDialog(butVerify, PluginServices.getText(this, "incorrect_expression")+"\n-"+
465
                                                                        PluginServices.getText(this,"the_result_is_not_a_boolean_value")+"\n");
466

    
467
                                        } catch (ExpressionException e2) {
468
                                                int type = e2.getType();
469
                                                String message = "";
470
                                                message+=PluginServices.getText(this, "incorrect_expression")+":";
471

    
472
                                                if ((type & ExpressionException.CLASS_CASTING_EXCEPTION) != 0){
473
                                                        type = type & ~ExpressionException.CLASS_CASTING_EXCEPTION;
474
                                                        message += "\n-"+PluginServices.getText(this, "some_arguments_are_incorrect")+".\n";
475
                                                }
476
                                                if ((type & ExpressionException.INCORRECT_NUMBER_OF_ARGUMENTS) != 0){
477
                                                        type = type & ~ExpressionException.INCORRECT_NUMBER_OF_ARGUMENTS;
478
                                                        message +=  "\n-"+PluginServices.getText(this,"the_number_or_arguments_is_incorrect")+".\n";
479
                                                }
480
                                                if ((type & ExpressionException.ARGUMENT_ADDED_TO_CONSTANT) != 0){
481
                                                        type = type & ~ExpressionException.ARGUMENT_ADDED_TO_CONSTANT;
482
                                                        message += "\n-"+PluginServices.getText(this, "arguments_added_to_a_costant")+".\n";
483
                                                }
484
                                                if ((type & ExpressionException.DIVIDED_BY_CERO) != 0){
485
                                                        type = type & ~ExpressionException.DIVIDED_BY_CERO;
486
                                                        message += "\n-"+PluginServices.getText(this, "divided_by_0")+".\n";
487
                                                }
488
                                                if ((type & ExpressionException.NO_CLASSIF_NAME) != 0){
489
                                                        type = type & ~ExpressionException.NO_CLASSIF_NAME;
490
                                                        message += "\n-"+PluginServices.getText(this, "classifying_field_name_not_found")+".\n";
491
                                                }
492

    
493
                                                JOptionPane.showMessageDialog(butVerify, message);
494
                                        } catch (ParseException e2) {
495

    
496
                                                String message = PluginServices.getText(this, "incorrect_expression");
497
                                                JOptionPane.showMessageDialog(butVerify, message);
498
                                        }
499

    
500
                                }
501
                        }
502
                }
503

    
504
        };
505

    
506
        private Hashtable<String, Value> getSymbolsTable(String[] fNames) {
507
                Hashtable<String, Value> parser_symbol_table = new Hashtable<String, Value>();
508

    
509
                for (int j = 0; j < fNames.length; j++) {
510
                        try {
511
                                parser_symbol_table.put(fNames[j], dataSource.getFieldValue(0, j));
512
                        } catch (ReadDriverException e) {
513
                                e.printStackTrace();
514
                        }
515
                }
516
                return parser_symbol_table;
517
        }
518

    
519

    
520
        private String getFieldType(Object selectedValue) {
521
                String cad = null;
522
                int type;
523

    
524
                try {
525
                        type = dataSource.getFieldType(dataSource.getFieldIndexByName(selectedValue.toString()));
526

    
527
                        switch (type) {
528
                        case Types.INTEGER:
529
                        case Types.BIGINT:
530
                        case Types.DECIMAL:
531
                        case Types.DOUBLE:
532
                        case Types.FLOAT:
533
                        case Types.NUMERIC:
534
                                cad = PluginServices.getText(this, "numeric_value");
535
                                break;
536
                        case Types.LONGVARCHAR:
537
                        case Types.VARCHAR:
538
                                cad=PluginServices.getText(this,"string_value");
539
                                break;
540
                        case Types.BOOLEAN:
541
                                cad=PluginServices.getText(this,"boolean_value");
542
                                break;
543
                        case Types.DATE:
544
                                cad=PluginServices.getText(this,"date_value");
545
                                break;
546
                        }
547
                } catch (ReadDriverException e) {
548
                        e.printStackTrace();
549
                }
550
                return cad;
551
        }
552

    
553

    
554
        private class MyListener implements MouseListener, MouseMotionListener,KeyListener {
555

    
556

    
557
                public void mouseEntered(MouseEvent e) {/*Nothing*/}
558
                public void mouseExited(MouseEvent e) {/*Nothing*/}
559
                public void mouseReleased(MouseEvent e) {/*Nothing*/}
560
                public void mouseDragged(MouseEvent e) {/*Nothing*/}
561
                public void mouseMoved(MouseEvent e) {/*Nothing*/}
562
                public void keyTyped(KeyEvent e) {/*Nothing*/}
563
                public void keyPressed(KeyEvent e) {/*Nothing*/}
564

    
565
                public void mouseClicked(MouseEvent e) {
566
                        if(e.getClickCount() == 2){
567
                                if(e.getSource().equals(operatorsList)){
568
                             int index = operatorsList.locationToIndex(e.getPoint());
569
                             ListModel dlm = operatorsList.getModel();
570
                             Object item = dlm.getElementAt(index);;
571
                             operatorsList.ensureIndexIsVisible(index);
572
                             expressionArea.append(" "+item);
573
                                }
574
                                else if(e.getSource().equals(fieldNamesList)){
575
                             int index = fieldNamesList.locationToIndex(e.getPoint());
576
                             ListModel dlm = fieldNamesList.getModel();
577
                             Object item = dlm.getElementAt(index);;
578
                             fieldNamesList.ensureIndexIsVisible(index);
579
                             expressionArea.append(" ["+item+"]");
580
                                }
581
                        }
582
                }
583

    
584
                public void keyReleased(KeyEvent e) {
585
                        if (operatorsList.getSelectedValue() != null)
586
                                operatorPatternText.setText(OperatorsUtils.getPatternByName(operatorsList.getSelectedValue().toString()));
587
                        if(fieldNamesList.getSelectedValue() != null)
588
                                fieldPatternText.setText(getFieldType(fieldNamesList.getSelectedValue()));
589

    
590
                        repaint();
591
                }
592

    
593
                public void mousePressed(MouseEvent e) {
594
                        if (operatorsList.getSelectedValue() != null)
595
                                operatorPatternText.setText(OperatorsUtils.getPatternByName(operatorsList.getSelectedValue().toString()));
596
                        if(fieldNamesList.getSelectedValue() != null)
597
                                fieldPatternText.setText(getFieldType(fieldNamesList.getSelectedValue()));
598

    
599
                        repaint();
600
                }
601
        }
602

    
603
        private boolean verifyExpression() {
604
                Hashtable<String, Value> symbols = getSymbolsTable(fieldNames);
605

    
606
                try {
607
                        parser = new LabelExpressionParser(new StringReader(expressionArea.getText()),symbols);
608

    
609
                        parser.LabelExpression();
610

    
611
                        Expression expr = ((Expression)parser.getStack().pop());
612

    
613
                        if(expr.evaluate().toString().compareTo("true") == 0
614
                                        || expr.evaluate().toString().compareTo("false") == 0)
615

    
616
                                return true;
617
                        else
618
                                return false;
619

    
620
                } catch (ExpressionException e2) {
621
                        return false;
622
                } catch (ParseException e) {
623
                        return false;
624
                }
625

    
626
        }
627

    
628
        public String[] getOperators() {return operators;}
629
        public void setOperators(String[] operators) {this.operators = operators;}
630
        public String getExpression() {return expression;}
631

    
632
        public Object[] getFieldNamesExpression() {return fieldNamesExpression;}
633
        public void setFieldNamesExpression(Object[] objects) {this.fieldNamesExpression = objects;}
634
        public void setDescriptionForExpression(String descriptionForExpression) {
635
                this.descriptionForExpression = descriptionForExpression;
636
                description.setText(this.descriptionForExpression);
637
        }
638
        public void setSymbolForExpression(ISymbol symbolForExpression) {
639
                this.symbolForExpression = symbolForExpression;
640
                this.symbol.setSymbol(this.symbolForExpression);
641
        }
642

    
643

    
644

    
645
        public void setExpression(String text) {
646
                this.expression =  text;
647
                expressionArea.setText(text);
648

    
649
        }
650
        public String getDescriptionForExpression() {
651

    
652
                if(descriptionForExpression == null || descriptionForExpression.compareTo("") == 0)
653
                        descriptionForExpression = this.expressionArea.getText();
654

    
655
                return descriptionForExpression;
656
        }
657

    
658
        public ISymbol getSymbolForExpression() {
659

    
660
                if(symbol.getSymbol() != null)
661
                        symbolForExpression =symbol.getSymbol();
662

    
663
                if(symbolForExpression == null)
664
                        symbolForExpression = SymbologyFactory.createDefaultSymbolByShapeType(shapeType);
665

    
666
                symbolForExpression.setDescription(getDescriptionForExpression());
667
                return symbolForExpression;
668
        }
669
}
670