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; |
package skrueger.swing; |
31 |
|
|
32 |
import java.awt.Color; |
import java.awt.Color; |
47 |
* |
* |
48 |
*/ |
*/ |
49 |
public class TranslationJTextField extends JTextField { |
public class TranslationJTextField extends JTextField { |
50 |
static final Logger log = Logger.getLogger(TranslationJTextField.class); |
static final protected Logger LOGGER = Logger |
51 |
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
.getLogger(TranslationJTextField.class); |
52 |
|
int wasValid = 99; // 0 = false, 1 = true, 99 = undefined |
53 |
private String langCode; |
private String langCode; |
54 |
private Translation trans; |
private Translation trans; |
55 |
|
|
56 |
/** |
/** |
57 |
* This RED indicates a missing translation |
* This RED indicates a missing translation |
58 |
* |
* |
59 |
* TODO to properties ;-) |
* TODO to properties ;-) |
60 |
*/ |
*/ |
61 |
final static public Color emptyColor = new Color(240,190,190); |
final static public Color emptyColor = new Color(240, 190, 190); |
62 |
|
|
63 |
/** |
/** |
64 |
* Creates a new {@link TranslationJTextField} |
* Creates a new {@link TranslationJTextField} |
65 |
* @param trans |
* |
66 |
|
* @param trans |
67 |
* @param langCode |
* @param langCode |
68 |
*/ |
*/ |
69 |
public TranslationJTextField(Translation trans, String langCode) { |
public TranslationJTextField(Translation trans, String langCode) { |
70 |
super(trans.get(langCode)); |
super(trans.get(langCode)); |
71 |
this.trans = trans; |
this.trans = trans; |
72 |
this.langCode = langCode; |
this.langCode = langCode; |
73 |
|
|
74 |
/** SK: Change 26.Mai |
/** |
75 |
* Use the default for an empty field **/ |
* SK: Change 26.Mai Use the default for an empty field |
76 |
|
**/ |
77 |
if (trans.get(langCode) == null) { |
if (trans.get(langCode) == null) { |
78 |
String defaultTrans = trans.get( Translation.DEFAULT_KEY); |
String defaultTrans = trans.get(Translation.DEFAULT_KEY); |
79 |
trans.put(langCode, defaultTrans); |
trans.put(langCode, defaultTrans); |
80 |
setText( defaultTrans ); |
setText(defaultTrans); |
81 |
} |
} |
82 |
|
|
83 |
checkValid(); |
checkValid(); |
84 |
|
|
85 |
// This Listener colors the JTextfiel red if it is empty |
// This Listener colors the JTextfiel red if it is empty |
105 |
} |
} |
106 |
|
|
107 |
/** |
/** |
108 |
* If the getText == "". then set Background color red. |
* If the getText().equals("") sets background color to red. The method also |
109 |
* This change is only done if the state really changed. |
* stored the changed string into the {@link Translation} object. |
|
* And directly store the change in the {@link Translation} object |
|
110 |
*/ |
*/ |
111 |
protected void checkValid() { |
protected void checkValid() { |
112 |
|
|
113 |
String trimmedText = getText().trim(); |
String trimmedText = getText().trim(); |
114 |
|
|
115 |
if (trimmedText.equals("")) { |
if (trimmedText.equals("")) { |
116 |
// Not valid |
// Not valid |
117 |
if (wasValid != 0) { |
if (wasValid != 0) { |
118 |
setBackground( emptyColor ); |
setBackground(emptyColor); |
119 |
repaint(); |
repaint(); |
120 |
wasValid = 0; |
wasValid = 0; |
121 |
} |
} |
122 |
|
|
123 |
// TODO testen! hat sich das bewährt? |
|
|
// Directly store the change in the Translation Object |
|
|
// trans.remove( langCode ); |
|
|
trans.put( langCode, trimmedText ); |
|
|
|
|
124 |
} else { |
} else { |
125 |
// valid |
// valid |
126 |
if (wasValid != 1) { |
if (wasValid != 1) { |
129 |
wasValid = 1; |
wasValid = 1; |
130 |
} |
} |
131 |
// Directly store the change in the Translation Object |
// Directly store the change in the Translation Object |
|
trans.put( langCode, trimmedText ); |
|
132 |
} |
} |
|
|
|
|
} |
|
133 |
|
|
134 |
|
// LOGGER.debug("putting '"+trimmedText+"' into the translation"); |
135 |
|
trans.put(langCode, trimmedText); |
136 |
|
} |
137 |
} |
} |