use uuid of namedAreas in propertyToLabelMap of distr editor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / e4 / DistributionEditor.java
index 22f589c1496c86f6fd075f915766a42a2ab8cde5..875bdeb00debfc55357c2372ca6310828520286f 100755 (executable)
@@ -42,6 +42,7 @@ import org.eclipse.nebula.widgets.nattable.freeze.CompositeFreezeLayer;
 import org.eclipse.nebula.widgets.nattable.freeze.FreezeHelper;
 import org.eclipse.nebula.widgets.nattable.freeze.FreezeLayer;
 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
+import org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand;
 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider;
 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider;
 import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider;
@@ -74,25 +75,27 @@ import ca.odell.glazedlists.EventList;
 import eu.etaxonomy.cdm.api.service.IVocabularyService;
 import eu.etaxonomy.cdm.api.service.dto.TaxonDescriptionDTO;
 import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;
 import eu.etaxonomy.cdm.model.common.Language;
-import eu.etaxonomy.cdm.model.common.OrderedTermBase;
-import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
-import eu.etaxonomy.cdm.model.common.Representation;
-import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
-import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
-import eu.etaxonomy.cdm.model.common.TermVocabulary;
 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.model.term.OrderedTermBase;
+import eu.etaxonomy.cdm.model.term.OrderedTermComparator;
+import eu.etaxonomy.cdm.model.term.Representation;
+import eu.etaxonomy.cdm.model.term.TermIdInVocabularyComparator;
+import eu.etaxonomy.cdm.model.term.TermLanguageComparator;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.cdm.model.term.TermVocabulary;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.preference.Resources;
+import eu.etaxonomy.taxeditor.preference.TermOrder;
 import eu.etaxonomy.taxeditor.preference.wizard.AvailableDistributionWizard;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.StoreUtil;
