View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.campaign;
2   
3   /*
4    * #%L
5    * ReefDb :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
27  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroCheckAction;
28  import fr.ifremer.reefdb.dto.data.survey.CampaignDTO;
29  import fr.ifremer.reefdb.ui.swing.action.QuitScreenAction;
30  import fr.ifremer.reefdb.ui.swing.content.manage.campaign.menu.CampaignsMenuUIModel;
31  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbBeanUIModel;
32  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
33  import jaxx.runtime.SwingUtil;
34  import jaxx.runtime.validator.swing.SwingValidator;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.nuiton.jaxx.application.swing.util.CloseableUI;
38  
39  import javax.swing.SwingUtilities;
40  import java.util.Collection;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * Controlleur pour l'onglet prelevements mesures.
46   */
47  public class CampaignsUIHandler extends AbstractReefDbUIHandler<CampaignsUIModel, CampaignsUI> implements CloseableUI {
48  
49      /**
50       * Logger.
51       */
52      private static final Log LOG = LogFactory.getLog(CampaignsUIHandler.class);
53      private ImportSynchroCheckAction importSynchroCheckAction;
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public void beforeInit(final CampaignsUI ui) {
60          super.beforeInit(ui);
61  
62          // create model and register to the JAXX context
63          final CampaignsUIModel model = new CampaignsUIModel();
64          ui.setContextValue(model);
65  
66          ui.setContextValue(SwingUtil.createActionIcon("campaign"));
67  
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public void afterInit(final CampaignsUI ui) {
75          initUI(ui);
76  
77          ui.getMenuUI().getHandler().enableContextFilter(false);
78  
79          // Save models
80          getModel().setCampaignsTableUIModel(ui.getCampaignsTableUI().getModel());
81  
82          // init image
83          SwingUtil.setComponentWidth(ui.getLeftImage(), ui.getMenu().getPreferredSize().width * 9 / 10);
84          ui.getLeftImage().setScaled(true);
85  
86          initListeners();
87  
88          ui.applyDataBinding(CampaignsUI.BINDING_SAVE_BUTTON_ENABLED);
89  
90          // check referential update (Mantis #47231)
91          SwingUtilities.invokeLater(this::checkForReferentialUpdates);
92      }
93  
94      @SuppressWarnings("unchecked")
95      private void initListeners() {
96  
97          // listen to menu results
98          getUI().getMenuUI().getModel().addPropertyChangeListener(CampaignsMenuUIModel.PROPERTY_RESULTS, evt -> {
99  
100             // affect campaigns
101             getModel().getCampaignsTableUIModel().setBeans((Collection<CampaignDTO>) evt.getNewValue());
102 
103             getModel().getCampaignsTableUIModel().getRows().forEach(campaign -> {
104 
105                 // set rows editable if save is allowed
106                 campaign.setEditable(getModel().isSaveEnabled());
107 
108                 // set row read-only if user or recorder department is not the same as the current user
109                 campaign.setReadOnly(!(campaign.getManager().getId().equals(getContext().getDataContext().getRecorderPersonId())
110                         || campaign.getRecorderDepartment().getId().equals(getContext().getDataContext().getRecorderDepartmentId())));
111 
112             });
113 
114         });
115 
116         // Listen valid state
117         getModel().getCampaignsTableUIModel().addPropertyChangeListener(AbstractReefDbBeanUIModel.PROPERTY_VALID, evt -> getValidator().doValidate());
118         listenModelModify(getModel().getCampaignsTableUIModel());
119 
120         // Add listener on saveEnabled property to disable change (Mantis #48002)
121         getModel().addPropertyChangeListener(CampaignsUIModel.PROPERTY_SAVE_ENABLED, evt -> getModel().getCampaignsTableUIModel().setSaveEnabled(getModel().isSaveEnabled()));
122 
123         // Register validator
124         registerValidators(getValidator());
125         listenValidatorValid(getValidator(), getModel());
126 
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public SwingValidator<CampaignsUIModel> getValidator() {
134         return getUI().getValidator();
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     @SuppressWarnings("unchecked")
142     public boolean quitUI() {
143         try {
144             QuitScreenAction action = new QuitScreenAction(this, false, SaveAction.class);
145             if (action.prepareAction()) {
146                 return true;
147             }
148         } catch (Exception e) {
149             LOG.error(e.getLocalizedMessage(), e);
150         }
151         return false;
152     }
153 
154     private void checkForReferentialUpdates() {
155         if (getContext().isNextImportSynchroCheckActionPrevented()) {
156             return;
157         }
158         if (getContext().getSynchroContext().isRunningStatus()) {
159             return;
160         }
161         getImportSynchroCheckAction().execute();
162         if (LOG.isDebugEnabled())
163             LOG.debug("checkForReferentialUpdates executed");
164     }
165 
166     private ImportSynchroCheckAction getImportSynchroCheckAction() {
167         if (importSynchroCheckAction == null) {
168             importSynchroCheckAction = getContext().getActionFactory().createNonBlockingUIAction(getContext().getSynchroHandler(), ImportSynchroCheckAction.class);
169         }
170         if (!importSynchroCheckAction.isConsumerSet()) {
171             importSynchroCheckAction.setConsumer(synchroUIContext -> {
172 
173                 if (LOG.isDebugEnabled())
174                     LOG.debug("check result: " + synchroUIContext.isImportReferential());
175 
176                 // If error occurs (eg. connection problem) set screen read-only (Mantis #48002)
177                 if (synchroUIContext.getStatus() == SynchroProgressionStatus.FAILED) {
178                     getModel().setSaveEnabled(false);
179                     getContext().getDialogHelper().showWarningDialog(t("reefdb.error.synchro.serverNotYetAvailable"));
180                 } else {
181                     getModel().setSaveEnabled(true);
182                     // get result
183                     if (synchroUIContext.isImportReferential()) {
184                         UpdateCampaignsAction updateCampaignsAction = getContext().getActionFactory().createLogicAction(this, UpdateCampaignsAction.class);
185                         getContext().getActionEngine().runAction(updateCampaignsAction);
186                     }
187                 }
188 
189                 // Reset correctly the synchro context and widget (Mantis #48832)
190                 getContext().getSynchroHandler().report(t("quadrige3.synchro.report.idle"), false);
191                 getContext().getSynchroContext().resetImportContext();
192                 getContext().getSynchroContext().saveImportContext(true, true);
193 
194             });
195         }
196         return importSynchroCheckAction;
197     }
198 }