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