Revision 44292

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.api/src/main/java/org/gvsig/fmap/dal/swing/searchpanel/FeatureStoreSearchPanel.java
14 14
    
15 15
    public static final String ACTION_CONTEXT_NAME = "FeatureStoreSearchPanel";
16 16
    
17
    public static final int DEFAULT_WIDTH = 700;
18
    public static final int DEFAULT_HEIGHT = 450;
19
    
20
    public Expression getFilter();
21
    
22
    public boolean setFilter(Expression filter);
23
    
17 24
    public Expression getCurrentSearch();
18 25
    
26
    public void setCurrentSearch(Expression filter);
27
    
19 28
    public Expression getSearchForSelectedFeature();
20 29
    
21 30
    public int getSelectedFeatureCount();
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/featureform/swing/impl/DefaultJFeaturesForm.java
67 67
import org.gvsig.fmap.dal.swing.DataSwingManager;
68 68
import org.gvsig.fmap.dal.swing.impl.DefaultDataSwingManager;
69 69
import org.gvsig.fmap.dal.swing.impl.actions.ShowFormAction.ShowFormActionFactory;
70
import org.gvsig.fmap.dal.swing.searchpanel.FeatureStoreSearchPanel;
70 71
import org.gvsig.tools.ToolsLocator;
71 72
import org.gvsig.tools.dispose.DisposeUtils;
72 73
import org.gvsig.tools.dynform.AbortActionException;
......
628 629
            LOGGER.trace("formAfterDelete");
629 630
        }
630 631

  
632
//        @Override
633
//        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
634
//            LOGGER.trace("formBeforeSearch");
635
//            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
636
//            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
637
//
638
//            JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
639
//            FeatureQuery currentQuery = getCurrentQuery();
640
//            if( currentQuery!=null ) {
641
//                Evaluator filter = currentQuery.getFilter();
642
//                if( filter instanceof ExpressionEvaluator ) {
643
//                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
644
//                    builder.setExpression(expression);
645
//                }
646
//            }
647
//            Dialog dialog = winmgr.createDialog(
648
//                    builder.asJComponent(),
649
//                    "Filtro",
650
//                    "Creacion de filtro sobre '"+store.getName()+"'",
651
//                    WindowManager_v2.BUTTONS_OK_CANCEL
652
//            );
653
//            dialog.show(WindowManager.MODE.DIALOG);
654
//            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
655
//                Expression expresion = builder.getExpression();
656
//                try {
657
//                    FeatureQuery query = store.createFeatureQuery();
658
//                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
659
//                        query.clearFilter();
660
//                    } else {
661
//                        query.setFilter(expresion);
662
//                    }
663
//                    setQuery(query);
664
//                } catch (Exception ex) {
665
//                    LOGGER.warn("Can't apply filter '" + expresion + "'.", ex);
666
//                }
667
//            }
668
//        }
669

  
631 670
        @Override
632 671
        public void formBeforeSearch(JDynFormSet dynformSet) throws AbortActionException {
633 672
            LOGGER.trace("formBeforeSearch");
634 673
            DataSwingManager dataSwingmanager = DALSwingLocator.getSwingManager();
635 674
            WindowManager_v2 winmgr = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
636 675

  
637
            JExpressionBuilder builder = dataSwingmanager.createQueryFilterExpresion(store);
676
            final FeatureStoreSearchPanel searchPanel = dataSwingmanager.createFeatureStoreSearchPanel(store);
677
            searchPanel.setShowActions(false);
638 678
            FeatureQuery currentQuery = getCurrentQuery();
639 679
            if( currentQuery!=null ) {
640 680
                Evaluator filter = currentQuery.getFilter();
641 681
                if( filter instanceof ExpressionEvaluator ) {
642 682
                    Expression expression = ((ExpressionEvaluator)filter).getExpression();
643
                    builder.setExpression(expression);
683
                    searchPanel.setFilter(expression);
644 684
                }
645 685
            }
686
            searchPanel.asJComponent().setPreferredSize(
687
                    new Dimension(FeatureStoreSearchPanel.DEFAULT_WIDTH, 320)
688
            );
646 689
            Dialog dialog = winmgr.createDialog(
647
                    builder.asJComponent(),
690
                    searchPanel.asJComponent(),
648 691
                    "Filtro",
649 692
                    "Creacion de filtro sobre '"+store.getName()+"'",
650 693
                    WindowManager_v2.BUTTONS_OK_CANCEL
651 694
            );
652 695
            dialog.show(WindowManager.MODE.DIALOG);
653 696
            if( dialog.getAction() == WindowManager_v2.BUTTON_OK ) {
654
                Expression expresion = builder.getExpression();
697
                Expression expresion = searchPanel.getFilter();
655 698
                try {
656 699
                    FeatureQuery query = store.createFeatureQuery();
657 700
                    if( ExpressionUtils.isPhraseEmpty(expresion) ) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/searchpanel/DefaultSearchPanel.java
23 23
import javax.swing.table.TableModel;
24 24
import org.apache.commons.io.FilenameUtils;
25 25
import org.apache.commons.lang.mutable.MutableObject;
26
import org.gvsig.expressionevaluator.Code;
26 27
import org.gvsig.expressionevaluator.Expression;
27 28
import org.gvsig.expressionevaluator.ExpressionBuilder;
28 29
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_AND;
......
67 68
        private final List<String> columnNames;
68 69
        private final FeatureType featureType;
69 70

  
71
        public FeaturesTableModel(FeatureType featureType) {
72
            this(featureType, null, null);
73
        }
74
        
70 75
        public FeaturesTableModel(FeatureType featureType, List<String> columnNames, List<Feature> features) {
71 76
            this.features = features;
72 77
            this.featureType = featureType;
......
369 374
            }
370 375
        });
371 376
        addActions();
372
        this.setPreferredSize(new Dimension(700, 450));
377
        this.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
373 378

  
374 379
        doSearch(null);
375 380
    }
