changeset 10:a5ef4fbfd90c

clean up: ConflictEditor should only contain code that mediates between the various GUI elements. The logic for dealing with conflicts moves to ConflictEditorController.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 13 Sep 2011 02:33:01 +0200
parents e7d9a9176e08
children 1298f5b16ecf
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/ConflictEditorFrame.java
diffstat 3 files changed, 68 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Tue Sep 13 02:20:11 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java	Tue Sep 13 02:33:01 2011 +0200
@@ -1,16 +1,10 @@
 
 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.io.Reader;
-import java.net.URL;
+import java.io.IOException;
 import java.util.List;
 
 import javax.swing.ListModel;
@@ -18,12 +12,11 @@
 import javax.swing.table.TableModel;
 
 import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.map.ObjectMapper;
 
 public class ConflictEditor
 {
+    private ConflictEditorController _controller;
     private ConflictEditorFrame _frame;
-    private CouchDb _database;
 
     public static void main(String[] args)
     {
@@ -40,6 +33,7 @@
 
     private void run()
     {
+        _controller = new ConflictEditorController();
         _frame = new ConflictEditorFrame();
         connectListeners();
         _frame.pack();
@@ -100,31 +94,19 @@
         _frame.getDifferencesTable().getSelectionModel().addListSelectionListener(listener);
     }
 
-    protected void findConflicts() throws Exception
+    protected void findConflicts() throws IOException
     {
-        String dbUrl = _frame.getDatabaseUrl();
-        _database = new CouchDb(dbUrl);
-        ConflictsView conflictsView = new ConflictsView(_database);
-
-        ListModel model = new ConflictsListModel(conflictsView.getConflicts());
+        String dbUrl = _frame.getDatabaseUrlTextField().getText();
+        JsonNode conflicts = _controller.findConflicts(dbUrl);
+        ListModel model = new ConflictsListModel(conflicts);
         _frame.getConflictsList().setModel(model);
     }
 
     protected void selectConflictDocument(int index) throws Exception
     {
         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);
+        List<Difference> differences = _controller.differencesForConflict(conflictNode);
+        TableModel model = new DifferenceTableModel(differences);
         _frame.getDifferencesTable().setModel(model);
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java	Tue Sep 13 02:33:01 2011 +0200
@@ -0,0 +1,57 @@
+
+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.io.IOException;
+import java.io.Reader;
+import java.net.URL;
+import java.util.List;
+
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
+
+public class ConflictEditorController extends Object
+{
+    private CouchDb _database;
+
+    public JsonNode findConflicts(String dbUrl) throws IOException
+    {
+        _database = new CouchDb(dbUrl);
+        ConflictsView conflictsView = new ConflictsView(_database);
+        return conflictsView.getConflicts();
+    }
+
+    public List<Difference> differencesForConflict(JsonNode conflictNode) throws IOException
+    {
+        Conflict conflict = new Conflict(conflictNode);
+        JsonNode currentDocument = conflict.getCurrentDocument();
+        JsonNode conflictDocument = loadConflictingDocument(conflict);
+        return new DocumentMatcher(currentDocument, conflictDocument).compare();
+    }
+
+    private JsonNode loadConflictingDocument(Conflict conflict) throws IOException
+    {
+        URL documentUrl = _database.urlForDocumentAndRevision(conflict.getId(),
+            conflict.getConflictRevision());
+        Reader reader = new UrlConnectionHttpAccess(documentUrl).get();
+        return parseJson(reader);
+    }
+
+    private JsonNode parseJson(Reader reader) throws IOException
+    {
+        try
+        {
+            return new ObjectMapper().readTree(reader);
+        }
+        finally
+        {
+            reader.close();
+        }
+    }
+}
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java	Tue Sep 13 02:20:11 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java	Tue Sep 13 02:33:01 2011 +0200
@@ -80,9 +80,9 @@
         _differencesTable.setModel(model);
     }
 
-    public String getDatabaseUrl()
+    public JTextField getDatabaseUrlTextField()
     {
-        return _databaseUrlTextField.getText();
+        return _databaseUrlTextField;
     }
 
     public JButton getFindConflictsButton()