ref #7095 Extract row comparator to own class file
authorPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 25 Jan 2018 15:26:27 +0000 (16:26 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 25 Jan 2018 18:11:20 +0000 (19:11 +0100)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/CharacterMatrix.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/DescriptionTreeFormat.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/MatrixRowComparator.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/RowWrapper.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/SpecimenColumnPropertyAccessor.java

index 7e472d667b950a579e8568ba46315453bce23971..f4b93c29041da1ddf200793b7eed8820eb128183 100644 (file)
@@ -43,6 +43,7 @@ import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.window.Window;
 import org.eclipse.nebula.widgets.nattable.NatTable;
 import org.eclipse.nebula.widgets.nattable.command.StructuralRefreshCommand;
+import org.eclipse.nebula.widgets.nattable.command.VisualRefreshCommand;
 import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
 import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
 import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
@@ -387,6 +388,7 @@ ICdmEntitySessionEnabled{
         btnCollapseAll.setEnabled(isTree);
         btnExpandAll.setEnabled(isTree);
         natTable.doCommand(new StructuralRefreshCommand());
+        natTable.doCommand(new VisualRefreshCommand());
     }
 
     public void init(UUID workingSetUuid, boolean treeView) {
@@ -407,7 +409,7 @@ ICdmEntitySessionEnabled{
         }
         // use the SortedList constructor with 'null' for the Comparator
         // because the Comparator will be set by configuration
-        SortedList<Object> sortedList = new SortedList<>(descriptions, null);
+        SortedList<Object> sortedList = new SortedList<>(descriptions, new MatrixRowComparator());
         // wrap the SortedList with the TreeList
         TreeList<Object> treeList = new TreeList(sortedList, new DescriptionTreeFormat(workingSet.getMaxRank()), TreeList.NODES_START_EXPANDED);
         /**
index 6fd9705be3386e56dc803dcb54463a7f07191015..6790a0a550b515f98528b44ad11d7601f265beb2 100644 (file)
@@ -16,11 +16,7 @@ import java.util.Set;
 import ca.odell.glazedlists.TreeList;
 import eu.etaxonomy.cdm.model.name.Rank;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
-import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
-import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
 /**
  * @author pplitzner
@@ -29,26 +25,17 @@ import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
  */
  public class DescriptionTreeFormat implements TreeList.Format<Object> {
 
-     private Comparator<TaxonNode> comparator;
-     private Rank minRank;
      private Rank maxRank;
 
      public DescriptionTreeFormat(Rank maxRank) {
          this.maxRank = maxRank;
-         if (PreferencesUtil.getSortNodesNaturally()){
-             comparator = new TaxonNaturalComparator();
-         } else if (PreferencesUtil.getSortNodesStrictlyAlphabetically()){
-             comparator = new TaxonNodeByNameComparator();
-         }else {
-             comparator = new TaxonNodeByRankAndNameComparator();
-         }
      }
 
      @Override
      public void getPath(List path, Object element) {
          if(element instanceof RowWrapper){
              //TODO: check for multiple taxon nodes in multiple classifications
-             Taxon taxon = (Taxon) ((RowWrapper) element).getAssociatedTaxa().iterator().next();
+             Taxon taxon = ((RowWrapper) element).getAssociatedTaxon();
              Set<TaxonNode> taxonNodes = taxon.getTaxonNodes();
              if(taxonNodes!=null){
                  TaxonNode node = taxonNodes.iterator().next();
@@ -83,16 +70,7 @@ import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
      @Override
      public Comparator<Object> getComparator(int depth) {
-         return new Comparator<Object>() {
-
-             @Override
-             public int compare(Object o1, Object o2) {
-                 if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){
-                     return comparator.compare((TaxonNode)o1, (TaxonNode)o2);
-                 }
-                 return o1.hashCode()-o2.hashCode();
-             }
-
-         };
+         return new MatrixRowComparator();
      }
+
  }
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/MatrixRowComparator.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/MatrixRowComparator.java
new file mode 100644 (file)
index 0000000..3054c7d
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.editor.workingSet.matrix;
+
+import java.util.Comparator;
+
+import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator;
+import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+
+/**
+ * @author pplitzner
+ * @since Jan 25, 2018
+ *
+ */
+public class MatrixRowComparator implements Comparator<Object>{
+
+    private Comparator<TaxonNode> comparator;
+
+    public MatrixRowComparator() {
+        if (PreferencesUtil.getSortNodesNaturally()){
+            comparator = new TaxonNaturalComparator();
+        } else if (PreferencesUtil.getSortNodesStrictlyAlphabetically()){
+            comparator = new TaxonNodeByNameComparator();
+        }else {
+            comparator = new TaxonNodeByRankAndNameComparator();
+        }
+    }
+
+    @Override
+    public int compare(Object o1, Object o2) {
+        if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){
+            return comparator.compare((TaxonNode)o1, (TaxonNode)o2);
+        }
+        if(o1 instanceof RowWrapper && o2 instanceof RowWrapper){
+            RowWrapper rowWrapper1 = (RowWrapper)o1;
+            RowWrapper rowWrapper2 = (RowWrapper)o2;
+            TaxonNode node1 = rowWrapper1.getAssociatedTaxon().getTaxonNodes().iterator().next().getParent();
+            TaxonNode node2 = rowWrapper2.getAssociatedTaxon().getTaxonNodes().iterator().next().getParent();
+            if(node1!=null && node2!=null){
+                return comparator.compare(node1, node2);
+            }
+        }
+        return o1.hashCode()-o2.hashCode();
+    }
+
+}
index 1d9edbfda0f1db51e5651422b557696249487216..4be78b86e5629549daacb7c4463450177b39fff8 100644 (file)
@@ -17,6 +17,7 @@ import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.store.CdmStore;
@@ -30,7 +31,7 @@ public class RowWrapper {
 
     private SpecimenDescription description;
 
-    private Collection<TaxonBase<?>> associatedTaxa;
+    private Taxon associatedTaxon;
     private FieldUnit fieldUnit;
     private String identifier;
     private NamedArea country;
@@ -41,7 +42,10 @@ public class RowWrapper {
         IOccurrenceService occurrenceService = CdmStore.getService(IOccurrenceService.class);
         SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(description.getDescribedSpecimenOrObservation(), SpecimenOrObservationBase.class);
         if(specimen!=null){
-            associatedTaxa = occurrenceService.listAssociatedTaxa(specimen, null, null, null, null);
+            Collection<TaxonBase<?>> associatedTaxa = occurrenceService.listAssociatedTaxa(specimen, null, null, null, null);
+            if(associatedTaxa!=null){
+                associatedTaxon = (Taxon) associatedTaxa.iterator().next();
+            }
             Collection<FieldUnit> fieldUnits = occurrenceService.getFieldUnits(specimen.getUuid());
             if(fieldUnits.size()!=1){
                 MessagingUtils.error(RowWrapper.class, "More than one or no field unit found for specimen", null);
@@ -62,8 +66,8 @@ public class RowWrapper {
         return description;
     }
 
-    public Collection<TaxonBase<?>> getAssociatedTaxa() {
-        return associatedTaxa;
+    public Taxon getAssociatedTaxon() {
+        return associatedTaxon;
     }
 
     public FieldUnit getFieldUnit() {
index 46fe34aa6be58f7851a18809fbc7e4d8917fe4b5..c1db2bdf7be48a589009b666f73a948d0cc59c48 100644 (file)
@@ -44,7 +44,7 @@ public class SpecimenColumnPropertyAccessor implements IColumnPropertyAccessor<O
             RowWrapper rowWrapper = (RowWrapper)rowObject;
             switch (columnIndex) {
             case 0:
-                return rowWrapper.getAssociatedTaxa();
+                return rowWrapper.getAssociatedTaxon();
             case 1:
                 return rowWrapper.getFieldUnit();
             case 2: