changeset 4:c396004a4e55

implement exception handling listeners, when selecting a conflict document, display all differences
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 12 Sep 2011 14:27:04 +0200
parents 84b19f2b1dbb
children ad2b4949967e
files conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ExceptionHandlingActionListener.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ExceptionHandlingListSelectionListener.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ExceptionUtil.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/Executable.java
diffstat 5 files changed, 146 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Mon Sep 12 13:49:42 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Mon Sep 12 14:27:04 2011 +0200
@@ -1,27 +1,30 @@
 
 package de.codedo.conflicteditor.gui;
 
+import de.codedo.conflicteditor.Conflict;
 import de.codedo.conflicteditor.ConflictsView;
 import de.codedo.conflicteditor.CouchDb;
 import de.codedo.conflicteditor.Difference;
+import de.codedo.conflicteditor.DocumentMatcher;
+import de.codedo.conflicteditor.UrlConnectionHttpAccess;
 
 import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.PrintStream;
-import java.util.ArrayList;
+import java.io.Reader;
+import java.net.URL;
 import java.util.List;
 
-import javax.swing.JOptionPane;
 import javax.swing.ListModel;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 import javax.swing.table.TableModel;
 
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
+
 public class ConflictEditor
 {
     private ConflictEditorFrame _frame;
+    private CouchDb _database;
 
     public static void main(String[] args)
     {
@@ -46,23 +49,28 @@
 
     private void connectListeners(ConflictEditorFrame frame)
     {
-        frame.getConnectButton().addActionListener(new ActionListener()
-        {
-            @Override
-            public void actionPerformed(ActionEvent e)
+        frame.getConnectButton().addActionListener(
+            new ExceptionHandlingActionListener(new Executable<ActionEvent>()
             {
-                connectAndFindConflicts();
-            }
-        });
+                @Override
+                public void execute(ActionEvent event) throws Exception
+                {
+                    connectAndFindConflicts();
+                }
+            }));
 
-        frame.getConflictsList().addListSelectionListener(new ListSelectionListener()
-        {
-            @Override
-            public void valueChanged(ListSelectionEvent e)
+        frame.getConflictsList().addListSelectionListener(
+            new ExceptionHandlingListSelectionListener(new Executable<ListSelectionEvent>()
             {
-                selectConflictDocument();
-            }
-        });
+                @Override
+                public void execute(ListSelectionEvent event) throws Exception
+                {
+                    if (event.getValueIsAdjusting() == false)
+                    {
+                        selectConflictDocument(event.getFirstIndex());
+                    }
+                }
+            }));
 
         frame.getDifferencesTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
         {
@@ -74,31 +82,29 @@
         });
     }
 
-    protected void connectAndFindConflicts()
+    protected void connectAndFindConflicts() throws Exception
     {
-        try
-        {
-            String dbUrl = _frame.getDatabaseUrl();
-            CouchDb database = new CouchDb(dbUrl);
-            ConflictsView conflictsView = new ConflictsView(database);
+        String dbUrl = _frame.getDatabaseUrl();
+        _database = new CouchDb(dbUrl);
+        ConflictsView conflictsView = new ConflictsView(_database);
 
-            ListModel model = new ConflictsListModel(conflictsView.getConflicts());
-            _frame.getConflictsList().setModel(model);
-        }
-        catch (IOException iox)
-        {
-            PrintStream out = new PrintStream(new ByteArrayOutputStream());
-            iox.printStackTrace(out);
-            out.close();
-
-            JOptionPane.showMessageDialog(_frame, out.toString());
-        }
+        ListModel model = new ConflictsListModel(conflictsView.getConflicts());
+        _frame.getConflictsList().setModel(model);
     }
 
-    protected void selectConflictDocument()
+    protected void selectConflictDocument(int index) throws Exception
     {
-        List<Difference> diffs = new ArrayList<Difference>();
-        diffs.add(new Difference("key", "current", "other"));
+        JsonNode conflictNode = (JsonNode)_frame.getConflictsList().getModel().getElementAt(index);
+        Conflict conflict = new Conflict(conflictNode);
+        JsonNode currentDocument = conflict.getCurrentDocument();
+
+        URL documentUrl = _database.urlForDocumentAndRevision(conflict.getId(),
+            conflict.getConflictRevision());
+        Reader reader = new UrlConnectionHttpAccess(documentUrl).get();
+
+        JsonNode conflictDocument = new ObjectMapper().readTree(reader);
+
+        List<Difference> diffs = new DocumentMatcher(currentDocument, conflictDocument).compare();
 
         TableModel model = new DifferenceTableModel(diffs);
         _frame.getDifferencesTable().setModel(model);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ExceptionHandlingActionListener.java	Mon Sep 12 14:27:04 2011 +0200
@@ -0,0 +1,34 @@
+
+package de.codedo.conflicteditor.gui;
+
+import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JOptionPane;
+
+public class ExceptionHandlingActionListener extends Object implements ActionListener
+{
+    private Executable<ActionEvent> _executable;
+
+    public ExceptionHandlingActionListener(Executable<ActionEvent> executable)
+    {
+        super();
+        _executable = executable;
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent event)
+    {
+        try
+        {
+            _executable.execute(event);
+        }
+        catch (Exception ex)
+        {
+            String stacktrace = ExceptionUtil.toString(ex);
+            Component source = (Component)event.getSource();
+            JOptionPane.showMessageDialog(source, stacktrace, "Error", JOptionPane.ERROR_MESSAGE);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ExceptionHandlingListSelectionListener.java	Mon Sep 12 14:27:04 2011 +0200
@@ -0,0 +1,34 @@
+
+package de.codedo.conflicteditor.gui;
+
+import java.awt.Component;
+
+import javax.swing.JOptionPane;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+
+public class ExceptionHandlingListSelectionListener extends Object implements ListSelectionListener
+{
+    private Executable<ListSelectionEvent> _executable;
+
+    public ExceptionHandlingListSelectionListener(Executable<ListSelectionEvent> executable)
+    {
+        super();
+        _executable = executable;
+    }
+
+    @Override
+    public void valueChanged(ListSelectionEvent event)
+    {
+        try
+        {
+            _executable.execute(event);
+        }
+        catch (Exception ex)
+        {
+            String stacktrace = ExceptionUtil.toString(ex);
+            Component source = (Component)event.getSource();
+            JOptionPane.showMessageDialog(source, stacktrace, "Error", JOptionPane.ERROR_MESSAGE);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ExceptionUtil.java	Mon Sep 12 14:27:04 2011 +0200
@@ -0,0 +1,25 @@
+
+package de.codedo.conflicteditor.gui;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+public class ExceptionUtil
+{
+    public static String toString(Throwable t)
+    {
+        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+        PrintStream out = new PrintStream(byteStream);
+        t.printStackTrace(out);
+        out.flush();
+        out.close();
+
+        return byteStream.toString();
+    }
+
+    // never create instances of this class
+    private ExceptionUtil()
+    {
+        super();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/Executable.java	Mon Sep 12 14:27:04 2011 +0200
@@ -0,0 +1,7 @@
+
+package de.codedo.conflicteditor.gui;
+
+public interface Executable<ET>
+{
+    void execute(ET event) throws Exception;
+}