9 package micropolisj.util;
 
   13 import javax.swing.table.*;
 
   15 class StringsModel 
extends AbstractTableModel
 
   17         StringInfo [] strings;
 
   18         ArrayList<MyLocaleInfo> locales = 
new ArrayList<MyLocaleInfo>();
 
   20         static class MyLocaleInfo
 
   23                 HashMap<String,Properties> propsMap = 
new HashMap<String,Properties>();
 
   26                 MyLocaleInfo(String code) {
 
   31         static class StringInfo
 
   36                 StringInfo(String file, String 
id)
 
   43         static final String [] FILES = {
 
   50         File workingDirectory;
 
   52         StringsModel() throws IOException
 
   54                 workingDirectory = 
new File(
 
   55                         new File(System.getProperty(
"user.home")),
 
   56                         "micropolis-translations" 
   59                 ArrayList<StringInfo> ss = 
new ArrayList<StringInfo>();
 
   60                 for (String f : FILES) {
 
   63                 strings = ss.toArray(
new StringInfo[0]);
 
   66         static void loadStrings(String file, ArrayList<StringInfo> ss)
 
   69                 Properties p = 
new Properties();
 
   70                 p.load(StringsModel.class.getResourceAsStream(
"/micropolisj/"+file+
".properties"));
 
   71                 String [] propNames = p.keySet().toArray(
new String[0]);
 
   72                 Arrays.sort(propNames);
 
   74                 for (String propName : propNames)
 
   76                         StringInfo si = 
new StringInfo(file, propName);
 
   81         public Object getValueAt(
int row, 
int col)
 
   83                 StringInfo si = strings[row];
 
   88                 MyLocaleInfo l = locales.get(col-1);
 
   89                 Properties p = l.propsMap.get(si.file);
 
   90                 return p.getProperty(si.id);
 
   94         public int getRowCount()
 
   96                 return strings.length;
 
  100         public int getColumnCount()
 
  102                 return 1 + locales.size();
 
  106         public Class getColumnClass(
int col)
 
  112         public String getColumnName(
int col)
 
  118                         MyLocaleInfo l = locales.get(col-1);
 
  119                         return l.code != null ? l.code : 
"C";
 
  124         public boolean isCellEditable(
int row, 
int col)
 
  130                         MyLocaleInfo l = locales.get(col-1);
 
  131                         return l.code != null;
 
  136         public void setValueAt(Object aValue, 
int row, 
int col)
 
  138                 StringInfo si = strings[row];
 
  143                 MyLocaleInfo l = locales.get(col-1);
 
  144                 Properties p = l.propsMap.get(si.file);
 
  145                 p.setProperty(si.id, (String)aValue);
 
  152         File getPFile(String file, String localeCode)
 
  154                 File d = 
new File(workingDirectory, 
"micropolisj");
 
  157                         +(localeCode != null ? 
"_"+localeCode : 
"")
 
  161         void addLocale(String localeCode)
 
  164                 MyLocaleInfo li = 
new MyLocaleInfo(localeCode);
 
  165                 for (String file : FILES)
 
  167                         Properties p = 
new Properties();
 
  170                                 String s = 
"/micropolisj/"+file+(localeCode != null ? 
"_"+localeCode : 
"") + 
".properties";
 
  171                                 InputStream in = getClass().getResourceAsStream(s);
 
  176                         File f = getPFile(file, localeCode);
 
  178                                 p.load(
new FileInputStream(f));
 
  180                         li.propsMap.put(file, p);
 
  184                 fireTableStructureChanged();
 
  187         String [] getAllLocaleCodes()
 
  189                 String [] rv = 
new String[locales.size()];
 
  190                 for (
int i = 0; i < rv.length; i++) {
 
  191                         rv[i] = locales.get(i).code;
 
  196         void removeLocale(String localeCode)
 
  198                 assert localeCode != null;
 
  200                 boolean found = 
false;
 
  201                 for (
int i = locales.size()-1; i >= 0; i--) {
 
  202                         String loc = locales.get(i).code;
 
  203                         if (localeCode.equals(loc)) {
 
  209                         fireTableStructureChanged();
 
  213         void makeDirectories(File f)
 
  216                 File d = f.getParentFile();
 
  225                 for (MyLocaleInfo l : locales)
 
  227                         if (!l.dirty) 
continue;
 
  229                         for (String file : FILES)
 
  231                                 Properties p = l.propsMap.get(file);
 
  232                                 File f = getPFile(file, l.code);
 
  234                                 p.store(
new FileOutputStream(f), l.code);