......
400 405
        this.advancedExpression.set(null);
401 406
    }
402 407

  
403
    private void doSearch() {
408
    @Override
409
    public Expression getFilter() {
410
        Expression filter;
404 411
        int searchMode = this.tabSearchMode.getSelectedIndex();
405 412
        if (searchMode == 1) { // Avanzada
406
            Expression filter = this.advancedExpression.get();
407
            if (ExpressionUtils.isPhraseEmpty(filter)) {
408
                return;
409
            }
410
            doSearch(filter);
411
            return;
412
        }
413

  
414
        ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
415
        String relational = OPERATOR_OR;
416
        for (SearchFieldController searchField : searchFields) {
417
            if (searchField.getAttribute() != null && searchField.getValue() != null) {
418
                ExpressionBuilder.BinaryOperator cond = builder.binaryOperator(
419
                        searchField.getRelationalOperator(),
420
                        searchField.isAttributeAnExpression()
421
                        ? builder.custom(searchField.getAttribute())
422
                        : builder.column(searchField.getAttribute()),
423
                        builder.constant(searchField.getValue())
424
                );
425
                if (relational.equals(OPERATOR_AND)) {
426
                    builder.and(cond);
427
                } else {
428
                    builder.or(cond);
413
            filter = this.advancedExpression.get();
414
        } else {
415
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
416
            String relational = OPERATOR_OR;
417
            for (SearchFieldController searchField : searchFields) {
418
                if (searchField.getAttribute() != null && searchField.getValue() != null) {
419
                    ExpressionBuilder.BinaryOperator cond = builder.binaryOperator(
420
                            searchField.getRelationalOperator(),
421
                            searchField.isAttributeAnExpression()
422
                            ? builder.custom(searchField.getAttribute())
423
                            : builder.column(searchField.getAttribute()),
424
                            builder.constant(searchField.getValue())
425
                    );
426
                    if (relational.equals(OPERATOR_AND)) {
427
                        builder.and(cond);
428
                    } else {
429
                        builder.or(cond);
430
                    }
431
                    relational = searchField.getLogicalOperator();
429 432
                }
430
                relational = searchField.getLogicalOperator();
431 433
            }
434
            if (builder.isEmpty()) {
435
                filter = null;
436
            } else {
437
                filter = ExpressionUtils.createExpression(builder.toString());
438
            }
432 439
        }
433
        if (builder.isEmpty()) {
434
            doSearch(null);
435
            return;
440
        if (ExpressionUtils.isPhraseEmpty(filter)) {
441
            return null;
436 442
        }
437
        this.txtAdvancedExpression.setText(builder.toString());
438
        doSearch(ExpressionUtils.createExpression(builder.toString()));
443
        return filter;
439 444
    }
445
    
446
    private void doSearch() {
447
        Expression filter = this.getFilter();
448
        doSearch(filter);
449
    }
440 450

  
441 451
    private void doSearch(final Expression exp) {
442 452
        final MutableObject model = new MutableObject(null);
......
484 494
    }
485 495

  
486 496
    @Override
497
    public boolean setFilter(Expression filter) {
498
        try {
499
            if( this.advancedExpression==null ) {
500
                this.initComponents();
501
            }
502
            if( ExpressionUtils.isPhraseEmpty(filter) ) {
503
                this.clear();
504
                return true;
505
            }
506
            this.advancedExpression.set(filter);
507
            this.tabSearchMode.setSelectedIndex(1);
508
            
509
            Code code = filter.getCode();
510
            if( code.code()==Code.CALLER) {
511
                SearchFieldController searchField = this.searchFields.get(0);
512
                Code.Caller caller = (Code.Caller)code;
513
                if( searchField.isAValidRelationOperator(caller.name())) {
514
                    Code op1 = caller.parameters().get(0);
515
                    Code op2 = caller.parameters().get(1);
516
                    if( op1.code()==Code.IDENTIFIER && op2.code()==Code.CONSTANT ) {
517
                        if( searchField.setAttribute(((Code.Identifier)op1).name())>=0 ) {
518
                            searchField.setRelationalOperator(caller.name());
519
                            searchField.setValue(((Code.Constant)op2).value());
520
                            this.tabSearchMode.setSelectedIndex(0);
521
                        }
522
                    }
523
                }
524
            }
525
            FeaturesTableModel model = new FeaturesTableModel(this.getStore().getDefaultFeatureType());
526
            tblResults.setModel(model);
527
            lblMsg.setText("");
528
            return true;
529
        } catch(Exception ex) {
530
            LOGGER.warn("Can't set current search", ex);
531
            return false;
532
        }
533
    }
534
   
535
    @Override
536
    public void setCurrentSearch(Expression filter) {
537
        if( this.setFilter(filter) ) {
538
            doSearch();
539
        }
540
    }
541
    
542
    @Override
487 543
    public Expression getSearchForSelectedFeature() {
488 544
        if( this.searchFields==null ) {
489 545
            return null;
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/searchpanel/SearchFieldController.java
110 110
    private final JLabel lblLogicalOperators;
111 111
    private final JLabel lblRelationalOperators;
112 112
    private final JComboBox cboValue;
113
    private Object valueAssigned = null;
113 114

  
114 115
    private DropDown ddnFields;
115 116
    private DropDown ddnLogicalOperators;
......
146 147
        this.initComponents();
147 148
    }
148 149

  
150
    public boolean isAValidRelationOperator(String name) {
151
        for (LabeledValue relationalOperator : relationalOperators) {
152
            if( StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) {
153
                return true;
154
            }
155
        }
156
        return false;
157
    }
158
    
149 159
    private void initComponents() {
150 160
        try {
151 161
            ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
......
365 375
                    @Override
366 376
                    public void run() {
367 377
                        cboValue.setModel(model);
378
                        if( valueAssigned!=null ) {
379
                            cboValue.setSelectedItem(valueAssigned);
380
                            valueAssigned = null;
381
                        }
368 382
                        setEnabled(true);
369 383
                    }
370 384
                });
......
390 404
        return op.getValue();
391 405
    }
392 406

  
407
    public int setRelationalOperator(String name) {
408
        int n = 0;
409
        for (LabeledValue relationalOperator : relationalOperators) {
410
            if( StringUtils.equalsIgnoreCase(name, (CharSequence) relationalOperator.getValue())) {
411
                break;
412
            }
413
            n++;
414
        }
415
        if( this.relationalOperators.length<=n ) {
416
            return -1;
417
        }
418
        this.ddnRelationalOperators.setSelectedIndex(n);
419
        return n;
420
    }
421
    
393 422
    public String getLogicalOperator() {
394 423
        if (this.ddnLogicalOperators == null) {
395 424
            return null;
......
428 457
            return null;
429 458
        }
430 459
    }
431

  
460
    
461
    public void setValue(Object value) {
462
        this.cboValue.setSelectedItem(value);
463
        this.valueAssigned = value;
464
    }
465
    
432 466
    public boolean isAttributeAnExpression() {
433 467
        final FeatureAttribute attribute = (FeatureAttribute) this.ddnFields.getSelectedItem();
434 468
        if (attribute == null) {
......
448 482
        return attribute.getValue();
449 483
    }
450 484

  
451
    public void setAttribute(String name) {
485
    public int setAttribute(String name) {
452 486
        ComboBoxModel<FeatureAttribute> model = this.ddnFields.getModel();
453 487
        for (int i = 0; i < model.getSize(); i++) {
454 488
            FeatureAttribute x = model.getElementAt(i);
455 489
            if (StringUtils.equalsIgnoreCase(name, x.getValue())) {
456 490
                this.setAttribute(i);
457
                return;
491
                return i;
458 492
            }
459 493
        }
460 494
        this.setAttribute(-1);
495
        return -1;
461 496
    }
462 497

  
463 498
    public void setAttribute(int index) {

Also available in: Unified diff