Revision 44842 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/SearchConditionFieldController.java

View differences:

SearchConditionFieldController.java
7 7
import java.awt.event.MouseAdapter;
8 8
import java.awt.event.MouseEvent;
9 9
import java.util.ArrayList;
10
import java.util.Arrays;
10 11
import java.util.HashMap;
11 12
import java.util.List;
12 13
import java.util.Map;
13 14
import java.util.Objects;
15
import java.util.logging.Level;
14 16
import javax.json.Json;
15 17
import javax.json.JsonArray;
16 18
import javax.json.JsonArrayBuilder;
......
23 25
import javax.swing.JLabel;
24 26
import javax.swing.JOptionPane;
25 27
import javax.swing.SwingUtilities;
28
import static org.apache.commons.lang3.ArrayUtils.toStringArray;
26 29
import org.apache.commons.lang3.StringUtils;
27 30
import org.gvsig.expressionevaluator.ExpressionBuilder;
28 31
import org.gvsig.fmap.dal.DALLocator;
......
153 156

  
154 157
    private LabeledValue[] relationalOperators;
155 158
    private LabeledValue[] logicalOperators;
159
    private final int SIZE_ORDERED_ATTRIBUTES = 20;
156 160

  
157 161
    public SearchConditionFieldController(
158 162
            FeatureStore store,
......
231 235
            List<Search.OrderedAttribute> orderedAttributes = search.getOrderedAttributes(
232 236
                    Search.BASIC_TYPES_FILTER,
233 237
                    Search.STR_INT_LONG_LABEL_ORDER,
234
                    20
238
                    SIZE_ORDERED_ATTRIBUTES
235 239
            );
236 240
            List<ImageIcon> icons = new ArrayList<>();
237 241
//            DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
......
547 551
        final Field field = (Field) this.ddnFields.getSelectedItem();
548 552
        return field;
549 553
    }
550
  
551
  public int setAttribute(String name) {
554

  
555
    public int setAttribute(String name) {
552 556
        ComboBoxModel<Field> model = this.ddnFields.getModel();
553 557
        for (int i = 0; i < model.getSize(); i++) {
554 558
            Field x = model.getElementAt(i);
......
559 563
        }
560 564
        this.setAttribute(-1);
561 565
        return -1;
562
  }
563
    
566
    }
567

  
564 568
    public void setAttribute(int index) {
565 569
        try {
566 570
            this.ddnFields.setSelectedIndex(index);
......
569 573
        }
570 574
        doUpdateValuesList();
571 575
    }
572
    
573
    
