Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit 815c4a4

Browse files
author
jcbvm
committed
Changed duplicate/rename behaviour and cleaned up some code.
1 parent fb8f386 commit 815c4a4

File tree

12 files changed

+180
-147
lines changed

12 files changed

+180
-147
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.jvms</groupId>
55
<artifactId>i18n-editor</artifactId>
6-
<version>0.5.0</version>
6+
<version>0.6.0</version>
77
<packaging>jar</packaging>
88
<dependencies>
99
<dependency>
@@ -51,7 +51,7 @@
5151
<configuration>
5252
<archive>
5353
<manifest>
54-
<mainClass>com.jvms.i18neditor.Main</mainClass>
54+
<mainClass>com.jvms.i18neditor.Editor</mainClass>
5555
</manifest>
5656
</archive>
5757
<descriptorRefs>
@@ -78,7 +78,7 @@
7878
<jar>${project.build.directory}/${project.artifactId}-${project.version}-jar-with-dependencies.jar</jar>
7979
<errTitle>i18n Editor</errTitle>
8080
<classPath>
81-
<mainClass>com.jvms.i18neditor.Main</mainClass>
81+
<mainClass>com.jvms.i18neditor.Editor</mainClass>
8282
</classPath>
8383
<jre>
8484
<minVersion>1.8.0</minVersion>

src/main/java/com/jvms/i18neditor/Editor.java

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
import javax.swing.JScrollPane;
3030
import javax.swing.JSplitPane;
3131
import javax.swing.SwingUtilities;
32+
import javax.swing.UIManager;
3233
import javax.swing.event.TreeSelectionEvent;
3334
import javax.swing.event.TreeSelectionListener;
3435

