View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.validation.actions;
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.Lists;
26  import fr.ifremer.tutti.persistence.entities.data.Cruise;
27  import fr.ifremer.tutti.service.catches.ValidateCruiseOperationsService;
28  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
29  import fr.ifremer.tutti.ui.swing.content.cruise.EditCruiseUI;
30  import fr.ifremer.tutti.ui.swing.content.cruise.EditCruiseUIModel;
31  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUIModel;
32  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUI;
33  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUIHandler;
34  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUIModel;
35  import org.nuiton.validator.NuitonValidatorResult;
36  
37  import java.beans.PropertyChangeEvent;
38  import java.beans.PropertyChangeListener;
39  import java.util.List;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * To edit the given cruise in the validation ui.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 3.6
48   */
49  public class EditCruiseInValidationUIAction extends LongActionSupport<ValidateCruiseUIModel, ValidateCruiseUI, ValidateCruiseUIHandler> {
50  
51      /** Validation service. */
52      private final ValidateCruiseOperationsService validationService;
53  
54      /**
55       * The incoming cruise to edit.
56       *
57       * Can be null (means do not edit any fishing operation), or with no id
58       * (means create a ne fishing operation), or with an id (means edit an
59       * existing fishing operation).
60       */
61      protected Cruise cruise;
62  
63      protected final PropertyChangeListener modelListener = new PropertyChangeListener() {
64  
65          protected final List<String> propertiesToIgnore = Lists.newArrayList(
66                  EditFishingOperationUIModel.PROPERTY_VALID,
67                  EditFishingOperationUIModel.PROPERTY_PERSISTED
68          );
69  
70          @Override
71          public void propertyChange(PropertyChangeEvent evt) {
72  
73              EditCruiseUIModel model = (EditCruiseUIModel) evt.getSource();
74  
75              if (EditCruiseUIModel.PROPERTY_MODIFY.equals(evt.getPropertyName())) {
76  
77                  if (!model.isModify()) {
78  
79                      // after a save, or a reset, reload model cruise, since the synchronizationStatus may have changed
80                      Cruise cruise = getDataContext().reloadCruise();
81                      getModel().setCruise(cruise);
82                  }
83  
84              } else if (!propertiesToIgnore.contains(evt.getPropertyName())) {
85  
86                  Cruise cruise = model.toEntity();
87                  NuitonValidatorResult validationResult = validationService.validateCruiseCruise(cruise);
88  
89                  ValidateCruiseUIModel uiModel = getModel();
90                  uiModel.setCruiseValidatorResult(validationResult);
91  
92                  getHandler().updateCurrentCruiseNode(validationResult);
93              }
94  
95          }
96      };
97  
98      public EditCruiseInValidationUIAction(ValidateCruiseUIHandler handler) {
99          super(handler, true);
100         setActionDescription(t("tutti.validateCruise.action.editCruise.tip"));
101         validationService = getContext().getValidateCruiseOperationsService();
102     }
103 
104     public void setCruise(Cruise cruise) {
105         this.cruise = cruise;
106     }
107 
108     @Override
109     public boolean prepareAction() throws Exception {
110         removeListener();
111 
112         return super.prepareAction();
113     }
114 
115     public void removeListener() {
116         EditCruiseUI operationPanel = getUI().getCruisePanel();
117         operationPanel.getModel().removePropertyChangeListener(modelListener);
118     }
119 
120     @Override
121     public void doAction() throws Exception {
122         getUI().getCruisePanel().getHandler().reloadCruise(cruise);
123     }
124 
125     @Override
126     public void postSuccessAction() {
127         super.postSuccessAction();
128 
129         EditCruiseUI cruisePanel = getUI().getCruisePanel();
130         cruisePanel.getModel().addPropertyChangeListener(modelListener);
131     }
132 
133     @Override
134     public void releaseAction() {
135         super.releaseAction();
136         cruise = null;
137     }
138 }