View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
28  import fr.ifremer.tutti.persistence.entities.protocol.CaracteristicMappingRow;
29  import fr.ifremer.tutti.persistence.entities.protocol.CaracteristicMappingRowBean;
30  import fr.ifremer.tutti.persistence.entities.protocol.CaracteristicType;
31  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
32  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
33  import org.nuiton.util.beans.Binder;
34  import org.nuiton.util.beans.BinderFactory;
35  
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  /**
41   * @author Kevin Morin (Code Lutin)
42   * @since 3.10
43   */
44  public class EditProtocolCaracteristicsRowModel extends AbstractTuttiBeanUIModel<CaracteristicMappingRow, EditProtocolCaracteristicsRowModel> {
45  
46      public static final String PROPERTY_PSFM = "psfm";
47  
48      public static final String PROPERTY_TYPE = "type";
49  
50      public static final String PROPERTY_IMPORT_COLUMN = "importColumn";
51  
52      private static final long serialVersionUID = 1L;
53  
54      protected Map<String, Caracteristic> caracteristicMap;
55  
56      protected Caracteristic psfm;
57  
58      protected CaracteristicType type;
59  
60      protected String importColumn;
61  
62      protected static final Binder<CaracteristicMappingRow, EditProtocolCaracteristicsRowModel> fromBeanBinder =
63              BinderFactory.newBinder(CaracteristicMappingRow.class,
64                                      EditProtocolCaracteristicsRowModel.class);
65  
66      protected static final Binder<EditProtocolCaracteristicsRowModel, CaracteristicMappingRow> toBeanBinder =
67              BinderFactory.newBinder(EditProtocolCaracteristicsRowModel.class,
68                                      CaracteristicMappingRow.class);
69  
70      public EditProtocolCaracteristicsRowModel(Collection<Caracteristic> caracteristics) {
71          super(fromBeanBinder, toBeanBinder);
72          caracteristicMap = caracteristics != null ? TuttiEntities.splitById(caracteristics) : new HashMap<>();
73      }
74  
75      public Caracteristic getPsfm() {
76          return psfm;
77      }
78  
79      public void setPsfm(Caracteristic psfm) {
80          Object oldValue = getPsfm();
81          this.psfm = psfm;
82          firePropertyChanged(PROPERTY_PSFM, oldValue, psfm);
83      }
84  
85      public CaracteristicType getType() {
86          return type;
87      }
88  
89      public void setType(CaracteristicType type) {
90          Object oldValue = getType();
91          this.type = type;
92          firePropertyChanged(PROPERTY_TYPE, oldValue, type);
93      }
94  
95      public String getImportColumn() {
96          return importColumn;
97      }
98  
99      public void setImportColumn(String importColumn) {
100         Object oldValue = getImportColumn();
101         this.importColumn = importColumn;
102         firePropertyChanged(PROPERTY_IMPORT_COLUMN, oldValue, importColumn);
103     }
104 
105     public String getPmfmId() {
106         return psfm == null ? null : psfm.getId();
107     }
108 
109     public void setPmfmId(String pmfmId) {
110         setPsfm(caracteristicMap.get(pmfmId));
111     }
112 
113     public String getTab() {
114         return type == null ? null : type.name();
115     }
116 
117     public void setTab(String tab) {
118         setType(CaracteristicType.valueOf(tab));
119     }
120 
121     @Override
122     protected CaracteristicMappingRow newEntity() {
123         return new CaracteristicMappingRowBean();
124     }
125 }