574
    public void setAttributePath(String[] pathNames) {
575
        if (pathNames.length==1) {
576
            String name = pathNames[pathNames.length-1];
577
            this.setAttribute(name);
578
            return;
579
        } else {
580
            //TODO
581
            throw new UnsupportedOperationException("Not able to set path attribute");
576

  
577
    public int setAttributePath(String[][] pathNames) {
578
        // [[attributeName, storeName],...]
579
        try {
580
            if (pathNames.length == 1) {
581
                String[] path = pathNames[pathNames.length - 1];
582
                String name = path[0];
583
                int index = this.setAttribute(name);
584
                if (index == -1) {
585
                    try {
586
                        FeatureAttributeDescriptor attrDescriptor = store.getDefaultFeatureType().getAttributeDescriptor(name);
587
                        FeatureAttributeDescriptor[] attributePath = new FeatureAttributeDescriptor[]{attrDescriptor};
588
                        doAddAndSelect(store, store.getDefaultFeatureType().getAttributeDescriptor(name), attributePath);
589
                    } catch (Exception ex) {
590
                        LOGGER.warn("Not able to set single path into controller", ex);
591
                        return -1;
592
                    }
593
                }
594
                return index;
595
            } else {
596
                ComboBoxModel<Field> model = this.ddnFields.getModel();
597
                String[] singleArrayPathNameDescriptors = new String[pathNames.length];
598
                for (int i = 0; i < pathNames.length; i++) {
599
                    singleArrayPathNameDescriptors[i] = pathNames[i][0];
600
                }
601
                // check the drop
602
                for (int i = 0; i < model.getSize(); i++) {
603
                    Field x = model.getElementAt(i);
604
                    String[] arrayDescriptors = new String[x.getPath().length];
605
                    FeatureAttributeDescriptor[] path = x.getPath();
606
                    for (int j = 0; j < path.length; j++) {
607
                        arrayDescriptors[j] = path[j].getName();
608
                    }
609
                    if (Arrays.equals(singleArrayPathNameDescriptors, arrayDescriptors)) {
610
                        this.setAttribute(i);
611
                        return i;
612
                    }
613
                }
614
                // if not, addit to the drop
615
                DataManager dataManager = DALLocator.getDataManager();
616
                String tableName = pathNames[pathNames.length - 1][1]; // del ultimo path, coger el nombre tabla
617
                FeatureStore theStore = (FeatureStore) dataManager.getStoresRepository().getStore(tableName);
618
                String attributeName = pathNames[pathNames.length - 1][0]; // del ultimo path, coger el nombre attribute
619
                if (theStore != null) {
620
                    FeatureAttributeDescriptor attr;
621
                    try {
622
                        attr = theStore.getDefaultFeatureType().getAttributeDescriptor(attributeName);
623
                        FeatureAttributeDescriptor[] attributePath = new FeatureAttributeDescriptor[2];
624
                        String firstAttrName = pathNames[0][0];
625
                        FeatureAttributeDescriptor firstAttr = store.getDefaultFeatureType().getAttributeDescriptor(firstAttrName);
626
                        attributePath[0] = firstAttr;
627

  
628
                        attributePath[1] = attr;
629
                        doAddAndSelect(theStore, attr, attributePath);
630
                        return SIZE_ORDERED_ATTRIBUTES - 1;
631
                    } catch (Exception ex) {
632
                        LOGGER.warn("Not able to set foreign path into controller", ex);
633
                    }
634

  
635
                }
636

  
637
            }
638
        } catch (Exception ex) {
639
            LOGGER.warn("Controller not set.", ex);
582 640
        }
641
        this.setAttribute(-1);
642
        return -1;
583 643
    }
584
    
644

  
585 645
    private boolean isTheSameStore(DataStore store1, DataStore store2) {
586 646
        String store1FullName = store1.getFullName();
587 647
        String store2FullName = store2.getFullName();
......
763 823

  
764 824
        JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
765 825
        for (FeatureAttributeDescriptor featureAttributeDescriptor : this.getCurrentField().getPath()) {
826
            JsonArrayBuilder pathArray = Json.createArrayBuilder();
827

  
828
            //first value: name field
766 829
            String fieldName = featureAttributeDescriptor.getName();
767
            arrayBuilder.add(fieldName);
830
            pathArray.add(fieldName);
831
            //second value: name store
832
            String storeName = featureAttributeDescriptor.getFeatureType().getStore().getName();
833
            pathArray.add(storeName);
834

  
835
            arrayBuilder.add(pathArray.build());
768 836
        }
769 837
        String relational = this.getRelationalOperator();
770 838
        Object value = this.getValue();
......
783 851
    }
784 852

  
785 853
    public void fromJson(JsonObject jsonState) {
786
        if (jsonState==null) {
854
        if (jsonState == null) {
787 855
            return;
788 856
        }
789 857

  
790 858
        JsonArray fieldPath = jsonState.getJsonArray("fieldPath");
791
//        String[] arrayStringPath = ArrayUtils.toStringArray(fieldPath.toArray());
792
        String[] arrayNew = new String[fieldPath.size()];
859

  
860
        // array of arrays
861
        String[][] arrayNew = new String[fieldPath.size()][2];
793 862
        for (int i = 0; i < fieldPath.size(); i++) {
794
            arrayNew[i] = fieldPath.getString(i);
863
            String[] arrayField = new String[2];
864
            arrayField[0] = fieldPath.getJsonArray(i).getString(0);
865
            arrayField[1] = fieldPath.getJsonArray(i).getString(1);
866
            arrayNew[i] = arrayField;
795 867
        }
796
        this.setAttributePath(arrayNew);
868
        this.setAttributePath(arrayNew);  //usar el doAddAndSelect
797 869

  
798 870
        String relational = jsonState.getString("relational");
799 871
        this.setRelationalOperator(relational);

Also available in: Unified diff