1 package fr.ifremer.tutti.ui.swing.content.operation.catches;
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.collect.ImmutableMap;
26 import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
27 import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesOrBenthosBatchUISupport;
28 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
29 import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIModel;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.beans.PropertyChangeEvent;
34 import java.beans.PropertyChangeListener;
35 import java.util.Map;
36
37
38
39
40
41
42
43 public abstract class AbstractTuttiBatchUIModel<R extends AbstractTuttiBeanUIModel, B extends AbstractTuttiBatchUIModel<R, B>> extends AbstractTuttiTableUIModel<FishingOperation, R, B> {
44
45
46 private static final Log log = LogFactory.getLog(AbstractTuttiBatchUIModel.class);
47
48 private static final long serialVersionUID = 1L;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 protected final EditCatchesUIModel catchesUIModel;
63
64 protected AbstractTuttiBatchUIModel(EditCatchesUIModel catchesUIModel, String... properties) {
65 super(FishingOperation.class, null, null);
66 this.catchesUIModel = catchesUIModel;
67 ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder();
68 for (String property : properties) {
69 builder.put(property, property);
70 }
71 catchesUIModel.addPropertyChangeListener(new ForwardPropertyChangeListener(builder.build()));
72
73 }
74
75 public AbstractTuttiBatchUIModel(SpeciesOrBenthosBatchUISupport batchUISupport, String... properties) {
76 super(FishingOperation.class, null, null);
77 this.catchesUIModel = batchUISupport.getCatchesUIModel();
78
79 ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder();
80 for (String property : properties) {
81 builder.put(property, batchUISupport.getCatchesUIModelPropertiesMapping().get(property));
82 }
83 catchesUIModel.addPropertyChangeListener(new ForwardPropertyChangeListener(builder.build()));
84 }
85
86 public final FishingOperation getFishingOperation() {
87 return catchesUIModel == null ? null : catchesUIModel.getFishingOperation();
88 }
89
90 @Override
91 protected FishingOperation newEntity() {
92 return null;
93 }
94
95
96 private class ForwardPropertyChangeListener implements PropertyChangeListener {
97
98 protected final Map<String, String> propagatePropertiesMapping;
99
100 private ForwardPropertyChangeListener(Map<String, String> propagatePropertiesMapping) {
101 this.propagatePropertiesMapping = propagatePropertiesMapping;
102 }
103
104 @Override
105 public void propertyChange(PropertyChangeEvent evt) {
106 String propertyName = evt.getPropertyName();
107
108 if (propagatePropertiesMapping.containsKey(propertyName)) {
109 if (log.isDebugEnabled()) {
110 log.debug("Propagates property [" + propertyName + "] change from catch model to " + AbstractTuttiBatchUIModel.this.getClass().getSimpleName());
111 }
112 String translatedPropertyName = propagatePropertiesMapping.get(propertyName);
113 firePropertyChange(translatedPropertyName, evt.getOldValue(), evt.getNewValue());
114 EditCatchesUIModel source = (EditCatchesUIModel) evt.getSource();
115 if (!source.isLoadingData() && !isModify()) {
116 setModify(true);
117 }
118 }
119
120 if (AbstractTuttiBeanUIModel.PROPERTY_MODIFY.equals(propertyName)) {
121
122 Boolean newValue = (Boolean) evt.getNewValue();
123 if (newValue != null && !newValue) {
124 setModify(false);
125 }
126
127 }
128 }
129 }
130 }