View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context;
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.context.ContextDTO;
27  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
28  import fr.ifremer.dali.ui.swing.action.QuitScreenAction;
29  import fr.ifremer.dali.ui.swing.content.manage.context.contextslist.ManageContextsListTableUIRowModel;
30  import fr.ifremer.dali.ui.swing.content.manage.context.menu.ManageContextsListMenuUIModel;
31  import fr.ifremer.dali.ui.swing.util.AbstractDaliBeanUIModel;
32  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
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  /**
43   * <p>ManageContextsUIHandler class.</p>
44   *
45   */
46  public class ManageContextsUIHandler extends AbstractDaliUIHandler<ManageContextsUIModel, ManageContextsUI> implements CloseableUI {
47  
48      /**
49       * Logger.
50       */
51      private static final Log LOG = LogFactory.getLog(ManageContextsUIHandler.class);
52  
53      /** {@inheritDoc} */
54      @Override
55      public void beforeInit(final ManageContextsUI ui) {
56          super.beforeInit(ui);
57  
58          // create model and register to the JAXX context
59          final ManageContextsUIModel model = new ManageContextsUIModel();
60          ui.setContextValue(model);
61  
62          ui.setContextValue(SwingUtil.createActionIcon("config"));
63  
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public void afterInit(final ManageContextsUI ui) {
69          initUI(ui);
70  
71          // Affect models
72          getModel().setContextListModel(getUI().getManageContextsListUI().getModel());
73          getModel().setFilterListModel(getUI().getManageFiltersListUI().getModel());
74  
75          // Initialiser les parametres des ecrans Observation et prelevemnts
76          getContext().clearObservationIds();
77  
78          // Binding save button
79          getUI().applyDataBinding(ManageContextsUI.BINDING_SAVE_BUTTON_ENABLED);
80  
81          initListeners();
82  
83          // reset model modify
84          getModel().setModify(false);
85  
86      }
87  
88      private void initListeners() {
89  
90          // Listen modify
91          listenModelModify(getModel().getContextListModel());
92          getModel().getFilterListModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, evt -> {
93              Boolean modify = (Boolean) evt.getNewValue();
94              if (modify != null) {
95                  getModel().setModify(modify);
96                  if (modify && getModel().getContextListModel().getSingleSelectedRow() != null) {
97                      getModel().getContextListModel().getSingleSelectedRow().setDirty(true);
98                  }
99              }
100         });
101 
102         // Listen valid
103         listenModelValid(getModel().getContextListModel());
104         listenModelValid(getModel().getFilterListModel());
105 
106         // Validator
107         registerValidators(getValidator());
108         listenValidatorValid(getValidator(), getModel());
109 
110 
111         // listen to search result
112         getUI().getManageContextsListMenuUI().getModel().addPropertyChangeListener(ManageContextsListMenuUIModel.PROPERTY_RESULTS, evt -> {
113 
114             getModel().getContextListModel().setBeans((Collection<ContextDTO>) evt.getNewValue());
115 
116             getUI().getManageFiltersListUI().getHandler().disable();
117             getUI().getManageFiltersListUI().getHandler().clearTable();
118             getUI().getManageFilterContentUI().getHandler().clearTable();
119 
120             // Auto select if unique row
121             if (getModel().getContextListModel().getRowCount() == 1) {
122                 ManageContextsListTableUIRowModel rowModel = getModel().getContextListModel().getRows().get(0);
123                 SwingUtilities.invokeLater(() -> {
124                     getUI().getManageContextsListUI().getHandler().selectRow(rowModel);
125                     getModel().getContextListModel().setSingleSelectedRow(rowModel);
126                 });
127             }
128         });
129     }
130 
131 
132     /** {@inheritDoc} */
133     @Override
134     public SwingValidator<ManageContextsUIModel> getValidator() {
135         return getUI().getValidator();
136     }
137 
138     /**
139      * <p>loadLocalContext.</p>
140      *
141      * @param context a {@link fr.ifremer.dali.dto.configuration.context.ContextDTO} object.
142      */
143     public void loadLocalContext(final ContextDTO context) {
144         getUI().getManageFilterContentUI().getHandler().clearTable();
145         getUI().getManageFiltersListUI().getHandler().loadContext(context);
146     }
147 
148     /**
149      * <p>loadFilterElements.</p>
150      *
151      * @param filter a {@link fr.ifremer.dali.dto.configuration.filter.FilterDTO} object.
152      */
153     public void loadFilterElements(FilterDTO filter) {
154         if (filter == null) {
155             getUI().getManageFilterContentUI().getHandler().clearTable();
156         } else {
157             getUI().getManageFilterContentUI().getHandler().loadFilterElements(filter.getId());
158         }
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     @SuppressWarnings("unchecked")
164     public boolean quitUI() {
165         try {
166             QuitScreenAction action = new QuitScreenAction(this, false, SaveAction.class);
167             if (action.prepareAction()) {
168                 return true;
169             }
170         } catch (Exception e) {
171             LOG.error(e.getLocalizedMessage(), e);
172         }
173         return false;
174     }
175 }