1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
|
* Stefan A. Krüger - additional utility classes |
29 |
|
|
******************************************************************************/ |
30 |
mojays |
2 |
package skrueger.swing; |
31 |
|
|
|
32 |
|
|
import java.awt.Color; |
33 |
alfonx |
298 |
import java.awt.event.ActionEvent; |
34 |
|
|
import java.awt.event.ActionListener; |
35 |
mojays |
2 |
|
36 |
|
|
import javax.swing.JTextArea; |
37 |
|
|
import javax.swing.JTextField; |
38 |
|
|
import javax.swing.event.DocumentEvent; |
39 |
|
|
import javax.swing.event.DocumentListener; |
40 |
|
|
|
41 |
|
|
import org.apache.log4j.Logger; |
42 |
|
|
|
43 |
|
|
import skrueger.i8n.Translation; |
44 |
|
|
|
45 |
|
|
/** |
46 |
alfonx |
298 |
* 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 |
mojays |
2 |
* |
50 |
|
|
* @author Stefan Alfons Krüger |
51 |
|
|
* |
52 |
|
|
*/ |
53 |
|
|
public class TranslationJTextField extends JTextField { |
54 |
alfonx |
292 |
static final protected Logger LOGGER = Logger |
55 |
|
|
.getLogger(TranslationJTextField.class); |
56 |
|
|
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
57 |
mojays |
2 |
private String langCode; |
58 |
|
|
private Translation trans; |
59 |
alfonx |
298 |
/** |
60 |
|
|
* Listens to changes in the {@link Translation} and update the |
61 |
|
|
* {@link JTextField} |
62 |
|
|
*/ |
63 |
|
|
private final ActionListener translationChangeActionListener; |
64 |
alfonx |
292 |
|
65 |
mojays |
2 |
/** |
66 |
alfonx |
292 |
* This RED indicates a missing translation |
67 |
mojays |
2 |
* |
68 |
|
|
* TODO to properties ;-) |
69 |
|
|
*/ |
70 |
alfonx |
292 |
final static public Color emptyColor = new Color(240, 190, 190); |
71 |
mojays |
2 |
|
72 |
|
|
/** |
73 |
|
|
* Creates a new {@link TranslationJTextField} |
74 |
alfonx |
292 |
* |
75 |
|
|
* @param trans |
76 |
mojays |
2 |
* @param langCode |
77 |
|
|
*/ |
78 |
alfonx |
298 |
public TranslationJTextField(final Translation trans, final String langCode) { |
79 |
mojays |
2 |
super(trans.get(langCode)); |
80 |
|
|
this.trans = trans; |
81 |
|
|
this.langCode = langCode; |
82 |
alfonx |
292 |
|
83 |
|
|
/** |
84 |
|
|
* SK: Change 26.Mai Use the default for an empty field |
85 |
|
|
**/ |
86 |
mojays |
2 |
if (trans.get(langCode) == null) { |
87 |
alfonx |
292 |
String defaultTrans = trans.get(Translation.DEFAULT_KEY); |
88 |
mojays |
2 |
trans.put(langCode, defaultTrans); |
89 |
alfonx |
292 |
setText(defaultTrans); |
90 |
mojays |
2 |
} |
91 |
alfonx |
292 |
|
92 |
mojays |
2 |
checkValid(); |
93 |
|
|
|
94 |
|
|
// This Listener colors the JTextfiel red if it is empty |
95 |
|
|
getDocument().addDocumentListener(new DocumentListener() { |
96 |
|
|
|
97 |
|
|
// This method is called after an insert into the document |
98 |
|
|
public void insertUpdate(DocumentEvent evt) { |
99 |
|
|
checkValid(); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
// This method is called after a removal from the document |
103 |
|
|
public void removeUpdate(DocumentEvent evt) { |
104 |
|
|
checkValid(); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
// This method is called after one or more attributes have changed. |
108 |
|
|
// This method is not called when characters are inserted with |
109 |
|
|
// attributes. |
110 |
|
|
public void changedUpdate(DocumentEvent evt) { |
111 |
|
|
checkValid(); |
112 |
|
|
} |
113 |
|
|
}); |
114 |
alfonx |
298 |
|
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 |
alfonx |
302 |
// LOGGER.debug("ActionListener called "); |
124 |
alfonx |
300 |
|
125 |
alfonx |
302 |
if (e.getSource() == trans) |
126 |
|
|
// && langCode.equals(e.getActionCommand())) |
127 |
|
|
{ |
128 |
|
|
// LOGGER.debug(" and omittet!\n"); |
129 |
alfonx |
300 |
return; |
130 |
|
|
} |
131 |
|
|
LOGGER.debug(" and performed!\n"); |
132 |
alfonx |
298 |
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 |
mojays |
2 |
} |
146 |
|
|
|
147 |
|
|
/** |
148 |
alfonx |
298 |
* 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 |
|
|
/** |
157 |
alfonx |
292 |
* If the getText().equals("") sets background color to red. The method also |
158 |
|
|
* stored the changed string into the {@link Translation} object. |
159 |
mojays |
2 |
*/ |
160 |
|
|
protected void checkValid() { |
161 |
alfonx |
292 |
|
162 |
mojays |
2 |
String trimmedText = getText().trim(); |
163 |
alfonx |
292 |
|
164 |
mojays |
2 |
if (trimmedText.equals("")) { |
165 |
|
|
// Not valid |
166 |
alfonx |
292 |
if (wasValid != 0) { |
167 |
|
|
setBackground(emptyColor); |
168 |
mojays |
2 |
repaint(); |
169 |
|
|
wasValid = 0; |
170 |
|
|
} |
171 |
alfonx |
292 |
|
172 |
mojays |
2 |
} else { |
173 |
|
|
// valid |
174 |
|
|
if (wasValid != 1) { |
175 |
|
|
setBackground(Color.white); |
176 |
|
|
repaint(); |
177 |
|
|
wasValid = 1; |
178 |
|
|
} |
179 |
|
|
// Directly store the change in the Translation Object |
180 |
|
|
} |
181 |
alfonx |
292 |
|
182 |
alfonx |
298 |
// LOGGER.debug("putting '"+trimmedText+"' into the translation"); |
183 |
alfonx |
292 |
trans.put(langCode, trimmedText); |
184 |
mojays |
2 |
} |
185 |
|
|
} |