View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.campaign;
2   
3   /*
4    * #%L
5    * Dali :: 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.dali.dto.data.survey.CampaignDTO;
27  import fr.ifremer.dali.ui.swing.action.QuitScreenAction;
28  import fr.ifremer.dali.ui.swing.content.manage.campaign.menu.CampaignsMenuUIModel;
29  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
30  import fr.ifremer.dali.ui.swing.util.DaliUIs;
31  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
32  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroCheckAction;
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.awt.Cursor;
41  import java.awt.event.MouseAdapter;
42  import java.awt.event.MouseEvent;
43  import java.util.Collection;
44  
45  import static org.nuiton.i18n.I18n.t;
46  
47  /**
48   * Controlleur pour l'onglet prelevements mesures.
49   */
50  public class CampaignsUIHandler extends AbstractDaliUIHandler<CampaignsUIModel, CampaignsUI> implements CloseableUI {
51  
52      /**
53       * Logger.
54       */
55      private static final Log LOG = LogFactory.getLog(CampaignsUIHandler.class);
56      private ImportSynchroCheckAction importSynchroCheckAction;
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public void beforeInit(final CampaignsUI ui) {
63          super.beforeInit(ui);
64  
65          // create model and register to the JAXX context
66          final CampaignsUIModel model = new CampaignsUIModel();
67          ui.setContextValue(model);
68  
69          ui.setContextValue(SwingUtil.createActionIcon("campaign"));
70  
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public void afterInit(final CampaignsUI ui) {
78          initUI(ui);
79  
80          ui.getMenuUI().getHandler().enableContextFilter(false);
81  
82          // Save models
83          getModel().setCampaignsTableUIModel(ui.getCampaignsTableUI().getModel());
84  
85          // sismer web page
86          ui.getSismerWebLink().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
87          ui.getSismerWebLink().addMouseListener(new MouseAdapter() {
88              @Override
89              public void mouseClicked(MouseEvent e) {
90                  if (e.getClickCount() == 1) {
91                      DaliUIs.openLink(getConfig().getSismerURL());
92                  }
93              }
94          });
95  
96          // init image
97          SwingUtil.setComponentWidth(ui.getLeftImage(), ui.getMenu().getPreferredSize().width * 9 / 10);
98          ui.getLeftImage().setScaled(true);
99  
100         initListeners();
101 
102         ui.applyDataBinding(CampaignsUI.BINDING_SAVE_BUTTON_ENABLED);
103 
104         // check referential update (Mantis #47231)
105         SwingUtilities.invokeLater(this::checkForReferentialUpdates);
106     }
107 
108     @SuppressWarnings("unchecked")
109     private void initListeners() {
110 
111         // listen to menu results
112         getUI().getMenuUI().getModel().addPropertyChangeListener(CampaignsMenuUIModel.PROPERTY_RESULTS, evt -> {
113 
114             // affect campaigns
115             getModel().getCampaignsTableUIModel().setBeans((Collection<CampaignDTO>) evt.getNewValue());
116 
117             getModel().getCampaignsTableUIModel().getRows().forEach(campaign -> {
118 
119                 // set rows editable if save is allowed
120                 campaign.setEditable(getModel().isSaveEnabled());
121 
122                 // set row read-only if user or recorder department is not the same as the current user
123                 campaign.setReadOnly(!(campaign.getManager().getId().equals(getContext().getDataContext().getRecorderPersonId())
124                         || campaign.getRecorderDepartment().getId().equals(getContext().getDataContext().getRecorderDepartmentId())));
125 
126             });
127 
128         });
129 
130         // Listen valid state
131         listenModelValid(getModel().getCampaignsTableUIModel());
132         listenModelModify(getModel().getCampaignsTableUIModel());
133 
134         // Add listener on saveEnabled property to disable change (Mantis #47532)
135         getModel().addPropertyChangeListener(CampaignsUIModel.PROPERTY_SAVE_ENABLED, evt -> getModel().getCampaignsTableUIModel().setSaveEnabled(getModel().isSaveEnabled()));
136 
137         // Register validator
138         registerValidators(getValidator());
139         listenValidatorValid(getValidator(), getModel());
140 
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public SwingValidator<CampaignsUIModel> getValidator() {
148         return getUI().getValidator();
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     @SuppressWarnings("unchecked")
156     public boolean quitUI() {
157         try {
158             QuitScreenAction action = new QuitScreenAction(this, false, SaveAction.class);
159             if (action.prepareAction()) {
160                 return true;
161             }
162         } catch (Exception e) {
163             LOG.error(e.getLocalizedMessage(), e);
164         }
165         return false;
166     }
167 
168     private void checkForReferentialUpdates() {
169         if (getContext().isNextImportSynchroCheckActionPrevented()) {
170             return;
171         }
172         if (getContext().getSynchroContext().isRunningStatus()) {
173             return;
174         }
175         getImportSynchroCheckAction().execute();
176         if (LOG.isDebugEnabled())
177             LOG.debug("checkForReferentialUpdates executed");
178     }
179 
180     private ImportSynchroCheckAction getImportSynchroCheckAction() {
181         if (importSynchroCheckAction == null) {
182             importSynchroCheckAction = getContext().getActionFactory().createNonBlockingUIAction(getContext().getSynchroHandler(), ImportSynchroCheckAction.class);
183         }
184         if (!importSynchroCheckAction.isConsumerSet()) {
185             importSynchroCheckAction.setConsumer(synchroUIContext -> {
186 
187                 if (LOG.isDebugEnabled())
188                     LOG.debug("check result: " + synchroUIContext.isImportReferential());
189 
190                 // If error occurs (eg. connection problem) set screen read-only (Mantis #47532)
191                 if (synchroUIContext.getStatus() == SynchroProgressionStatus.FAILED) {
192                     getModel().setSaveEnabled(false);
193                     getContext().getDialogHelper().showWarningDialog(t("dali.error.synchro.serverNotYetAvailable"));
194                 } else {
195                     getModel().setSaveEnabled(true);
196                     // get result
197                     if (synchroUIContext.isImportReferential()) {
198                         UpdateCampaignsAction updateCampaignsAction = getContext().getActionFactory().createLogicAction(this, UpdateCampaignsAction.class);
199                         getContext().getActionEngine().runAction(updateCampaignsAction);
200                     }
201                 }
202 
203                 // Reset correctly the synchro context and widget (Mantis #48842)
204                 getContext().getSynchroHandler().report(t("quadrige3.synchro.report.idle"), false);
205                 getContext().getSynchroContext().resetImportContext();
206                 getContext().getSynchroContext().saveImportContext(true, true);
207 
208             });
209         }
210         return importSynchroCheckAction;
211     }
212 }