View Javadoc
1   package fr.ifremer.quadrige3.core;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Shared
6    * %%
7    * Copyright (C) 2017 - 2019 Ifremer
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  import org.jdesktop.beans.AbstractSerializableBean;
24  
25  /**
26   * New progression model with long total and current
27   * org.nuiton.jaxx.application.type.ApplicationProgressionModel is now deprecated
28   *
29   * @author peck7 on 04/07/2019.
30   */
31  public class ProgressionCoreModel extends AbstractSerializableBean {
32  
33      private static final double DEFAULT_RATE = 1;
34  
35      public static final String PROPERTY_TOTAL = "total";
36  
37      public static final String PROPERTY_CURRENT = "current";
38  
39      public static final String PROPERTY_MESSAGE = "message";
40  
41      private long total;
42  
43      private long current;
44  
45      private double rate = DEFAULT_RATE;
46  
47      private String message;
48  
49      /**
50       * get the progression total always as int
51       *
52       * @return total as int
53       */
54      public int getTotal() {
55          return (int) (total * rate);
56      }
57  
58      /**
59       * set the progression total
60       *
61       * @param total as long
62       */
63      public void setTotal(long total) {
64          // compute the long to int rate
65          rate = (total > Integer.MAX_VALUE) ? ((double) Integer.MAX_VALUE / total) : DEFAULT_RATE;
66          this.total = total;
67          firePropertyChange(PROPERTY_TOTAL, null, getTotal());
68          setCurrent(0);
69      }
70  
71      /**
72       * adapt the progression total only if greater than actual
73       *
74       * @param total as long
75       */
76      public void adaptTotal(long total) {
77          if (total > this.total) {
78              int current = getCurrent();
79              setTotal(total);
80              setCurrent(current);
81          }
82      }
83  
84      /**
85       * get the current progression always as int
86       *
87       * @return current as int
88       */
89      public int getCurrent() {
90          return (int) (current * rate);
91      }
92  
93      /**
94       * set the current progression
95       *
96       * @param current as long
97       */
98      public void setCurrent(long current) {
99          this.current = (current > total) ? total : current;
100         firePropertyChange(PROPERTY_CURRENT, null, getCurrent());
101     }
102 
103     public void increments(int nb) {
104         setCurrent(current + nb);
105     }
106 
107     public String getMessage() {
108         return message;
109     }
110 
111     public void increments(String message) {
112         increments(1);
113         setMessage(message);
114     }
115 
116     public void setMessage(String message) {
117         String oldMessage = getMessage();
118         this.message = message;
119         firePropertyChange(PROPERTY_MESSAGE, oldMessage, message);
120     }
121 
122 }