# HG changeset patch # User dirk # Date 1317958913 -7200 # Node ID a3f88353554ae8df8a26fb824174d09c5240afb6 # Parent 4ccf7e7f03b0658b7bfe689649639f237959c6ff do not use a custom combo box model, instead fill a default one from preferences and attach an action listener to the combo that ensures that yet unknown database URLs will be stored in preferences. diff -r 4ccf7e7f03b0 -r a3f88353554a conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java --- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java Fri Oct 07 03:58:41 2011 +0200 +++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java Fri Oct 07 05:41:53 2011 +0200 @@ -2,13 +2,17 @@ package de.codedo.conflicteditor.gui; import de.codedo.conflicteditor.Difference; +import de.codedo.conflicteditor.gui.util.PreferencesAccess; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.io.IOException; import java.io.StringWriter; import java.util.List; +import java.util.Vector; +import javax.swing.ComboBoxModel; +import javax.swing.DefaultComboBoxModel; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; @@ -41,12 +45,14 @@ _controller = new ConflictEditorController(); _frame = new ConflictEditorFrame(); connectListeners(); + connectModels(); _frame.pack(); _frame.setVisible(true); } private void connectListeners() { + connectDatabaseUrlCombo(); connectFindConflictsButton(); connectConflictsList(); connectDifferencesTable(); @@ -56,6 +62,20 @@ connectSaveButton(); } + private void connectDatabaseUrlCombo() + { + Executable executable = new Executable() + { + @Override + public void execute(ActionEvent event) throws Exception + { + databaseUrlEdited(event); + } + }; + ExceptionHandlingActionListener listener = new ExceptionHandlingActionListener(executable); + _frame.getDatabaseUrlCombo().addActionListener(listener); + } + private void connectFindConflictsButton() { Executable executable = new Executable() @@ -156,9 +176,35 @@ _frame.getSaveButton().addActionListener(listener); } + private void connectModels() + { + createAndSetDatabaseUrlComboModel(); + } + + private void createAndSetDatabaseUrlComboModel() + { + Vector databaseUrls = new PreferencesAccess().getDatabaseUrls(); + ComboBoxModel model = new DefaultComboBoxModel(databaseUrls); + _frame.getDatabaseUrlCombo().setModel(model); + } + + protected void databaseUrlEdited(ActionEvent event) + { + if ("comboBoxEdited".equals(event.getActionCommand())) + { + databaseUrlEdited(); + } + } + + private void databaseUrlEdited() + { + String dbUrl = _frame.getDatabaseUrl(); + new PreferencesAccess().storeDatabaseUrl(dbUrl); + } + protected void findConflicts() throws IOException { - String dbUrl = (String)_frame.getDatabaseUrlCombo().getSelectedItem(); + String dbUrl = _frame.getDatabaseUrl(); JsonNode conflicts = _controller.findConflicts(dbUrl); ListModel model = new ConflictsListModel(conflicts); _frame.getConflictsList().setModel(model); diff -r 4ccf7e7f03b0 -r a3f88353554a conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java --- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java Fri Oct 07 03:58:41 2011 +0200 +++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java Fri Oct 07 05:41:53 2011 +0200 @@ -60,7 +60,6 @@ _databaseUrlCombo = new JComboBox(); _databaseUrlCombo.setEditable(true); _databaseUrlCombo.setEditor(new TextFieldComboBoxEditor()); - _databaseUrlCombo.setModel(new DatabaseUrlComboBoxModel()); databasePanel.add(_databaseUrlCombo); _findConflictsButton = new JButton("Find Conflicts"); @@ -165,6 +164,11 @@ return _databaseUrlCombo; } + public String getDatabaseUrl() + { + return (String)_databaseUrlCombo.getSelectedItem(); + } + public JButton getFindConflictsButton() { return _findConflictsButton; diff -r 4ccf7e7f03b0 -r a3f88353554a conflict-editor/src/main/java/de/codedo/conflicteditor/gui/DatabaseUrlComboBoxModel.java --- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/DatabaseUrlComboBoxModel.java Fri Oct 07 03:58:41 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ - -package de.codedo.conflicteditor.gui; - -import de.codedo.conflicteditor.gui.util.PreferencesAccess; - -import java.util.List; - -import javax.swing.AbstractListModel; -import javax.swing.MutableComboBoxModel; - -public class DatabaseUrlComboBoxModel extends AbstractListModel implements MutableComboBoxModel -{ - private List _urls; - private Object _selected; - - public DatabaseUrlComboBoxModel() - { - super(); - _urls = new PreferencesAccess().getDatabaseUrls(); - if (_urls.size() > 0) - { - _selected = _urls.get(0); - } - } - - @Override - public int getSize() - { - return _urls.size(); - } - - @Override - public Object getElementAt(int index) - { - return _urls.get(index); - } - - @Override - public Object getSelectedItem() - { - return _selected; - } - - @Override - public void setSelectedItem(Object anItem) - { - _selected = anItem; - } - - @Override - public void addElement(Object obj) - { - _urls.add(obj.toString()); - } - - @Override - public void removeElement(Object obj) - { - _urls.remove(obj); - } - - @Override - public void insertElementAt(Object obj, int index) - { - _urls.add(index, obj.toString()); - } - - @Override - public void removeElementAt(int index) - { - _urls.remove(index); - } -} diff -r 4ccf7e7f03b0 -r a3f88353554a conflict-editor/src/main/java/de/codedo/conflicteditor/gui/util/PreferencesAccess.java --- a/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/util/PreferencesAccess.java Fri Oct 07 03:58:41 2011 +0200 +++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/gui/util/PreferencesAccess.java Fri Oct 07 05:41:53 2011 +0200 @@ -3,8 +3,9 @@ import de.codedo.conflicteditor.gui.ConflictEditor; -import java.util.ArrayList; -import java.util.List; +import java.text.DecimalFormat; +import java.util.Arrays; +import java.util.Vector; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; @@ -18,7 +19,7 @@ _rootNode = Preferences.userNodeForPackage(ConflictEditor.class); } - public List getDatabaseUrls() + public Vector getDatabaseUrls() { try { @@ -30,10 +31,10 @@ } } - private List collectDatabaseUrls() throws BackingStoreException + private Vector collectDatabaseUrls() throws BackingStoreException { - List urls = new ArrayList(); - Preferences databaseUrls = _rootNode.node("databaseUrls"); + Vector urls = new Vector(); + Preferences databaseUrls = databaseUrlsNode(); for (String key : databaseUrls.keys()) { String url = databaseUrls.get(key, null); @@ -41,4 +42,57 @@ } return urls; } + + public void storeDatabaseUrl(String url) + { + try + { + if (isDatabaseUrlStored(url) == false) + { + storeUrl(url); + } + } + catch (BackingStoreException bse) + { + throw new IllegalStateException(bse); + } + } + + private boolean isDatabaseUrlStored(String url) throws BackingStoreException + { + Preferences databaseUrls = databaseUrlsNode(); + for (String key : databaseUrls.keys()) + { + String storedUrl = databaseUrls.get(key, null); + if (storedUrl.equals(url)) + { + return true; + } + } + return false; + } + + private void storeUrl(String url) throws BackingStoreException + { + Preferences databaseUrls = databaseUrlsNode(); + String key = keyForStoringDatabaseUrl(); + databaseUrls.put(key, url); + databaseUrls.flush(); + } + + private String keyForStoringDatabaseUrl() throws BackingStoreException + { + Preferences databaseUrls = databaseUrlsNode(); + String[] keys = databaseUrls.keys(); + Arrays.sort(keys); + + int lastKey = Integer.valueOf(keys[keys.length - 1]).intValue(); + int nextKey = lastKey + 1; + return new DecimalFormat("00").format(nextKey); + } + + private Preferences databaseUrlsNode() + { + return _rootNode.node("databaseUrls"); + } }