Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
TranslationTool.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.awt.*;
12 import java.awt.event.*;
13 import java.io.*;
14 import java.util.*;
15 import javax.swing.*;
16 import javax.swing.table.*;
17 
18 public class TranslationTool extends JFrame
19 {
20  JTable stringsTable;
21  StringsModel stringsModel;
22 
23  JButton removeBtn;
24  JButton testBtn;
25  JButton submitBtn;
26 
27  public TranslationTool()
28  {
29  setTitle("MicropolisJ Translation Tool");
30 
31  try {
32  stringsModel = new StringsModel();
33  stringsModel.addLocale(null);
34 
35  }
36  catch (IOException e) {
37  JOptionPane.showMessageDialog(this,
38  e, "Error", JOptionPane.ERROR_MESSAGE);
39  System.exit(1);
40  }
41 
42  stringsTable = new TranslatedStringsTable(stringsModel);
43  JScrollPane scrollPane = new JScrollPane(stringsTable);
44  stringsTable.setFillsViewportHeight(true);
45  getContentPane().add(scrollPane, BorderLayout.CENTER);
46 
47  JPanel buttonPane = new JPanel();
48  getContentPane().add(buttonPane, BorderLayout.SOUTH);
49 
50  JButton btn;
51  btn = new JButton("Add Locale...");
52  btn.addActionListener(new ActionListener() {
53  public void actionPerformed(ActionEvent evt) {
55  }});
56  buttonPane.add(btn);
57 
58  removeBtn = new JButton("Remove Locale");
59  removeBtn.addActionListener(new ActionListener() {
60  public void actionPerformed(ActionEvent evt) {
62  }});
63  buttonPane.add(removeBtn);
64 
65  testBtn = new JButton("Test");
66  testBtn.addActionListener(new ActionListener() {
67  public void actionPerformed(ActionEvent evt) {
68  onTestClicked();
69  }});
70  buttonPane.add(testBtn);
71 
72  submitBtn = new JButton("Submit");
73  submitBtn.addActionListener(new ActionListener() {
74  public void actionPerformed(ActionEvent evt) {
76  }});
77  buttonPane.add(submitBtn);
78 
80 
81  pack();
82  setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
83  setLocationRelativeTo(null);
84 
85  addWindowListener(new WindowAdapter() {
86  public void windowClosing(WindowEvent evt) {
87  closeWindow();
88  }});
89  }
90 
91  private void closeWindow()
92  {
93  maybeSave();
94  dispose();
95  }
96 
97  private File getMicropolisJarFile()
98  {
99  try{
100 
101  Class mclass = micropolisj.engine.Micropolis.class;
102  return new File(
103  mclass.getProtectionDomain()
104  .getCodeSource().getLocation().toURI().getPath()
105  );
106 
107  } catch (java.net.URISyntaxException e) {
108  throw new Error("unexpected: "+e, e);
109  }
110  }
111 
112  private void onTestClicked()
113  {
114  maybeSave();
115 
116  String code = pickLocale(
117  "Which locale do you want to test?",
118  "Test Locale"
119  );
120  if (code == null) {
121  return;
122  }
123 
124  String [] localeParts = code.split("_");
125  String selLanguage = localeParts.length >= 1 ? localeParts[0] : "";
126  String selCountry = localeParts.length >= 2 ? localeParts[1] : "";
127  String selVariant = localeParts.length >= 3 ? localeParts[2] : "";
128 
129  File javaExe = new File(System.getProperty("java.home"));
130  javaExe = new File(javaExe, "bin");
131  javaExe = new File(javaExe, "java");
132 
133  try
134  {
135  String javaPath = javaExe.toString();
136  String classPath = stringsModel.workingDirectory.toString()
137  + System.getProperty("path.separator")
138  + getMicropolisJarFile().toString();
139 
140  ProcessBuilder processBuilder =
141  new ProcessBuilder(javaPath,
142  "-Duser.language="+selLanguage,
143  "-Duser.country="+selCountry,
144  "-Duser.variant="+selVariant,
145  "-cp",
146  classPath,
147  "micropolisj.Main"
148  );
149  processBuilder.start();
150  }
151  catch (Exception e)
152  {
153  JOptionPane.showMessageDialog(this,
154  e.getMessage(),
155  "Error",
156  JOptionPane.ERROR_MESSAGE);
157  }
158  }
159 
160  private void maybeSave()
161  {
162  try
163  {
164  stringsModel.save();
165  }
166  catch (Exception e)
167  {
168  JOptionPane.showMessageDialog(this,
169  e.getMessage(),
170  "Error",
171  JOptionPane.ERROR_MESSAGE);
172  }
173  }
174 
175  private void onAddLocaleClicked()
176  {
177  maybeSave();
178 
179  Locale L = Locale.getDefault();
180 
181  JTextField langEntry = new JTextField(L.getLanguage());
182  JTextField countryEntry = new JTextField(L.getCountry());
183  JTextField variantEntry = new JTextField(L.getVariant());
184 
185  JComponent [] inputs = new JComponent[] {
186  new JLabel("Language"),
187  langEntry,
188  new JLabel("Country"),
189  countryEntry,
190  new JLabel("Variant (optional)"),
191  variantEntry
192  };
193  int rv = JOptionPane.showOptionDialog(this,
194  inputs,
195  "Add Locale",
196  JOptionPane.OK_CANCEL_OPTION,
197  JOptionPane.PLAIN_MESSAGE,
198  null, null, null);
199  if (rv != JOptionPane.OK_OPTION)
200  return;
201 
202  try
203  {
204  String lastLanguage = langEntry.getText();
205  String lastCountry = countryEntry.getText();
206  String lastVariant = variantEntry.getText();
207 
208  if (lastLanguage.length() == 0) {
209  throw new Exception("Language is required");
210  }
211 
212  String code = lastLanguage;
213  if (lastCountry.length() != 0) {
214  code += "_" + lastCountry;
215  if (lastVariant.length() != 0) {
216  code += "_" + lastVariant;
217  }
218  }
219  else if (lastVariant.length() != 0) {
220  throw new Exception("Cannot specify variant without a country code.");
221  }
222 
223  stringsModel.addLocale(code);
225  }
226  catch (Exception e)
227  {
228  JOptionPane.showMessageDialog(this,
229  e.getMessage(),
230  "Error",
231  JOptionPane.ERROR_MESSAGE);
232  }
233  }
234 
235  private void updateButtonsEnabled()
236  {
237  int count = stringsModel.getAllLocaleCodes().length;
238  removeBtn.setEnabled(count > 1);
239  testBtn.setEnabled(count > 1);
240  submitBtn.setEnabled(count > 1);
241  }
242 
243  String pickLocale(String message, String dlgTitle)
244  {
245  String[] locales = stringsModel.getAllLocaleCodes();
246  JComboBox<String> localeCb = new JComboBox<String>();
247  for (int i = 0; i < locales.length; i++) {
248  if (locales[i] != null) {
249  localeCb.addItem(locales[i]);
250  }
251  }
252 
253  if (localeCb.getItemCount() == 1) {
254  return (String) localeCb.getItemAt(0);
255  }
256  else if (localeCb.getItemCount() == 0) {
257  return null;
258  }
259 
260  localeCb.setSelectedIndex(localeCb.getItemCount()-1);
261 
262  JComponent [] inputs = new JComponent[] {
263  new JLabel(message),
264  localeCb
265  };
266  int rv = JOptionPane.showOptionDialog(this,
267  inputs,
268  dlgTitle,
269  JOptionPane.OK_CANCEL_OPTION,
270  JOptionPane.PLAIN_MESSAGE,
271  null, null, null);
272  if (rv != JOptionPane.OK_OPTION)
273  return null;
274 
275  return (String) localeCb.getSelectedItem();
276  }
277 
278  private void onRemoveLocaleClicked()
279  {
280  maybeSave();
281  String code = pickLocale(
282  "Which locale do you want to remove?",
283  "Remove Locale"
284  );
285  if (code != null) {
286  stringsModel.removeLocale(code);
288  }
289  }
290 
291  private void onSubmitClicked()
292  {
293  maybeSave();
294  String code = pickLocale(
295  "Which locale do you want to submit?",
296  "Submit Locale"
297  );
298  if (code == null) return;
299 
300  String msg = "";
301  msg = msg + "Your translated strings have been saved to\n";
302  msg = msg + new File(stringsModel.workingDirectory, "micropolisj").toString() + "\n";
303  msg = msg + "as:\n";
304  for (int i = 0; i < stringsModel.FILES.length; i++) {
305  msg = msg + " * "
306  + stringsModel.FILES[i]+"_"+code+".properties"
307  + "\n";
308  }
309  msg = msg + "\n";
310  msg = msg + "Submit these files to the Micropolis website\n";
311  msg = msg + "https://code.google.com/p/micropolis\n";
312  msg = msg + "(Open a new \"Issue\" and attach the files to the issue.)";
313 
314  JOptionPane.showMessageDialog(this,
315  msg, "Submit Locale",
316  JOptionPane.INFORMATION_MESSAGE);
317  }
318 
319  public static void main(String [] args)
320  throws Exception
321  {
322  new TranslationTool().setVisible(true);
323  }
324 
325 }