View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.report;
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.ImmutableSet;
26  import com.google.common.collect.Lists;
27  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28  import fr.ifremer.tutti.service.report.ReportGenerationService;
29  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
30  import jaxx.runtime.validator.swing.SwingValidator;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.nuiton.jaxx.application.swing.util.CloseableUI;
34  
35  import javax.swing.JComponent;
36  import java.beans.PropertyChangeEvent;
37  import java.beans.PropertyChangeListener;
38  import java.io.File;
39  import java.util.List;
40  import java.util.Set;
41  
42  /**
43   * Handler of {@link ReportUI}.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 2.9
47   */
48  public class ReportUIHandler extends AbstractTuttiUIHandler<ReportUIModel, ReportUI> implements CloseableUI {
49  
50      /** Logger. */
51      private static final Log log = LogFactory.getLog(ReportUIHandler.class);
52  
53      @Override
54      public void beforeInit(ReportUI ui) {
55  
56          super.beforeInit(ui);
57  
58          ReportGenerationService reportService = getContext().getReportGenerationService();
59  
60          ReportUIModel model = new ReportUIModel();
61  
62          // set programId
63          String programId = getDataContext().getProgramId();
64          model.setProgramId(programId);
65  
66          // set cruiseId
67          Integer cruiseId = getDataContext().getCruiseId();
68          model.setCruiseId(cruiseId);
69  
70          // get all report availables
71          List<File> reports = reportService.getAvailableReports();
72          model.setReports(reports);
73  
74          // get all fishing operation
75          List<FishingOperation> fishingOperations =
76                  getContext().getPersistenceService().getAllFishingOperation(cruiseId);
77          model.setFishingOperations(fishingOperations);
78  
79          listModelIsModify(model);
80  
81          model.addPropertyChangeListener(new PropertyChangeListener() {
82  
83              Set<String> removeReportProperties= ImmutableSet.of(
84                      ReportUIModel.PROPERTY_FISHING_OPERATION,
85                      ReportUIModel.PROPERTY_REPORT
86              );
87  
88              @Override
89              public void propertyChange(PropertyChangeEvent evt) {
90  
91                  ReportUIModel source = (ReportUIModel) evt.getSource();
92  
93                  if (source .isReportDone() && removeReportProperties.contains(evt.getPropertyName())) {
94  
95                      // there was a result and input has changed, remove result
96                      source.setReportGenerationResult(null);
97  
98                  }
99  
100             }
101         });
102         ui.setContextValue(model);
103     }
104 
105     @Override
106     public void afterInit(ReportUI ui) {
107 
108         initUI(ui);
109 
110         ReportUIModel model = getModel();
111 
112         initBeanFilterableComboBox(ui.getReportComboBox(),
113                                    Lists.newArrayList(model.getReports()),
114                                    model.getReport());
115 
116         initBeanFilterableComboBox(ui.getFishingOperationComboBox(),
117                                    Lists.newArrayList(model.getFishingOperations()),
118                                    model.getFishingOperation());
119 
120         SwingValidator validator = ui.getValidator();
121         listenValidatorValid(validator, model);
122 
123         registerValidators(validator);
124     }
125 
126     @Override
127     protected JComponent getComponentToFocus() {
128         return getUI().getFishingOperationComboBox();
129     }
130 
131     @Override
132     public void onCloseUI() {
133         if (log.isDebugEnabled()) {
134             log.debug("closing: " + ui);
135         }
136         clearValidators();
137     }
138 
139     @Override
140     public boolean quitUI() {
141         return true;
142     }
143 
144     @Override
145     public SwingValidator<ReportUIModel> getValidator() {
146         return ui.getValidator();
147     }
148 
149 }