1 |
mojays |
2 |
package skrueger.swing; |
2 |
|
|
|
3 |
|
|
import java.awt.Color; |
4 |
|
|
|
5 |
|
|
import javax.swing.JTextArea; |
6 |
|
|
import javax.swing.JTextField; |
7 |
|
|
import javax.swing.event.DocumentEvent; |
8 |
|
|
import javax.swing.event.DocumentListener; |
9 |
|
|
|
10 |
|
|
import org.apache.log4j.Logger; |
11 |
|
|
|
12 |
|
|
import skrueger.i8n.Translation; |
13 |
|
|
|
14 |
|
|
/** |
15 |
|
|
* A {@link JTextArea} that signals red when it is not filled by a value. |
16 |
|
|
* |
17 |
|
|
* @author Stefan Alfons Krüger |
18 |
|
|
* |
19 |
|
|
*/ |
20 |
|
|
public class TranslationJTextField extends JTextField { |
21 |
|
|
static final Logger log = Logger.getLogger(TranslationJTextField.class); |
22 |
|
|
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
23 |
|
|
private String langCode; |
24 |
|
|
private Translation trans; |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* This RED indicates a missing translation |
28 |
|
|
* |
29 |
|
|
* TODO to properties ;-) |
30 |
|
|
*/ |
31 |
|
|
final static public Color emptyColor = new Color(240,190,190); |
32 |
|
|
|
33 |
|
|
/** |
34 |
|
|
* Creates a new {@link TranslationJTextField} |
35 |
|
|
* @param trans |
36 |
|
|
* @param langCode |
37 |
|
|
*/ |
38 |
|
|
public TranslationJTextField(Translation trans, String langCode) { |
39 |
|
|
super(trans.get(langCode)); |
40 |
|
|
this.trans = trans; |
41 |
|
|
this.langCode = langCode; |
42 |
|
|
|
43 |
|
|
/** SK: Change 26.Mai |
44 |
|
|
* Use the default for an empty field **/ |
45 |
|
|
if (trans.get(langCode) == null) { |
46 |
|
|
String defaultTrans = trans.get( Translation.DEFAULT_KEY); |
47 |
|
|
trans.put(langCode, defaultTrans); |
48 |
|
|
setText( defaultTrans ); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
checkValid(); |
52 |
|
|
|
53 |
|
|
// This Listener colors the JTextfiel red if it is empty |
54 |
|
|
getDocument().addDocumentListener(new DocumentListener() { |
55 |
|
|
|
56 |
|
|
// This method is called after an insert into the document |
57 |
|
|
public void insertUpdate(DocumentEvent evt) { |
58 |
|
|
checkValid(); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
// This method is called after a removal from the document |
62 |
|
|
public void removeUpdate(DocumentEvent evt) { |
63 |
|
|
checkValid(); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
// This method is called after one or more attributes have changed. |
67 |
|
|
// This method is not called when characters are inserted with |
68 |
|
|
// attributes. |
69 |
|
|
public void changedUpdate(DocumentEvent evt) { |
70 |
|
|
checkValid(); |
71 |
|
|
} |
72 |
|
|
}); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* If the getText == "". then set Background color red. |
77 |
|
|
* This change is only done if the state really changed. |
78 |
|
|
* And directly store the change in the {@link Translation} object |
79 |
|
|
*/ |
80 |
|
|
protected void checkValid() { |
81 |
|
|
|
82 |
|
|
String trimmedText = getText().trim(); |
83 |
|
|
|
84 |
|
|
if (trimmedText.equals("")) { |
85 |
|
|
// Not valid |
86 |
|
|
if (wasValid != 0) { |
87 |
|
|
setBackground( emptyColor ); |
88 |
|
|
repaint(); |
89 |
|
|
wasValid = 0; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
// TODO testen! hat sich das bewährt? |
93 |
|
|
// Directly store the change in the Translation Object |
94 |
|
|
// trans.remove( langCode ); |
95 |
|
|
trans.put( langCode, trimmedText ); |
96 |
|
|
|
97 |
|
|
} else { |
98 |
|
|
// valid |
99 |
|
|
if (wasValid != 1) { |
100 |
|
|
setBackground(Color.white); |
101 |
|
|
repaint(); |
102 |
|
|
wasValid = 1; |
103 |
|
|
} |
104 |
|
|
// Directly store the change in the Translation Object |
105 |
|
|
trans.put( langCode, trimmedText ); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
} |