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.util.Numbers;
26  import org.jdesktop.beans.AbstractSerializableBean;
27  import org.nuiton.jaxx.application.bean.JavaBeanObject;
28  import org.nuiton.jaxx.application.bean.JavaBeanObjectPropagateChangeListener;
29  
30  
31  /**
32   * A number data plus a possible computed value.
33   *
34   * @author Kevin Morin - kmorin@codelutin.com
35   * @since 1.0
36   */
37  public class ComputableData<N extends Number> extends AbstractSerializableBean implements JavaBeanObject {
38  
39      public static final String PROPERTY_DATA = "data";
40  
41      public static final String PROPERTY_COMPUTED_DATA = "computedData";
42  
43      private static final long serialVersionUID = 1L;
44  
45      protected N data;
46  
47      protected N computedData;
48  
49      public ComputableData() {
50          data = null;
51          computedData = null;
52      }
53  
54      public ComputableData(N data, N computedData) {
55          this.data = data;
56          this.computedData = computedData;
57      }
58  
59      public N getData() {
60          return data;
61      }
62  
63      public void setData(N data) {
64          Object oldValue = getData();
65          this.data = data;
66          firePropertyChange(PROPERTY_DATA, oldValue, data);
67      }
68  
69      public N getComputedData() {
70          return computedData;
71      }
72  
73      public void setComputedData(N computedData) {
74          Object oldValue = getComputedData();
75          this.computedData = computedData;
76          firePropertyChange(PROPERTY_COMPUTED_DATA, oldValue, computedData);
77      }
78  
79      public N getDataOrComputedData() {
80          return Numbers.getValueOrComputedValue(data, computedData);
81      }
82  
83      @Override
84      public String toString() {
85          String result = null;
86          N dataOrComputedData = getDataOrComputedData();
87          if (dataOrComputedData != null) {
88              result = dataOrComputedData.toString();
89          }
90          return result;
91      }
92  
93      /**
94       * Add a listener to propagate the modification of the
95       * {@link #PROPERTY_DATA} property to a given {@code propertyName}.
96       *
97       * @param propertyName name of the property to fire on given bean
98       * @param otherBean    bean that will fires
99       * @since 1.2
100      */
101     public void addPropagateListener(String propertyName,
102                                      JavaBeanObject otherBean) {
103 
104         JavaBeanObjectPropagateChangeListener.listenAndPropagate(this, otherBean, PROPERTY_DATA, propertyName);
105     }
106 
107     @Override
108     public void firePropertyChanged(String propertyName, Object oldValue, Object newValue) {
109         super.firePropertyChange(propertyName, oldValue, newValue);
110     }
111 
112 }