From 8f67c7338342559bfe42f52f0614f6dae19569bd Mon Sep 17 00:00:00 2001 From: "jason@long.name" Date: Tue, 21 May 2013 13:27:57 +0000 Subject: [PATCH] TranslationTool: now able to load/save strings files git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@660 d9718cc8-9f43-0410-858b-315f434eb58c --- src/micropolisj/util/TranslationTool.java | 239 ++++++++++++++++++++-- 1 file changed, 226 insertions(+), 13 deletions(-) diff --git a/src/micropolisj/util/TranslationTool.java b/src/micropolisj/util/TranslationTool.java index b745063..43b9e15 100644 --- a/src/micropolisj/util/TranslationTool.java +++ b/src/micropolisj/util/TranslationTool.java @@ -1,6 +1,7 @@ package micropolisj.util; import java.awt.*; +import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; @@ -17,7 +18,10 @@ public class TranslationTool extends JFrame setTitle("MicropolisJ Translation Tool"); stringsModel = new StringsModel(); + stringsModel.addLocale(null); + stringsTable = new JTable(stringsModel); + stringsTable.setDefaultEditor(String.class, new DefaultCellEditor(new JTextField())); JScrollPane scrollPane = new JScrollPane(stringsTable); stringsTable.setFillsViewportHeight(true); getContentPane().add(scrollPane, BorderLayout.CENTER); @@ -27,6 +31,17 @@ public class TranslationTool extends JFrame JButton btn; btn = new JButton("Add Locale"); + btn.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + onAddLocaleClicked(); + }}); + buttonPane.add(btn); + + btn = new JButton("Save"); + btn.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + onSaveClicked(); + }}); buttonPane.add(btn); pack(); @@ -34,47 +49,245 @@ public class TranslationTool extends JFrame setLocationRelativeTo(null); } + private void onSaveClicked() + { + try + { + stringsModel.save(); + } + catch (Exception e) + { + JOptionPane.showMessageDialog(this, + e.getMessage(), + "Error", + JOptionPane.ERROR_MESSAGE); + } + } + + private void onAddLocaleClicked() + { + Locale L = Locale.getDefault(); + + JTextField langEntry = new JTextField(L.getLanguage()); + JTextField countryEntry = new JTextField(L.getCountry()); + JTextField variantEntry = new JTextField(L.getVariant()); + + JComponent [] inputs = new JComponent[] { + new JLabel("Language"), + langEntry, + new JLabel("Country"), + countryEntry, + new JLabel("Variant (optional)"), + variantEntry + }; + int rv = JOptionPane.showOptionDialog(this, + inputs, + "Add Locale", + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.PLAIN_MESSAGE, + null, null, null); + if (rv != JOptionPane.OK_OPTION) + return; + + try + { + String language = langEntry.getText(); + String country = countryEntry.getText(); + String variant = variantEntry.getText(); + + if (language.length() == 0) { + throw new Exception("Language is required"); + } + + String code = language; + if (country.length() != 0) { + code += "_" + country; + if (variant.length() != 0) { + code += "_" + variant; + } + } + else if (variant.length() != 0) { + throw new Exception("Cannot specify variant without a country code."); + } + + stringsModel.addLocale(code); + } + catch (Exception e) + { + JOptionPane.showMessageDialog(this, + e.getMessage(), + "Error", + JOptionPane.ERROR_MESSAGE); + } + } + public static void main(String [] args) throws Exception { new TranslationTool().setVisible(true); } - class StringsModel extends AbstractTableModel + static class StringsModel extends AbstractTableModel { - Properties p; - String [] propNames; + StringInfo [] strings; + ArrayList locales = new ArrayList<>(); + + static class MyLocaleInfo + { + String code; + HashMap propsMap = new HashMap<>(); + boolean dirty; + + MyLocaleInfo(String code) { + this.code = code; + } + } + + static class StringInfo + { + String file; + String id; + + StringInfo(String file, String id) + { + this.file = file; + this.id = id; + } + } + + static final String [] FILES = { + "CityMessages", + "CityStrings", + "GuiStrings", + "StatusMessages" + }; StringsModel() throws IOException { - p = new Properties(); - p.load(new FileInputStream("strings/CityMessages.properties")); - p.load(new FileInputStream("strings/CityStrings.properties")); - p.load(new FileInputStream("strings/GuiStrings.properties")); - p.load(new FileInputStream("strings/StatusMessages.properties")); + ArrayList ss = new ArrayList<>(); + for (String f : FILES) { + loadStrings(f, ss); + } + strings = ss.toArray(new StringInfo[0]); + } - propNames = p.keySet().toArray(new String[0]); + static void loadStrings(String file, ArrayList 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) { - return propNames[row]; + StringInfo si = strings[row]; + if (col == 0) { + return si.id; + } + + MyLocaleInfo l = locales.get(col-1); + Properties p = l.propsMap.get(si.file); + return p.getProperty(si.id); } + @Override public int getRowCount() { - return propNames.length; + return strings.length; } + @Override public int getColumnCount() { - return 1; + return 1 + locales.size(); } + @Override + public Class getColumnClass(int col) + { + return String.class; + } + + @Override public String getColumnName(int col) { - return "String Id"; + 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) + { + return col != 0; + } + + @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("strings/" + +file + +(localeCode != null ? "_"+localeCode : "") + +".properties"); + } + + void addLocale(String localeCode) + throws IOException + { + MyLocaleInfo li = new MyLocaleInfo(localeCode); + for (String file : FILES) + { + Properties p = new Properties(); + File f = getPFile(file, localeCode); + if (f.exists()) { + p.load(new FileInputStream(f)); + } + li.propsMap.put(file, p); + } + + locales.add(li); + fireTableStructureChanged(); + } + + 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); + p.store(new FileOutputStream(f), l.code); + } + } } } }