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