Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
StringsModel.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.util;
10 
11 import java.io.*;
12 import java.util.*;
13 import javax.swing.table.*;
14 
15 class StringsModel extends AbstractTableModel
16 {
17  StringInfo [] strings;
18  ArrayList<MyLocaleInfo> locales = new ArrayList<MyLocaleInfo>();
19 
20  static class MyLocaleInfo
21  {
22  String code;
23  HashMap<String,Properties> propsMap = new HashMap<String,Properties>();
24  boolean dirty;
25 
26  MyLocaleInfo(String code) {
27  this.code = code;
28  }
29  }
30 
31  static class StringInfo
32  {
33  String file;
34  String id;
35 
36  StringInfo(String file, String id)
37  {
38  this.file = file;
39  this.id = id;
40  }
41  }
42 
43  static final String [] FILES = {
44  "CityMessages",
45  "CityStrings",
46  "GuiStrings",
47  "StatusMessages"
48  };
49 
50  File workingDirectory;
51 
52  StringsModel() throws IOException
53  {
54  workingDirectory = new File(
55  new File(System.getProperty("user.home")),
56  "micropolis-translations"
57  );
58 
59  ArrayList<StringInfo> ss = new ArrayList<StringInfo>();
60  for (String f : FILES) {
61  loadStrings(f, ss);
62  }
63  strings = ss.toArray(new StringInfo[0]);
64  }
65 
66  static void loadStrings(String file, ArrayList<StringInfo> ss)
67  throws IOException
68  {
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);
73 
74  for (String propName : propNames)
75  {
76  StringInfo si = new StringInfo(file, propName);
77  ss.add(si);
78  }
79  }
80 
81  public Object getValueAt(int row, int col)
82  {
83  StringInfo si = strings[row];
84  if (col == 0) {
85  return si.id;
86  }
87 
88  MyLocaleInfo l = locales.get(col-1);
89  Properties p = l.propsMap.get(si.file);
90  return p.getProperty(si.id);
91  }
92 
93  @Override
94  public int getRowCount()
95  {
96  return strings.length;
97  }
98 
99  @Override
100  public int getColumnCount()
101  {
102  return 1 + locales.size();
103  }
104 
105  @Override
106  public Class getColumnClass(int col)
107  {
108  return String.class;
109  }
110 
111  @Override
112  public String getColumnName(int col)
113  {
114  if (col == 0) {
115  return "String";
116  }
117  else {
118  MyLocaleInfo l = locales.get(col-1);
119  return l.code != null ? l.code : "C";
120  }
121  }
122 
123  @Override
124  public boolean isCellEditable(int row, int col)
125  {
126  if (col == 0) {
127  return false;
128  }
129  else {
130  MyLocaleInfo l = locales.get(col-1);
131  return l.code != null;
132  }
133  }
134 
135  @Override
136  public void setValueAt(Object aValue, int row, int col)
137  {
138  StringInfo si = strings[row];
139  if (col == 0) {
140  return;
141  }
142 
143  MyLocaleInfo l = locales.get(col-1);
144  Properties p = l.propsMap.get(si.file);
145  p.setProperty(si.id, (String)aValue);
146  l.dirty = true;
147  }
148 
152  File getPFile(String file, String localeCode)
153  {
154  File d = new File(workingDirectory, "micropolisj");
155  return new File(d,
156  file
157  +(localeCode != null ? "_"+localeCode : "")
158  +".properties");
159  }
160 
161  void addLocale(String localeCode)
162  throws IOException
163  {
164  MyLocaleInfo li = new MyLocaleInfo(localeCode);
165  for (String file : FILES)
166  {
167  Properties p = new Properties();
168  {
169  // load strings from our jar file
170  String s = "/micropolisj/"+file+(localeCode != null ? "_"+localeCode : "") + ".properties";
171  InputStream in = getClass().getResourceAsStream(s);
172  if (in != null) {
173  p.load(in);
174  }
175  }
176  File f = getPFile(file, localeCode);
177  if (f.exists()) {
178  p.load(new FileInputStream(f));
179  }
180  li.propsMap.put(file, p);
181  }
182 
183  locales.add(li);
184  fireTableStructureChanged();
185  }
186 
187  String [] getAllLocaleCodes()
188  {
189  String [] rv = new String[locales.size()];
190  for (int i = 0; i < rv.length; i++) {
191  rv[i] = locales.get(i).code;
192  }
193  return rv;
194  }
195 
196  void removeLocale(String localeCode)
197  {
198  assert localeCode != null;
199 
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)) {
204  locales.remove(i);
205  found = true;
206  }
207  }
208  if (found) {
209  fireTableStructureChanged();
210  }
211  }
212 
213  void makeDirectories(File f)
214  throws IOException
215  {
216  File d = f.getParentFile();
217  if (d != null) {
218  d.mkdirs();
219  }
220  }
221 
222  void save()
223  throws IOException
224  {
225  for (MyLocaleInfo l : locales)
226  {
227  if (!l.dirty) continue;
228 
229  for (String file : FILES)
230  {
231  Properties p = l.propsMap.get(file);
232  File f = getPFile(file, l.code);
233  makeDirectories(f);
234  p.store(new FileOutputStream(f), l.code);
235  }
236  l.dirty = false;
237  }
238  }
239 }