View Javadoc
1   package fr.ifremer.tutti.ui.swing.util;
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.persistence.entities.TuttiEntity;
26  import org.jdesktop.beans.AbstractSerializableBean;
27  import org.nuiton.jaxx.application.bean.JavaBeanObject;
28  import org.nuiton.util.CollectionUtil;
29  import org.nuiton.util.beans.Binder;
30  
31  import java.util.Collection;
32  import java.util.List;
33  
34  /**
35   * Abstract UI model to edit a bean.
36   *
37   * @author Tony Chemit - chemit@codelutin.com
38   * @since 0.1
39   */
40  public abstract class AbstractTuttiBeanUIModel<E, B extends AbstractTuttiBeanUIModel<E, B>> extends AbstractSerializableBean implements TuttiEntity, JavaBeanObject {
41  
42      private static final long serialVersionUID = 1L;
43  
44      public static final String PROPERTY_MODIFY = "modify";
45  
46      public static final String PROPERTY_VALID = "valid";
47  
48      protected String id;
49  
50      protected boolean modify;
51  
52      protected boolean valid;
53  
54      private final Binder<E, B> fromBeanBinder;
55  
56      private final Binder<B, E> toBeanBinder;
57  
58      protected AbstractTuttiBeanUIModel(Binder<E, B> fromBeanBinder,
59                                         Binder<B, E> toBeanBinder) {
60          this.fromBeanBinder = fromBeanBinder;
61          this.toBeanBinder = toBeanBinder;
62      }
63  
64      public void fromEntity(E entity) {
65          fromBean(entity);
66      }
67  
68      public E toEntity() {
69          return toBean();
70      }
71  
72      public final void fromBean(E bean) {
73          fromBeanBinder.copy(bean, (B) this);
74      }
75  
76      public final E toBean() {
77          E result = newEntity();
78          toBeanBinder.copy((B) this, result);
79          return result;
80      }
81  
82      protected abstract E newEntity();
83  
84      public boolean isModify() {
85          return modify;
86      }
87  
88      public void setModify(boolean modify) {
89          Object oldValue = isModify();
90          this.modify = modify;
91          firePropertyChange(PROPERTY_MODIFY, oldValue, modify);
92      }
93  
94      public boolean isValid() {
95          return valid;
96      }
97  
98      public void setValid(boolean valid) {
99          Object oldValue = isValid();
100         this.valid = valid;
101         firePropertyChange(PROPERTY_VALID, oldValue, valid);
102     }
103 
104     public boolean isCreate() {
105         return id == null;
106     }
107 
108     //------------------------------------------------------------------------//
109     //-- TuttiEntity methods                                                --//
110     //------------------------------------------------------------------------//
111 
112     @Override
113     public Integer getIdAsInt() {
114         return id == null ? null : Integer.valueOf(id);
115     }
116 
117     @Override
118     public void setId(Integer id) {
119         if (id == null) {
120             this.id = null;
121         } else {
122             this.id = id.toString();
123         }
124     }
125 
126     @Override
127     public String getId() {
128         return id;
129     }
130 
131     @Override
132     public void setId(String id) {
133         Object oldValue = getId();
134         this.id = id;
135         firePropertyChange(PROPERTY_ID, oldValue, id);
136     }
137 
138     //------------------------------------------------------------------------//
139     //-- PropagatePropertyChangeListener methods                            --//
140     //------------------------------------------------------------------------//
141 
142     @Override
143     public void firePropertyChanged(String propertyName,
144                                     Object oldValue,
145                                     Object newValue) {
146         firePropertyChange(propertyName, oldValue, newValue);
147     }
148 
149     protected <B> B getChild(Collection<B> child, int index) {
150         return CollectionUtil.getOrNull(child, index);
151     }
152 
153     protected <B> B getChild(List<B> child, int index) {
154         return CollectionUtil.getOrNull(child, index);
155     }
156 
157 }