View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.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.base.Preconditions;
26  import fr.ifremer.tutti.persistence.ProgressionModel;
27  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28  import fr.ifremer.tutti.service.TuttiDataContext;
29  import fr.ifremer.tutti.service.ValidationService;
30  import fr.ifremer.tutti.service.cruise.CruiseCacheLoader;
31  import fr.ifremer.tutti.ui.swing.TuttiScreen;
32  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
33  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
34  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUI;
35  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUI;
36  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  import javax.swing.JComponent;
41  import javax.swing.SwingUtilities;
42  
43  /**
44   * Opens the operations edition screen to edit the selected operations.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 1.0
48   */
49  public abstract class EditCatchesSupportAction extends AbstractChangeScreenAction {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(EditCatchesSupportAction.class);
53  
54      public EditCatchesSupportAction(MainUIHandler handler) {
55          super(handler, true, TuttiScreen.EDIT_FISHING_OPERATION);
56      }
57  
58      protected abstract boolean isLoadReferential();
59  
60      protected boolean loadReferential;
61      protected boolean loadCruiseCache;
62  
63      @Override
64      public boolean prepareAction() throws Exception {
65          boolean doAction = super.prepareAction();
66  
67          TuttiDataContext dataContext = getDataContext();
68          if (doAction) {
69  
70              loadReferential = isLoadReferential();
71              loadCruiseCache = !(dataContext.isCruiseCacheLoaded() && dataContext.isCruiseCacheUpToDate());
72  
73              int totalSteps = 1;
74  
75              if (loadReferential) {
76                  totalSteps += 5;
77              }
78  
79              // chargement des utilisateurs
80              totalSteps++;
81  
82              // chargement de la série de campagne
83              totalSteps++;
84  
85              // chargement de la campagne
86              totalSteps++;
87  
88              // chargement des navires
89              totalSteps++;
90  
91              // chargement des engins
92              totalSteps++;
93  
94              // chargement des caractéristiques
95              totalSteps++;
96  
97              if (loadCruiseCache) {
98  
99                  // Calcul des étapes (nb de traits dans la campagne)
100                 long cruiseFishingOperationIds = getDataContext().getCruiseFishingOperationIds().stream().count();
101                 totalSteps += cruiseFishingOperationIds + 1;
102 
103                 if (log.isInfoEnabled()) {
104                     log.info("Found " + cruiseFishingOperationIds + " fishing operations to load in sampling cache.");
105                 }
106             }
107 
108             if (totalSteps > 1) {
109                 ProgressionModel progressionModel = new ProgressionModel();
110                 progressionModel.setTotal(1);
111                 setProgressionModel(progressionModel);
112                 progressionModel.adaptTotal(totalSteps);
113             }
114 
115         }
116         return doAction;
117     }
118 
119     @Override
120     public void doAction() throws Exception {
121         TuttiUIContext context = getContext();
122         Preconditions.checkState(context.isCruiseFilled());
123         Integer cruiseId = context.getCruiseId();
124         if (log.isInfoEnabled()) {
125             log.info("Edit operations of cruise: " + cruiseId);
126         }
127         context.setValidationContext(ValidationService.VALIDATION_CONTEXT_EDIT);
128         if (loadReferential) {
129 
130             loadReferantials(false);
131 
132         }
133         ProgressionModel progressionModel = getProgressionModel();
134 
135         progressionModel.increments("Chargement des utilisateurs");
136         getDataContext().getPersons();
137 
138         progressionModel.increments("Chargement de la série de campagne");
139         getDataContext().getProgram();
140 
141         progressionModel.increments("Chargement de la campagne");
142         getDataContext().getCruise();
143 
144         progressionModel.increments("Chargement des navires");
145         getDataContext().getScientificVessels();
146         getDataContext().getFishingVessels();
147 
148         progressionModel.increments("Chargement des engins");
149         getDataContext().getScientificGears();
150         getDataContext().getFishingGears();
151 
152         progressionModel.increments("Chargement des caractéristiques");
153         getDataContext().getCaracteristics();
154 
155         if (loadCruiseCache) {
156 
157 
158 
159             CruiseCacheLoader cruiseCacheLoader = context.createCruiseCacheLoader(progressionModel);
160             getDataContext().loadCruiseCache(cruiseCacheLoader);
161 
162             progressionModel.increments("Chargement de l'interface graphique");
163 
164         }
165         super.doAction();
166     }
167 
168     @Override
169     public void postSuccessAction() {
170         super.postSuccessAction();
171 
172         SwingUtilities.invokeLater(
173                 () -> {
174 
175                     FishingOperationsUI currentBody = (FishingOperationsUI) getHandler().getCurrentBody();
176 
177                     BeanFilterableComboBox<FishingOperation> comboBox = currentBody.getFishingOperationComboBox();
178                     if (!comboBox.isEmpty()) {
179                         FishingOperation selectedOperation = comboBox.getData().get(0);
180                         currentBody.getModel().setSelectedFishingOperation(selectedOperation);
181                     }
182 
183                     EditFishingOperationUI fishingOperationTabContent = currentBody.getFishingOperationTabContent();
184 
185                     JComponent componentToFocus = fishingOperationTabContent.getHandler().getComponentToFocus();
186                     componentToFocus.requestFocusInWindow();
187 
188                 }
189         );
190 
191     }
192 
193 }