@@ -106,7 +109,7 @@ import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
 public class DistributionEditor extends Composite {
 
 
-    private static final String DISTRIBUTION_EDITOR = "Distribution Editor";
+    private static final String DISTRIBUTION_EDITOR = "Distribution Editor"; //$NON-NLS-1$
     private static final String LOADING_TAXA = Messages.ChecklistEditor_LOAD_TAXA;
     private static final String UNKNOWN = Messages.ChecklistEditor_UNKNOWN;
     private static final String ELEMENT_COUNT = Messages.ChecklistEditor_ELEMENT_COUNT;
@@ -129,14 +132,14 @@ public class DistributionEditor extends Composite {
     private ListDataProvider<TaxonDistributionDTO> bodyDataProvider;
     private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
 
-    private boolean isShowRank;
+    private boolean isShowRank = false;
     private Integer countNodes;
     private Text searchText;
 
     EventList<TaxonDistributionDTO> taxonList;
-    Map<UUID,Map<NamedArea,Set<DescriptionElementBase>>> taxonDistributionMap = new HashMap();
+    Map<UUID,Map<NamedArea,Set<DescriptionElementBase>>> taxonDistributionMap = new HashMap<>();
 
-    List<TaxonDistributionDTO> descriptionsToSave = new ArrayList();
+    List<TaxonDistributionDTO> descriptionsToSave = new ArrayList<>();
 
 
     SortedSet<DefinedTermBase> areas;
@@ -159,6 +162,7 @@ public class DistributionEditor extends Composite {
      */
     public DistributionEditor(Composite parent, DistributionEditorPart part) {
         super(parent, SWT.NULL);
+        isShowRank = PreferencesUtil.getBooleanValue(PreferencePredicate.ShowRankInDistributionEditor.getKey());
         this.part = part;
         this.setLayout(new GridLayout());
         GridLayoutFactory.fillDefaults().applyTo(parent);
@@ -166,17 +170,14 @@ public class DistributionEditor extends Composite {
         Composite topComposite = new Composite(parent, SWT.NONE);
         createTopComposite(topComposite);
 
-        Composite bottomComposite = new Composite(parent, SWT.NONE);
-        GridLayoutFactory.fillDefaults().applyTo(bottomComposite);
+
 
         natTable = new NatTable(parent, false);
         parent.pack();
 
-
-//       // natTable = new NatTable(parent, viewportLayer);
-//        natTable = new NatTable(parent, false);
-
-        createStatusBar(parent);
+        Composite bottomComposite = new Composite(parent, SWT.NONE);
+        GridLayoutFactory.fillDefaults().applyTo(bottomComposite);
+        createStatusBar(bottomComposite);
     }
 
     public boolean isShowRank() {
@@ -225,6 +226,16 @@ public class DistributionEditor extends Composite {
 
     private void createStatusBar(Composite composite) {
         GridData gridData = new GridData();
+        gridData.horizontalSpan = 2;
+        gridData.grabExcessHorizontalSpace = true;
+        gridData.horizontalAlignment = GridData.FILL;
+        GridLayout gridLayout = new GridLayout();
+        gridLayout.numColumns = 2;
+        composite.setLayoutData(gridData);
+        composite.setLayout(gridLayout);
+
+
+        gridData = new GridData();
         gridData.horizontalSpan = 1;
         gridData.grabExcessHorizontalSpace = true;
         gridData.horizontalAlignment = GridData.FILL;
@@ -238,7 +249,7 @@ public class DistributionEditor extends Composite {
         statusLabelSourceReference.setLayoutData(gridData);
 
         if (defaultSource != null){
-            statusLabelSourceReference.setText("Default Source Reference: " + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
+            statusLabelSourceReference.setText(Messages.DistributionEditor_defaultSource + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
 
         }
     }
@@ -266,6 +277,8 @@ public class DistributionEditor extends Composite {
         //add default configuration because autoconfigure is set to false in constructor
         natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
 
+
+
         // override the default sort configuration and change the mouse bindings
         // to sort on a single click
 
@@ -305,8 +318,9 @@ public class DistributionEditor extends Composite {
         GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
 
         this.layout();
+        natTable.doCommand(new ClientAreaResizeCommand(natTable));
       //  boolean ok = natTable.doCommand(new ClientAreaResizeCommand(natTable));
-      //  System.out.println(ok);
+
     }
 
 
@@ -406,6 +420,7 @@ public class DistributionEditor extends Composite {
         GridData gridData2 = new GridData();
         gridData2.horizontalSpan = 1;
         gridData2.horizontalAlignment = SWT.RIGHT;
+
         button1.setLayoutData(gridData2);
 
 
@@ -437,11 +452,11 @@ public class DistributionEditor extends Composite {
 
             @Override
             public void widgetSelected(SelectionEvent event) {
-                defaultSource = part.getCdmEntitySession().load(ReferenceSelectionDialog.select(AbstractUtility.getShell(), null), true);
+                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
 
-                String defaultSourceStr = (defaultSource == null) ? "" : "Default Source Reference: " + defaultSource.getTitleCache();
+                String defaultSourceStr = (defaultSource == null) ? "" : Messages.DistributionEditor_defaultSource + defaultSource.getTitleCache(); //$NON-NLS-1$
                 if (defaultSourceStr.length()> 100){
-                    defaultSourceStr = defaultSourceStr.substring(0, 98) + "...";
+                    defaultSourceStr = defaultSourceStr.substring(0, 98) + "..."; //$NON-NLS-1$
                 }
                 statusLabelSourceReference.setText(defaultSourceStr);
 
@@ -450,6 +465,28 @@ public class DistributionEditor extends Composite {
 
         });
 
+        Button button3 = new Button(parent, SWT.PUSH );
+
+        button3.setLayoutData(gridData3);
+        button3.setText(Messages.ChecklistEditor_REMOVE_DEFAULT_SOURCE);
+        button2.setToolTipText(Messages.ChecklistEditor_REMOVE_DEFAULT_SOURCE_TOOLTIP);
+        button3.addSelectionListener(new SelectionAdapter() {
+
+            @Override
+            public void widgetSelected(SelectionEvent event) {
+                defaultSource = null;
+
+                String defaultSourceStr = (defaultSource == null) ? "" : Messages.DistributionEditor_defaultSource + defaultSource.getTitleCache(); //$NON-NLS-1$
+                if (defaultSourceStr.length()> 100){
+                    defaultSourceStr = defaultSourceStr.substring(0, 98) + "..."; //$NON-NLS-1$
+                }
+                statusLabelSourceReference.setText(defaultSourceStr);
+
+                button3.setBackground(EditorUtil.getColor(Resources.COLOR_CONTROL_SELECTED));
+            }
+
+        });
+
         parent.pack();
         return searchText;
     }
@@ -476,6 +513,11 @@ public class DistributionEditor extends Composite {
         GridLayout gridLayout = new GridLayout(3, false);
         gridLayout.marginWidth = 0;
         gridLayout.marginHeight = 0;
+        GridData gridData2 = new GridData();
+        gridData2.horizontalSpan = 1;
+        gridData2.horizontalAlignment = SWT.RIGHT;
+        gridData2.horizontalIndent = 3;
+        parent.setLayoutData(gridData2);
         parent.setLayout(gridLayout);
         searchText = createSearchBar(parent);
 
@@ -532,6 +574,9 @@ public class DistributionEditor extends Composite {
         }
 
         loadNamedAreas();
+        if (areas == null){
+            areas = new TreeSet<>();
+        }
         for (DefinedTermBase area: areas) {
             this.areaToColumnIndexMap.put(index++, (NamedArea)area);
             String areaLabel;
@@ -565,50 +610,70 @@ public class DistributionEditor extends Composite {
 
 
             //String areaLabel = area.getLabel();
-            String property = areaLabel;
+            String property = area.getUuid().toString();
             propertyToLabelMap.put(property, areaLabel);
         }
+
     }
 
     private SortedSet<DefinedTermBase> loadNamedAreas() {
         //IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
 
-        String valuesAreas = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaTerms.getKey());
+        String valuesAreas = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaTerms.getKey(), true);
         String values = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
+        Set<UUID> uuidList = new HashSet<UUID>();
+        String[] split;
+        List<String> listValue;
+        List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
         if (values != null && values != "") { //$NON-NLS-1$
-            String[] split = values.split(";"); //$NON-NLS-1$
-            List<String> listValue = Arrays.asList(split);
-            List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
-            Set<UUID> uuidList = new HashSet<UUID>();
+            split = values.split(";"); //$NON-NLS-1$
+            listValue = Arrays.asList(split);
+
+
             UUID uuid;
             for(String s : listValue){
                 uuid = UUID.fromString(s);
                 uuidList.add(uuid);
 
             }
-            IVocabularyService service =  CdmStore.getService(IVocabularyService.class);
-            List<TermVocabulary> vocs = service.find(uuidList);
-            split = valuesAreas.split(";");
+        }
+
+        List<TermVocabulary> vocs = new ArrayList<>();
+        IVocabularyService service =  CdmStore.getService(IVocabularyService.class);
+        if (uuidList.isEmpty()){
+            List<TermVocabulary<DefinedTermBase>> vocList = CdmStore.getService(IVocabularyService.class).findByTermType(TermType.NamedArea, null);
+            vocs.addAll(vocList);
+        }else{
+            vocs= service.find(uuidList);
+        }
+        for (TermVocabulary voc: vocs){
+            termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
+        }
+        List<DefinedTermBase> filteredList = new ArrayList();
+        if (valuesAreas != null && valuesAreas != "") {
+            split = valuesAreas.split(";"); //$NON-NLS-1$
             listValue = Arrays.asList(split);
-            for (TermVocabulary voc: vocs){
-                termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
-            }
-            List<DefinedTermBase> filteredList = new ArrayList();
+
             for (DefinedTermBase area: termlist){
                 if (listValue.contains(area.getUuid().toString())) {
                     filteredList.add(area);
                 }
 
             }
+        }else{
+            filteredList.addAll(termlist);
+        }
 
-            if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
-                areas =  getTermsOrderedByVocabularyOrder(filteredList);
-            } else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
-                areas = getTermsOrderedByIdInVocabulary(filteredList);
-            }else{
-                areas =  getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
-            }
+
+        String sortOrder = PreferencesUtil.getSortNamedAreasInDistributionEditor();
+        if (sortOrder.equals(TermOrder.Natural.getKey())){
+            areas =  getTermsOrderedByVocabularyOrder(filteredList);
+        } else if (sortOrder.equals(TermOrder.IdInVoc.getKey())){
+            areas = getTermsOrderedByIdInVocabulary(filteredList);
+        }else{
+            areas =  getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
         }
+
         return null;
     }
 
@@ -619,6 +684,7 @@ public class DistributionEditor extends Composite {
         if(listTerm != null){
             result.addAll(listTerm);
         }
+
         return result;
     }
 
@@ -689,13 +755,13 @@ public class DistributionEditor extends Composite {
         return defaultSource;
     }
 
-    /**
-     * @param result
-     */
-    public void reloadDistributions() {
-       loadDistributions(taxonList);
-
-    }
+//    /**
+//     * @param result
+//     */
+//    public void reloadDistributions() {
+//       loadDistributions(taxonList);
+//
+//    }