changeset 33:cc648f83cdbc

remove the selected conflict after saving - does not fully work yet, e.g. does not proplerly reset the differences table
author dirk
date Tue, 20 Sep 2011 17:47:59 +0200
parents a6388599b106
children 4ccf7e7f03b0
files conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictsListModel.java
diffstat 3 files changed, 83 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Tue Sep 20 17:21:03 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Tue Sep 20 17:47:59 2011 +0200
@@ -180,12 +180,14 @@
     {
         // the index that's passed in from the list selection event is wrong!
         index = _frame.getDifferencesTable().getSelectedRow();
+        if (index > -1)
+        {
+            DifferenceTableModel model = (DifferenceTableModel)_frame.getDifferencesTable().getModel();
+            Difference difference = model.differenceAtIndex(index);
+            _controller.setCurrentDifference(difference);
 
-        DifferenceTableModel model = (DifferenceTableModel)_frame.getDifferencesTable().getModel();
-        Difference difference = model.differenceAtIndex(index);
-        _controller.setCurrentDifference(difference);
-
-        _frame.enableButtons();
+            _frame.enableButtons();
+        }
     }
 
     protected void useCurrentValue()
@@ -221,5 +223,27 @@
     protected void save() throws Exception
     {
         _controller.save();
+
+        resetDifferencesTableModel();
+
+        JsonNode resolvedConflict = _controller.getCurrentConflict();
+        ConflictsListModel conflictsModel = (ConflictsListModel)_frame.getConflictsList().getModel();
+        conflictsModel.remove(resolvedConflict);
+
+        clearSelectionInConflictsList();
+    }
+
+    private void clearSelectionInConflictsList()
+    {
+        _frame.getConflictsList().getSelectionModel().clearSelection();
+    }
+
+    private void resetDifferencesTableModel()
+    {
+        _controller.setCurrentDifference(null);
+
+        DifferenceTableModel model = new DifferenceTableModel();
+        _frame.getDifferencesTable().setModel(model);
+        _frame.repaint();
     }
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Tue Sep 20 17:21:03 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Tue Sep 20 17:47:59 2011 +0200
@@ -115,8 +115,11 @@
         URL updateUrl = _database.urlForDocument(id);
         InputStream input = new HttpClientHttpAccess(updateUrl).put(_currentConflict.toString());
 
-        // TODO take care of the return value
-        System.out.println(new ObjectMapper().readTree(input));
+        JsonNode result = parseJson(input);
+        if (result.get("ok").getValueAsBoolean() == false)
+        {
+            throw new IllegalStateException("update failed: " + result.toString());
+        }
     }
 
     private void deleteConflictVersion() throws Exception
@@ -125,7 +128,10 @@
         URL deleteUrl = _database.urlForDocumentAndRevision(id, _conflictRevision);
         InputStream input = new HttpClientHttpAccess(deleteUrl).delete();
 
-        // TODO take care of the return value
-        System.out.println(new ObjectMapper().readTree(input));
+        JsonNode result = parseJson(input);
+        if (result.get("ok").getValueAsBoolean() == false)
+        {
+            throw new IllegalStateException("delete failed: " + result.toString());
+        }
     }
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictsListModel.java	Tue Sep 20 17:21:03 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictsListModel.java	Tue Sep 20 17:47:59 2011 +0200
@@ -1,29 +1,69 @@
 
 package de.codedo.conflicteditor.gui;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.swing.AbstractListModel;
 
 import org.codehaus.jackson.JsonNode;
 
 public class ConflictsListModel extends AbstractListModel
 {
-    private JsonNode _conflictsNode;
+    private List<JsonNode> _conflicts;
 
     public ConflictsListModel(JsonNode conflictsNode)
     {
         super();
-        _conflictsNode = conflictsNode;
+        initConflicts(conflictsNode);
+    }
+
+    private void initConflicts(JsonNode conflictsNode)
+    {
+        _conflicts = new ArrayList<JsonNode>();
+
+        int size = conflictsNode.size();
+        for (int i = 0; i < size; i++)
+        {
+            _conflicts.add(conflictsNode.get(i));
+        }
     }
 
     @Override
     public int getSize()
     {
-        return _conflictsNode.size();
+        return _conflicts.size();
     }
 
     @Override
     public Object getElementAt(int index)
     {
-        return _conflictsNode.get(index);
+        return _conflicts.get(index);
+    }
+
+    public void remove(JsonNode resolvedConflict)
+    {
+        int index = findIndex(resolvedConflict);
+        if (index > -1)
+        {
+            _conflicts.remove(index);
+            fireContentsChanged(this, index, index);
+        }
+    }
+
+    private int findIndex(JsonNode node)
+    {
+        JsonNode id = node.get("_id");
+        int index = 0;
+        for (JsonNode currentNode : _conflicts)
+        {
+            JsonNode currentId = currentNode.get("id");
+            if (currentId.equals(id))
+            {
+                return index;
+            }
+            index++;
+        }
+        return -1;
     }
 }