/[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

trunk/src/skrueger/swing/TranslationJTextField.java revision 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC branches/1.0-gt2-2.6/src/skrueger/swing/TranslationJTextField.java revision 315 by mojays, Wed Aug 26 11:03:27 2009 UTC
# Line 30  Line 30 
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 41  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 Krüger
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 99  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                                            setText(newString);
139                                    }
140                            }
141    
142                    };
143    
144                    LOGGER.debug("registering translationChangeActionListener");
145                    trans.addTranslationChangeListener(translationChangeActionListener);
146          }          }
147    
148          /**          /**
149           * If the getText == "". then set Background color red.           * TODO Is never called. Should be called!
150           * This change is only done if the state really changed.           */
151           * And directly store the change in the {@link Translation} object          public void dispose() {
152                    boolean b = trans
153                                    .removeTranslationChangeListener(translationChangeActionListener);
154                    LOGGER.debug("removing translationChangeActionListener = " + b);
155            }
156    
157            /**
158             * If the getText().equals("") sets background color to red. The method also
159             * stored the changed string into the {@link Translation} object.
160           */           */
161          protected void checkValid() {          protected void checkValid() {
162                    
163                  String trimmedText = getText().trim();                  String trimmedText = getText().trim();
164                    
165                  if (trimmedText.equals("")) {                  if (trimmedText.equals("")) {
166                          // Not valid                          // Not valid
167                          if (wasValid != 0) {                                      if (wasValid != 0) {
168                                  setBackground( emptyColor );                                  setBackground(emptyColor);
169                                  repaint();                                  repaint();
170                                  wasValid = 0;                                  wasValid = 0;
171                          }                          }
172                            
                         // TODO testen! hat sich das bewährt?  
                         // Directly store the change in the Translation Object  
                         // trans.remove( langCode );  
                         trans.put( langCode, trimmedText );  
                           
173                  } else {                  } else {
174                          // valid                          // valid
175                          if (wasValid != 1) {                          if (wasValid != 1) {
# Line 131  public class TranslationJTextField exten Line 178  public class TranslationJTextField exten
178                                  wasValid = 1;                                  wasValid = 1;
179                          }                          }
180                          // Directly store the change in the Translation Object                          // Directly store the change in the Translation Object
                         trans.put( langCode, trimmedText );  
181                  }                  }
                   
         }  
182    
183                    // LOGGER.debug("putting '"+trimmedText+"' into the translation");
184                    trans.put(langCode, trimmedText);
185            }
186  }  }

Legend:
Removed from v.256  
changed lines
  Added in v.315

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26