/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/swing/TranslationJTextField.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/swing/TranslationJTextField.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 292 - (hide annotations)
Wed Aug 5 12:34:15 2009 UTC (15 years, 6 months ago) by alfonx
Original Path: trunk/src/skrueger/swing/TranslationJTextField.java
File size: 4008 byte(s)
* Big Code Cleanup in Atlas-Framework
* More options in DesignAtlasChart
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 alfonx 292 static final protected Logger LOGGER = Logger
51     .getLogger(TranslationJTextField.class);
52     int wasValid = 99; // 0 = false, 1 = true, 99 = undefined
53 mojays 2 private String langCode;
54     private Translation trans;
55 alfonx 292
56 mojays 2 /**
57 alfonx 292 * This RED indicates a missing translation
58 mojays 2 *
59     * TODO to properties ;-)
60     */
61 alfonx 292 final static public Color emptyColor = new Color(240, 190, 190);
62 mojays 2
63     /**
64     * Creates a new {@link TranslationJTextField}
65 alfonx 292 *
66     * @param trans
67 mojays 2 * @param langCode
68     */
69     public TranslationJTextField(Translation trans, String langCode) {
70     super(trans.get(langCode));
71     this.trans = trans;
72     this.langCode = langCode;
73 alfonx 292
74     /**
75     * SK: Change 26.Mai Use the default for an empty field
76     **/
77 mojays 2 if (trans.get(langCode) == null) {
78 alfonx 292 String defaultTrans = trans.get(Translation.DEFAULT_KEY);
79 mojays 2 trans.put(langCode, defaultTrans);
80 alfonx 292 setText(defaultTrans);
81 mojays 2 }
82 alfonx 292
83 mojays 2 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 alfonx 292 * If the getText().equals("") sets background color to red. The method also
109     * stored the changed string into the {@link Translation} object.
110 mojays 2 */
111     protected void checkValid() {
112 alfonx 292
113 mojays 2 String trimmedText = getText().trim();
114 alfonx 292
115 mojays 2 if (trimmedText.equals("")) {
116     // Not valid
117 alfonx 292 if (wasValid != 0) {
118     setBackground(emptyColor);
119 mojays 2 repaint();
120     wasValid = 0;
121     }
122 alfonx 292
123    
124 mojays 2 } 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 alfonx 292
134     // LOGGER.debug("putting '"+trimmedText+"' into the translation");
135     trans.put(langCode, trimmedText);
136 mojays 2 }
137     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26