Started implementation of config loading.... doesn't seem to work yet.pull/2/head
| @@ -1,6 +1,12 @@ | |||
| package xyz.veronie.bgg.result; | |||
| import java.util.HashMap; | |||
| import java.util.Iterator; | |||
| import java.util.Map.Entry; | |||
| import org.eclipse.core.runtime.preferences.InstanceScope; | |||
| import org.osgi.service.prefs.BackingStoreException; | |||
| import org.osgi.service.prefs.Preferences; | |||
| import xyz.veronie.bgg.types.FamilyType; | |||
| import xyz.veronie.bgg.types.FilterFlagState; | |||
| @@ -11,7 +17,19 @@ import xyz.veronie.bgg.types.UserFlag; | |||
| /// DTO / container for configuration of result download. Also defines startup settings for controls. | |||
| /// This container is handled by the ResultConfigManager. | |||
| public class ResultConfig { | |||
| // TODO: integrate different filters (or extend?) | |||
| // change version if you change any of the constants or enum strings | |||
| private static final String VERSION = "1.0"; | |||
| private static final String XYZ_VERONI_BGG = "xyz.veroni.bgg"; | |||
| private static final String USER_FLAGS = "UserFlags"; | |||
| private static final String RESULT_CONFIG = "ResultConfig"; | |||
| private static final String FAMILY_TYPE_KEY = "FamilyType"; | |||
| private static final String SUBTYPE_KEY = "Subtype"; | |||
| private static final String VERSION_KEY = "Version"; | |||
| private static final String SOURCE_FILTER_KEY = "SourceFilter"; | |||
| public SourceFilter source = SourceFilter.BGG_USER; | |||
| public Subtype subtype = Subtype.BOARDGAME; | |||
| @@ -33,6 +51,10 @@ public class ResultConfig { | |||
| public Integer familyId = null; | |||
| public FamilyType familyType = FamilyType.ALL; | |||
| public ResultConfig() { | |||
| initFromPrefrences(); | |||
| } | |||
| @Override | |||
| public String toString() { | |||
| switch(source) { | |||
| @@ -47,4 +69,68 @@ public class ResultConfig { | |||
| } | |||
| } | |||
| public void saveToPreferences() { | |||
| // We access the instanceScope | |||
| Preferences preferences = InstanceScope.INSTANCE.getNode(XYZ_VERONI_BGG); | |||
| Preferences prefResultConfig = preferences.node(RESULT_CONFIG); | |||
| prefResultConfig.put(VERSION_KEY, VERSION); | |||
| prefResultConfig.put(SOURCE_FILTER_KEY, source.name()); | |||
| prefResultConfig.put(SUBTYPE_KEY, subtype.name()); | |||
| prefResultConfig.put(SourceFilter.GEEKLIST.name(), (geeklistId == null) ? "" : geeklistId.toString()); | |||
| prefResultConfig.put(SourceFilter.FAMILY.name(), (familyId == null) ? "" : familyId.toString()); | |||
| prefResultConfig.put(FAMILY_TYPE_KEY, familyType.name()); | |||
| Preferences prefUserFlags = preferences.node(USER_FLAGS); | |||
| Iterator<Entry<UserFlag, FilterFlagState>> it = userFlags.entrySet().iterator(); | |||
| while (it.hasNext()) { | |||
| Entry<UserFlag, FilterFlagState> pair = it.next(); | |||
| prefUserFlags.put(pair.getKey().toString(), pair.getValue().name()); | |||
| } | |||
| try { | |||
| // forces the application to save the preferences | |||
| preferences.flush(); | |||
| } catch (BackingStoreException e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| public void initFromPrefrences() { | |||
| Preferences preferences = InstanceScope.INSTANCE.getNode("xyz.veron.bgg"); | |||
| if(preferences != null) { | |||
| Preferences prefResultConfig = preferences.node(RESULT_CONFIG); | |||
| // assume latest version | |||
| String version = prefResultConfig.get(VERSION_KEY, VERSION); | |||
| if(version != VERSION) { | |||
| System.out.println("WARN: Version mismatch. Trying to read config anyways."); | |||
| return; | |||
| } | |||
| source = SourceFilter.getEnum(prefResultConfig.get(SOURCE_FILTER_KEY, source.name())); | |||
| subtype = Subtype.getEnum(prefResultConfig.get(SUBTYPE_KEY, subtype.name())); | |||
| String geeklist = prefResultConfig.get(SourceFilter.GEEKLIST.name(), ""); | |||
| if(!geeklist.isEmpty()) { | |||
| geeklistId = Integer.valueOf(geeklist); | |||
| } | |||
| String family = prefResultConfig.get(SourceFilter.FAMILY.name(), ""); | |||
| if(!family.isEmpty()) { | |||
| familyId = Integer.valueOf(family); | |||
| } | |||
| familyType = FamilyType.getEnum(prefResultConfig.get(FAMILY_TYPE_KEY, familyType.name())); | |||
| Preferences prefUserFlags = preferences.node(USER_FLAGS); | |||
| Iterator<Entry<UserFlag, FilterFlagState>> it = userFlags.entrySet().iterator(); | |||
| while (it.hasNext()) { | |||
| Entry<UserFlag, FilterFlagState> pair = it.next(); | |||
| String storedUserFlagValue = prefUserFlags.get(pair.getKey().toString(), pair.getValue().name()); | |||
| pair.setValue(FilterFlagState.getEnum(storedUserFlagValue)); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -30,12 +30,15 @@ public class ResultConfigManager { | |||
| resultConfig = new ResultConfig(); | |||
| } | |||
| // TODO: store only the config value that changed instead of the whole config... | |||
| @Inject | |||
| @Optional | |||
| private void subscribeTopicSourceChanged | |||
| (@UIEventTopic(EventConstants.TOPIC_SOURCE_CHANGED) | |||
| SourceFilter source) { | |||
| getResultConfig().source = source; | |||
| storePreferences(); | |||
| System.out.println("TOPIC_SOURCE_CHANGED: source = " + source); | |||
| } | |||
| @@ -45,6 +48,7 @@ public class ResultConfigManager { | |||
| (@UIEventTopic(EventConstants.TOPIC_SUBTYPE_CHANGED) | |||
| Subtype subtype) { | |||
| getResultConfig().subtype = subtype; | |||
| storePreferences(); | |||
| System.out.println("TOPIC_SUBTYPE_CHANGED: " + subtype); | |||
| } | |||
| @@ -55,6 +59,7 @@ public class ResultConfigManager { | |||
| String user) { | |||
| System.out.println("set user to '" + user + "'"); | |||
| getResultConfig().user = user; | |||
| storePreferences(); | |||
| System.out.println("TOPIC_USER_CHANGED: user = " + user); | |||
| } | |||
| @@ -64,6 +69,7 @@ public class ResultConfigManager { | |||
| (@UIEventTopic(EventConstants.TOPIC_USERFLAG_CHANGED) | |||
| UserFlagEvent e) { | |||
| getResultConfig().userFlags.put(e.flag, e.state); | |||
| storePreferences(); | |||
| System.out.println("TOPIC_USERFLAG_CHANGED: " + e.flag + " " + e.state); | |||
| } | |||
| @@ -73,6 +79,7 @@ public class ResultConfigManager { | |||
| (@UIEventTopic(EventConstants.TOPIC_GEEKLIST_CHANGED) | |||
| Integer id) { | |||
| getResultConfig().geeklistId = id; | |||
| storePreferences(); | |||
| System.out.println("TOPIC_GEEKLIST_CHANGED: geeklistId = " + id); | |||
| } | |||
| @@ -83,6 +90,7 @@ public class ResultConfigManager { | |||
| Integer id) { | |||
| getResultConfig().familyId = id; | |||
| System.out.println("TOPIC_FAMILY_CHANGED: geeklistId = " + id); | |||
| storePreferences(); | |||
| } | |||
| @Inject | |||
| @@ -92,6 +100,7 @@ public class ResultConfigManager { | |||
| FamilyType ft) { | |||
| getResultConfig().familyType = ft; | |||
| System.out.println("TOPIC_FAMILYTYPE_CHANGED: family type = " + ft); | |||
| storePreferences(); | |||
| } | |||
| public ResultConfig getResultConfig() { | |||
| @@ -99,4 +108,8 @@ public class ResultConfigManager { | |||
| } | |||
| private void storePreferences() { | |||
| resultConfig.saveToPreferences(); | |||
| } | |||
| } | |||
| @@ -23,4 +23,17 @@ public enum FamilyType { | |||
| public String toApiString() { | |||
| return this.familyApiString; | |||
| } | |||
| public static FamilyType getEnum(String s) { | |||
| if(ALL.name().equals(s)){ | |||
| return ALL; | |||
| } else if(BG_FAMILY.name().equals(s)){ | |||
| return BG_FAMILY; | |||
| } else if(RPG.name().equals(s)){ | |||
| return RPG; | |||
| } else if(RPG_PERIODIC.name().equals(s)){ | |||
| return RPG_PERIODIC; | |||
| } | |||
| throw new IllegalArgumentException("No Enum specified for String '" + s + "'."); | |||
| } | |||
| } | |||
| @@ -15,4 +15,15 @@ public enum FilterFlagState { | |||
| public String toString() { | |||
| return name; | |||
| } | |||
| public static FilterFlagState getEnum(String s) { | |||
| if(IS.name().equals(s)){ | |||
| return IS; | |||
| } else if(ISNOT.name().equals(s)){ | |||
| return ISNOT; | |||
| } else if(IGNORE.name().equals(s)){ | |||
| return IGNORE; | |||
| } | |||
| throw new IllegalArgumentException("No Enum specified for String '" + s + "'."); | |||
| } | |||
| } | |||
| @@ -4,11 +4,11 @@ public enum SourceFilter { | |||
| BGG_USER("from BGG user"), //< by bgg user name (additional filters apply) | |||
| GEEKLIST("from geeklist"), //< by geeklist id | |||
| FAMILY("from family"), //< by family id | |||
| RANK("by rank"), //< filter by rank | |||
| YEAR("by year"), //< filter by year | |||
| AGE("by age"), //< by age (w.r.t. publishing date) | |||
| SEARCH("search"); //< more complex search by name and other filters | |||
| FAMILY("from family"); //< by family id | |||
| // RANK("by rank"), //< filter by rank | |||
| // YEAR("by year"), //< filter by year | |||
| // AGE("by age"), //< by age (w.r.t. publishing date) | |||
| // SEARCH("search"); //< more complex search by name and other filters | |||
| private String name; | |||
| @@ -21,4 +21,16 @@ public enum SourceFilter { | |||
| return name; | |||
| } | |||
| public static SourceFilter getEnum(String s) { | |||
| if(BGG_USER.name().equals(s)){ | |||
| return BGG_USER; | |||
| } else if(GEEKLIST.name().equals(s)){ | |||
| return GEEKLIST; | |||
| } else if(FAMILY.name().equals(s)){ | |||
| return FAMILY; | |||
| } | |||
| throw new IllegalArgumentException("No Enum specified for String '" + s + "'."); | |||
| } | |||
| } | |||
| @@ -25,5 +25,22 @@ public enum Subtype { | |||
| public String toApiString() { | |||
| return this.thingTypeString; | |||
| } | |||
| public static Subtype getEnum(String s) { | |||
| if(BOARDGAME.name().equals(s)){ | |||
| return BOARDGAME; | |||
| } else if(BGEXPANSION.name().equals(s)){ | |||
| return BGEXPANSION; | |||
| } else if(BGACCESSORIES.name().equals(s)){ | |||
| return BGACCESSORIES; | |||
| } else if(RPG_ISSUES.name().equals(s)){ | |||
| return RPG_ISSUES; | |||
| } else if(RPG_ITEMS.name().equals(s)){ | |||
| return RPG_ITEMS; | |||
| } else if(VIDEOGAMES.name().equals(s)){ | |||
| return VIDEOGAMES; | |||
| } | |||
| throw new IllegalArgumentException("No Enum specified for String '" + s + "'."); | |||
| } | |||
| } | |||
| @@ -46,7 +46,6 @@ import xyz.veronie.bgg.ui.helpers.BatColors; | |||
| import xyz.veronie.bgg.ui.helpers.BatLayouts; | |||
| // TODO: exchange only new and replace buttons | |||
| // TODO: remember config | |||
| // TODO: load thing list from DB | |||
| // TODO: image sizes | |||
| @@ -38,6 +38,7 @@ import xyz.veronie.bgg.ui.filters.FamilySourceFilter; | |||
| import xyz.veronie.bgg.ui.filters.GeeklistSourceFilter; | |||
| import xyz.veronie.bgg.ui.helpers.BatColors; | |||
| import xyz.veronie.bgg.ui.helpers.BatLayouts; | |||
| import org.eclipse.swt.widgets.Label; | |||
| public class FetchPart { | |||
| @@ -142,16 +143,6 @@ public class FetchPart { | |||
| applyComposite.setLayout(new GridLayout(5, false)); | |||
| applyComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false, 1, 1)); | |||
| Button btnOnlyNew = new Button(applyComposite, SWT.NONE); | |||
| btnOnlyNew.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/only_new_60x60.png")); | |||
| btnOnlyNew.setToolTipText("Keep only new"); | |||
| btnOnlyNew.addMouseListener(new MouseAdapter() { | |||
| @Override | |||
| public void mouseUp(MouseEvent e) { | |||
| fetchEntries(ResultAction.ONLY_NEW); | |||
| } | |||
| }); | |||
| Button btnReplace = new Button(applyComposite, SWT.NONE); | |||
| btnReplace.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_replace_60x60.png")); | |||
| btnReplace.setToolTipText("Replace"); | |||
| @@ -162,23 +153,23 @@ public class FetchPart { | |||
| } | |||
| }); | |||
| Button btnAdd = new Button(applyComposite, SWT.NONE); | |||
| btnAdd.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_add_60x60.png")); | |||
| btnAdd.setToolTipText("Add"); | |||
| btnAdd.addMouseListener(new MouseAdapter() { | |||
| Button btnOnlyNew = new Button(applyComposite, SWT.NONE); | |||
| btnOnlyNew.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/only_new_60x60.png")); | |||
| btnOnlyNew.setToolTipText("Keep only new"); | |||
| btnOnlyNew.addMouseListener(new MouseAdapter() { | |||
| @Override | |||
| public void mouseUp(MouseEvent e) { | |||
| fetchEntries(ResultAction.ADD); | |||
| fetchEntries(ResultAction.ONLY_NEW); | |||
| } | |||
| }); | |||
| Button btnSubtract = new Button(applyComposite, SWT.NONE); | |||
| btnSubtract.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_subtract_60x60.png")); | |||
| btnSubtract.setToolTipText("Subtract"); | |||
| btnSubtract.addMouseListener(new MouseAdapter() { | |||
| Button btnAdd = new Button(applyComposite, SWT.NONE); | |||
| btnAdd.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_add_60x60.png")); | |||
| btnAdd.setToolTipText("Add"); | |||
| btnAdd.addMouseListener(new MouseAdapter() { | |||
| @Override | |||
| public void mouseUp(MouseEvent e) { | |||
| fetchEntries(ResultAction.SUBTRACT); | |||
| fetchEntries(ResultAction.ADD); | |||
| } | |||
| }); | |||
| @@ -192,6 +183,16 @@ public class FetchPart { | |||
| } | |||
| }); | |||
| Button btnSubtract = new Button(applyComposite, SWT.NONE); | |||
| btnSubtract.setImage(ResourceManager.getPluginImage("xyz.veronie.bgg.ui", "icons/result_subtract_60x60.png")); | |||
| btnSubtract.setToolTipText("Subtract"); | |||
| btnSubtract.addMouseListener(new MouseAdapter() { | |||
| @Override | |||
| public void mouseUp(MouseEvent e) { | |||
| fetchEntries(ResultAction.SUBTRACT); | |||
| } | |||
| }); | |||
| // init filter using config manager | |||
| SourceFilter source = configManager.getResultConfig().source; | |||
| selectFilter(source); | |||