View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.program.programs.departments;
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 com.google.common.collect.Lists;
27  import fr.ifremer.reefdb.dto.referential.DepartmentDTO;
28  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
29  import org.nuiton.jaxx.application.swing.util.Cancelable;
30  
31  import java.beans.PropertyChangeEvent;
32  import java.beans.PropertyChangeListener;
33  import java.util.Collection;
34  import java.util.List;
35  
36  import static org.nuiton.i18n.I18n.t;
37  
38  /**
39   * Handler.
40   */
41  public class DepartmentsDialogUIHandler extends AbstractReefDbUIHandler<DepartmentsDialogUIModel, DepartmentsDialogUI> implements Cancelable {
42  
43      /** Constant <code>DOUBLE_LIST="doubleList"</code> */
44      public static final String DOUBLE_LIST = "doubleList";
45      /** Constant <code>LIST="list"</code> */
46      public static final String LIST = "list";
47  
48      /** {@inheritDoc} */
49      @Override
50      public void beforeInit(DepartmentsDialogUI ui) {
51          super.beforeInit(ui);
52  
53          DepartmentsDialogUIModel model = new DepartmentsDialogUIModel();
54          ui.setContextValue(model);
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      @SuppressWarnings("unchecked")
60      public void afterInit(final DepartmentsDialogUI ui) {
61          initUI(ui);
62  
63          getUI().getDepartmentsList().setCellRenderer(newListCellRender(DepartmentDTO.class));
64  
65          getModel().addPropertyChangeListener(DepartmentsDialogUIModel.PROPERTY_PROGRAM, evt -> {
66  
67              if (getModel().getProgram() == null) {
68                  return;
69              }
70  
71              getUI().setTitle(t("reefdb.program.program.recorderDepartments.title", decorate(getModel().getProgram())));
72  
73              Collection<DepartmentDTO> departments = getModel().getProgram().getRecorderDepartments();
74  
75              if (getModel().getProgram().isEditable()) {
76                  getUI().getListPanelLayout().setSelected(DOUBLE_LIST);
77  
78                  // update filterUI
79                  getUI().getFilterElementDepartmentUI().getFilterDoubleList().getModel().setSelected(Lists.newArrayList(departments));
80                  getUI().getFilterElementDepartmentUI().getHandler().enable();
81  
82                  if (!getModel().getProgram().isLocal()) {
83                      // if program is national, department menu must search only national departments
84                      getUI().getFilterElementDepartmentUI().getHandler().forceLocal(false);
85                  }
86  
87              } else {
88                  getUI().getListPanelLayout().setSelected(LIST);
89  
90                  getUI().getDepartmentsList().setListData(departments.toArray(new DepartmentDTO[departments.size()]));
91              }
92  
93          });
94  
95      }
96  
97      /**
98       * <p>valid.</p>
99       */
100     public void valid() {
101 
102         // set privilege to user
103         if (getModel().getProgram().isEditable()) {
104 
105             List<DepartmentDTO> departments = getUI().getFilterElementDepartmentUI().getModel().getElements();
106             getModel().getProgram().setRecorderDepartments(departments);
107         }
108 
109         // close the dialog box
110         closeDialog();
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public void cancel() {
116         closeDialog();
117     }
118 }