1 package fr.ifremer.tutti.ui.swing.content.operation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
37
38
39
40
41 public class FishingOperationsUIModel extends AbstractSerializableBean {
42
43 private static final long serialVersionUID = 1L;
44
45
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
58
59
60
61 public static final String PROPERTY_VALIDATION_ERROR_MESSAGE = "validationErrorMessage";
62
63
64
65
66
67
68
69
70
71 protected List<FishingOperation> fishingOperation;
72
73
74
75
76
77
78
79
80
81
82
83
84 protected FishingOperation selectedFishingOperation;
85
86
87
88
89
90
91 protected FishingOperation editFishingOperation;
92
93
94
95
96
97
98
99
100
101
102 protected boolean selectionAdjusting;
103
104
105
106
107
108
109
110
111
112
113 protected boolean editionAdjusting;
114
115
116
117
118
119
120 protected boolean catchEnabled = true;
121
122
123
124
125
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
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
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 }