# HG changeset patch # User Dirk Olmes # Date 1315873981 -7200 # Node ID a5ef4fbfd90c792481228fcc2db37554194f0a5a # Parent e7d9a9176e08ff60dd4d403500ee3013d7139c06 clean up: ConflictEditor should only contain code that mediates between the various GUI elements. The logic for dealing with conflicts moves to ConflictEditorController. diff -r e7d9a9176e08 -r a5ef4fbfd90c conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java --- 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 diffs = new DocumentMatcher(currentDocument, conflictDocument).compare(); - - TableModel model = new DifferenceTableModel(diffs); + List differences = _controller.differencesForConflict(conflictNode); + TableModel model = new DifferenceTableModel(differences); _frame.getDifferencesTable().setModel(model); } diff -r e7d9a9176e08 -r a5ef4fbfd90c conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorController.java --- /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 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(); + } + } +} diff -r e7d9a9176e08 -r a5ef4fbfd90c conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java --- 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()