/[schmitzm]/trunk/src/skrueger/swing/TranslationJTextField.java
ViewVC logotype

Diff of /trunk/src/skrueger/swing/TranslationJTextField.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by mojays, Tue Feb 24 22:43:52 2009 UTC revision 976 by alfonx, Thu Aug 12 14:14:08 2010 UTC
# Line 1  Line 1 
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. Tzeggai - additional utility classes
29     ******************************************************************************/
30  package skrueger.swing;  package skrueger.swing;
31    
32  import java.awt.Color;  import java.awt.Color;
33    import java.awt.event.ActionEvent;
34    import java.awt.event.ActionListener;
35    
36  import javax.swing.JTextArea;  import javax.swing.JTextArea;
37  import javax.swing.JTextField;  import javax.swing.JTextField;
# Line 12  import org.apache.log4j.Logger; Line 43  import org.apache.log4j.Logger;
43  import skrueger.i8n.Translation;  import skrueger.i8n.Translation;
44    
45  /**  /**
46   * A {@link JTextArea} that signals red when it is not filled by a value.   * 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   *   *
50   * @author Stefan Alfons Krüger   * @author Stefan Alfons Tzeggai
51   *   *
52   */   */
53  public class TranslationJTextField extends JTextField {  public class TranslationJTextField extends JTextField {
54          static final Logger log = Logger.getLogger(TranslationJTextField.class);          static final protected Logger LOGGER = Logger
55      int wasValid = 99; // 0 = false, 1 = true, 99 = undefined                          .getLogger(TranslationJTextField.class);
56            int wasValid = 99; // 0 = false, 1 = true, 99 = undefined
57          private String langCode;          private String langCode;
58          private Translation trans;          private Translation trans;
           
59          /**          /**
60           * This RED indicates a missing translation           * Listens to changes in the {@link Translation} and update the
61             * {@link JTextField}
62             */
63            private final ActionListener translationChangeActionListener;
64    
65            /**
66             * This RED indicates a missing translation
67           *           *
68           * TODO to properties ;-)           * TODO to properties ;-)
69           */           */
70          final static public Color emptyColor = new Color(240,190,190);          final static public Color emptyColor = new Color(240, 190, 190);
71    
72          /**          /**
73           * Creates a new {@link TranslationJTextField}           * Creates a new {@link TranslationJTextField}
74           * @param trans           *
75             * @param trans
76           * @param langCode           * @param langCode
77           */           */
78          public TranslationJTextField(Translation trans, String langCode) {          public TranslationJTextField(final Translation trans, final String langCode) {
79                  super(trans.get(langCode));                  super(trans.get(langCode));
80                  this.trans = trans;                  this.trans = trans;
81                  this.langCode = langCode;                  this.langCode = langCode;
82                    
83                  /** SK: Change  26.Mai                  /**
84                   * Use the default for an empty field **/                   * SK: Change 26.Mai Use the default for an empty field
85                     **/
86                  if (trans.get(langCode) == null) {                  if (trans.get(langCode) == null) {
87                          String defaultTrans = trans.get( Translation.DEFAULT_KEY);                          String defaultTrans = trans.get(Translation.DEFAULT_KEY);
88                          trans.put(langCode, defaultTrans);                          trans.put(langCode, defaultTrans);
89                          setText( defaultTrans );                          setText(defaultTrans);
90                  }                  }
91                    
92                  checkValid();                  checkValid();
93    
94                  // This Listener colors the JTextfiel red if it is empty                  // This Listener colors the JTextfiel red if it is empty
# Line 70  public class TranslationJTextField exten Line 111  public class TranslationJTextField exten
111                                  checkValid();                                  checkValid();
112                          }                          }
113                  });                  });
114    
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                                    // LOGGER.debug("ActionListener called ");
124    
125                                    // if (e.getSource() == trans)
126                                    // // && langCode.equals(e.getActionCommand()))
127                                    // {
128                                    // // LOGGER.debug(" and omittet!\n");
129                                    // return;
130                                    // }
131                                    // LOGGER.debug(" and performed!\n");
132                                    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    
139                                            try {
140                                                    setText(newString);
141                                            } catch (java.lang.IllegalStateException ee) {
142                                                    /*
143                                                     *
144                                                     * ERROR Geopublisher uncaughtException An uncaught
145                                                     * exception happened on Thread
146                                                     * Thread[AWT-EventQueue-0,6,main]
147                                                     * java.lang.IllegalStateException: Attempt to mutate in
148                                                     * notification at
149                                                     * javax.swing.text.AbstractDocument.writeLock
150                                                     * (AbstractDocument.java:1323) at
151                                                     * javax.swing.text.AbstractDocument
152                                                     * .replace(AbstractDocument.java:644) at
153                                                     * javax.swing.text
154                                                     * .JTextComponent.setText(JTextComponent.java:1693) at
155                                                     * skrueger
156                                                     * .swing.TranslationJTextField$2.actionPerformed
157                                                     * (TranslationJTextField.java:138) at
158                                                     * skrueger.i8n.Translation
159                                                     * .fireTranslationChangedEvents(Translation.java:358)
160                                                     * at skrueger.i8n.Translation.put(Translation.java:365)
161                                                     * atskrueger.swing.TranslationJTextField.checkValid(
162                                                     * TranslationJTextField.java:184) at
163                                                     * skrueger.swing.TranslationJTextField$1
164                                                     * .removeUpdate(TranslationJTextField.java:104) at
165                                                     * javax.swing.text.AbstractDocument.fireRemoveUpdate(
166                                                     * AbstractDocument.java:243) _
167                                                     */
168                                                    LOGGER.warn(ee);
169                                            }
170                                    }
171                            }
172    
173                    };
174    
175                    trans.addTranslationChangeListener(translationChangeActionListener);
176            }
177    
178            /**
179             * TODO Is never called. Should be called!
180             */
181            public void dispose() {
182                    boolean b = trans
183                                    .removeTranslationChangeListener(translationChangeActionListener);
184                    LOGGER.debug("removing translationChangeActionListener = " + b);
185          }          }
186    
187          /**          /**
188           * If the getText == "". then set Background color red.           * If the getText().equals("") sets background color to red. The method also
189           * 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  
190           */           */
191          protected void checkValid() {          protected void checkValid() {
192                    
193                  String trimmedText = getText().trim();                  String trimmedText = getText().trim();
194                    
195                  if (trimmedText.equals("")) {                  if (trimmedText.equals("")) {
196                          // Not valid                          // Not valid
197                          if (wasValid != 0) {                                      if (wasValid != 0) {
198                                  setBackground( emptyColor );                                  setBackground(emptyColor);
199                                  repaint();                                  repaint();
200                                  wasValid = 0;                                  wasValid = 0;
201                          }                          }
202                            
                         // TODO testen! hat sich das bewährt?  
                         // Directly store the change in the Translation Object  
                         // trans.remove( langCode );  
                         trans.put( langCode, trimmedText );  
                           
203                  } else {                  } else {
204                          // valid                          // valid
205                          if (wasValid != 1) {                          if (wasValid != 1) {
# Line 102  public class TranslationJTextField exten Line 208  public class TranslationJTextField exten
208                                  wasValid = 1;                                  wasValid = 1;
209                          }                          }
210                          // Directly store the change in the Translation Object                          // Directly store the change in the Translation Object
                         trans.put( langCode, trimmedText );  
211                  }                  }
212                    
213                    // LOGGER.debug("putting '"+trimmedText+"' into the translation");
214                    if (!trimmedText.equals(trans.get(langCode)))
215                            trans.put(langCode, trimmedText);
216          }          }
217            //
218            // ActionListener listenToChangesOfTheTranslationAndUpdateTheGUI = new
219            // ActionListener() {
220            //
221            // @Override
222            // public void actionPerformed(ActionEvent e) {
223            // if (!hasFocus())
224            // translationChangedExternally();
225            // }
226            // };
227            //
228            // /**
229            // * Call it when the translation object has changed to update the
230            // JTextFields
231            // */
232            // public void translationChangedExternally() {
233            // setText(trans.get(langCode));
234            // }
235    
236  }  }

Legend:
Removed from v.2  
changed lines
  Added in v.976

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26