View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.taxon.taxonsDialog;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.reefdb.dto.referential.TaxonDTO;
27  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
28  import org.nuiton.jaxx.application.swing.util.Cancelable;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Handler.
35   */
36  public class TaxonsDialogUIHandler extends AbstractReefDbUIHandler<TaxonsDialogUIModel, TaxonsDialogUI>  implements Cancelable {
37  
38      /** Constant <code>FILTER="filter"</code> */
39      public static final String FILTER = "filter";
40      /** Constant <code>TABLE="table"</code> */
41      public static final String TABLE = "table";
42  
43      /** {@inheritDoc} */
44      @Override
45      public void beforeInit(TaxonsDialogUI ui) {
46          super.beforeInit(ui);
47  
48          ui.setContextValue(new TaxonsDialogUIModel());
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public void afterInit(TaxonsDialogUI ui) {
54          initUI(ui);
55  
56          getModel().addPropertyChangeListener(evt -> {
57  
58              if (TaxonsDialogUIModel.PROPERTY_TAXON_GROUP.equals(evt.getPropertyName())) {
59  
60                  List<TaxonDTO> taxons = getModel().getTaxonGroup() != null ? new ArrayList<>(getModel().getTaxonGroup().getTaxons()) : null;
61                  getUI().getTaxonsTable().getModel().setBeans(taxons);
62                  getUI().getTaxonsFilter().getHandler().loadSelectedElements(taxons);
63  
64              } else if (TaxonsDialogUIModel.PROPERTY_TAXONS.equals(evt.getPropertyName())) {
65  
66                  getUI().getTaxonsTable().getModel().setBeans(getModel().getTaxons());
67                  getUI().getTaxonsFilter().getHandler().loadSelectedElements(getModel().getTaxons());
68  
69              } else if (TaxonsDialogUIModel.PROPERTY_EDITABLE.equals(evt.getPropertyName())) {
70  
71                  getUI().getListPanelLayout().setSelected(getModel().isEditable() ? FILTER : TABLE);
72              }
73          });
74  
75          // hide menu
76          ui.getTaxonsTable().getTaxonsNationalMenuUI().setVisible(false);
77      }
78  
79      /**
80       * <p>valid.</p>
81       */
82      public void valid() {
83  
84          // set selected taxon in taxon group
85          if (getModel().isEditable() && getModel().getTaxonGroup() != null) {
86              getModel().getTaxonGroup().setTaxons(new ArrayList<>(getUI().getTaxonsFilter().getModel().getElements()));
87          }
88  
89          // close the dialog box
90          closeDialog();
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public void cancel() {
96          closeDialog();
97      }
98  }