OmegaT の翻訳状況(Statistics)を Excel で表示します。
OmegaT 3.1 対応 (#50224)
Excel テンプレートに集計列を追加
テンプレートのバージョンを ini ファイルで管理
@@ -0,0 +1,26 @@ | ||
1 | +/************************************************************************** | |
2 | + This code is only a stub. | |
3 | + **************************************************************************/ | |
4 | + | |
5 | +package org.omegat.util; | |
6 | + | |
7 | +/** | |
8 | + * Utilities for string processing. | |
9 | + * | |
10 | + * @author Maxym Mykhalchuk | |
11 | + * @author Didier Briel | |
12 | + * @author Tiago Saboga | |
13 | + * @author Zoltan Bartko | |
14 | + * @author Andrzej Sawula | |
15 | + * @author Alex Buloichik (alex73mail@gmail.com) | |
16 | + */ | |
17 | +public class StringUtil { | |
18 | + | |
19 | + /** | |
20 | + * Check if string is empty, i.e. null or length==0 | |
21 | + */ | |
22 | + public static boolean isEmpty(final String str) { | |
23 | + throw new UnsupportedOperationException("This method is only a stub."); | |
24 | + } | |
25 | + | |
26 | +} |
@@ -0,0 +1,92 @@ | ||
1 | +/************************************************************************** | |
2 | + Stats2Excel - open Statistics with Excel from standard Tools menu. | |
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/ | |
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.stats2excel; | |
18 | + | |
19 | +import java.io.BufferedReader; | |
20 | +import java.io.BufferedWriter; | |
21 | +import java.io.File; | |
22 | +import java.io.FileInputStream; | |
23 | +import java.io.FileWriter; | |
24 | +import java.io.IOException; | |
25 | +import java.io.InputStreamReader; | |
26 | +import java.io.Writer; | |
27 | +import java.util.Properties; | |
28 | +import org.omegat.util.OConsts; | |
29 | + | |
30 | +/** | |
31 | + * | |
32 | + * @author Yu-Tang | |
33 | + */ | |
34 | +public class Settings { | |
35 | + | |
36 | + private final File file; | |
37 | + private final Properties properties; | |
38 | + | |
39 | + // Key | |
40 | + private final String TEMPLATE_VERSION = "TemplateVersion"; | |
41 | + | |
42 | + public Settings(File file) throws IOException { | |
43 | + this.file = file; | |
44 | + properties = new Properties(getDefaultProperties()); | |
45 | + | |
46 | + if (file.isFile()) { | |
47 | + BufferedReader reader = null; | |
48 | + try { | |
49 | + FileInputStream in = new FileInputStream(file); | |
50 | + reader = new BufferedReader(new InputStreamReader(in, OConsts.UTF8)); | |
51 | + properties.load(reader); | |
52 | + } finally { | |
53 | + try { | |
54 | + if (reader != null) { | |
55 | + reader.close(); | |
56 | + } | |
57 | + } catch(IOException ex) { /* ignore */ } | |
58 | + } | |
59 | + } | |
60 | + } | |
61 | + | |
62 | + public String getTemplateVersion() { | |
63 | + return properties.getProperty(TEMPLATE_VERSION); | |
64 | + } | |
65 | + | |
66 | + public void setTemplateVersion(String versionString) { | |
67 | + properties.setProperty(TEMPLATE_VERSION, versionString); | |
68 | + } | |
69 | + | |
70 | + public void save() throws IOException { | |
71 | + Writer writer = null; | |
72 | + try { | |
73 | + writer = new BufferedWriter(new FileWriter(file)); | |
74 | + properties.store(writer, null); | |
75 | + } finally { | |
76 | + try { | |
77 | + if (writer != null) { | |
78 | + writer.close(); | |
79 | + } | |
80 | + } catch(IOException ex) { /* ignore */ } | |
81 | + } | |
82 | + } | |
83 | + | |
84 | + private Properties getDefaultProperties() { | |
85 | + Properties p = new Properties(); | |
86 | + | |
87 | + // set default property here | |
88 | + p.setProperty(TEMPLATE_VERSION, "V1"); | |
89 | + | |
90 | + return p; | |
91 | + } | |
92 | +} |
@@ -19,15 +19,19 @@ | ||
19 | 19 | import java.io.ByteArrayInputStream; |
20 | 20 | import java.io.ByteArrayOutputStream; |
21 | 21 | import java.io.File; |
22 | +import java.io.FileNotFoundException; | |
22 | 23 | import java.io.IOException; |
23 | 24 | import java.io.InputStream; |
24 | 25 | import java.util.ArrayList; |
25 | 26 | import java.util.Arrays; |
26 | 27 | import java.util.List; |
28 | +import javax.swing.JOptionPane; | |
29 | +import org.omegat.core.Core; | |
27 | 30 | import org.omegat.util.LFileCopy; |
28 | 31 | import org.omegat.util.Log; |
29 | 32 | import org.omegat.util.OConsts; |
30 | 33 | import org.omegat.util.StaticUtils; |
34 | +import org.omegat.util.StringUtil; | |
31 | 35 | |
32 | 36 | /** |
33 | 37 | * |
@@ -39,11 +43,16 @@ | ||
39 | 43 | private static final String LEGACY_STANDARD_TEMPLATE_NAME = "StandardTemplate.xlt"; |
40 | 44 | private static final String STANDARD_TEMPLATE_NAME = "StandardTemplate.xltx"; |
41 | 45 | private static final String TXT_NAME = "Stats2Excel.txt"; |
46 | + private static final String SETTINGS_FILE_NAME = "Stats2Excel.ini"; | |
42 | 47 | private static final File _wsf; |
43 | 48 | private static final File _txt; |
44 | 49 | private static String windowTitle; |
45 | 50 | private static int installedExcelVersion; |
46 | 51 | |
52 | + private static enum TemplateVersion { | |
53 | + NA, CUSTOMIZED, V1, V2 | |
54 | + } | |
55 | + | |
47 | 56 | public static final int INSTALLED_VERSION_NOT_FOUND = -1; |
48 | 57 | |
49 | 58 | static { |
@@ -145,10 +154,45 @@ | ||
145 | 154 | LEGACY_STANDARD_TEMPLATE_NAME; // Excel 2003 or earlier |
146 | 155 | File template = new File(parent, templateName); |
147 | 156 | |
148 | - if (! template.exists()) { | |
157 | + // check if we should replace the template file with the latest one. | |
158 | + final Settings settings = new Settings(new File(parent, SETTINGS_FILE_NAME)); | |
159 | + TemplateVersion ver = getTemplateVersion(settings, template); | |
160 | + switch (ver) { | |
161 | + case NA: | |
162 | + // File not found, version not available. | |
163 | + break; | |
164 | + case V1: | |
165 | + // Probably older version is found. Ask user for upgrade. | |
166 | + String msg = L10n.getUpdateTemplateDialogMessage(); | |
167 | + String title = L10n.getUpdateTemplateDialogTitle(); | |
168 | + int ret = Core.getMainWindow().showConfirmDialog(msg, title, | |
169 | + JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); | |
170 | + if (ret == JOptionPane.YES_OPTION) { | |
171 | + template.delete(); | |
172 | + } else { | |
173 | + // set template version setting to CUSTOMIZED | |
174 | + settings.setTemplateVersion(TemplateVersion.CUSTOMIZED.name()); | |
175 | + settings.save(); | |
176 | + } | |
177 | + break; | |
178 | + case V2: | |
179 | + // OK, we have the latest version. Go on. | |
180 | + break; | |
181 | + case CUSTOMIZED: | |
182 | + // Maybe user uses the customized template file. Don't touch it. | |
183 | + break; | |
184 | + default: | |
185 | + // We shouldn't have come here. Something is wrong. | |
186 | + throw new IOException("Unknown Template Version Error: " + ver.toString()); | |
187 | + } | |
188 | + | |
189 | + if (!template.exists()) { | |
149 | 190 | InputStream in = ExcelModel.class.getResourceAsStream(templateName); |
150 | 191 | try { |
151 | 192 | LFileCopy.copy(in, template); |
193 | + | |
194 | + settings.setTemplateVersion(TemplateVersion.V2.name()); | |
195 | + settings.save(); | |
152 | 196 | } finally { |
153 | 197 | in.close(); |
154 | 198 | } |
@@ -156,6 +200,23 @@ | ||
156 | 200 | return template; |
157 | 201 | } |
158 | 202 | |
203 | + private static TemplateVersion getTemplateVersion(Settings settings, File template) throws IOException { | |
204 | + if (!template.isFile()) { | |
205 | + return TemplateVersion.NA; | |
206 | + } | |
207 | + | |
208 | + String ver = settings.getTemplateVersion(); | |
209 | + if (StringUtil.isEmpty(ver)) { | |
210 | + return TemplateVersion.V1; | |
211 | + } else { | |
212 | + try { | |
213 | + return TemplateVersion.valueOf(ver); | |
214 | + } catch (IllegalArgumentException ex) { | |
215 | + return TemplateVersion.V1; | |
216 | + } | |
217 | + } | |
218 | + } | |
219 | + | |
159 | 220 | private static File getWSF() throws IOException { |
160 | 221 | if (! _wsf.exists()) { |
161 | 222 | InputStream in = ExcelModel.class.getResourceAsStream(WSF_NAME); |
@@ -42,6 +42,16 @@ | ||
42 | 42 | return bundle.getString("S2E_EXCEL_WINDOW_TITLE"); |
43 | 43 | } |
44 | 44 | |
45 | + // Update template dialog | |
46 | + | |
47 | + public static String getUpdateTemplateDialogTitle() { | |
48 | + return bundle.getString("S2E_UPDATE_TEMPLATE_DIALOG_TITLE"); | |
49 | + } | |
50 | + | |
51 | + public static String getUpdateTemplateDialogMessage() { | |
52 | + return bundle.getString("S2E_UPDATE_TEMPLATE_DIALOG_MESSAGE"); | |
53 | + } | |
54 | + | |
45 | 55 | // Error |
46 | 56 | |
47 | 57 | public static String getLoadingErrorFormat() { |