View Javadoc
1   package fr.ifremer.quadrige2.ui.swing.common.table;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 com.google.common.base.Preconditions;
28  import com.google.common.collect.Lists;
29  import org.apache.commons.collections4.CollectionUtils;
30  import org.jdesktop.swingx.table.TableColumnModelExt;
31  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
32  
33  import java.util.Collections;
34  import java.util.List;
35  
36  /**
37   * <p>Abstract AbstractTableModel class.</p>
38   *
39   * @param <R> type of AbstractRowUIModel
40   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
41   */
42  public abstract class AbstractTableModel<R extends AbstractRowUIModel<?, ?>> extends AbstractApplicationTableModel<R> {
43  
44      public static final String DEFAULT_STATE_CONTEXT = "DEFAULT_STATE_CONTEXT";
45      private AbstractTableUIModel tableUIModel;
46  
47      private boolean hasCheckTableColumn;
48  
49      /**
50       * <p>Constructor for AbstractTableModel.</p>
51       *
52       * @param columnModel a {@link TableColumnModelExt} object.
53       * @param createNewRow a boolean.
54       * @param createEmptyRowIsEmpty a boolean.
55       */
56      public AbstractTableModel(TableColumnModelExt columnModel, boolean createNewRow, boolean createEmptyRowIsEmpty) {
57          super(columnModel, createNewRow, createEmptyRowIsEmpty);
58      }
59  
60      /**
61       * First column idenitifer for editing.
62       *
63       * @return Column idenitifer
64       */
65      public abstract ColumnIdentifier<R> getFirstColumnEditing();
66  
67      /** {@inheritDoc} */
68      @Override
69      public boolean isCellEditable(int rowIndex, int columnIndex, org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R> propertyName) {
70  
71          // the CheckTableColumn (identified by 'selected' property) is always editable
72          if (AbstractRowUIModel.PROPERTY_SELECTED.equals(propertyName.getPropertyName())) {
73              return true;
74          }
75  
76          R row = getEntry(rowIndex);
77  
78          // if the row is editable (such a new row) this method force to return true
79          if (!row.isEditable()) {
80              return false;
81          }
82  
83          // otherwise, try to get the super implementation
84          try {
85              return super.isCellEditable(rowIndex, columnIndex, propertyName);
86          } catch (NullPointerException e) {
87              // if the exception is fired, then there is no non-editable columns
88              return true;
89          }
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public Class<?> getColumnClass(int columnIndex) {
95          org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R> colId = getIdentifier(columnIndex);
96          if (colId instanceof ColumnIdentifier) {
97              return ((ColumnIdentifier) colId).getPropertyType();
98          }
99          return super.getColumnClass(columnIndex);
100     }
101 
102     /**
103      * <p>getIdentifiers.</p>
104      *
105      * @return a {@link List} object.
106      */
107     public List<org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R>> getIdentifiers() {
108         return identifiers;
109     }
110 
111     /**
112      * <p>getMandatoryIdentifiers.</p>
113      *
114      * @return a {@link List} object.
115      */
116     public List<ColumnIdentifier<R>> getMandatoryIdentifiers() {
117         List<ColumnIdentifier<R>> mandatoryIdentifiers = Lists.newArrayList();
118         if (CollectionUtils.isNotEmpty(getIdentifiers())) {
119             for (org.nuiton.jaxx.application.swing.table.ColumnIdentifier<R> identifier : getIdentifiers()) {
120                 if ((identifier instanceof ColumnIdentifier) && ((ColumnIdentifier<R>) identifier).isMandatory()) {
121                     mandatoryIdentifiers.add((ColumnIdentifier<R>) identifier);
122                 }
123             }
124         }
125         return mandatoryIdentifiers;
126     }
127 
128     /**
129      * Get the table UI model
130      *
131      * @return table UI model
132      */
133     public AbstractTableUIModel getTableUIModel() {
134         return tableUIModel;
135     }
136 
137     /**
138      * Set the table UI model
139      *
140      * @param tableUIModel table UI model
141      */
142     public void setTableUIModel(AbstractTableUIModel tableUIModel) {
143         this.tableUIModel = tableUIModel;
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     protected void onRowAdded(int rowIndex, R row) {
149         super.onRowAdded(rowIndex, row);
150 
151         // tell the table UI model that a new row has been added
152         if (getTableUIModel() != null) {
153             getTableUIModel().firePropertyChanged(AbstractTableUIModel.PROPERTY_ROWS_ADDED, null, Collections.singletonList(row));
154         }
155 
156     }
157 
158     /**
159      * <p>addNonEditableColumn.</p>
160      *
161      * @param identifier a {@link ColumnIdentifier} object.
162      */
163     public void addNonEditableColumn(ColumnIdentifier identifier) {
164         if (noneEditableCols == null) {
165             setNoneEditableCols(identifier);
166         } else {
167             noneEditableCols.add(identifier);
168         }
169     }
170 
171     /**
172      * <p>addCheckTableColumnIdentifier.</p>
173      *
174      * @param identifier a {@link ColumnIdentifier} object.
175      */
176     public void addCheckTableColumnIdentifier(ColumnIdentifier<R> identifier) {
177         Preconditions.checkArgument(identifier instanceof CheckTableColumnIdentifier);
178         identifiers.add(identifier);
179         hasCheckTableColumn = true;
180     }
181 
182     /**
183      * <p>hasCheckTableColumn.</p>
184      *
185      * @return a boolean.
186      */
187     public boolean hasCheckTableColumn() {
188         return hasCheckTableColumn;
189     }
190 
191     /**
192      * get the state context for this table model
193      *
194      * @return a String representing the state context
195      */
196     public String getStateContext() {
197         return DEFAULT_STATE_CONTEXT;
198     }
199 }