OmegaT の背景に画像を表示します。
リファクタリング:ConfigSet クラス
| @@ -24,11 +24,13 @@ | ||
| 24 | 24 | import java.io.IOException; |
| 25 | 25 | import java.io.Writer; |
| 26 | 26 | import java.net.URL; |
| 27 | +import java.util.AbstractMap; | |
| 27 | 28 | import java.util.ArrayList; |
| 28 | 29 | import java.util.Collections; |
| 29 | 30 | import java.util.Comparator; |
| 30 | 31 | import java.util.List; |
| 31 | 32 | import java.util.Map; |
| 33 | +import java.util.Set; | |
| 32 | 34 | import java.util.TreeSet; |
| 33 | 35 | import jp.sourceforge.users.yutang.omegat.plugin.moenizer.MoeUtil; |
| 34 | 36 | import org.omegat.util.Log; |
| @@ -40,18 +42,47 @@ | ||
| 40 | 42 | */ |
| 41 | 43 | public class ConfigSet { |
| 42 | 44 | |
| 45 | + private final String CONF_GENERAL = "__general__"; | |
| 46 | + | |
| 43 | 47 | private final File file; |
| 44 | 48 | private ConfigObject config = null; |
| 45 | - private TreeSet<Config> configSet = null; | |
| 49 | + private final TreeSet<Map.Entry<String, ConfigObject>> configs | |
| 50 | + = new TreeSet<Map.Entry<String, ConfigObject>> | |
| 51 | + (new Comparator<Map.Entry<String, ConfigObject>>() { | |
| 52 | + @Override | |
| 53 | + public int compare(Map.Entry<String, ConfigObject> conf1, Map.Entry<String, ConfigObject> conf2) { | |
| 54 | + int index1 = (Integer) conf1.getValue().get(Config.KEY_INDEX); | |
| 55 | + int index2 = (Integer) conf2.getValue().get(Config.KEY_INDEX); | |
| 56 | + return index1 - index2; | |
| 57 | + } | |
| 58 | + }); | |
| 59 | +; | |
| 46 | 60 | |
| 47 | 61 | public ConfigSet(File file) throws IOException { |
| 48 | 62 | this.file = file; |
| 49 | 63 | if (file.isFile()) { |
| 50 | 64 | config = new ConfigSlurper().parse(file.toURI().toURL()); |
| 51 | - configSet = getConfigSet(config); | |
| 65 | + buildProviderConfigs(config); | |
| 52 | 66 | } |
| 53 | 67 | } |
| 54 | 68 | |
| 69 | + public Set<Map.Entry<String, ConfigObject>> getConfigs() { | |
| 70 | + return configs; | |
| 71 | + } | |
| 72 | + | |
| 73 | + public Map.Entry<String, ConfigObject> getCurrentConfig() { | |
| 74 | + for (Map.Entry<String, ConfigObject> entry : configs) { | |
| 75 | + boolean enabled = (Boolean) entry.getValue().get("enabled"); | |
| 76 | + if (enabled) { | |
| 77 | + return entry; | |
| 78 | + } | |
| 79 | + } | |
| 80 | + | |
| 81 | + Map.Entry<String, ConfigObject> defaultConfig = getDefaultConfig(); | |
| 82 | + boolean enabled = (Boolean) defaultConfig.getValue().get("enabled"); | |
| 83 | + return enabled ? defaultConfig : null; | |
| 84 | + } | |
| 85 | + | |
| 55 | 86 | public void save() throws IOException { |
| 56 | 87 | Writer writer = null; |
| 57 | 88 | try { |
| @@ -66,45 +97,21 @@ | ||
| 66 | 97 | } |
| 67 | 98 | } |
| 68 | 99 | |
| 69 | - private TreeSet<Config> getConfigSet(ConfigObject config) { | |
| 70 | - TreeSet<Config> ts = new TreeSet<Config>(new Comparator<Config>() { | |
| 71 | - | |
| 72 | - @Override | |
| 73 | - public int compare(Config o1, Config o2) { | |
| 74 | - return o1.getIndex() - o2.getIndex(); | |
| 75 | - } | |
| 76 | - }); | |
| 77 | - | |
| 100 | + private void buildProviderConfigs(ConfigObject config) { | |
| 78 | 101 | for (Object entry : config.entrySet()) { |
| 79 | 102 | Map.Entry<String, ConfigObject> e = (Map.Entry<String, ConfigObject>) entry; |
| 80 | 103 | String name = e.getKey(); |
| 81 | 104 | ConfigObject conf = e.getValue(); |
| 82 | - String providerName = (String) conf.get(Config.KEY_PROVIDER_TYPE); | |
| 83 | - ts.add(createConfig(name, conf, Config.ProviderType.valueOf(providerName))); | |
| 105 | + if (!name.equals(CONF_GENERAL)) { | |
| 106 | + configs.add(e); | |
| 107 | + } | |
| 84 | 108 | } |
| 85 | - | |
| 86 | - return ts; | |
| 87 | 109 | } |
| 88 | 110 | |
| 89 | - private Config createConfig(String name, ConfigObject conf, | |
| 90 | - Config.ProviderType provider) { | |
| 91 | - switch(provider) { | |
| 92 | - case Color: | |
| 93 | - return new ColorConfig(name, conf); | |
| 94 | - case SimpleImage: | |
| 95 | - return new SimpleImageConfig(name, conf); | |
| 96 | - default: | |
| 97 | - throw new AssertionError(provider.name()); | |
| 98 | - } | |
| 99 | - } | |
| 111 | + public static Map.Entry<String, ConfigObject> getDefaultConfig() { | |
| 112 | + boolean enabled = false; | |
| 113 | + String image = ""; | |
| 100 | 114 | |
| 101 | - public static Config getDefaultConfig() { | |
| 102 | - String name = "__default__"; | |
| 103 | - int index = -1; | |
| 104 | - int opacity = 50; | |
| 105 | - boolean enabled = false; | |
| 106 | - String image = null; | |
| 107 | - | |
| 108 | 115 | // search image files |
| 109 | 116 | File parentDir = MoeUtil.getMoeConfigDir(); |
| 110 | 117 | try { |
| @@ -117,7 +124,13 @@ | ||
| 117 | 124 | Log.log(ex.getMessage()); |
| 118 | 125 | } |
| 119 | 126 | |
| 120 | - return new SimpleImageConfig(name, index, enabled, opacity, image); | |
| 127 | + ConfigObject conf = new ConfigObject(); | |
| 128 | + conf.put("index", -1); | |
| 129 | + conf.put("enabled", enabled); | |
| 130 | + conf.put("opacity", 50); | |
| 131 | + conf.put("image", image); | |
| 132 | + | |
| 133 | + return new AbstractMap.SimpleEntry<String, ConfigObject>("__default__",conf); | |
| 121 | 134 | } |
| 122 | 135 | |
| 123 | 136 | private static List<String> getImagePaths(File parentDir) throws IOException { |