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

Commit c156e72

Browse files
author
Jacob van Mourik
committed
Improved translation key input fields. Fixes #27
1 parent 1337bfd commit c156e72

File tree

5 files changed

+73
-20
lines changed

5 files changed

+73
-20
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class Editor extends JFrame {
109109
private JPanel translationsPanel;
110110
private JScrollPane resourcesScrollPane;
111111
private TranslationTree translationTree;
112-
private TranslationField translationField;
112+
private TranslationKeyField translationField;
113113
private JPanel resourcesPanel;
114114
private List<ResourceField> resourceFields = Lists.newArrayList();
115115

@@ -405,7 +405,7 @@ public void showRenameTranslationDialog(String key) {
405405
newKey = Dialogs.showInputDialog(this,
406406
MessageBundle.get("dialogs.translation.rename.title"),
407407
MessageBundle.get("dialogs.translation.rename.text"),
408-
JOptionPane.QUESTION_MESSAGE, key, true);
408+
JOptionPane.QUESTION_MESSAGE, key, new TranslationKeyCaret());
409409
if (newKey != null) {
410410
if (!ResourceKeys.isValid(newKey)) {
411411
showError(MessageBundle.get("dialogs.translation.rename.error"));
@@ -435,7 +435,7 @@ public void showDuplicateTranslationDialog(String key) {
435435
newKey = Dialogs.showInputDialog(this,
436436
MessageBundle.get("dialogs.translation.duplicate.title"),
437437
MessageBundle.get("dialogs.translation.duplicate.text"),
438-
JOptionPane.QUESTION_MESSAGE, key, true);
438+
JOptionPane.QUESTION_MESSAGE, key, new TranslationKeyCaret());
439439
if (newKey != null) {
440440
newKey = newKey.trim();
441441
if (!ResourceKeys.isValid(newKey)) {
@@ -470,7 +470,7 @@ public void showAddTranslationDialog(TranslationTreeNode node) {
470470
newKey = Dialogs.showInputDialog(this,
471471
MessageBundle.get("dialogs.translation.add.title"),
472472
MessageBundle.get("dialogs.translation.add.text"),
473-
JOptionPane.QUESTION_MESSAGE, key, false);
473+
JOptionPane.QUESTION_MESSAGE, key, new TranslationKeyCaret());
474474
if (newKey != null) {
475475
newKey = newKey.trim();
476476
if (!ResourceKeys.isValid(newKey)) {
@@ -688,7 +688,7 @@ private void setupUI() {
688688
translationTree.addTreeSelectionListener(new TranslationTreeNodeSelectionListener());
689689
translationTree.addMouseListener(new TranslationTreeMouseListener());
690690

691-
translationField = new TranslationField();
691+
translationField = new TranslationKeyField();
692692
translationField.addKeyListener(new TranslationFieldKeyListener());
693693
translationField.setBorder(BorderFactory.createCompoundBorder(
694694
BorderFactory.createMatteBorder(1,0,0,1,borderColor),
@@ -1012,7 +1012,7 @@ private class TranslationFieldKeyListener extends KeyAdapter {
10121012
@Override
10131013
public void keyReleased(KeyEvent e) {
10141014
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
1015-
TranslationField field = (TranslationField) e.getSource();
1015+
TranslationKeyField field = (TranslationKeyField) e.getSource();
10161016
String key = field.getValue();
10171017
if (ResourceKeys.isValid(key)) {
10181018
addTranslationKey(key);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.jvms.i18neditor.editor;
2+
3+
import java.awt.event.MouseEvent;
4+
5+
import javax.swing.SwingUtilities;
6+
7+
import com.jvms.i18neditor.swing.JTextField;
8+
import com.jvms.i18neditor.swing.text.BlinkCaret;
9+
10+
public class TranslationKeyCaret extends BlinkCaret {
11+
private final static long serialVersionUID = -4481421558690248419L;
12+
13+
@Override
14+
public void mouseClicked(MouseEvent e) {
15+
if (!e.isConsumed() && SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
16+
handleDoubleClick(e);
17+
return;
18+
}
19+
super.mouseClicked(e);
20+
}
21+
22+
@Override
23+
public void mousePressed(MouseEvent e) {
24+
if (!e.isConsumed() && SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
25+
handleDoubleClick(e);
26+
return;
27+
}
28+
super.mousePressed(e);
29+
}
30+
31+
private void handleDoubleClick(MouseEvent e) {
32+
JTextField field = (JTextField)e.getComponent();
33+
34+
int caretPos = field.getCaretPosition();
35+
int start = caretPos;
36+
int end = caretPos;
37+
String text = field.getText();
38+
39+
while (start > 0) {
40+
if (text.charAt(start-1) == '.') {
41+
break;
42+
}
43+
start--;
44+
}
45+
while (end < text.length()) {
46+
if (text.charAt(end) == '.') {
47+
break;
48+
}
49+
end++;
50+
}
51+
52+
field.setSelectionStart(start);
53+
field.setSelectionEnd(end);
54+
}
55+
}

src/main/java/com/jvms/i18neditor/editor/TranslationField.java src/main/java/com/jvms/i18neditor/editor/TranslationKeyField.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
*
88
* @author Jacob van Mourik
99
*/
10-
public class TranslationField extends JTextField {
10+
public class TranslationKeyField extends JTextField {
1111
private final static long serialVersionUID = -3951187528785224704L;
1212

13-
public TranslationField() {
13+
public TranslationKeyField() {
1414
super();
1515
setupUI();
1616
}
@@ -30,5 +30,6 @@ public void setValue(String value) {
3030

3131
private void setupUI() {
3232
setEditable(false);
33+
setCaret(new TranslationKeyCaret());
3334
}
3435
}

src/main/java/com/jvms/i18neditor/swing/text/BlinkCaret.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
*/
1111
public class BlinkCaret extends DefaultCaret {
1212
private final static long serialVersionUID = -3365578081904749196L;
13-
public final static int DEFAULT_BLINK_RATE = 500;
14-
13+
1514
public BlinkCaret() {
16-
int blinkRate = DEFAULT_BLINK_RATE;
15+
int blinkRate = 0;
1716
Object o = UIManager.get("TextArea.caretBlinkRate");
1817
if (o != null && o instanceof Integer) {
1918
blinkRate = ((Integer) o).intValue();

src/main/java/com/jvms/i18neditor/swing/util/Dialogs.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.swing.JLabel;
1010
import javax.swing.JOptionPane;
1111
import javax.swing.JPanel;
12+
import javax.swing.text.Caret;
1213

1314
import com.google.common.base.Strings;
1415
import com.jvms.i18neditor.swing.JHtmlPane;
@@ -55,7 +56,11 @@ public static boolean showConfirmDialog(Component parent, String title, String m
5556
return JOptionPane.showConfirmDialog(parent, message, title, type) == JOptionPane.YES_OPTION;
5657
}
5758

58-
public static String showInputDialog(Component parent, String title, String label, int type, String initialText, boolean selectAll) {
59+
public static String showInputDialog(Component parent, String title, String label, int type) {
60+
return showInputDialog(parent, title, label, type, null, new BlinkCaret());
61+
}
62+
63+
public static String showInputDialog(Component parent, String title, String label, int type, String initialText, Caret caret) {
5964
JPanel content = new JPanel(new GridLayout(0, 1));
6065

6166
if (!Strings.isNullOrEmpty(label)) {
@@ -64,13 +69,10 @@ public static String showInputDialog(Component parent, String title, String labe
6469

6570
JTextField field = new JTextField(initialText);
6671
field.addAncestorListener(new RequestInitialFocusListener());
67-
field.setCaret(new BlinkCaret());
72+
field.setCaret(caret);
6873
if (initialText != null) {
6974
field.setCaretPosition(initialText.length());
7075
}
71-
if (selectAll) {
72-
field.selectAll();
73-
}
7476
content.add(field);
7577

7678
JPanel container = new JPanel(new GridBagLayout());
@@ -79,8 +81,4 @@ public static String showInputDialog(Component parent, String title, String labe
7981
int result = JOptionPane.showConfirmDialog(parent, container, title, JOptionPane.OK_CANCEL_OPTION, type);
8082
return result == JOptionPane.OK_OPTION ? field.getText() : null;
8183
}
82-
83-
public static String showInputDialog(Component parent, String title, String label, int type) {
84-
return showInputDialog(parent, title, label, type, null, false);
85-
}
8684
}

0 commit comments

Comments
 (0)