View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.model;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import org.jdesktop.beans.AbstractBean;
28  import org.nuiton.util.beans.PropertyDiff;
29  
30  import java.util.Map;
31  
32  /**
33   * To monitor a bean and know when it changes.
34   *
35   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
36   * @param <B> bean type to monitor
37   */
38  public class BeanMonitor<B> extends AbstractBean {
39  
40      /** Constant <code>PROPERTY_BEAN="bean"</code> */
41      public static final String PROPERTY_BEAN = "bean";
42  
43      private final org.nuiton.util.beans.BeanMonitor monitor;
44  
45      /**
46       * <p>Constructor for BeanMonitor.</p>
47       *
48       * @param properties a {@link java.lang.String} object.
49       */
50      public BeanMonitor(String... properties) {
51          this.monitor = new org.nuiton.util.beans.BeanMonitor(properties);
52      }
53  
54      /**
55       * <p>setProperties.</p>
56       *
57       * @param properties a {@link java.lang.String} object.
58       */
59      public void setProperties(String... properties) {
60          monitor.setProperties(properties);
61      }
62  
63      /**
64       * <p>getBean.</p>
65       *
66       * @return a B object.
67       */
68      @SuppressWarnings("unchecked")
69      public B getBean() {
70          return (B) monitor.getBean();
71      }
72  
73      /**
74       * <p>setBean.</p>
75       *
76       * @param bean a B object.
77       */
78      public void setBean(B bean) {
79          Object oldValue = getBean();
80          monitor.setBean(bean);
81          firePropertyChange(PROPERTY_BEAN, oldValue, bean);
82      }
83  
84      /**
85       * <p>Getter for the field <code>monitor</code>.</p>
86       *
87       * @return a {@link org.nuiton.util.beans.BeanMonitor} object.
88       */
89      public org.nuiton.util.beans.BeanMonitor getMonitor() {
90          return monitor;
91      }
92  
93      /**
94       * <p>wasModified.</p>
95       *
96       * @return a boolean.
97       */
98      public boolean wasModified() {
99          return monitor.wasModified();
100     }
101 
102     /**
103      * <p>getModifiedProperties.</p>
104      *
105      * @return an array of {@link java.lang.String} objects.
106      */
107     public String[] getModifiedProperties() {
108         return monitor.getModifiedProperties();
109     }
110 
111     /**
112      * <p>getOriginalValues.</p>
113      *
114      * @return a {@link java.util.Map} object.
115      */
116     public Map<String, Object> getOriginalValues() {
117         return monitor.getOriginalValues();
118     }
119 
120     /**
121      * <p>getPropertyDiffs.</p>
122      *
123      * @return an array of {@link org.nuiton.util.beans.PropertyDiff} objects.
124      */
125     public PropertyDiff[] getPropertyDiffs() {
126         return monitor.getPropertyDiffs();
127     }
128 
129     /**
130      * <p>clearModified.</p>
131      */
132     public void clearModified() {
133         monitor.clearModified();
134     }
135 }