Mercurial > hg > ConflictEditor
changeset 35:a3f88353554a
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.
author | dirk |
---|---|
date | Fri, 07 Oct 2011 05:41:53 +0200 |
parents | 4ccf7e7f03b0 |
children | 8262e3a8ed6f |
files | conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditor.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/ConflictEditorFrame.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/DatabaseUrlComboBoxModel.java conflict-editor/src/main/java/de/codedo/conflicteditor/gui/util/PreferencesAccess.java |
diffstat | 4 files changed, 112 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- 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<ActionEvent> executable = new Executable<ActionEvent>() + { + @Override + public void execute(ActionEvent event) throws Exception + { + databaseUrlEdited(event); + } + }; + ExceptionHandlingActionListener listener = new ExceptionHandlingActionListener(executable); + _frame.getDatabaseUrlCombo().addActionListener(listener); + } + private void connectFindConflictsButton() { Executable<ActionEvent> executable = new Executable<ActionEvent>() @@ -156,9 +176,35 @@ _frame.getSaveButton().addActionListener(listener); } + private void connectModels() + { + createAndSetDatabaseUrlComboModel(); + } + + private void createAndSetDatabaseUrlComboModel() + { + Vector<String> 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);
--- 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;
--- 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<String> _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); - } -}
--- 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<String> getDatabaseUrls() + public Vector<String> getDatabaseUrls() { try { @@ -30,10 +31,10 @@ } } - private List<String> collectDatabaseUrls() throws BackingStoreException + private Vector<String> collectDatabaseUrls() throws BackingStoreException { - List<String> urls = new ArrayList<String>(); - Preferences databaseUrls = _rootNode.node("databaseUrls"); + Vector<String> urls = new Vector<String>(); + 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"); + } }