View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.computable;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.type.WeightUnit;
26  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
27  import jaxx.runtime.JAXXUtil;
28  import org.nuiton.jaxx.widgets.number.NumberEditorHandler;
29  import org.nuiton.jaxx.widgets.number.NumberEditorModel;
30  
31  import javax.swing.JTextField;
32  import java.awt.Color;
33  import java.awt.event.FocusEvent;
34  import java.awt.event.FocusListener;
35  import java.beans.PropertyChangeListener;
36  
37  /**
38   * @author Kevin Morin - kmorin@codelutin.com
39   * @since 1.0
40   */
41  public class ComputableDataEditorHandler extends NumberEditorHandler {
42  
43      private WeightUnit weightUnit;
44  
45      @Override
46      public void init() {
47          final PropertyChangeListener l = evt -> setComputedTextIfNullModel();
48  
49          ComputableData computableData = (ComputableData) ui.getModel().getBean();
50          if (computableData != null) {
51              computableData.addPropertyChangeListener(l);
52          }
53          ui.getModel().addPropertyChangeListener(NumberEditorModel.PROPERTY_BEAN, evt -> {
54  
55              ComputableData oldComputableData = (ComputableData) evt.getOldValue();
56              if (oldComputableData != null) {
57                  oldComputableData.removePropertyChangeListener(ComputableData.PROPERTY_COMPUTED_DATA, l);
58              }
59  
60              ComputableData newComputableData = (ComputableData) evt.getNewValue();
61              if (newComputableData != null) {
62                  newComputableData.removePropertyChangeListener(ComputableData.PROPERTY_COMPUTED_DATA, l);
63                  newComputableData.addPropertyChangeListener(ComputableData.PROPERTY_COMPUTED_DATA, l);
64              }
65          });
66  
67          //FIXME NumberEditor
68  //        ui.addPropertyChangeListener(ComputableDataEditor.PROPERTY_MODEL, l);
69  
70          ui.getTextField().addFocusListener(new FocusListener() {
71  
72              public void focusGained(FocusEvent e) {
73                  JTextField tf = ui.getTextField();
74                  tf.setFont(TuttiUI.TEXTFIELD_NORMAL_FONT);
75                  tf.setForeground(Color.BLACK);
76                  if (ui.getModel().getNumberValue() == null) {
77                      tf.setText("");
78                  }
79              }
80  
81              public void focusLost(FocusEvent e) {
82                  setComputedTextIfNullModel();
83              }
84          });
85  
86          ui.addPropertyChangeListener(ComputableDataEditor.PROPERTY_WEIGHT_UNIT, evt -> {
87              weightUnit = (WeightUnit) evt.getNewValue();
88              ui.setNumberPattern(weightUnit.getNumberEditorPattern());
89          });
90  
91          super.init();
92  
93      }
94  
95      /**
96       * Pour afficher le 0.0 en bleu italique  si le moèdle est null.
97       * On passera à faux lors d'un reset (voir http://forge.codelutin.com/issues/7088)
98       *
99       * @since 4.0.2
100      */
101     boolean displayNullComputedValue = true;
102 
103     @Override
104     public void reset() {
105 
106         displayNullComputedValue = false;
107 
108         try {
109 
110             setTextValue("");
111 
112         } finally {
113 
114             displayNullComputedValue = true;
115 
116         }
117 
118     }
119 
120     protected void setComputedTextIfNullModel() {
121         ComputableData bean = (ComputableData) ui.getModel().getBean();
122         JTextField tf = ui.getTextField();
123 //        if (bean != null && ui.getModel() == null) {
124         //FIXME NumberEditor
125         if (bean != null && bean.getData() == null && displayNullComputedValue && !tf.isFocusOwner()) {
126             tf.setFont(TuttiUI.TEXTFIELD_COMPUTED_FONT);
127             tf.setForeground(((ComputableDataEditor) ui).getComputedDataColor());
128 
129             String modelText;
130             Number computedData = bean.getComputedData();
131             if (weightUnit != null) {
132                 modelText = weightUnit.renderWeight((Float) computedData);
133             } else {
134                 modelText = JAXXUtil.getStringValue(computedData);
135             }
136 //            if (ui.isUseFloat() && decimalNumber != null && computedData != null) {
137 //            if (decimalNumber != null && computedData != null) {
138 //                DecimalFormat decimalFormat = Weights.getDecimalFormat(1, decimalNumber);
139 //                modelText = decimalFormat.format(computedData);
140 //            } else {
141 //                modelText = JAXXUtil.getStringValue(computedData);
142 //            }
143             tf.setText(modelText);
144 
145         } else {
146             tf.setFont(TuttiUI.TEXTFIELD_NORMAL_FONT);
147             tf.setForeground(Color.BLACK);
148         }
149     }
150 
151 }