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 |
|
|
|
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 Logger log = Logger.getLogger(TranslationJTextField.class); |
51 |
|
|
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
52 |
|
|
private String langCode; |
53 |
|
|
private Translation trans; |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* This RED indicates a missing translation |
57 |
|
|
* |
58 |
|
|
* TODO to properties ;-) |
59 |
|
|
*/ |
60 |
|
|
final static public Color emptyColor = new Color(240,190,190); |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* Creates a new {@link TranslationJTextField} |
64 |
|
|
* @param trans |
65 |
|
|
* @param langCode |
66 |
|
|
*/ |
67 |
|
|
public TranslationJTextField(Translation trans, String langCode) { |
68 |
|
|
super(trans.get(langCode)); |
69 |
|
|
this.trans = trans; |
70 |
|
|
this.langCode = langCode; |
71 |
|
|
|
72 |
|
|
/** SK: Change 26.Mai |
73 |
|
|
* Use the default for an empty field **/ |
74 |
|
|
if (trans.get(langCode) == null) { |
75 |
|
|
String defaultTrans = trans.get( Translation.DEFAULT_KEY); |
76 |
|
|
trans.put(langCode, defaultTrans); |
77 |
|
|
setText( defaultTrans ); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
checkValid(); |
81 |
|
|
|
82 |
|
|
// This Listener colors the JTextfiel red if it is empty |
83 |
|
|
getDocument().addDocumentListener(new DocumentListener() { |
84 |
|
|
|
85 |
|
|
// This method is called after an insert into the document |
86 |
|
|
public void insertUpdate(DocumentEvent evt) { |
87 |
|
|
checkValid(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
// This method is called after a removal from the document |
91 |
|
|
public void removeUpdate(DocumentEvent evt) { |
92 |
|
|
checkValid(); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
// This method is called after one or more attributes have changed. |
96 |
|
|
// This method is not called when characters are inserted with |
97 |
|
|
// attributes. |
98 |
|
|
public void changedUpdate(DocumentEvent evt) { |
99 |
|
|
checkValid(); |
100 |
|
|
} |
101 |
|
|
}); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* If the getText == "". then set Background color red. |
106 |
|
|
* This change is only done if the state really changed. |
107 |
|
|
* And directly store the change in the {@link Translation} object |
108 |
|
|
*/ |
109 |
|
|
protected void checkValid() { |
110 |
|
|
|
111 |
|
|
String trimmedText = getText().trim(); |
112 |
|
|
|
113 |
|
|
if (trimmedText.equals("")) { |
114 |
|
|
// Not valid |
115 |
|
|
if (wasValid != 0) { |
116 |
|
|
setBackground( emptyColor ); |
117 |
|
|
repaint(); |
118 |
|
|
wasValid = 0; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
// TODO testen! hat sich das bewährt? |
122 |
|
|
// Directly store the change in the Translation Object |
123 |
|
|
// trans.remove( langCode ); |
124 |
|
|
trans.put( langCode, trimmedText ); |
125 |
|
|
|
126 |
|
|
} else { |
127 |
|
|
// valid |
128 |
|
|
if (wasValid != 1) { |
129 |
|
|
setBackground(Color.white); |
130 |
|
|
repaint(); |
131 |
|
|
wasValid = 1; |
132 |
|
|
} |
133 |
|
|
// Directly store the change in the Translation Object |
134 |
|
|
trans.put( langCode, trimmedText ); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
} |