9 package micropolisj.util;
12 import java.awt.event.*;
16 import javax.swing.table.*;
21 StringsModel stringsModel;
29 setTitle(
"MicropolisJ Translation Tool");
32 stringsModel =
new StringsModel();
33 stringsModel.addLocale(null);
36 catch (IOException e) {
37 JOptionPane.showMessageDialog(
this,
38 e,
"Error", JOptionPane.ERROR_MESSAGE);
43 JScrollPane scrollPane =
new JScrollPane(stringsTable);
44 stringsTable.setFillsViewportHeight(
true);
45 getContentPane().add(scrollPane, BorderLayout.CENTER);
47 JPanel buttonPane =
new JPanel();
48 getContentPane().add(buttonPane, BorderLayout.SOUTH);
51 btn =
new JButton(
"Add Locale...");
52 btn.addActionListener(
new ActionListener() {
53 public void actionPerformed(ActionEvent evt) {
58 removeBtn =
new JButton(
"Remove Locale");
59 removeBtn.addActionListener(
new ActionListener() {
60 public void actionPerformed(ActionEvent evt) {
63 buttonPane.add(removeBtn);
65 testBtn =
new JButton(
"Test");
66 testBtn.addActionListener(
new ActionListener() {
67 public void actionPerformed(ActionEvent evt) {
70 buttonPane.add(testBtn);
72 submitBtn =
new JButton(
"Submit");
73 submitBtn.addActionListener(
new ActionListener() {
74 public void actionPerformed(ActionEvent evt) {
77 buttonPane.add(submitBtn);
82 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
83 setLocationRelativeTo(null);
85 addWindowListener(
new WindowAdapter() {
86 public void windowClosing(WindowEvent evt) {
101 Class mclass = micropolisj.engine.Micropolis.class;
103 mclass.getProtectionDomain()
104 .getCodeSource().getLocation().toURI().getPath()
107 }
catch (java.net.URISyntaxException e) {
108 throw new Error(
"unexpected: "+e, e);
116 String code = pickLocale(
117 "Which locale do you want to test?",
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] :
"";
129 File javaExe =
new File(System.getProperty(
"java.home"));
130 javaExe =
new File(javaExe,
"bin");
131 javaExe =
new File(javaExe,
"java");
135 String javaPath = javaExe.toString();
136 String classPath = stringsModel.workingDirectory.toString()
137 + System.getProperty(
"path.separator")
140 ProcessBuilder processBuilder =
141 new ProcessBuilder(javaPath,
142 "-Duser.language="+selLanguage,
143 "-Duser.country="+selCountry,
144 "-Duser.variant="+selVariant,
149 processBuilder.start();
153 JOptionPane.showMessageDialog(
this,
156 JOptionPane.ERROR_MESSAGE);
168 JOptionPane.showMessageDialog(
this,
171 JOptionPane.ERROR_MESSAGE);
179 Locale L = Locale.getDefault();
181 JTextField langEntry =
new JTextField(L.getLanguage());
182 JTextField countryEntry =
new JTextField(L.getCountry());
183 JTextField variantEntry =
new JTextField(L.getVariant());
185 JComponent [] inputs =
new JComponent[] {
186 new JLabel(
"Language"),
188 new JLabel(
"Country"),
190 new JLabel(
"Variant (optional)"),
193 int rv = JOptionPane.showOptionDialog(
this,
196 JOptionPane.OK_CANCEL_OPTION,
197 JOptionPane.PLAIN_MESSAGE,
199 if (rv != JOptionPane.OK_OPTION)
204 String lastLanguage = langEntry.getText();
205 String lastCountry = countryEntry.getText();
206 String lastVariant = variantEntry.getText();
208 if (lastLanguage.length() == 0) {
209 throw new Exception(
"Language is required");
212 String code = lastLanguage;
213 if (lastCountry.length() != 0) {
214 code +=
"_" + lastCountry;
215 if (lastVariant.length() != 0) {
216 code +=
"_" + lastVariant;
219 else if (lastVariant.length() != 0) {
220 throw new Exception(
"Cannot specify variant without a country code.");
223 stringsModel.addLocale(code);
228 JOptionPane.showMessageDialog(
this,
231 JOptionPane.ERROR_MESSAGE);
237 int count = stringsModel.getAllLocaleCodes().length;
238 removeBtn.setEnabled(count > 1);
239 testBtn.setEnabled(count > 1);
240 submitBtn.setEnabled(count > 1);
243 String pickLocale(String message, String dlgTitle)
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]);
253 if (localeCb.getItemCount() == 1) {
254 return (String) localeCb.getItemAt(0);
256 else if (localeCb.getItemCount() == 0) {
260 localeCb.setSelectedIndex(localeCb.getItemCount()-1);
262 JComponent [] inputs =
new JComponent[] {
266 int rv = JOptionPane.showOptionDialog(
this,
269 JOptionPane.OK_CANCEL_OPTION,
270 JOptionPane.PLAIN_MESSAGE,
272 if (rv != JOptionPane.OK_OPTION)
275 return (String) localeCb.getSelectedItem();
281 String code = pickLocale(
282 "Which locale do you want to remove?",
286 stringsModel.removeLocale(code);
294 String code = pickLocale(
295 "Which locale do you want to submit?",
298 if (code == null)
return;
301 msg = msg +
"Your translated strings have been saved to\n";
302 msg = msg +
new File(stringsModel.workingDirectory,
"micropolisj").toString() +
"\n";
304 for (
int i = 0; i < stringsModel.FILES.length; i++) {
306 + stringsModel.FILES[i]+
"_"+code+
".properties"
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.)";
314 JOptionPane.showMessageDialog(
this,
315 msg,
"Submit Locale",
316 JOptionPane.INFORMATION_MESSAGE);
319 public static void main(String [] args)