View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation;
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 com.google.common.collect.Lists;
27  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.jdesktop.beans.AbstractSerializableBean;
32  
33  import java.util.List;
34  
35  /**
36   * Model fo UI {@link FishingOperationsUI}.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 0.1
40   */
41  public class FishingOperationsUIModel extends AbstractSerializableBean {
42  
43      private static final long serialVersionUID = 1L;
44  
45      /** Logger. */
46      private static final Log log =
47              LogFactory.getLog(FishingOperationsUIModel.class);
48  
49      public static final String PROPERTY_FISHING_OPERATION = "fishingOperation";
50  
51      public static final String PROPERTY_SELECTED_FISHING_OPERATION = "selectedFishingOperation";
52  
53      public static final String PROPERTY_EDITED_FISHING_OPERATION = "editedFishingOperation";
54  
55      public static final String PROPERTY_CATCH_ENABLED = "catchEnabled";
56  
57  //    public static final String PROPERTY_CATCH_NOT_FOUND = "catchNotFound";
58  //
59  //    public static final String PROPERTY_SAMPLE_CATCH_MODEL_VALID = "sampleCatchModelValid";
60  
61      public static final String PROPERTY_VALIDATION_ERROR_MESSAGE = "validationErrorMessage";
62  
63      /**
64       * List of existing fishing operation for the selected cruise.
65       *
66       * <strong>Note:</strong> These objects are not fully loaded, they will
67       * never be edited.
68       *
69       * @since 0.1
70       */
71      protected List<FishingOperation> fishingOperation;
72  
73      /**
74       * Selected fishing operation in the combo box.
75       *
76       * For example if you create a new fishing operation then
77       * {@code selectedFishingOperation} will be {@code null}.
78       *
79       * <strong>Note:</strong> These object ise not fully loaded, they will
80       * never be edited.
81       *
82       * @since 0.1
83       */
84      protected FishingOperation selectedFishingOperation;
85  
86      /**
87       * Current edited fishing operation.
88       *
89       * @since 1.0
90       */
91      protected FishingOperation editFishingOperation;
92  
93      /**
94       * Flag to not listen the {@link #selectedFishingOperation}
95       * changes while adjusting the model.
96       *
97       * When flag is {@code true}, then the changes of the
98       * {@link #selectedFishingOperation} should not trigger any changes.
99       *
100      * @since 1.0
101      */
102     protected boolean selectionAdjusting;
103 
104     /**
105      * Flag to not listen the {@link #editFishingOperation}
106      * changes while adjusting the model.
107      *
108      * When flag is {@code true}, then the changes of the
109      * {@link #editFishingOperation} should not trigger any changes.
110      *
111      * @since 1.0
112      */
113     protected boolean editionAdjusting;
114 
115     /**
116      * Flag to use or not catch tab.
117      *
118      * @since 1.1
119      */
120     protected boolean catchEnabled = true;
121 
122     /**
123      * contains if any error message while loading the fishing operation catch batch.
124      *
125      * @since 3.5
126      */
127     protected String validationErrorMessage;
128 
129     public List<FishingOperation> getFishingOperation() {
130         return fishingOperation;
131     }
132 
133     public void setFishingOperation(List<FishingOperation> fishingOperation) {
134         Object oldValue = getFishingOperation();
135         this.fishingOperation = fishingOperation;
136         firePropertyChange(PROPERTY_FISHING_OPERATION, oldValue, fishingOperation);
137     }
138 
139     public void addFishingOperation(FishingOperation fishingOperation) {
140         Object oldValue = Lists.newArrayList(getFishingOperation());
141         this.fishingOperation.add(fishingOperation);
142         firePropertyChange(PROPERTY_FISHING_OPERATION, oldValue, this.fishingOperation);
143     }
144 
145     public void removeFishingOperation(FishingOperation fishingOperation) {
146         Object oldValue = Lists.newArrayList(getFishingOperation());
147         this.fishingOperation.remove(fishingOperation);
148         firePropertyChange(PROPERTY_FISHING_OPERATION, oldValue, this.fishingOperation);
149     }
150 
151     public void updateFishingOperation(FishingOperation newFishingOperation) {
152 
153         Preconditions.checkNotNull(newFishingOperation);
154         String id = newFishingOperation.getId();
155         Preconditions.checkNotNull(id);
156         FishingOperation oldFishingOperation = getFishingOperation(id);
157         Preconditions.checkNotNull(oldFishingOperation);
158 
159         if (log.isInfoEnabled()) {
160             log.info("Update existing fishing operation: " + id);
161         }
162 
163         int oldFishingOperationIndex = fishingOperation.indexOf(oldFishingOperation);
164         fishingOperation.remove(oldFishingOperation);
165         fishingOperation.add(oldFishingOperationIndex, newFishingOperation);
166         firePropertyChange(PROPERTY_FISHING_OPERATION, null, fishingOperation);
167     }
168 
169     public FishingOperation getSelectedFishingOperation() {
170         return selectedFishingOperation;
171     }
172 
173     public void setSelectedFishingOperation(FishingOperation selectedFishingOperation) {
174         Object oldValue = getSelectedFishingOperation();
175         this.selectedFishingOperation = selectedFishingOperation;
176         if ((oldValue != null || selectedFishingOperation != null)
177             && !isSelectionAdjusting()) {
178 
179             // only fires when authorize to
180             firePropertyChange(PROPERTY_SELECTED_FISHING_OPERATION, oldValue, selectedFishingOperation);
181         }
182     }
183 
184     public FishingOperation getEditFishingOperation() {
185         return editFishingOperation;
186     }
187 
188     public void setEditFishingOperation(FishingOperation editFishingOperation) {
189         Object oldValue = getEditFishingOperation();
190         this.editFishingOperation = editFishingOperation;
191         if (!isEditionAdjusting()) {
192 
193             // only fires when authorize to
194             firePropertyChange(PROPERTY_EDITED_FISHING_OPERATION, oldValue, editFishingOperation);
195         }
196     }
197 
198     public boolean isSelectionAdjusting() {
199         return selectionAdjusting;
200     }
201 
202     public void setSelectionAdjusting(boolean selectionAdjusting) {
203         this.selectionAdjusting = selectionAdjusting;
204     }
205 
206     public boolean isEditionAdjusting() {
207         return editionAdjusting;
208     }
209 
210     public void setEditionAdjusting(boolean editionAdjusting) {
211         this.editionAdjusting = editionAdjusting;
212     }
213 
214     public FishingOperation getFishingOperation(String id) {
215         return TuttiEntities.findById(fishingOperation, id);
216     }
217 
218     public boolean isCatchEnabled() {
219         return catchEnabled;
220     }
221 
222     public void setCatchEnabled(boolean catchEnabled) {
223         boolean oldValue = isCatchEnabled();
224         this.catchEnabled = catchEnabled;
225         firePropertyChange(PROPERTY_CATCH_ENABLED, oldValue, catchEnabled);
226     }
227 
228     public String getValidationErrorMessage() {
229         return validationErrorMessage;
230     }
231 
232     public void setValidationErrorMessage(String validationErrorMessage) {
233         String oldValue = getValidationErrorMessage();
234         this.validationErrorMessage = validationErrorMessage;
235         firePropertyChange(PROPERTY_VALIDATION_ERROR_MESSAGE, oldValue, validationErrorMessage);
236     }
237 }