View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches;
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.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   * Abstract model for ui in batch tabs.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 0.3
42   */
43  public abstract class AbstractTuttiBatchUIModel<R extends AbstractTuttiBeanUIModel, B extends AbstractTuttiBatchUIModel<R, B>> extends AbstractTuttiTableUIModel<FishingOperation, R, B> {
44  
45      /** Logger. */
46      private static final Log log = LogFactory.getLog(AbstractTuttiBatchUIModel.class);
47  
48      private static final long serialVersionUID = 1L;
49  
50  //    /**
51  //     * Fishing Operation to prapagate to operations model.
52  //     *
53  //     * @since 0.3
54  //     */
55  //    protected final Set<String> propagateProperties;
56  
57      /**
58       * Editing fishing operations model.
59       *
60       * @since 0.3
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      //FIXME Use the one from JavaBeanObject ... later
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 }