View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.rule;
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.configuration.control.ControlRuleDTO;
27  import fr.ifremer.dali.dto.configuration.control.RuleListDTO;
28  import fr.ifremer.dali.ui.swing.action.QuitScreenAction;
29  import fr.ifremer.dali.ui.swing.content.manage.rule.menu.RulesMenuUIModel;
30  import fr.ifremer.dali.ui.swing.content.manage.rule.rulelist.RuleListRowModel;
31  import fr.ifremer.dali.ui.swing.util.AbstractDaliBeanUIModel;
32  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
33  import fr.ifremer.quadrige3.synchro.vo.SynchroProgressionStatus;
34  import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroCheckAction;
35  import jaxx.runtime.SwingUtil;
36  import jaxx.runtime.validator.swing.SwingValidator;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.nuiton.jaxx.application.swing.util.CloseableUI;
40  
41  import javax.swing.SwingUtilities;
42  import java.awt.Dimension;
43  import java.awt.event.ComponentAdapter;
44  import java.awt.event.ComponentEvent;
45  import java.beans.PropertyChangeListener;
46  import java.util.List;
47  
48  import static org.nuiton.i18n.I18n.t;
49  
50  /**
51   * Main handler of rule list management.
52   */
53  public class RulesUIHandler extends AbstractDaliUIHandler<RulesUIModel, RulesUI> implements CloseableUI {
54  
55      private static final Log LOG = LogFactory.getLog(RulesUIHandler.class);
56      private ImportSynchroCheckAction importSynchroCheckAction;
57  
58      /** {@inheritDoc} */
59      @Override
60      public void beforeInit(final RulesUI ui) {
61          super.beforeInit(ui);
62  
63          // create model and register to the JAXX context
64          RulesUIModel model = new RulesUIModel();
65          ui.setContextValue(model);
66  
67          ui.setContextValue(SwingUtil.createActionIcon("rule-list"));
68  
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public void afterInit(final RulesUI ui) {
74          initUI(ui);
75  
76          // Save models
77          getModel().setRuleListUIModel(getUI().getRuleListUI().getModel());
78          getModel().setProgramsUIModel(getUI().getControlProgramTableUI().getModel());
79          getModel().setDepartmentsUIModel(getUI().getControlDepartmentTableUI().getModel());
80          getModel().setControlRuleUIModel(getUI().getControlRuleTableUI().getModel());
81          getModel().setPmfmUIModel(getUI().getControlPmfmTableUI().getModel());
82  
83          // Clear context
84          getContext().clearObservationIds();
85  
86          disableControls();
87  
88          // Listen modify property and set dirty to the selected program
89          initListeners();
90  
91          registerValidators(getValidator());
92          listenValidatorValid(getValidator(), getModel());
93  
94          SwingUtilities.invokeLater(this::clearRuleList);
95  
96          // check referential update (Mantis #46969)
97          SwingUtilities.invokeLater(this::checkForReferentialUpdates);
98  
99      }
100 
101     private void initListeners() {
102 
103         // Listen search results
104         getUI().getRulesMenuUI().getModel().addPropertyChangeListener(RulesMenuUIModel.PROPERTY_RESULTS, evt ->
105             getUI().getRuleListUI().getHandler().loadRuleLists((List<RuleListDTO>) evt.getNewValue())
106         );
107 
108         // Listen modify property and set dirty to the selected rule list
109         listenModelModify(getModel().getRuleListUIModel());
110         PropertyChangeListener modifyListener = evt -> {
111 
112             Boolean modify = (Boolean) evt.getNewValue();
113             if (modify != null) {
114                 getModel().setModify(modify);
115 
116                 RuleListRowModel selectedRuleList = getModel().getRuleListUIModel().getSingleSelectedRow();
117                 if (selectedRuleList != null) {
118                     if (modify) {
119                         selectedRuleList.setDirty(true);
120                     }
121                     getUI().getRuleListUI().getHandler().recomputeRowValidState(selectedRuleList);
122                 }
123                 getValidator().doValidate();
124 
125             }
126         };
127         getModel().getProgramsUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, modifyListener);
128         getModel().getDepartmentsUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, modifyListener);
129         getModel().getControlRuleUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, modifyListener);
130         getModel().getPmfmUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, modifyListener);
131 
132         // listen pmfm changes and validate rule
133         getModel().getPmfmUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, evt -> {
134             if (getModel().getControlRuleUIModel().getSingleSelectedRow() != null)
135                 getUI().getControlRuleTableUI().getHandler().recomputeRowValidState(getModel().getControlRuleUIModel().getSingleSelectedRow());
136         });
137 
138         // Listen valid state
139         listenModelValid(getModel().getRuleListUIModel());
140 
141         PropertyChangeListener validListener = evt -> {
142             Boolean valid = (Boolean) evt.getNewValue();
143             if (valid != null) {
144                 getModel().setValid(valid);
145                 forceRevalidateModel();
146             }
147         };
148 
149         getModel().getProgramsUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_VALID, validListener);
150         getModel().getDepartmentsUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_VALID, validListener);
151         getModel().getControlRuleUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_VALID, validListener);
152         getModel().getPmfmUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_VALID, validListener);
153 
154         getUI().addComponentListener(new ComponentAdapter() {
155             @Override
156             public void componentResized(ComponentEvent e) {
157                 int h = getUI().getHeight()
158                         - getUI().getTopPanel().getPreferredSize().height
159                         - getUI().getBottomPanel().getPreferredSize().height - 30;
160                 h = Math.max(h, getUI().getRuleListUI().getPreferredSize().height);
161                 Dimension d = new Dimension(getUI().getControlRuleTableUI().getPreferredSize().width, h);
162                 getUI().getControlRuleTableUI().setPreferredSize(d);
163                 getUI().getControlRuleTableUI().invalidate();
164             }
165         });
166 
167         // Add listener on saveEnabled property to disable change (Mantis #47532)
168         getModel().addPropertyChangeListener(RulesUIModel.PROPERTY_SAVE_ENABLED, evt -> getModel().getRuleListUIModel().setSaveEnabled(getModel().isSaveEnabled()));
169 
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public SwingValidator<RulesUIModel> getValidator() {
175         return getUI().getValidator();
176     }
177 
178     /**
179      * <p>desactiverBoutton.</p>
180      */
181     private void disableControls() {
182 
183         // Disable all controls
184         getUI().getControlProgramTableUI().getAddProgramButton().setEnabled(false);
185         getUI().getControlDepartmentTableUI().getAddDepartmentButton().setEnabled(false);
186         getUI().getControlRuleTableUI().getAddControlRuleButton().setEnabled(false);
187         getUI().getControlPmfmTableUI().getAddPmfmButton().setEnabled(false);
188         getUI().getRuleMessageEditor().setEnabled(false);
189         getUI().getRuleDescriptionEditor().setEnabled(false);
190     }
191 
192     private void clearRuleList() {
193 
194         getUI().getRuleListUI().getHandler().loadRuleLists(null);
195         clearChildrenList();
196     }
197 
198     private void clearChildrenList() {
199 
200         getUI().getControlProgramTableUI().getHandler().clearTable();
201         getUI().getControlDepartmentTableUI().getHandler().clearTable();
202         getUI().getControlRuleTableUI().getHandler().clearTable();
203         getUI().getControlPmfmTableUI().getHandler().clearTable();
204         getUI().getControlRuleTableUI().getHandler().clearControlRuleInformation();
205     }
206 
207 
208 
209     /** {@inheritDoc} */
210     @Override
211     @SuppressWarnings("unchecked")
212     public boolean quitUI() {
213         try {
214             QuitScreenAction action = new QuitScreenAction(this, false, SaveAction.class);
215             if (action.prepareAction()) {
216                 return true;
217             }
218         } catch (Exception e) {
219             LOG.error(e.getLocalizedMessage(), e);
220         }
221         return false;
222     }
223 
224     /**
225      * <p>setRuleErrorMessage.</p>
226      */
227     public void setRuleErrorMessage() {
228         ControlRuleDTO selectedRule = getModel().getControlRuleUIModel().getSingleSelectedRow();
229         selectedRule.setMessage(getUI().getRuleMessageEditor().getText());
230     }
231 
232     /**
233      * <p>setRuleDescription.</p>
234      */
235     public void setRuleDescription() {
236         ControlRuleDTO selectedRule = getModel().getControlRuleUIModel().getSingleSelectedRow();
237         selectedRule.setDescription(getUI().getRuleDescriptionEditor().getText());
238     }
239 
240     private void checkForReferentialUpdates() {
241         if (getContext().isNextImportSynchroCheckActionPrevented()) {
242             return;
243         }
244         if (getContext().getSynchroContext().isRunningStatus()) {
245             return;
246         }
247         getImportSynchroCheckAction().execute();
248         if (LOG.isDebugEnabled())
249             LOG.debug("checkForReferentialUpdates executed");
250     }
251 
252     private ImportSynchroCheckAction getImportSynchroCheckAction() {
253         if (importSynchroCheckAction == null) {
254             importSynchroCheckAction = getContext().getActionFactory().createNonBlockingUIAction(getContext().getSynchroHandler(), ImportSynchroCheckAction.class);
255         }
256         if (!importSynchroCheckAction.isConsumerSet()) {
257             importSynchroCheckAction.setConsumer(synchroUIContext -> {
258 
259                 if (LOG.isDebugEnabled())
260                     LOG.debug("check result: " + synchroUIContext.isImportReferential());
261 
262                 // If error occurs (eg. connection problem) set screen read-only (Mantis #47532)
263                 if (synchroUIContext.getStatus() == SynchroProgressionStatus.FAILED) {
264                     getModel().setSaveEnabled(false);
265                     getContext().getDialogHelper().showWarningDialog(t("dali.error.synchro.serverNotYetAvailable"));
266                 } else {
267                     getModel().setSaveEnabled(true);
268                     // get result
269                     if (synchroUIContext.isImportReferential()) {
270                         UpdateRulesAction updateRulesAction = getContext().getActionFactory().createLogicAction(this, UpdateRulesAction.class);
271                         getContext().getActionEngine().runAction(updateRulesAction);
272                     }
273                 }
274 
275                 // Reset correctly the synchro context and widget (Mantis #48842)
276                 getContext().getSynchroHandler().report(t("quadrige3.synchro.report.idle"), false);
277                 getContext().getSynchroContext().resetImportContext();
278                 getContext().getSynchroContext().saveImportContext(true, true);
279 
280             });
281         }
282         return importSynchroCheckAction;
283     }
284 }