View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.rtp;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 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.protocol.Rtp;
28  import fr.ifremer.tutti.persistence.entities.protocol.RtpBean;
29  import fr.ifremer.tutti.persistence.entities.protocol.Rtps;
30  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesTableModel;
32  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import javax.swing.RowSorter;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  /**
41   * Created on 14/01/16.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   */
45  public class RtpEditorUIModel extends AbstractTuttiBeanUIModel<EditProtocolSpeciesRowModel, RtpEditorUIModel> {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(RtpEditorUIModel.class);
49  
50      public static final String PROPERTY_RTP_MALE_A = "rtpMaleA";
51      public static final String PROPERTY_RTP_MALE_B = "rtpMaleB";
52      public static final String PROPERTY_RTP_FEMALE_A = "rtpFemaleA";
53      public static final String PROPERTY_RTP_FEMALE_B = "rtpFemaleB";
54      public static final String PROPERTY_RTP_UNDEFINED_A = "rtpUndefinedA";
55      public static final String PROPERTY_RTP_UNDEFINED_B = "rtpUndefinedB";
56  
57      public static final String PROPERTY_FIRST_ROW = "firstRow";
58      public static final String PROPERTY_LAST_ROW = "lastRow";
59      public static final String PROPERTY_ROW = "row";
60  
61      protected final Rtp rtpMale = new RtpBean();
62  
63      protected final Rtp rtpFemale = new RtpBean();
64  
65      protected final Rtp rtpUndefined = new RtpBean();
66  
67      protected boolean firstRow;
68  
69      protected boolean lastRow;
70  
71      protected EditProtocolSpeciesTableModel tableModel;
72  
73      // list of the indexes of the rows in the table model
74      // only the rows with lengthstep caracteristics are in the list
75      protected List<Integer> rows = new ArrayList<>();
76  
77      protected int row = -1;
78  
79      public RtpEditorUIModel() {
80          super(null, null);
81      }
82  
83      @Override
84      protected EditProtocolSpeciesRowModel newEntity() {
85          return null;
86      }
87  
88      public Rtp getRtpMale() {
89          return rtpMale;
90      }
91  
92      public Rtp getRtpUndefined() {
93          return rtpUndefined;
94      }
95  
96      public Rtp getRtpFemale() {
97          return rtpFemale;
98      }
99  
100     public Double getRtpMaleA() {
101         return rtpMale.getA();
102     }
103 
104     public void setRtpMaleA(Double a) {
105         rtpMale.setA(a);
106         firePropertyChanged(PROPERTY_RTP_MALE_A, null, a);
107     }
108 
109     public Float getRtpMaleB() {
110         return rtpMale.getB();
111     }
112 
113     public void setRtpMaleB(Float b) {
114         rtpMale.setB(b);
115         firePropertyChanged(PROPERTY_RTP_MALE_B, null, b);
116     }
117 
118     public Double getRtpFemaleA() {
119         return rtpFemale.getA();
120     }
121 
122     public void setRtpFemaleA(Double a) {
123         rtpFemale.setA(a);
124         firePropertyChanged(PROPERTY_RTP_FEMALE_A, null, a);
125     }
126 
127     public Float getRtpFemaleB() {
128         return rtpFemale.getB();
129     }
130 
131     public void setRtpFemaleB(Float b) {
132         rtpFemale.setB(b);
133         firePropertyChanged(PROPERTY_RTP_FEMALE_B, null, b);
134     }
135 
136     public Double getRtpUndefinedA() {
137         return rtpUndefined.getA();
138     }
139 
140     public void setRtpUndefinedA(Double a) {
141         rtpUndefined.setA(a);
142         firePropertyChanged(PROPERTY_RTP_UNDEFINED_A, null, a);
143     }
144 
145     public Float getRtpUndefinedB() {
146         return rtpUndefined.getB();
147     }
148 
149     public void setRtpUndefinedB(Float b) {
150         rtpUndefined.setB(b);
151         firePropertyChanged(PROPERTY_RTP_UNDEFINED_B, null, b);
152     }
153 
154     public boolean isFirstRow() {
155         return firstRow;
156     }
157 
158     public void setFirstRow(boolean firstRow) {
159         Object oldValue = isFirstRow();
160         this.firstRow = firstRow;
161         firePropertyChange(PROPERTY_FIRST_ROW, oldValue, firstRow);
162     }
163 
164     public boolean isLastRow() {
165         return lastRow;
166     }
167 
168     public void setLastRow(boolean lastRow) {
169         Object oldValue = isLastRow();
170         this.lastRow = lastRow;
171         firePropertyChange(PROPERTY_LAST_ROW, oldValue, lastRow);
172     }
173 
174     public int getRow() {
175         return row;
176     }
177 
178     public void setRow(int row) {
179         Object oldValue = getRow();
180         this.row = row;
181         firePropertyChange(PROPERTY_ROW, oldValue, row);
182     }
183 
184     public void setRowModel(EditProtocolSpeciesTableModel tableModel, RowSorter rowSorter, int row) {
185 
186         this.tableModel = tableModel;
187 
188         rows.clear();
189         int rowIndex = 0;
190 
191         for (int i = 0 ; i < tableModel.getRowCount() ; i++) {
192 
193             int rowIndexInModel = rowSorter.convertRowIndexToModel(i);
194             EditProtocolSpeciesRowModel rowModel = tableModel.getEntry(rowIndexInModel);
195 
196             if (rowModel.withLengthStepPmfm()) {
197 
198                 rows.add(rowIndexInModel);
199                 if (row == i) {
200                     rowIndex = rows.size() - 1;
201                 }
202 
203             }
204 
205         }
206 
207         setRowModel(rowIndex);
208     }
209 
210     public void setRowModel(int row) {
211 
212         if (log.isInfoEnabled()) {
213             log.info("setRowModel " + row);
214         }
215 
216         setRow(row);
217         setFirstRow(row == 0);
218         setLastRow(row == rows.size() - 1);
219 
220         EditProtocolSpeciesRowModel rowModel = getRowModel();
221 
222         Rtp rtpMale = rowModel.getRtpMale();
223         setRtpMaleA(rtpMale == null ? null : rtpMale.getA());
224         setRtpMaleB(rtpMale == null ? null : rtpMale.getB());
225 
226         Rtp rtpFemale = rowModel.getRtpFemale();
227         setRtpFemaleA(rtpFemale == null ? null : rtpFemale.getA());
228         setRtpFemaleB(rtpFemale == null ? null : rtpFemale.getB());
229 
230         Rtp rtpUndefined = rowModel.getRtpUndefined();
231         setRtpUndefinedA(rtpUndefined == null ? null : rtpUndefined.getA());
232         setRtpUndefinedB(rtpUndefined == null ? null : rtpUndefined.getB());
233 
234         setModify(false);
235     }
236 
237     public EditProtocolSpeciesRowModel getRowModel() {
238         int rowIndex = rows.get(row);
239         return tableModel.getEntry(rowIndex);
240     }
241 
242     public void reset() {
243         if (row >= 0 && row < rows.size()) {
244             setRowModel(row);
245         }
246     }
247 
248     public void updateRowRtp() {
249 
250         EditProtocolSpeciesRowModel rowModel = getRowModel();
251         rowModel.setRtpMale(Rtps.newRtp(getRtpMale()));
252         rowModel.setRtpFemale(Rtps.newRtp(getRtpFemale()));
253         rowModel.setRtpUndefined(Rtps.newRtp(getRtpUndefined()));
254 
255         setModify(false);
256 
257         tableModel.fireTableRowsUpdated(row, row);
258 
259     }
260 
261 }