1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* 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 |
package skrueger.swing; |
31 |
|
32 |
import java.awt.Color; |
33 |
|
34 |
import javax.swing.JTextArea; |
35 |
import javax.swing.JTextField; |
36 |
import javax.swing.event.DocumentEvent; |
37 |
import javax.swing.event.DocumentListener; |
38 |
|
39 |
import org.apache.log4j.Logger; |
40 |
|
41 |
import skrueger.i8n.Translation; |
42 |
|
43 |
/** |
44 |
* A {@link JTextArea} that signals red when it is not filled by a value. |
45 |
* |
46 |
* @author Stefan Alfons Krüger |
47 |
* |
48 |
*/ |
49 |
public class TranslationJTextField extends JTextField { |
50 |
static final protected Logger LOGGER = Logger |
51 |
.getLogger(TranslationJTextField.class); |
52 |
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
53 |
private String langCode; |
54 |
private Translation trans; |
55 |
|
56 |
/** |
57 |
* This RED indicates a missing translation |
58 |
* |
59 |
* TODO to properties ;-) |
60 |
*/ |
61 |
final static public Color emptyColor = new Color(240, 190, 190); |
62 |
|
63 |
/** |
64 |
* Creates a new {@link TranslationJTextField} |
65 |
* |
66 |
* @param trans |
67 |
* @param langCode |
68 |
*/ |
69 |
public TranslationJTextField(Translation trans, String langCode) { |
70 |
super(trans.get(langCode)); |
71 |
this.trans = trans; |
72 |
this.langCode = langCode; |
73 |
|
74 |
/** |
75 |
* SK: Change 26.Mai Use the default for an empty field |
76 |
**/ |
77 |
if (trans.get(langCode) == null) { |
78 |
String defaultTrans = trans.get(Translation.DEFAULT_KEY); |
79 |
trans.put(langCode, defaultTrans); |
80 |
setText(defaultTrans); |
81 |
} |
82 |
|
83 |
checkValid(); |
84 |
|
85 |
// This Listener colors the JTextfiel red if it is empty |
86 |
getDocument().addDocumentListener(new DocumentListener() { |
87 |
|
88 |
// This method is called after an insert into the document |
89 |
public void insertUpdate(DocumentEvent evt) { |
90 |
checkValid(); |
91 |
} |
92 |
|
93 |
// This method is called after a removal from the document |
94 |
public void removeUpdate(DocumentEvent evt) { |
95 |
checkValid(); |
96 |
} |
97 |
|
98 |
// This method is called after one or more attributes have changed. |
99 |
// This method is not called when characters are inserted with |
100 |
// attributes. |
101 |
public void changedUpdate(DocumentEvent evt) { |
102 |
checkValid(); |
103 |
} |
104 |
}); |
105 |
} |
106 |
|
107 |
/** |
108 |
* If the getText().equals("") sets background color to red. The method also |
109 |
* stored the changed string into the {@link Translation} object. |
110 |
*/ |
111 |
protected void checkValid() { |
112 |
|
113 |
String trimmedText = getText().trim(); |
114 |
|
115 |
if (trimmedText.equals("")) { |
116 |
// Not valid |
117 |
if (wasValid != 0) { |
118 |
setBackground(emptyColor); |
119 |
repaint(); |
120 |
wasValid = 0; |
121 |
} |
122 |
|
123 |
|
124 |
} else { |
125 |
// valid |
126 |
if (wasValid != 1) { |
127 |
setBackground(Color.white); |
128 |
repaint(); |
129 |
wasValid = 1; |
130 |
} |
131 |
// Directly store the change in the Translation Object |
132 |
} |
133 |
|
134 |
// LOGGER.debug("putting '"+trimmedText+"' into the translation"); |
135 |
trans.put(langCode, trimmedText); |
136 |
} |
137 |
} |