View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.table;
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.ui.swing.util.AbstractTuttiBeanUIModel;
26  import org.apache.commons.collections4.CollectionUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.nuiton.util.beans.Binder;
30  
31  import java.beans.PropertyChangeEvent;
32  import java.beans.PropertyChangeListener;
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Set;
38  
39  /**
40   * @param <E> type of incoming bean to edit
41   * @param <R> type of the row of the table model
42   * @param <B> type of this model
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 0.2
45   */
46  public abstract class AbstractTuttiTableUIModel<E, R extends AbstractTuttiBeanUIModel, B extends AbstractTuttiTableUIModel<E, R, B>> extends AbstractTuttiBeanUIModel<E, B> {
47  
48      private static final long serialVersionUID = 1L;
49  
50      private static final Log log = LogFactory.getLog(AbstractTuttiTableUIModel.class);
51  
52      public static final String PROPERTY_ROWS = "rows";
53  
54      public static final String PROPERTY_ROWS_IN_ERROR = "rowsInError";
55  
56      protected List<R> rows;
57  
58      protected Set<R> rowsInError;
59  
60      protected AbstractTuttiTableUIModel(Class<E> entityType,
61                                          Binder<E, B> fromBeanBinder,
62                                          Binder<B, E> toBeanBinder) {
63          super(fromBeanBinder, toBeanBinder);
64          addPropertyChangeListener(PROPERTY_ROWS_IN_ERROR, new PropertyChangeListener() {
65  
66              @Override
67              public void propertyChange(PropertyChangeEvent evt) {
68                  Set<R> rowsInErorr = (Set<R>) evt.getNewValue();
69                  if (log.isDebugEnabled()) {
70                      log.debug(PROPERTY_ROWS_IN_ERROR + " changed " + rowsInErorr.size());
71                  }
72                  setValid(CollectionUtils.isEmpty(rowsInErorr));
73              }
74          });
75          setRowsInError(new HashSet<>());
76      }
77  
78      public List<R> getRows() {
79          return rows;
80      }
81  
82      public void setRows(List<R> rows) {
83          if (rows == null) {
84              rows = new ArrayList<>();
85          }
86          this.rows = rows;
87  
88          // always propagates (since empty list will not fire and we want it)
89          firePropertyChange(PROPERTY_ROWS, null, rows);
90  
91          rowsInError.clear();
92          for (R row : rows) {
93              if (!row.isValid()) {
94                  rowsInError.add(row);
95              }
96          }
97          setRowsInError(rowsInError);
98      }
99  
100     public int getRowCount() {
101         return rows == null ? 0 : rows.size();
102     }
103 
104     public Set<R> getRowsInError() {
105         return rowsInError;
106     }
107 
108     public void setRowsInError(Set<R> rowsInError) {
109         this.rowsInError = rowsInError;
110         firePropertyChange(PROPERTY_ROWS_IN_ERROR, null, rowsInError);
111     }
112 
113     public void addRowInError(R row) {
114         rowsInError.add(row);
115         firePropertyChange(PROPERTY_ROWS_IN_ERROR, null, rowsInError);
116     }
117 
118     public void removeRowInError(R row) {
119         rowsInError.remove(row);
120         firePropertyChange(PROPERTY_ROWS_IN_ERROR, null, rowsInError);
121     }
122 
123     public void removeRowsInError(Collection<R> rows) {
124         rowsInError.removeAll(rows);
125         firePropertyChange(PROPERTY_ROWS_IN_ERROR, null, rowsInError);
126     }
127 }