diff gwt-client/src/main/java/org/dive4elements/river/client/client/ui/AbstractPairRecommendationPanel.java @ 9580:839b2aa84dd0

minuend/subtrahend-UI-switch
author gernotbelger
date Tue, 08 Jan 2019 17:01:09 +0100
parents 84397da33d17
children
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/AbstractPairRecommendationPanel.java	Tue Dec 11 17:08:36 2018 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/AbstractPairRecommendationPanel.java	Tue Jan 08 17:01:09 2019 +0100
@@ -49,8 +49,7 @@
  * Panel containing a Grid and a "next" button. The Grid is fed by a
  * DatacagePairWidget which is put in the input-helper area.
  */
-public abstract class AbstractPairRecommendationPanel
-extends      TextProvider {
+public abstract class AbstractPairRecommendationPanel extends TextProvider {
 
     /**
      * Allows for abstraction on how to handle/serialize the recommendation and the used factories.
@@ -58,12 +57,13 @@
      *
      * @author Gernot Belger
      */
-    public static interface IRecommendationInfo	{
+    public static interface IRecommendationInfo {
 
         String getFactory(String originalFactory);
 
         /**
          * Separate factory for the 'createDataString' method, because in the case of waterlevels. See HOTFIX/FIXME there.
+         *
          * @param recommendation
          */
         String getDataStringFactory(Recommendation recommendation);
@@ -75,8 +75,7 @@
         void adjustRecommendation(Recommendation recommendation);
     }
 
-    public static interface IValidator
-    {
+    public static interface IValidator {
         List<String> validate(ListGrid differencesList, FLYSConstants msgProvider);
     }
 
@@ -93,16 +92,13 @@
      * List to track previously selected but now removed pairs. (Needed to
      * be able to identify artifacts that can be removed from the collection.
      */
-    private final List<RecommendationPairRecord> removedPairs =
-            new ArrayList<RecommendationPairRecord>();
+    private final List<RecommendationPairRecord> removedPairs = new ArrayList<RecommendationPairRecord>();
 
     /** Service handle to clone and add artifacts to collection. */
-    private final LoadArtifactServiceAsync loadArtifactService = GWT.create(
-            org.dive4elements.river.client.client.services.LoadArtifactService.class);
+    private final LoadArtifactServiceAsync loadArtifactService = GWT.create(org.dive4elements.river.client.client.services.LoadArtifactService.class);
 
     /** Service to remove artifacts from collection. */
-    private final RemoveArtifactServiceAsync removeArtifactService = GWT.create(
-            org.dive4elements.river.client.client.services.RemoveArtifactService.class);
+    private final RemoveArtifactServiceAsync removeArtifactService = GWT.create(org.dive4elements.river.client.client.services.RemoveArtifactService.class);
 
     private final IRecommendationInfo leftInfo;
 
@@ -110,12 +106,19 @@
 
     private final IValidator validator;
 
+    protected final boolean minuendSubtrahendUiReversed;
+
     /**
-     * @param Validates the content of this form when the user clicks 'apply'
-     * @param leftInfo Delegate for handling the left part of the recommendation-pair
-     * @param rightInfo Delegate for handling the right part of the recommendation-pair
+     * @param Validates
+     *            the content of this form when the user clicks 'apply'
+     * @param leftInfo
+     *            Delegate for handling the left part of the recommendation-pair
+     * @param rightInfo
+     *            Delegate for handling the right part of the recommendation-pair
      */
-    public AbstractPairRecommendationPanel(final User user, final IValidator validator, final IRecommendationInfo leftInfo, final IRecommendationInfo rightInfo ) {
+    public AbstractPairRecommendationPanel(final User user, final IValidator validator, final IRecommendationInfo leftInfo, final IRecommendationInfo rightInfo,
+            final boolean minuendSubtrahendUiReversed) {
+        this.minuendSubtrahendUiReversed = minuendSubtrahendUiReversed;
         this.user = user;
         this.validator = validator;
         this.leftInfo = leftInfo;
@@ -129,7 +132,9 @@
 
     /**
      * Remove first occurrence of "[" and "]" (if both do occur).
-     * @param value String to be stripped of [] (might be null).
+     *
+     * @param value
+     *            String to be stripped of [] (might be null).
      * @return input string but with [ and ] removed, or input string if no
      *         brackets were found.
      * @see StringUtil.unbracket
@@ -137,10 +142,11 @@
     // FIXME: check if this is the same as STringUItils#unbracket
     private static final String unbracket(final String value) {
         // null- guard.
-        if (value == null) return value;
+        if (value == null)
+            return value;
 
         final int start = value.indexOf("[");
-        final int end   = value.indexOf("]");
+        final int end = value.indexOf("]");
 
         if (start < 0 || end < 0) {
             return value;
@@ -151,7 +157,9 @@
 
     /**
      * Create a recommendation from a string representation of it.
-     * @param from string in format as shown above.
+     *
+     * @param from
+     *            string in format as shown above.
      * @param leftInfo2
      * @return recommendation from input string.
      */
@@ -159,38 +167,33 @@
         // TODO Construct "real" filter.
         final String[] parts = unbracket(from).split(";");
         final Recommendation.Filter filter = new Recommendation.Filter();
-        final Recommendation.Facet  facet  = new Recommendation.Facet(
-                parts[1],
-                parts[2]);
+        final Recommendation.Facet facet = new Recommendation.Facet(parts[1], parts[2]);
 
         final List<Recommendation.Facet> facets = new ArrayList<Recommendation.Facet>();
         facets.add(facet);
         filter.add("longitudinal_section", facets);
 
-        final String factory = info.getFactory( parts[1] );
+        final String factory = info.getFactory(parts[1]);
 
-        final  Recommendation r = new Recommendation(factory, parts[0], this.artifact.getUuid(), filter);
+        final Recommendation r = new Recommendation(factory, parts[0], this.artifact.getUuid(), filter);
         r.setDisplayName(parts[3]);
         return r;
     }
 
-
     /**
      * Add RecomendationPairRecords from input String to the ListGrid.
      */
-    private void populateGridFromString(final String from){
+    private void populateGridFromString(final String from) {
         // Split this string.
         // Create according recommendations and display strings.
         final String[] recs = from.split("#");
-        if (recs.length % 2 != 0) return;
-        for (int i = 0; i < recs.length; i+=2) {
-            final Recommendation minuend =
-                    createRecommendationFromString(recs[i+0], this.leftInfo);
-            final Recommendation subtrahend =
-                    createRecommendationFromString(recs[i+1], this.rightInfo);
+        if (recs.length % 2 != 0)
+            return;
+        for (int i = 0; i < recs.length; i += 2) {
+            final Recommendation minuend = createRecommendationFromString(recs[i + 0], this.leftInfo);
+            final Recommendation subtrahend = createRecommendationFromString(recs[i + 1], this.rightInfo);
 
-            final RecommendationPairRecord pr = new RecommendationPairRecord(
-                    minuend, subtrahend);
+            final RecommendationPairRecord pr = new RecommendationPairRecord(minuend, subtrahend);
             // This Recommendation Pair comes from the data string and was thus
             // already cloned.
             pr.setIsAlreadyLoaded(true);
@@ -200,7 +203,9 @@
 
     /**
      * Creates the graphical representation and interaction widgets for the data.
-     * @param dataList the data.
+     *
+     * @param dataList
+     *            the data.
      * @return graphical representation and interaction widgets for data.
      */
     @Override
@@ -216,18 +221,18 @@
     }
 
     /**
-     * Creates the individual parts of the input-helper area ('Eingabeunterstützung') for choosing the content of this widget.
+     * Creates the individual parts of the input-helper area ('Eingabeunterstützung') for choosing the content of this
+     * widget.
      */
     protected abstract Canvas createChooserWidgets(final Canvas widget, final DataList dataList, final User auser, final ListGrid diffList);
 
     private void populateGrid(final DataList dataList) {
-        final Data data     = dataList.get(0);
+        final Data data = dataList.get(0);
         this.dataName = data.getLabel();
         for (int i = 0; i < dataList.size(); i++) {
             if (dataList.get(i) != null && dataList.get(i).getItems() != null) {
                 if (dataList.get(i).getItems() != null) {
-                    populateGridFromString(
-                            dataList.get(i).getItems()[0].getStringValue());
+                    populateGridFromString(dataList.get(i).getItems()[0].getStringValue());
                 }
             }
         }
@@ -242,7 +247,7 @@
      * Creates layout with grid that displays selection inside.
      */
     protected final Canvas createWidget() {
-        final VLayout layout  = new VLayout();
+        final VLayout layout = new VLayout();
         this.differencesList = new ListGrid();
 
         this.differencesList.setCanEdit(false);
@@ -251,42 +256,40 @@
         this.differencesList.setHeight(150);
         this.differencesList.setShowAllRecords(true);
 
-        final ListGridField nameField    = new ListGridField("first",  "Minuend");
-        final ListGridField capitalField = new ListGridField("second", "Subtrahend");
+        final ListGridField nameField = new ListGridField(this.minuendSubtrahendUiReversed ? "second" : "first", "Minuend");
+        final ListGridField capitalField = new ListGridField(this.minuendSubtrahendUiReversed ? "first" : "second", "Subtrahend");
         // Track removed rows, therefore more or less reimplement
         // setCanRecomeRecords.
-        final ListGridField removeField  =
-                new ListGridField("_removeRecord", "Remove Record"){{
-                    setType(ListGridFieldType.ICON);
-                    setIcon(GWT.getHostPageBaseURL() + msg().removeFeature());
-                    setCanEdit(false);
-                    setCanFilter(false);
-                    setCanSort(false);
-                    setCanGroupBy(false);
-                    setCanFreeze(false);
-                    setWidth(25);
-                }};
-
-                this.differencesList.setFields(new ListGridField[] {nameField,
-                        capitalField, removeField});
+        final ListGridField removeField = new ListGridField("_removeRecord", "Remove Record") {
+            {
+                setType(ListGridFieldType.ICON);
+                setIcon(GWT.getHostPageBaseURL() + msg().removeFeature());
+                setCanEdit(false);
+                setCanFilter(false);
+                setCanSort(false);
+                setCanGroupBy(false);
+                setCanFreeze(false);
+                setWidth(25);
+            }
+        };
+        this.differencesList.setFields(new ListGridField[] { nameField, capitalField, removeField });
 
-                this.differencesList.addRecordClickHandler(new RecordClickHandler() {
-                    @Override
-                    public void onRecordClick(final RecordClickEvent event) {
-                        // Just handle remove-clicks
-                        if(!event.getField().getName().equals(removeField.getName())) {
-                            return;
-                        }
-                        trackRemoved(event.getRecord());
-                        event.getViewer().removeData(event.getRecord());
-                    }
-                });
-                layout.addMember(this.differencesList);
+        this.differencesList.addRecordClickHandler(new RecordClickHandler() {
+            @Override
+            public void onRecordClick(final RecordClickEvent event) {
+                // Just handle remove-clicks
+                if (!event.getField().getName().equals(removeField.getName())) {
+                    return;
+                }
+                trackRemoved(event.getRecord());
+                event.getViewer().removeData(event.getRecord());
+            }
+        });
+        layout.addMember(this.differencesList);
 
-                return layout;
+        return layout;
     }
 
-
     /**
      * Add record to list of removed records.
      */
@@ -314,40 +317,38 @@
 
         final ListGridRecord[] records = this.differencesList.getRecords();
 
-        final List<Recommendation> ar  = new ArrayList<Recommendation>();
+        final List<Recommendation> ar = new ArrayList<Recommendation>();
         final List<Recommendation> all = new ArrayList<Recommendation>();
 
         for (final ListGridRecord record : records) {
-            final RecommendationPairRecord r =
-                    (RecommendationPairRecord) record;
+            final RecommendationPairRecord r = (RecommendationPairRecord) record;
             // Do not add "old" recommendations.
             if (!r.isAlreadyLoaded()) {
                 // Check whether one of those is a dike or similar.
                 // TODO differentiate and merge: new clones, new, old.
                 final Recommendation firstR = r.getFirst();
                 this.leftInfo.adjustRecommendation(firstR);
-
                 final Recommendation secondR = r.getSecond();
                 this.rightInfo.adjustRecommendation(secondR);
+
                 ar.add(firstR);
                 ar.add(secondR);
-            }
-            else {
+            } else {
                 all.add(r.getFirst());
                 all.add(r.getSecond());
             }
         }
 
         final Recommendation[] toClone = ar.toArray(new Recommendation[ar.size()]);
-        final Recommendation[] toUse   = all.toArray(new Recommendation[all.size()]);
+        final Recommendation[] toUse = all.toArray(new Recommendation[all.size()]);
 
         // Find out whether "old" artifacts have to be removed.
         final List<String> artifactIdsToRemove = new ArrayList<String>();
-        for (final RecommendationPairRecord rp: this.removedPairs) {
-            Recommendation first  = rp.getFirst();
+        for (final RecommendationPairRecord rp : this.removedPairs) {
+            Recommendation first = rp.getFirst();
             Recommendation second = rp.getSecond();
 
-            for (final Recommendation recommendation: toUse) {
+            for (final Recommendation recommendation : toUse) {
                 if (first != null && first.getIDs().equals(recommendation.getIDs())) {
                     first = null;
                 }
@@ -369,15 +370,13 @@
 
         // Remove old artifacts, if any. Do this asychronously without much
         // feedback.
-        for(final String uuid: artifactIdsToRemove) {
-            this.removeArtifactService.remove(this.collection,
-                    uuid,
-                    locale,
-                    new AsyncCallback<Collection>() {
+        for (final String uuid : artifactIdsToRemove) {
+            this.removeArtifactService.remove(this.collection, uuid, locale, new AsyncCallback<Collection>() {
                 @Override
                 public void onFailure(final Throwable caught) {
                     GWT.log("RemoveArtifact (" + uuid + ") failed.");
                 }
+
                 @Override
                 public void onSuccess(final Collection coll) {
                     GWT.log("RemoveArtifact succeeded");
@@ -387,28 +386,25 @@
 
         // Clone new ones (and spawn statics), go forward.
         this.parameterList.lockUI();
-        this.loadArtifactService.loadMany(
-                this.collection,
-                toClone,
-                //"staticwkms" and "waterlevel"
-                null,
-                locale,
-                new AsyncCallback<Artifact[]>() {
+        this.loadArtifactService.loadMany(this.collection, toClone,
+                // "staticwkms" and "waterlevel"
+                null, locale, new AsyncCallback<Artifact[]>() {
+
                     @Override
                     public void onFailure(final Throwable caught) {
                         caught.printStackTrace();
                         GWT.log("Failure of cloning with factories!");
                         AbstractPairRecommendationPanel.this.parameterList.unlockUI();
                     }
+
                     @Override
                     public void onSuccess(final Artifact[] artifacts) {
-                        GWT.log("Successfully cloned " + toClone.length +
-                                " with factories.");
+                        GWT.log("Successfully cloned " + toClone.length + " with factories.");
 
-                        fireStepForwardEvent(new StepForwardEvent(
-                                getData(toClone, artifacts, toUse)));
+                        fireStepForwardEvent(new StepForwardEvent(getData(toClone, artifacts, toUse)));
                         AbstractPairRecommendationPanel.this.parameterList.unlockUI();
                     }
+
                 });
     }
 
@@ -416,19 +412,18 @@
      * Create Data and DataItem from selection (a long string with identifiers
      * to construct diff-pairs).
      *
-     * @param newRecommendations "new" recommendations (did not survive a
-     *        backjump).
-     * @param newArtifacts artifacts cloned from newRecommendations.
-     * @param oldRecommendations old recommendations that survived a backjump.
+     * @param newRecommendations
+     *            "new" recommendations (did not survive a
+     *            backjump).
+     * @param newArtifacts
+     *            artifacts cloned from newRecommendations.
+     * @param oldRecommendations
+     *            old recommendations that survived a backjump.
      *
      * @return dataitem with a long string with identifiers to construct
      *         diff-pairs.
      */
-    protected final Data[] getData(
-            final Recommendation[] newRecommendations,
-            final Artifact[] newArtifacts,
-            final Recommendation[] oldRecommendations)
-    {
+    protected final Data[] getData(final Recommendation[] newRecommendations, final Artifact[] newArtifacts, final Recommendation[] oldRecommendations) {
         // Construct string with info about selections.
         String dataItemString = "";
         for (int i = 0; i < newRecommendations.length; i++) {
@@ -437,7 +432,7 @@
             final String uuid = newArtifact.getUuid();
             r.setMasterArtifact(uuid);
 
-            if (i>0)
+            if (i > 0)
                 dataItemString += "#";
 
             // REMARK: ugly, but we know that the recommandations comes in left/right pairs.
@@ -462,26 +457,28 @@
         // TODO some hassle could be resolved by using multiple DataItems
         // (e.g. one per pair).
         final DataItem item = new DefaultDataItem(this.dataName, this.dataName, dataItemString);
-        return new Data[] { new DefaultData(
-                this.dataName, null, null, new DataItem[] {item}) };
+        return new Data[] { new DefaultData(this.dataName, null, null, new DataItem[] { item }) };
     }
 
     /**
      * Creates part of the String that encodes minuend or subtrahend.
-     * @param recommendation Recommendation to wrap in string.
-     * @param info Provides the factory to encode.
+     *
+     * @param recommendation
+     *            Recommendation to wrap in string.
+     * @param info
+     *            Provides the factory to encode.
      */
     protected static final String createDataString(final String artifactUuid, final Recommendation recommendation, final IRecommendationInfo info) {
-        final String factory = info.getDataStringFactory( recommendation );
+        final String factory = info.getDataStringFactory(recommendation);
 
         final Filter filter = recommendation.getFilter();
-        Facet  f      = null;
+        Facet f = null;
 
-        if(filter != null) {
-            final Map<String, List<Facet>>               outs = filter.getOuts();
+        if (filter != null) {
+            final Map<String, List<Facet>> outs = filter.getOuts();
             final Set<Map.Entry<String, List<Facet>>> entries = outs.entrySet();
 
-            for (final Map.Entry<String, List<Facet>> entry: entries) {
+            for (final Map.Entry<String, List<Facet>> entry : entries) {
                 final List<Facet> fs = entry.getValue();
 
                 f = fs.get(0);
@@ -490,17 +487,9 @@
                 }
             }
 
-            return "[" + artifactUuid + ";"
-            + f.getName()
-            + ";"
-            + f.getIndex()
-            + ";"
-            + recommendation.getDisplayName() + "]";
+            return "[" + artifactUuid + ";" + f.getName() + ";" + f.getIndex() + ";" + recommendation.getDisplayName() + "]";
         }
 
-        return "["
-        + artifactUuid
-        + ";" + factory + ";0;"
-        + recommendation.getDisplayName() + "]";
+        return "[" + artifactUuid + ";" + factory + ";0;" + recommendation.getDisplayName() + "]";
     }
 }
\ No newline at end of file

http://dive4elements.wald.intevation.org