From cac857de4b728e3983e73d23f27031dfc978991c Mon Sep 17 00:00:00 2001 From: liying2008 Date: Sun, 29 Apr 2018 12:52:09 +0800 Subject: [PATCH] fix fatal bugs in saving state. --- .../cc/duduhuo/git/conflict/BuiltInColor.java | 5 ++- .../duduhuo/git/conflict/model/MarkColor.java | 3 +- .../git/conflict/model/PersistentState.java | 2 +- .../conflict/setting/ColorSettingsPanel.form | 42 +++++++------------ .../setting/ColorSettingsPanelImpl.java | 36 +++++++++------- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/main/java/cc/duduhuo/git/conflict/BuiltInColor.java b/src/main/java/cc/duduhuo/git/conflict/BuiltInColor.java index cac8b3c..dea49ce 100644 --- a/src/main/java/cc/duduhuo/git/conflict/BuiltInColor.java +++ b/src/main/java/cc/duduhuo/git/conflict/BuiltInColor.java @@ -15,10 +15,11 @@ public interface BuiltInColor { String INTELLIJ_SCHEME_NAME = "IntelliJ"; String DARCULA_SCHEME_NAME = "Darcula"; - String DEFAULT_SCHEME = AUTO_SCHEME_NAME; - MarkColor AUTO = new MarkColor(AUTO_SCHEME_NAME, true, 0x9fe3d6, 0xd9f4ef, 0x9fd2ff, 0xd9edff, 0x2f7366, 0x25403b, 0x2f628f, 0x25394b); MarkColor INTELLIJ = new MarkColor(INTELLIJ_SCHEME_NAME, true, 0x9fe3d6, 0xd9f4ef, 0x9fd2ff, 0xd9edff); MarkColor DARCULA = new MarkColor(DARCULA_SCHEME_NAME, true, 0x2f7366, 0x25403b, 0x2f628f, 0x25394b); + + String DEFAULT_SCHEME_NAME = AUTO_SCHEME_NAME; + MarkColor DEFAULT_MARK_COLOR = AUTO; } diff --git a/src/main/java/cc/duduhuo/git/conflict/model/MarkColor.java b/src/main/java/cc/duduhuo/git/conflict/model/MarkColor.java index 54304ca..af7dde3 100644 --- a/src/main/java/cc/duduhuo/git/conflict/model/MarkColor.java +++ b/src/main/java/cc/duduhuo/git/conflict/model/MarkColor.java @@ -2,7 +2,6 @@ import cc.duduhuo.git.conflict.BuiltInColor; -import java.io.Serializable; import java.util.Objects; /** @@ -13,7 +12,7 @@ * Remarks: * ======================================================= */ -public final class MarkColor implements Serializable { +public final class MarkColor { private String mSchemeName; private boolean mIsBuiltIn; diff --git a/src/main/java/cc/duduhuo/git/conflict/model/PersistentState.java b/src/main/java/cc/duduhuo/git/conflict/model/PersistentState.java index 56061e3..d1a1259 100644 --- a/src/main/java/cc/duduhuo/git/conflict/model/PersistentState.java +++ b/src/main/java/cc/duduhuo/git/conflict/model/PersistentState.java @@ -17,7 +17,7 @@ public final class PersistentState { private LinkedHashMap mMarkColors; public PersistentState() { - mSchemeName = BuiltInColor.DEFAULT_SCHEME; + mSchemeName = BuiltInColor.DEFAULT_SCHEME_NAME; mMarkColors = new LinkedHashMap<>(); mMarkColors.put(BuiltInColor.AUTO_SCHEME_NAME, BuiltInColor.AUTO); mMarkColors.put(BuiltInColor.INTELLIJ_SCHEME_NAME, BuiltInColor.INTELLIJ); diff --git a/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanel.form b/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanel.form index 2776533..eed7ca9 100644 --- a/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanel.form +++ b/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanel.form @@ -3,7 +3,7 @@ - + @@ -39,7 +39,7 @@ - + @@ -55,7 +55,7 @@ - + @@ -63,8 +63,8 @@ - - + + @@ -83,8 +83,8 @@ - - + + @@ -95,8 +95,8 @@ - - + + @@ -107,7 +107,7 @@ - + @@ -137,28 +137,18 @@ - - - - - - - - - - - + - - + + @@ -169,7 +159,7 @@ - + @@ -177,7 +167,7 @@ - + @@ -192,7 +182,7 @@ - + diff --git a/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.java b/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.java index a8d160f..bb31b7a 100644 --- a/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.java +++ b/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.java @@ -87,13 +87,12 @@ private void setListeners() { String selectedItem = (String) cbColorScheme.getSelectedItem(); int confirm = JOptionPane.showConfirmDialog(mainPanel, "Delete scheme: " + selectedItem + " ?", "Delete", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { - MarkColor markColor = mMarkColors.get((String) cbColorScheme.getSelectedItem()); + MarkColor markColor = mMarkColors.get(selectedItem); // Double check. Prevent the built-in color scheme from being deleted. if (!markColor.isBuiltIn()) { mMarkColors.remove(selectedItem); cbColorScheme.removeItem(selectedItem); mAllSchemeNames.remove(selectedItem); - updateUI(markColor); } else { JOptionPane.showMessageDialog(mainPanel, "The built-in color scheme cannot be deleted.", "Failed", JOptionPane.ERROR_MESSAGE); @@ -102,27 +101,35 @@ private void setListeners() { }); } + @SuppressWarnings("unchecked") private void initUI() { PersistentState persistentState = GlobalSettings.getPersistentState(); - mMarkColors = persistentState.getMarkColors(); + LinkedHashMap map = persistentState.getMarkColors(); mSchemeName = persistentState.getSchemeName(); - String name; + // clone markColors map + mMarkColors = (LinkedHashMap) map.clone(); + // make sure the built-in color is displayed at the top. cbColorScheme.addItem(BuiltInColor.AUTO_SCHEME_NAME); cbColorScheme.addItem(BuiltInColor.INTELLIJ_SCHEME_NAME); cbColorScheme.addItem(BuiltInColor.DARCULA_SCHEME_NAME); - Set keySet = mMarkColors.keySet(); + Set keySet = map.keySet(); for (String key : keySet) { - name = mMarkColors.get(key).getSchemeName(); - if (!name.equals(BuiltInColor.AUTO_SCHEME_NAME) && - !name.equals(BuiltInColor.INTELLIJ_SCHEME_NAME) && - !name.equals(BuiltInColor.DARCULA_SCHEME_NAME)) { - cbColorScheme.addItem(name); + if (!key.equals(BuiltInColor.AUTO_SCHEME_NAME) && + !key.equals(BuiltInColor.INTELLIJ_SCHEME_NAME) && + !key.equals(BuiltInColor.DARCULA_SCHEME_NAME)) { + cbColorScheme.addItem(key); } - mAllSchemeNames.add(name); + mAllSchemeNames.add(key); + } + // Double check. + if (keySet.contains(mSchemeName)) { + cbColorScheme.setSelectedItem(mSchemeName); + mOldMarkColor.copy(map.get(mSchemeName)); + } else { + cbColorScheme.setSelectedItem(BuiltInColor.DEFAULT_SCHEME_NAME); + mOldMarkColor.copy(BuiltInColor.DEFAULT_MARK_COLOR); } - cbColorScheme.setSelectedItem(mSchemeName); - mOldMarkColor.copy(mMarkColors.get(mSchemeName)); updateUI(mOldMarkColor); } @@ -258,9 +265,10 @@ public void reset() { } @Override + @SuppressWarnings("unchecked") public void apply() { boolean isModified = isModified(); - GlobalSettings.getPersistentState().setMarkColors(mMarkColors); + GlobalSettings.getPersistentState().setMarkColors((LinkedHashMap) mMarkColors.clone()); GlobalSettings.getPersistentState().setSchemeName((String) cbColorScheme.getSelectedItem()); if (isModified) { mSchemeName = (String) cbColorScheme.getSelectedItem();