View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.component.number;
2   
3   /*
4    * #%L
5    * JAXX :: Widgets
6    * %%
7    * Copyright (C) 2008 - 2014 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (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 Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import com.google.common.base.Predicate;
25  import org.apache.commons.lang3.StringUtils;
26  import org.jdesktop.beans.AbstractSerializableBean;
27  import org.nuiton.jaxx.widgets.ModelToBean;
28  
29  import java.io.Serializable;
30  
31  /**
32   * Created on 11/23/14.
33   *
34   * @author Tony Chemit - chemit@codelutin.com
35   * @since 2.17
36   */
37  public class NumberEditorModel extends AbstractSerializableBean implements ModelToBean {
38  
39      private static final long serialVersionUID = 1L;
40  
41      public static final String PROPERTY_BEAN = "bean";
42  
43      public static final String PROPERTY_TEXT_VALUE = "textValue";
44  
45      public static final String PROPERTY_NUMBER_VALUE = "numberValue";
46  
47      public static final String PROPERTY_NUMBER_PATTERN = "numberPattern";
48  
49      private final NumberEditorConfig config;
50  
51      /** Optional bean where to push data. */
52      protected Serializable bean;
53  
54      /**
55       * Current text representation of the number (this value is always displayed in editor).
56       *
57       * Meanwhile the value can be different than the string represention of the numberValue, for example we can have as
58       * textValue {@code 0.} which represents the number {@code 0}.
59       */
60      protected String textValue;
61  
62      /**
63       * Current number value of the editor.
64       */
65      protected Number numberValue;
66  
67      /**
68       * Optional pattern to validate input text.
69       */
70      protected String numberPattern;
71  
72      /**
73       * To avoid reentrant code while adjusting text value.
74       */
75      protected boolean textValueIsAdjusting;
76  
77      /**
78       * To avoid reentrant code while adjusting number value.
79       */
80      protected boolean numberValueIsAdjusting;
81  
82      public NumberEditorModel(NumberEditorConfig config) {
83          this.config = config;
84      }
85  
86      NumberEditorConfig getConfig() {
87          return config;
88      }
89  
90      @Override
91      public Serializable getBean() {
92          return bean;
93      }
94  
95      public void setBean(Serializable bean) {
96          Object oldValue = getBean();
97          this.bean = bean;
98          firePropertyChange(PROPERTY_BEAN, oldValue, bean);
99      }
100 
101     public String getNumberPattern() {
102         return numberPattern;
103     }
104 
105     public void setNumberPattern(String numberPattern) {
106 
107         String oldValue = getNumberPattern();
108         this.numberPattern = numberPattern;
109         firePropertyChange(PROPERTY_NUMBER_PATTERN, oldValue, numberPattern);
110 
111     }
112 
113     public Number getNumberValue() {
114         return numberValue;
115     }
116 
117     public void setNumberValue(Number numberValue) {
118 
119         if (!numberValueIsAdjusting) {
120 
121             numberValueIsAdjusting = true;
122 
123             try {
124                 Number oldValue = getNumberValue();
125                 this.numberValue = numberValue;
126                 firePropertyChange(PROPERTY_NUMBER_VALUE, oldValue, numberValue);
127             } finally {
128 
129                 numberValueIsAdjusting = false;
130 
131             }
132 
133         }
134 
135     }
136 
137     public String getTextValue() {
138         return textValue;
139     }
140 
141     public void setTextValue(String textValue) {
142 
143         if (!textValueIsAdjusting) {
144 
145             textValueIsAdjusting = true;
146 
147             try {
148                 String oldValue = getTextValue();
149                 this.textValue = textValue;
150                 firePropertyChange(PROPERTY_TEXT_VALUE, oldValue, textValue);
151                 firePropertyChange("canUseDot", null, isCanUseDot());
152                 firePropertyChange("canUseSign", null, isCanUseSign());
153                 firePropertyChange("canUseZero", null, isCanUseZero());
154                 firePropertyChange("canClearAll", null, isCanClearAll());
155             } finally {
156 
157                 textValueIsAdjusting = false;
158 
159             }
160 
161         }
162 
163     }
164 
165     public boolean isCanUseDot() {
166         Boolean useDecimal = config.getUseDecimal();
167         return useDecimal != null && useDecimal && !textValue.contains(".");
168     }
169 
170     public boolean isCanUseSign() {
171         return config.isUseSign() && StringUtils.isNotBlank(textValue);
172     }
173 
174     public boolean isCanUseZero() {
175         return StringUtils.isNotBlank(textValue) && "0".equals(textValue);
176     }
177 
178     public boolean isCanClearAll() {
179         return StringUtils.isNotBlank(textValue);
180     }
181 
182     public boolean isTextValueIsAdjusting() {
183         return textValueIsAdjusting;
184     }
185 
186     public boolean isNumberValueIsAdjusting() {
187         return numberValueIsAdjusting;
188     }
189 
190     @SuppressWarnings("Guava")
191     protected Predicate<NumberEditorModel> canUpdateBeanNumberValuePredicate() {
192         return input -> true;
193     }
194 
195 }