• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

OmegaT の背景に画像を表示します。


Commit MetaInfo

Revision24 (tree)
Time2014-05-13 18:23:46
Authoryu-tang

Log Message

MoeConfig クラスを /config/ConfigSet.java で置き換えて、/config パッケージを廃止に

Change Summary

Incremental Difference

--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/moenizer/config/Config.java (revision 23)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/moenizer/config/Config.java (nonexistent)
@@ -1,65 +0,0 @@
1-/**************************************************************************
2- Moenizer - Allow to set background image for OmegaT.
3-
4- Copyright (C) 2014 Yu Tang
5- Home page: http://sourceforge.jp/users/yu-tang/
6- Support center: http://sourceforge.jp/users/yu-tang/pf/Moenizer/
7-
8- This file is part of plugin for OmegaT.
9- http://www.omegat.org/
10-
11- License: GNU GPL version 3 or (at your option) any later version.
12-
13- You should have received a copy of the GNU General Public License
14- along with this program. If not, see <http://www.gnu.org/licenses/>.
15- **************************************************************************/
16-
17-package jp.sourceforge.users.yutang.omegat.plugin.moenizer.config;
18-
19-/**
20- * Abstract class for storing config data.
21- *
22- * @author Yu-Tang
23- */
24-public abstract class Config {
25-
26- // common
27- public static final String KEY_INDEX = "index";
28- public static final String KEY_ENABLED = "enabled";
29- public static final String KEY_PROVIDER_TYPE = "providerType";
30-
31- // Color
32- public static final String KEY_RGB = "rgb";
33-
34- // SimpleImage
35- public static final String KEY_OPACITY = "opacity";
36- public static final String KEY_IMAGE = "image";
37-
38- enum ProviderType {
39- Color, SimpleImage
40- }
41-
42- private final String name;
43- private final int index;
44- private final boolean enabled;
45-
46- protected Config(String name, int index, boolean enabled) {
47- this.name = name;
48- this.index = index;
49- this.enabled = enabled;
50- }
51-
52- public String getName() {
53- return name;
54- }
55-
56- public int getIndex() {
57- return index;
58- }
59-
60- public boolean isEnabled() {
61- return enabled;
62- }
63-
64- abstract ProviderType getProviderType();
65-}
--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/moenizer/config/ConfigSet.java (revision 23)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/moenizer/config/ConfigSet.java (nonexistent)
@@ -1,158 +0,0 @@
1-/**************************************************************************
2- Moenizer - Allow to set background image for OmegaT.
3-
4- Copyright (C) 2014 Yu Tang
5- Home page: http://sourceforge.jp/users/yu-tang/
6- Support center: http://sourceforge.jp/users/yu-tang/pf/Moenizer/
7-
8- This file is part of plugin for OmegaT.
9- http://www.omegat.org/
10-
11- License: GNU GPL version 3 or (at your option) any later version.
12-
13- You should have received a copy of the GNU General Public License
14- along with this program. If not, see <http://www.gnu.org/licenses/>.
15- **************************************************************************/
16-
17-package jp.sourceforge.users.yutang.omegat.plugin.moenizer.config;
18-
19-import groovy.util.ConfigObject;
20-import groovy.util.ConfigSlurper;
21-import java.io.BufferedWriter;
22-import java.io.File;
23-import java.io.FileWriter;
24-import java.io.IOException;
25-import java.io.Writer;
26-import java.net.URL;
27-import java.util.AbstractMap;
28-import java.util.ArrayList;
29-import java.util.Collections;
30-import java.util.Comparator;
31-import java.util.List;
32-import java.util.Map;
33-import java.util.Set;
34-import java.util.TreeSet;
35-import jp.sourceforge.users.yutang.omegat.plugin.moenizer.MoeUtil;
36-import org.omegat.util.Log;
37-
38-/**
39- * Manage config collection
40- *
41- * @author Yu-Tang
42- */
43-public class ConfigSet {
44-
45- private final String CONF_GENERAL = "__general__";
46-
47- private final File file;
48- private ConfigObject config = 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-;
60-
61- public ConfigSet(File file) throws IOException {
62- this.file = file;
63- if (file.isFile()) {
64- config = new ConfigSlurper().parse(file.toURI().toURL());
65- buildProviderConfigs(config);
66- }
67- }
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-
86- public void save() throws IOException {
87- Writer writer = null;
88- try {
89- writer = new BufferedWriter(new FileWriter(file));
90- config.writeTo(writer);
91- } finally {
92- try {
93- if (writer != null) {
94- writer.close();
95- }
96- } catch(IOException ex) { /* ignore */ }
97- }
98- }
99-
100- private void buildProviderConfigs(ConfigObject config) {
101- for (Object entry : config.entrySet()) {
102- Map.Entry<String, ConfigObject> e = (Map.Entry<String, ConfigObject>) entry;
103- String name = e.getKey();
104- ConfigObject conf = e.getValue();
105- if (!name.equals(CONF_GENERAL)) {
106- configs.add(e);
107- }
108- }
109- }
110-
111- public static Map.Entry<String, ConfigObject> getDefaultConfig() {
112- boolean enabled = false;
113- String image = "";
114-
115- // search image files
116- File parentDir = MoeUtil.getMoeConfigDir();
117- try {
118- List<String> imagePaths = getImagePaths(parentDir);
119- if (!imagePaths.isEmpty()) {
120- enabled = true;
121- image = imagePaths.get(0);
122- }
123- } catch (IOException ex) {
124- Log.log(ex.getMessage());
125- }
126-
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);
134- }
135-
136- private static List<String> getImagePaths(File parentDir) throws IOException {
137- ArrayList<String> array = new ArrayList<String>();
138- if (parentDir.isDirectory()) {
139- for (File f: parentDir.listFiles()) {
140- if (f.isFile()) {
141- if (MoeUtil.isImage(f)) {
142- array.add(f.getCanonicalPath());
143- }else if (MoeUtil.isURLShortcut(f)) {
144- URL url = MoeUtil.getURL(f);
145- array.add(url.toExternalForm());
146- }
147- }
148- }
149- }
150-
151- if (!array.isEmpty()) {
152- Collections.sort(array);
153- }
154-
155- return array;
156- }
157-
158-}
--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/moenizer/MoeConfig.java (revision 23)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/moenizer/MoeConfig.java (revision 24)
@@ -1,7 +1,7 @@
11 /**************************************************************************
22 Moenizer - Allow to set background image for OmegaT.
33
4- Copyright (C) 2013-2014 Yu Tang
4+ Copyright (C) 2014 Yu Tang
55 Home page: http://sourceforge.jp/users/yu-tang/
66 Support center: http://sourceforge.jp/users/yu-tang/pf/Moenizer/
77
@@ -14,313 +14,166 @@
1414 along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 **************************************************************************/
1616
17-package jp.sourceforge.users.yutang.omegat.plugin.moenizer;
17+package jp.sourceforge.users.yutang.omegat.plugin.moenizer.config;
1818
19+import groovy.util.ConfigObject;
20+import groovy.util.ConfigSlurper;
21+import java.io.BufferedWriter;
1922 import java.io.File;
20-import java.io.FileFilter;
23+import java.io.FileWriter;
24+import java.io.IOException;
25+import java.io.Writer;
26+import java.net.URISyntaxException;
2127 import java.net.URL;
22-import java.util.HashSet;
28+import java.util.ArrayList;
29+import java.util.Collections;
30+import java.util.Comparator;
31+import java.util.List;
32+import java.util.Map;
2333 import java.util.Set;
24-import java.util.regex.Matcher;
25-import java.util.regex.Pattern;
26-import org.omegat.util.FileUtil;
34+import java.util.TreeSet;
35+import java.util.logging.Level;
36+import java.util.logging.Logger;
37+import jp.sourceforge.users.yutang.omegat.plugin.moenizer.MoeUtil;
2738 import org.omegat.util.Log;
2839
2940 /**
30- *
41+ * Manage config tree
42+ *
3143 * @author Yu-Tang
3244 */
33-public class MoeConfig {
34- private static final String CONFIG_FILE_NAME = "moenizer.conf";
35- private static final String SCRIPT_FILE_NAME = "moenizer.groovy";
36-
37- private static final float DEFAULT_OPACITY = 0.5f;
38- private static final int MAX_OPACITY_IN_PERCENTAGE = 100; // Opaque
39- private static final int MIN_OPACITY_IN_PERCENTAGE = 0; // Transparent
45+public class ConfigSet {
4046
41- private static final long DEFAULT_INTERVAL = 60L * 1000L; // 1 minute in milliseconds
42- private static final long MAX_INTERVAL = 86400L * 1000L; // 24 hours
43- private static final long MIN_INTERVAL = 3L * 1000L; // 3 seconds
47+ public enum ProviderType {
48+ Color, SimpleImage
49+ }
50+ private final String CONF_GENERAL = "__general__";
51+ private final String KEY_INDEX = "index";
52+ private final String KEY_ENABLED = "enabled";
4453
45- private static final Pattern RE_LINEBREAK = Pattern.compile("[\\n\\r]+");
46- private static final Pattern RE_OPACITY = Pattern.compile("(\\d+)\\s*%$");
47- private static final Pattern RE_INTERVAL = Pattern.compile("(\\d+)\\s*([msh])", Pattern.CASE_INSENSITIVE);
48- private static final Pattern RE_SCRIPT = Pattern.compile("(?:/|\\\\\\\\[^\\\\]|[a-zA-Z]:\\\\).+\\.groovy");
49- private static final Pattern RE_SOURCE = Pattern.compile("(?:http://|https://|/|\\\\\\\\[^\\\\]|[a-zA-Z]:\\\\).+");
50- private static final Pattern RE_HAS_IMAGE_EXTENSION = Pattern.compile(".+\\.(?:bmp|png|jpg|jpeg|gif)");
51- private static final Pattern RE_HAS_INTERNET_SHORTCUT_EXTENSION = Pattern.compile(".+\\.(?:url|website|webloc)");
52- private static final Pattern RE_HTTP_OR_HTTPS_PROTOCOL = Pattern.compile("(?:http://|https://).+");
53- private static final Pattern RE_SOURCE_IN_PLUGIN_DIR = Pattern.compile(".+\\.(?:bmp|png|jpg|jpeg|gif|url|website|webloc)");
54-
55- private static ConfigData data;
56- private static HashSet<Object> sources;
57-
58- static {
59- // load config from file
60- ConfigData cd = loadConfig();
61-
62- // flatten source dir to files (images only)
63- HashSet<Object> flattenSources = getFlattenSources(cd.source);
64-
65- // scan plugin dir if both sources and script are empty
66- if (flattenSources.isEmpty() && cd.script.isEmpty()) {
67- cd.script = getScriptInPluginDir();
68- flattenSources = getSourcesInPluginDir();
54+ private final File file;
55+ private ConfigObject config = null;
56+ private final TreeSet<ConfigObject> configs
57+ = new TreeSet<ConfigObject>
58+ (new Comparator<ConfigObject>() {
59+ @Override
60+ public int compare(ConfigObject conf1, ConfigObject conf2) {
61+ int index1 = (Integer) conf1.get(KEY_INDEX);
62+ int index2 = (Integer) conf2.get(KEY_INDEX);
63+ return index1 - index2;
6964 }
65+ });
66+;
7067
71- // set default interval if current interval is 0 and has multiple sources
72- if (cd.interval == 0 && flattenSources.size() > 1) {
73- cd.interval = DEFAULT_INTERVAL;
68+ public ConfigSet(File file) throws IOException {
69+ this.file = file;
70+ if (file.isFile()) {
71+ config = new ConfigSlurper().parse(file.toURI().toURL());
72+ buildProviderConfigs(config);
7473 }
75-
76- data = cd;
77- sources = flattenSources;
7874 }
79-
80- public static long getInterval() {
81- return data.interval;
75+
76+ public Set<ConfigObject> getConfigs() {
77+ return configs;
8278 }
83-
84- public static float getOpacity() {
85- return data.opacity;
79+
80+ public ConfigObject getCurrentConfig() {
81+ for (ConfigObject entry : configs) {
82+ boolean enabled = (Boolean) entry.get(KEY_ENABLED);
83+ if (enabled) {
84+ return entry;
85+ }
86+ }
87+
88+ // No enabled config found. Fallback to default config.
89+ ConfigObject defaultConfig = getDefaultConfig();
90+ boolean enabled = (Boolean) defaultConfig.get(KEY_ENABLED);
91+ return enabled ? defaultConfig : null;
8692 }
87-
88- public static File getScript() {
89- return data.script.isEmpty() ? null : new File(data.script);
90- }
91-
92- public static Object[] getSources() {
93- return sources.toArray();
94- }
9593
96-
97- private static ConfigData loadConfig() {
98- ConfigData data = getDefaultConfig();
94+ public void save() throws IOException {
95+ Writer writer = null;
9996 try {
100- File file = new File(MoeUtil.getPluginJarDir(), CONFIG_FILE_NAME);
101- if ( file.isFile() ) {
102- String text = FileUtil.readTextFile(file);
103- String[] lines = RE_LINEBREAK.split(text);
104- for (String line: lines) {
105- if ( ! line.isEmpty() ) {
106- if (setOpacity(data, line) == true) {
107- // probably opacity value is changed from default
108- } else if (setInterval(data, line) == true) {
109- // probably interval value is changed from default
110- } else if (setScript(data, line) == true) {
111- // script URI
112- } else if (addSource(data, line) == true) {
113- // source URI (image)
114- } else {
115- // ignore
116- }
117- }
97+ writer = new BufferedWriter(new FileWriter(file));
98+ config.writeTo(writer);
99+ } finally {
100+ try {
101+ if (writer != null) {
102+ writer.close();
118103 }
119- } else {
120- }
121- } catch (Exception ex) {
122- Log.log(ex);
104+ } catch(IOException ex) { /* ignore */ }
123105 }
124- return data;
125106 }
126-
127- private static ConfigData getDefaultConfig() {
128- ConfigData cd = new ConfigData();
129- cd.opacity = DEFAULT_OPACITY;
130- cd.interval = 0L;
131- cd.script = "";
132- cd.source = new HashSet<String>(); // should be unique
133- return cd;
134- }
135-
136- /**
137- * set new opacity value to ConfigData
138- * @param data
139- * @param line
140- * @return true if line contains opacity value
141- */
142- private static boolean setOpacity(ConfigData data, String line) {
143- Matcher matcher = RE_OPACITY.matcher(line);
144- if ( matcher.matches() ) {
145- String num = matcher.group(1);
146- int i = Integer.parseInt(num); // in percent
147- if (i > MAX_OPACITY_IN_PERCENTAGE) {
148- data.opacity = MAX_OPACITY_IN_PERCENTAGE;
149- } else if (i < MIN_OPACITY_IN_PERCENTAGE) {
150- data.opacity = MIN_OPACITY_IN_PERCENTAGE;
151- } else {
152- float f = ((float) i / MAX_OPACITY_IN_PERCENTAGE);
153- data.opacity = f;
154- }
155- return true;
156- }
157- return false;
158- }
159107
160- /**
161- * set new interval value to ConfigData
162- * @param data
163- * @param line
164- * @return true if line contains interval value
165- */
166- private static boolean setInterval(ConfigData data, String line) {
167- Matcher matcher = RE_INTERVAL.matcher(line);
168- if ( matcher.matches() ) {
169- String num = matcher.group(1);
170- String unit = matcher.group(2).toLowerCase(); // s | m | h
171-
172- long i = Long.parseLong(num);
173- if (unit.equals("s")) {
174- i = i * 1000;
175- } else if (unit.equals("m")) {
176- i = i * 60 * 1000;
177- } else if (unit.equals("h")) {
178- i = i * 60 * 60 * 1000;
108+ private void buildProviderConfigs(ConfigObject config) {
109+ for (Object entry : config.entrySet()) {
110+ Map.Entry<String, ConfigObject> e = (Map.Entry<String, ConfigObject>) entry;
111+ String name = e.getKey();
112+ ConfigObject conf = e.getValue();
113+ if (!name.equals(CONF_GENERAL)) {
114+ configs.add(conf);
179115 }
180-
181- // validate range
182- if (i > MAX_INTERVAL) {
183- data.interval = MAX_INTERVAL;
184- } else if (i < MIN_INTERVAL) {
185- data.interval = MIN_INTERVAL;
186- } else {
187- data.interval = i;
188- }
189- return true;
190116 }
191- return false;
192117 }
193118
194- /**
195- * set new script URI to ConfigData
196- * @param data
197- * @param line
198- * @return true if line contains script URI
199- */
200- private static boolean setScript(ConfigData data, String line) {
201- Matcher matcher = RE_SCRIPT.matcher(line);
202- if ( matcher.matches() ) {
203- data.script = line;
204- return true;
205- }
206- return false;
207- }
119+ public static ConfigObject getDefaultConfig() {
120+ // search image files from MoeConfigDir
121+ File parentDir = MoeUtil.getMoeConfigDir();
122+ String image = getFirstImagePath(parentDir);
208123
209- /**
210- * add new source URI to ConfigData
211- * @param data
212- * @param line
213- * @return true if line contains source URI
214- */
215- private static boolean addSource(ConfigData data, String line) {
216- Matcher matcher = RE_SOURCE.matcher(line);
217- if ( matcher.matches() ) {
218- data.source.add(line);
219- return true;
220- }
221- return false;
222- }
223-
224- /**
225- *
226- * @param source
227- * @return URL object or File object (file only)
228- */
229- private static HashSet<Object> getFlattenSources(Set<String> source) {
230- HashSet<Object> newSource = new HashSet<Object>(); // should be unique
231- for (String s: source) {
124+ // If image file not found, search image files from PluginJarDir
125+ // for backward compatibility.
126+ if (image.isEmpty()) {
232127 try {
233- if (RE_HTTP_OR_HTTPS_PROTOCOL.matcher(s.toLowerCase()).matches()) {
234- newSource.add(new URL(s));
235- } else {
236- File file = new File(s);
237- // Single file and image file ?
238- if (file.isFile() && RE_HAS_IMAGE_EXTENSION.matcher(s.toLowerCase()).matches()) {
239- newSource.add(file);
240- } else if (file.isDirectory()) {
241- File[] listFiles = file.listFiles(new FileFilter() {
242- @Override
243- public boolean accept(File f) {
244- if (f.isFile()) {
245- String fileName = f.getName().toLowerCase();
246- if (RE_HAS_IMAGE_EXTENSION.matcher(fileName).matches() ||
247- RE_HAS_INTERNET_SHORTCUT_EXTENSION.matcher(fileName).matches()) {
248- return true;
249- }
250- }
251- return false;
252- }
253- });
254- for (File f: listFiles) {
255- String fileName = f.getName().toLowerCase();
256- // image file
257- if (RE_HAS_IMAGE_EXTENSION.matcher(fileName).matches()) {
258- newSource.add(file);
259- // internet shortcut
260- } else if (RE_HAS_INTERNET_SHORTCUT_EXTENSION.matcher(fileName).matches()) {
261- URL url = MoeUtil.getURL(file);
262- if (url != null) {
263- newSource.add(url);
264- }
265- }
266- }
267- }
268- }
269- } catch (Exception ex) {
270- Log.log(ex);
128+ parentDir = MoeUtil.getPluginJarDir();
129+ image = getFirstImagePath(parentDir);
130+ } catch (URISyntaxException ex) {
131+ Log.log(ex.getMessage());
271132 }
272133 }
273- return newSource;
134+
135+ ConfigObject conf = new ConfigObject();
136+ conf.put("index", -1);
137+ conf.put("enabled", !image.isEmpty());
138+ conf.put("providerType", ProviderType.SimpleImage.name());
139+ conf.put("opacity", 0.5f); // 0.0f (Transparent) <--> 1.0f (Opaque)
140+ conf.put("image", image);
141+
142+ return conf;
274143 }
275-
276- private static String getScriptInPluginDir() {
144+
145+ private static String getFirstImagePath(File parentDir) {
277146 try {
278- File file = new File(MoeUtil.getPluginJarDir(), SCRIPT_FILE_NAME);
279- if (file.isFile()) {
280- return file.getCanonicalPath();
147+ List<String> imagePaths = getImagePaths(parentDir);
148+ if (!imagePaths.isEmpty()) {
149+ return imagePaths.get(0);
281150 }
282- } catch (Exception ex) { /* ignore */ }
151+ } catch (IOException ex) {
152+ Log.log(ex.getMessage());
153+ }
283154 return "";
284155 }
285156
286- private static HashSet<Object> getSourcesInPluginDir() {
287- HashSet<Object> newSource = new HashSet<Object>(); // should be unique
288- try {
289- File dir = MoeUtil.getPluginJarDir();
290- File[] listFiles = dir.listFiles(new FileFilter() {
291- @Override
292- public boolean accept(File f) {
293- if (f.isFile()) {
294- String fileName = f.getName().toLowerCase();
295- if (RE_SOURCE_IN_PLUGIN_DIR.matcher(fileName).matches()) {
296- return true;
297- }
157+ private static List<String> getImagePaths(File parentDir) throws IOException {
158+ ArrayList<String> array = new ArrayList<String>();
159+ if (parentDir.isDirectory()) {
160+ for (File f: parentDir.listFiles()) {
161+ if (f.isFile()) {
162+ if (MoeUtil.isImage(f)) {
163+ array.add(f.getCanonicalPath());
164+ }else if (MoeUtil.isURLShortcut(f)) {
165+ URL url = MoeUtil.getURL(f);
166+ array.add(url.toExternalForm());
298167 }
299- return false;
300168 }
301- });
302- for (File f: listFiles) {
303- String fileName = f.getName().toLowerCase();
304- // image file
305- if (RE_HAS_IMAGE_EXTENSION.matcher(fileName).matches()) {
306- newSource.add(f);
307- // internet shortcut
308- } else if (RE_HAS_INTERNET_SHORTCUT_EXTENSION.matcher(fileName).matches()) {
309- URL url = MoeUtil.getURL(f);
310- if (url != null) {
311- newSource.add(url);
312- }
313- }
314169 }
315- } catch (Exception ex) { /* ignore */ }
316- return newSource;
170+ }
171+
172+ if (!array.isEmpty()) {
173+ Collections.sort(array);
174+ }
175+
176+ return array;
317177 }
318178
319- private static class ConfigData {
320- protected float opacity; // 0.0f <--> 1.0f
321- protected long interval; // by milliseconds
322- protected String script;
323- protected Set<String> source;
324- }
325179 }
326-