translation-tool: save working files in user's home directory

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@665 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-05-21 13:29:26 +00:00
parent 11a5489fb7
commit ac980de693

View file

@ -84,7 +84,9 @@ public class TranslationTool extends JFrame
try try
{ {
String javaPath = "java"; String javaPath = "java";
String classPath = "." + System.getProperty("path.separator") + "micropolisj.jar"; String classPath = stringsModel.workingDirectory.toString()
+ System.getProperty("path.separator")
+ "micropolisj.jar";
ProcessBuilder processBuilder = ProcessBuilder processBuilder =
new ProcessBuilder(javaPath, new ProcessBuilder(javaPath,
@ -194,187 +196,199 @@ public class TranslationTool extends JFrame
new TranslationTool().setVisible(true); new TranslationTool().setVisible(true);
} }
static class StringsModel extends AbstractTableModel }
class StringsModel extends AbstractTableModel
{
StringInfo [] strings;
ArrayList<MyLocaleInfo> locales = new ArrayList<MyLocaleInfo>();
static class MyLocaleInfo
{ {
StringInfo [] strings; String code;
ArrayList<MyLocaleInfo> locales = new ArrayList<MyLocaleInfo>(); HashMap<String,Properties> propsMap = new HashMap<String,Properties>();
boolean dirty;
static class MyLocaleInfo MyLocaleInfo(String code) {
this.code = code;
}
}
static class StringInfo
{
String file;
String id;
StringInfo(String file, String id)
{ {
String code; this.file = file;
HashMap<String,Properties> propsMap = new HashMap<String,Properties>(); this.id = id;
boolean dirty; }
}
MyLocaleInfo(String code) { static final String [] FILES = {
this.code = code; "CityMessages",
} "CityStrings",
"GuiStrings",
"StatusMessages"
};
File workingDirectory;
StringsModel() throws IOException
{
workingDirectory = new File(
new File(System.getProperty("user.home")),
"micropolis-translations"
);
ArrayList<StringInfo> ss = new ArrayList<StringInfo>();
for (String f : FILES) {
loadStrings(f, ss);
}
strings = ss.toArray(new StringInfo[0]);
}
static void loadStrings(String file, ArrayList<StringInfo> ss)
throws IOException
{
Properties p = new Properties();
p.load(new FileInputStream("strings/"+file+".properties"));
String [] propNames = p.keySet().toArray(new String[0]);
Arrays.sort(propNames);
for (String propName : propNames)
{
StringInfo si = new StringInfo(file, propName);
ss.add(si);
}
}
public Object getValueAt(int row, int col)
{
StringInfo si = strings[row];
if (col == 0) {
return si.id;
} }
static class StringInfo MyLocaleInfo l = locales.get(col-1);
{ Properties p = l.propsMap.get(si.file);
String file; return p.getProperty(si.id);
String id; }
StringInfo(String file, String id) @Override
{ public int getRowCount()
this.file = file; {
this.id = id; return strings.length;
} }
@Override
public int getColumnCount()
{
return 1 + locales.size();
}
@Override
public Class getColumnClass(int col)
{
return String.class;
}
@Override
public String getColumnName(int col)
{
if (col == 0) {
return "String";
}
else {
MyLocaleInfo l = locales.get(col-1);
return l.code != null ? l.code : "C";
}
}
@Override
public boolean isCellEditable(int row, int col)
{
if (col == 0) {
return false;
}
else {
MyLocaleInfo l = locales.get(col-1);
return l.code != null;
}
}
@Override
public void setValueAt(Object aValue, int row, int col)
{
StringInfo si = strings[row];
if (col == 0) {
return;
} }
static final String [] FILES = { MyLocaleInfo l = locales.get(col-1);
"CityMessages", Properties p = l.propsMap.get(si.file);
"CityStrings", p.setProperty(si.id, (String)aValue);
"GuiStrings", l.dirty = true;
"StatusMessages" }
};
StringsModel() throws IOException /**
{ * Gets the file in the user's working directory.
ArrayList<StringInfo> ss = new ArrayList<StringInfo>(); */
for (String f : FILES) { File getPFile(String file, String localeCode)
loadStrings(f, ss); {
} File d = new File(workingDirectory, "micropolisj");
strings = ss.toArray(new StringInfo[0]); return new File(d,
} file
+(localeCode != null ? "_"+localeCode : "")
+".properties");
}
static void loadStrings(String file, ArrayList<StringInfo> ss) void addLocale(String localeCode)
throws IOException throws IOException
{
MyLocaleInfo li = new MyLocaleInfo(localeCode);
for (String file : FILES)
{ {
Properties p = new Properties(); Properties p = new Properties();
p.load(new FileInputStream("strings/"+file+".properties")); if (localeCode == null) {
String [] propNames = p.keySet().toArray(new String[0]); p.load(getClass().getResourceAsStream("/micropolisj/"+file+".properties"));
Arrays.sort(propNames);
for (String propName : propNames)
{
StringInfo si = new StringInfo(file, propName);
ss.add(si);
} }
} File f = getPFile(file, localeCode);
if (f.exists()) {
public Object getValueAt(int row, int col) p.load(new FileInputStream(f));
{
StringInfo si = strings[row];
if (col == 0) {
return si.id;
} }
li.propsMap.put(file, p);
MyLocaleInfo l = locales.get(col-1);
Properties p = l.propsMap.get(si.file);
return p.getProperty(si.id);
} }
@Override locales.add(li);
public int getRowCount() fireTableStructureChanged();
{ }
return strings.length;
void makeDirectories(File f)
throws IOException
{
File d = f.getParentFile();
if (d != null) {
d.mkdirs();
} }
}
@Override void save()
public int getColumnCount() throws IOException
{
for (MyLocaleInfo l : locales)
{ {
return 1 + locales.size(); if (!l.dirty) continue;
}
@Override
public Class getColumnClass(int col)
{
return String.class;
}
@Override
public String getColumnName(int col)
{
if (col == 0) {
return "String";
}
else {
MyLocaleInfo l = locales.get(col-1);
return l.code != null ? l.code : "C";
}
}
@Override
public boolean isCellEditable(int row, int col)
{
if (col == 0) {
return false;
}
else {
MyLocaleInfo l = locales.get(col-1);
return l.code != null;
}
}
@Override
public void setValueAt(Object aValue, int row, int col)
{
StringInfo si = strings[row];
if (col == 0) {
return;
}
MyLocaleInfo l = locales.get(col-1);
Properties p = l.propsMap.get(si.file);
p.setProperty(si.id, (String)aValue);
l.dirty = true;
}
File getPFile(String file, String localeCode)
{
return new File("micropolisj/"
+file
+(localeCode != null ? "_"+localeCode : "")
+".properties");
}
void addLocale(String localeCode)
throws IOException
{
MyLocaleInfo li = new MyLocaleInfo(localeCode);
for (String file : FILES) for (String file : FILES)
{ {
Properties p = new Properties(); Properties p = l.propsMap.get(file);
if (localeCode == null) { File f = getPFile(file, l.code);
p.load(getClass().getResourceAsStream("/micropolisj/"+file+".properties")); makeDirectories(f);
} p.store(new FileOutputStream(f), l.code);
File f = getPFile(file, localeCode);
if (f.exists()) {
p.load(new FileInputStream(f));
}
li.propsMap.put(file, p);
}
locales.add(li);
fireTableStructureChanged();
}
void makeDirectories(File f)
throws IOException
{
File d = f.getParentFile();
if (d != null) {
d.mkdirs();
}
}
void save()
throws IOException
{
for (MyLocaleInfo l : locales)
{
if (!l.dirty) continue;
for (String file : FILES)
{
Properties p = l.propsMap.get(file);
File f = getPFile(file, l.code);
makeDirectories(f);
p.store(new FileOutputStream(f), l.code);
}
l.dirty = false;
} }
l.dirty = false;
} }
} }
} }