View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.referential.user.privileges;
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.PrivilegeDTO;
28  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
29  import org.nuiton.jaxx.application.swing.util.Cancelable;
30  
31  import java.util.List;
32  
33  /**
34   * Handler.
35   */
36  public class PrivilegesDialogUIHandler extends AbstractDaliUIHandler<PrivilegesDialogUIModel, PrivilegesDialogUI> implements Cancelable {
37  
38      /** Constant <code>DOUBLE_LIST="doubleList"</code> */
39      public static final String DOUBLE_LIST = "doubleList";
40      /** Constant <code>LIST="list"</code> */
41      public static final String LIST = "list";
42  
43      /** {@inheritDoc} */
44      @Override
45      public void beforeInit(PrivilegesDialogUI ui) {
46          super.beforeInit(ui);
47  
48          PrivilegesDialogUIModel model = new PrivilegesDialogUIModel();
49          ui.setContextValue(model);
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      @SuppressWarnings("unchecked")
55      public void afterInit(final PrivilegesDialogUI ui) {
56          initUI(ui);
57  
58          initBeanList(getUI().getPrivilegesDoubleList(), getContext().getUserService().getAvailablePrivileges(), null);
59          getUI().getPrivilegesList().setCellRenderer(newListCellRender(PrivilegeDTO.class));
60  
61          getModel().addPropertyChangeListener(PrivilegesDialogUIModel.PROPERTY_USER, evt -> {
62  
63              // update list & double list (as a copy)
64              List<PrivilegeDTO> privileges = Lists.newArrayList(getModel().getUser().getPrivilege());
65              getUI().getPrivilegesDoubleList().getModel().setSelected(privileges);
66              getUI().getPrivilegesList().setListData(privileges.toArray(new PrivilegeDTO[privileges.size()]));
67          });
68  
69          getModel().addPropertyChangeListener(PrivilegesDialogUIModel.PROPERTY_EDITABLE, evt -> {
70  
71              // select list only if model is not editable
72              if (getModel().isEditable()) {
73                  getUI().getListPanelLayout().setSelected(DOUBLE_LIST);
74              } else {
75                  getUI().getListPanelLayout().setSelected(LIST);
76              }
77  
78          });
79      }
80  
81      /**
82       * <p>valid.</p>
83       */
84      public void valid() {
85  
86          // set privilege to user
87          if (getModel().isEditable()) {
88              getModel().getUser().setPrivilege(getUI().getPrivilegesDoubleList().getModel().getSelected());
89          }
90  
91          // close the dialog box
92          closeDialog();
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public void cancel() {
98          closeDialog();
99      }
100 }