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 com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
27  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
28  import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
29  
30  import java.io.Serializable;
31  
32  /**
33   * To identify a column that represents a caracteristic.
34   *
35   * @author Tony Chemit - chemit@codelutin.com
36   * @since 2.5
37   */
38  public class CaracteristicColumnIdentifier<R> extends ColumnIdentifier<R> {
39  
40      private static final long serialVersionUID = 1L;
41  
42      public static <R> CaracteristicColumnIdentifier<R> newCaracteristicId(
43              Caracteristic caracteristic,
44              String propertyName,
45              String headerI18nKey,
46              String headerTipI18nKey) {
47  
48          return new CaracteristicColumnIdentifier<>(caracteristic, propertyName,
49                                                      headerI18nKey,
50                                                      headerTipI18nKey
51          );
52      }
53  
54      protected Caracteristic caracteristic;
55  
56      protected CaracteristicColumnIdentifier(Caracteristic caracteristic,
57                                              String propertyName,
58                                              String headerI18nKey,
59                                              String headerTipI18nKey) {
60          super(propertyName, headerI18nKey, headerTipI18nKey);
61          this.caracteristic = caracteristic;
62      }
63  
64      @Override
65      public Object getValue(R entry) {
66          CaracteristicMap map = (CaracteristicMap) super.getValue(entry);
67          return map.get(caracteristic);
68      }
69  
70      @Override
71      public void setValue(R entry, Object value) {
72          CaracteristicMap map = (CaracteristicMap) super.getValue(entry);
73          Preconditions.checkNotNull(map, "caracteristicMap (" + getPropertyName() + ") is null in " + entry);
74          // do a copy for the old value to be different than the new value
75          map = CaracteristicMap.copy(map);
76          map.put(caracteristic, (Serializable) value);
77          // reset the map in bean to fire property
78          super.setValue(entry, map);
79      }
80  
81      public Caracteristic getCaracteristic() {
82          return caracteristic;
83      }
84  }