changeset 26:ca9c62a68e87

wire up the current value, conflict value and preview buttons
author dirk
date Sun, 18 Sep 2011 11:00:16 +0200
parents 7593675c1eff
children d7f357434800
files conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java
diffstat 2 files changed, 111 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Sun Sep 18 10:13:48 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Sun Sep 18 11:00:16 2011 +0200
@@ -3,14 +3,20 @@
 
 import de.codedo.conflicteditor.Difference;
 
+import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.io.IOException;
+import java.io.StringWriter;
 import java.util.List;
 
+import javax.swing.JFrame;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
 import javax.swing.ListModel;
 import javax.swing.table.TableModel;
 
 import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
 
 public class ConflictEditor
 {
@@ -44,6 +50,9 @@
         connectFindConflictsButton();
         connectConflictsList();
         connectDifferencesTable();
+        connectCurrentValueButton();
+        connectConflictValueButton();
+        connectPreviewButton();
     }
 
     private void connectFindConflictsButton()
@@ -90,6 +99,48 @@
         _frame.getDifferencesTable().getSelectionModel().addListSelectionListener(listener);
     }
 
+    private void connectCurrentValueButton()
+    {
+        Executable<ActionEvent> executable = new Executable<ActionEvent>()
+        {
+            @Override
+            public void execute(ActionEvent event) throws Exception
+            {
+                useCurrentValue();
+            }
+        };
+        ExceptionHandlingActionListener listener = new ExceptionHandlingActionListener(executable);
+        _frame.getCurrentValueButton().addActionListener(listener);
+    }
+
+    private void connectConflictValueButton()
+    {
+        Executable<ActionEvent> executable = new Executable<ActionEvent>()
+        {
+            @Override
+            public void execute(ActionEvent event) throws Exception
+            {
+                useConflictValue();
+            }
+        };
+        ExceptionHandlingActionListener listener = new ExceptionHandlingActionListener(executable);
+        _frame.getConflictValueButton().addActionListener(listener);
+    }
+
+    private void connectPreviewButton()
+    {
+        Executable<ActionEvent> executable = new Executable<ActionEvent>()
+        {
+            @Override
+            public void execute(ActionEvent event) throws Exception
+            {
+                preview();
+            }
+        };
+        ExceptionHandlingActionListener listener = new ExceptionHandlingActionListener(executable);
+        _frame.getPreviewButton().addActionListener(listener);
+    }
+
     protected void findConflicts() throws IOException
     {
         String dbUrl = _frame.getDatabaseUrlTextField().getText();
@@ -118,4 +169,34 @@
 
         _frame.enableButtons();
     }
+
+    protected void useCurrentValue()
+    {
+        _controller.useCurrentValue();
+    }
+
+    protected void useConflictValue()
+    {
+        _controller.useConflictValue();
+    }
+
+    protected void preview() throws Exception
+    {
+        JsonNode currentConflict = _controller.getCurrentConflict();
+        StringWriter writer = new StringWriter();
+        new ObjectMapper().defaultPrettyPrintingWriter().writeValue(writer, currentConflict);
+
+        JTextArea text = new JTextArea();
+        text.setText(writer.toString());
+
+        JScrollPane scrollPane = new JScrollPane(text);
+
+        // TODO modal
+        JFrame preview = new JFrame();
+        preview.setPreferredSize(new Dimension(800, 600));
+        preview.add(scrollPane);
+
+        preview.pack();
+        preview.setVisible(true);
+    }
 }
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Sun Sep 18 10:13:48 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Sun Sep 18 11:00:16 2011 +0200
@@ -15,6 +15,7 @@
 
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.node.ObjectNode;
 
 public class ConflictEditorController extends Object
 {
@@ -57,9 +58,36 @@
         }
     }
 
-    public void setCurrentConflict(JsonNode conflictNode)
+    public void useCurrentValue()
+    {
+        JsonNode currentValue = (JsonNode)_currentDifference.currentValue;
+        ((ObjectNode)_currentConflict).put(_currentDifference.key, currentValue);
+    }
+
+    public void useConflictValue()
+    {
+        JsonNode value = (JsonNode)_currentDifference.otherValue;
+        ((ObjectNode)_currentConflict).put(_currentDifference.key, value);
+    }
+
+    public JsonNode getCurrentConflict()
     {
-        _currentConflict = conflictNode;
+        return _currentConflict;
+    }
+
+    public void setCurrentConflict(JsonNode conflictNode) throws Exception
+    {
+        JsonNode node = conflictNode.get("key");
+        _currentConflict = deepCopyWithoutConflict(node);
+    }
+
+    private ObjectNode deepCopyWithoutConflict(JsonNode node) throws Exception
+    {
+        // take a deep copy of the conflict node, we will be editing it here.
+        String docString = node.toString();
+        ObjectNode clonedNode = (ObjectNode)new ObjectMapper().readTree(docString);
+        clonedNode.remove("_conflicts");
+        return clonedNode;
     }
 
     public void setCurrentDifference(Difference difference)