36+
import org.apache.commons.lang3.SystemUtils;
37+
3538
import com.google.common.collect.Lists;
3639
import com.google.common.collect.Maps;
3740
import com.jvms.i18neditor.Resource.ResourceType;
@@ -52,7 +55,7 @@ public class Editor extends JFrame {
5255

5356
public final static Path SETTINGS_PATH = Paths.get(System.getProperty("user.home"), ".i18n-editor");
5457
public final static String TITLE = "i18n Editor";
55-
public final static String VERSION = "0.5.0";
58+
public final static String VERSION = "0.6.0";
5659
public final static String COPYRIGHT_YEAR = "2016";
5760
public final static int DEFAULT_WIDTH = 1024;
5861
public final static int DEFAULT_HEIGHT = 768;
@@ -112,7 +115,7 @@ public void importResources(Path dir) {
112115
Map<String,String> keys = Maps.newTreeMap();
113116
resources.forEach(resource -> keys.putAll(resource.getTranslations()));
114117
List<String> keyList = Lists.newArrayList(keys.keySet());
115-
translationTree.setModel(new TranslationTreeModel(MessageBundle.get("translations.model.name"), keyList));
118+
translationTree.setModel(new TranslationTreeModel(keyList));
116119

117120
updateUI();
118121
} catch (IOException e) {
@@ -364,6 +367,41 @@ public boolean closeCurrentSession() {
364367
return true;
365368
}
366369

370+
public void reset() {
371+
translationTree.clear();
372+
resources.clear();
373+
resourceFields.clear();
374+
setDirty(false);
375+
updateUI();
376+
}
377+
378+
public void updateUI() {
379+
TranslationTreeNode selectedNode = translationTree.getSelectedNode();
380+
381+
resourcesPanel.removeAll();
382+
resourceFields.stream().sorted().forEach(field -> {
383+
field.setEditable(selectedNode != null && selectedNode.isEditable());
384+
resourcesPanel.add(Box.createVerticalStrut(5));
385+
resourcesPanel.add(new JLabel(field.getResource().getLocale().getDisplayName()));
386+
resourcesPanel.add(Box.createVerticalStrut(5));
387+
resourcesPanel.add(field);
388+
resourcesPanel.add(Box.createVerticalStrut(5));
389+
});
390+
if (!resourceFields.isEmpty()) {
391+
resourcesPanel.remove(0);
392+
resourcesPanel.remove(resourcesPanel.getComponentCount()-1);
393+
}
394+
395+
editorMenu.setEnabled(resourcesDir != null);
396+
editorMenu.setEditable(!resources.isEmpty());
397+
translationTree.setEditable(!resources.isEmpty());
398+
translationField.setEditable(!resources.isEmpty());
399+
400+
updateTitle();
401+
validate();
402+
repaint();
403+
}
404+
367405
public void launch() {
368406
settings.load(SETTINGS_PATH);
369407

@@ -398,39 +436,18 @@ public void launch() {
398436
}
399437
}
400438

401-
public void reset() {
402-
translationTree.clear();
403-
resources.clear();
404-
resourceFields.clear();
405-
setDirty(false);
406-
updateUI();
407-
}
408-
409-
public void updateUI() {
410-
TranslationTreeNode selectedNode = translationTree.getSelectedNode();
411-
412-
resourcesPanel.removeAll();
413-
resourceFields.stream().sorted().forEach(field -> {
414-
field.setEditable(selectedNode != null && selectedNode.isEditable());
415-
resourcesPanel.add(Box.createVerticalStrut(5));
416-
resourcesPanel.add(new JLabel(field.getResource().getLocale().getDisplayName()));
417-
resourcesPanel.add(Box.createVerticalStrut(5));
418-
resourcesPanel.add(field);
419-
resourcesPanel.add(Box.createVerticalStrut(5));
439+
public static void main(String[] args) {
440+
SwingUtilities.invokeLater(() -> {
441+
try {
442+
// Only use native look an feel when not running Linux, Linux might cause visual issues
443+
if (!SystemUtils.IS_OS_LINUX) {
444+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
445+
}
446+
} catch (Exception e) {
447+
//
448+
}
449+
new Editor().launch();
420450
});
421-
if (!resourceFields.isEmpty()) {
422-
resourcesPanel.remove(0);
423-
resourcesPanel.remove(resourcesPanel.getComponentCount()-1);
424-
}
425-
426-
editorMenu.setEnabled(resourcesDir != null);
427-
editorMenu.setEditable(!resources.isEmpty());
428-
translationTree.setEditable(!resources.isEmpty());
429-
translationField.setEditable(!resources.isEmpty());
430-
431-
updateTitle();
432-
validate();
433-
repaint();
434451
}
435452

436453
private boolean loadResourcesFromHistory() {
@@ -479,7 +496,8 @@ private void setupUI() {
479496
translationsPanel = new JPanel(new BorderLayout());
480497
translationTree = new TranslationTree(this);
481498
translationTree.addTreeSelectionListener(new TranslationTreeNodeSelectionListener());
482-
translationField = new TranslationField(this, translationTree);
499+
translationField = new TranslationField();
500+
translationField.addKeyListener(new TranslationFieldKeyListener());
483501
translationsPanel.add(new JScrollPane(translationTree));
484502
translationsPanel.add(translationField, BorderLayout.SOUTH);
485503

@@ -571,11 +589,29 @@ private class ResourceFieldKeyListener extends KeyAdapter {
571589
public void keyReleased(KeyEvent e) {
572590
ResourceField field = (ResourceField) e.getSource();
573591
String key = translationTree.getSelectedNode().getKey();
574-
String value = field.getText().trim();
592+
String value = field.getValue();
575593
field.getResource().storeTranslation(key, value);
576594
}
577595
}
578596

597+
private class TranslationFieldKeyListener extends KeyAdapter {
598+
@Override
599+
public void keyReleased(KeyEvent e) {
600+
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
601+
TranslationField field = (TranslationField) e.getSource();
602+
String key = field.getValue();
603+
if (TranslationKeys.isValid(key)) {
604+
TranslationTreeNode node = translationTree.getNodeByKey(key);
605+
if (node == null) {
606+
addTranslationKey(key);
607+
} else {
608+
translationTree.setSelectedNode(node);
609+
}
610+
}
611+
}
612+
}
613+
}
614+
579615
private class EditorWindowListener extends WindowAdapter {
580616
@Override
581617
public void windowClosing(WindowEvent e) {

src/main/java/com/jvms/i18neditor/EditorMenu.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
*/
2525
public class EditorMenu extends JMenuBar {
2626
private final static long serialVersionUID = -101788804096708514L;
27-
2827
private final Editor editor;
2928
private final TranslationTree tree;
30-
3129
private JMenuItem saveMenuItem;
3230
private JMenuItem reloadMenuItem;
3331
private JMenuItem addTranslationMenuItem;

src/main/java/com/jvms/i18neditor/Main.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/main/java/com/jvms/i18neditor/Resource.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* <li>{@code type} the type of the resource, either {@code JSON} or {@code ES6}.</li>
1919
* <li>{@code path} the path to the translation file on disk.</li>
2020
* <li>{@code locale} the locale of the translations.</li>
21-
* <li>{@code translations} a sorted map containing the translations.</li>
21+
* <li>{@code translations} a sorted map containing the translations by key value pair.</li>
2222
* </ul>
2323
*
2424
* <p>Objects can listen to a resource by adding a {@link ResourceListener} which
@@ -102,6 +102,16 @@ public SortedMap<String,String> getTranslations() {
102102
return ImmutableSortedMap.copyOf(translations);
103103
}
104104

105+
/**
106+
* Gets a translation from the resource's translations.
107+
*
108+
* @param key the key of the translation to get.
109+
* @return value of the translation or <code>null</code> if there is no translation for the given key.
110+
*/
111+
public String getTranslation(String key) {
112+
return translations.get(key);
113+
}
114+
105115
/**
106116
* Stores a translation to the resource's translations.
107117
*
@@ -118,7 +128,6 @@ public SortedMap<String,String> getTranslations() {
118128
public void storeTranslation(String key, String value) {
119129
String existing = translations.get(key);
120130
if (existing != null && existing.equals(value)) return;
121-
//if (existing == null && value.isEmpty()) return;
122131
removeParents(key);
123132
removeChildren(key);
124133
if (value.isEmpty()) {
@@ -194,13 +203,11 @@ private void duplicateTranslation(String key, String newKey, boolean keepOld) {
194203
if (translations.containsKey(key)) {
195204
newTranslations.put(newKey, translations.get(key));
196205
}
197-
removeChildren(newKey);
198-
translations.remove(newKey);
199206
if (!keepOld) {
200207
removeChildren(key);
201208
translations.remove(key);
202209
}
203-
translations.putAll(newTranslations);
210+
newTranslations.forEach(this::storeTranslation);
204211
}
205212

206213
private void removeChildren(String key) {

src/main/java/com/jvms/i18neditor/ResourceField.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
public class ResourceField extends JTextArea implements Comparable<ResourceField> {
2020
private final static long serialVersionUID = 2034814490878477055L;
21-
2221
private final Resource resource;
2322
private final UndoManager undoManager = new UndoManager();
2423

@@ -28,8 +27,12 @@ public ResourceField(Resource resource) {
2827
setupUI();
2928
}
3029

30+
public String getValue() {
31+
return getText().trim();
32+
}
33+
3134
public void updateValue(String key) {
32-
setText(resource.getTranslations().get(key));
35+
setText(resource.getTranslation(key));
3336
undoManager.discardAllEdits();
3437
}
3538

@@ -56,7 +59,6 @@ private void setupUI() {
5659
getInputMap().put(KeyStroke.getKeyStroke('Y', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo");
5760
}
5861

59-
@SuppressWarnings("serial")
6062
private class UndoAction extends AbstractAction {
6163
@Override
6264
public void actionPerformed(ActionEvent e) {
@@ -66,7 +68,6 @@ public void actionPerformed(ActionEvent e) {
6668
}
6769
}
6870

69-
@SuppressWarnings("serial")
7071
private class RedoAction extends AbstractAction {
7172
@Override
7273
public void actionPerformed(ActionEvent e) {
@@ -78,6 +79,8 @@ public void actionPerformed(ActionEvent e) {
7879

7980
@Override
8081
public int compareTo(ResourceField o) {
81-
return getResource().getLocale().getDisplayName().compareTo(o.getResource().getLocale().getDisplayName());
82+
String a = getResource().getLocale().getDisplayName();
83+
String b = o.getResource().getLocale().getDisplayName();
84+
return a.compareTo(b);
8285
}
8386
}
Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,28 @@
11
package com.jvms.i18neditor;
22

33
import java.awt.Insets;
4-
import java.awt.event.KeyAdapter;
5-
import java.awt.event.KeyEvent;
64

75
import javax.swing.JTextField;
86

9-
import com.jvms.i18neditor.util.TranslationKeys;
10-
117
/**
128
* This class represents a text field for adding a new translation key.
139
*
14-
* <p>When the entered value targets an existing translation, instead of adding a new
15-
* translation key the existing translation key will be selected.</p>
16-
*
1710
* @author Jacob
1811
*/
1912
public class TranslationField extends JTextField {
2013
private final static long serialVersionUID = -3951187528785224704L;
2114

22-
private final TranslationTree tree;
23-
private final Editor editor;
24-
25-
public TranslationField(Editor editor, TranslationTree tree) {
15+
public TranslationField() {
2616
super();
27-
this.tree = tree;
28-
this.editor = editor;
2917
setupUI();
3018
}
3119

20+
public String getValue() {
21+
return getText().trim();
22+
}
23+
3224
private void setupUI() {
3325
setMargin(new Insets(4,4,4,4));
34-
addKeyListener(new TranslationFieldKeyListener());
3526
setEditable(false);
3627
}
37-
38-
private class TranslationFieldKeyListener extends KeyAdapter {
39-
@Override
40-
public void keyReleased(KeyEvent e) {
41-
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
42-
String key = getText().trim();
43-
if (TranslationKeys.isValid(key)) {
44-
TranslationTreeNode node = tree.getNodeByKey(key);
45-
if (node == null) {
46-
editor.addTranslationKey(key);
47-
} else {
48-
tree.setSelectedNode(node);
49-
}
50-
}
51-
}
52-
}
53-
}
5428
}

0 commit comments

Comments
 (0)