30 |
package skrueger.swing; |
package skrueger.swing; |
31 |
|
|
32 |
import java.awt.Color; |
import java.awt.Color; |
33 |
|
import java.awt.event.ActionEvent; |
34 |
|
import java.awt.event.ActionListener; |
35 |
|
|
36 |
import javax.swing.JTextArea; |
import javax.swing.JTextArea; |
37 |
import javax.swing.JTextField; |
import javax.swing.JTextField; |
43 |
import skrueger.i8n.Translation; |
import skrueger.i8n.Translation; |
44 |
|
|
45 |
/** |
/** |
46 |
* A {@link JTextArea} that signals red when it is not filled by a value. |
* A {@link JTextArea} that signals red when it is not filled by a value. A |
47 |
|
* listener is automatically registered with the {@link Translation} to update |
48 |
|
* on {@link Translation} changes. |
49 |
* |
* |
50 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Krüger |
51 |
* |
* |
56 |
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
57 |
private String langCode; |
private String langCode; |
58 |
private Translation trans; |
private Translation trans; |
59 |
|
/** |
60 |
|
* Listens to changes in the {@link Translation} and update the |
61 |
|
* {@link JTextField} |
62 |
|
*/ |
63 |
|
private final ActionListener translationChangeActionListener; |
64 |
|
|
65 |
/** |
/** |
66 |
* This RED indicates a missing translation |
* This RED indicates a missing translation |
75 |
* @param trans |
* @param trans |
76 |
* @param langCode |
* @param langCode |
77 |
*/ |
*/ |
78 |
public TranslationJTextField(Translation trans, String langCode) { |
public TranslationJTextField(final Translation trans, final String langCode) { |
79 |
super(trans.get(langCode)); |
super(trans.get(langCode)); |
80 |
this.trans = trans; |
this.trans = trans; |
81 |
this.langCode = langCode; |
this.langCode = langCode; |
111 |
checkValid(); |
checkValid(); |
112 |
} |
} |
113 |
}); |
}); |
114 |
|
|
115 |
|
/** |
116 |
|
* Add a Listener to the Translation, so the JTextField can update on |
117 |
|
* Translation changes. |
118 |
|
*/ |
119 |
|
translationChangeActionListener = new ActionListener() { |
120 |
|
|
121 |
|
@Override |
122 |
|
public void actionPerformed(ActionEvent e) { |
123 |
|
// LOGGER.debug("ActionListener called "); |
124 |
|
|
125 |
|
if (e.getSource() == trans) |
126 |
|
// && langCode.equals(e.getActionCommand())) |
127 |
|
{ |
128 |
|
// LOGGER.debug(" and omittet!\n"); |
129 |
|
return; |
130 |
|
} |
131 |
|
LOGGER.debug(" and performed!\n"); |
132 |
|
String newString = trans.get(langCode); |
133 |
|
if (newString == null) |
134 |
|
newString = ""; |
135 |
|
if ((newString != null && !newString.equals(getText()))) { |
136 |
|
LOGGER.debug("Setting text to " + newString |
137 |
|
+ " becuse oldString was " + getText()); |
138 |
|
setText(newString); |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
}; |
143 |
|
|
144 |
|
trans.addTranslationChangeListener(translationChangeActionListener); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
|
* TODO Is never called. Should be called! |
149 |
|
*/ |
150 |
|
public void dispose() { |
151 |
|
boolean b = trans |
152 |
|
.removeTranslationChangeListener(translationChangeActionListener); |
153 |
|
LOGGER.debug("removing translationChangeActionListener = " + b); |
154 |
} |
} |
155 |
|
|
156 |
/** |
/** |
169 |
wasValid = 0; |
wasValid = 0; |
170 |
} |
} |
171 |
|
|
|
|
|
172 |
} else { |
} else { |
173 |
// valid |
// valid |
174 |
if (wasValid != 1) { |
if (wasValid != 1) { |
179 |
// Directly store the change in the Translation Object |
// Directly store the change in the Translation Object |
180 |
} |
} |
181 |
|
|
182 |
// LOGGER.debug("putting '"+trimmedText+"' into the translation"); |
// LOGGER.debug("putting '"+trimmedText+"' into the translation"); |
183 |
trans.put(langCode, trimmedText); |
trans.put(langCode, trimmedText); |
184 |
} |
} |
185 |
} |
} |