View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.program.strategies.duplicate;
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.programStrategy.ProgramDTO;
27  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
28  import org.nuiton.jaxx.application.swing.util.Cancelable;
29  
30  import javax.swing.JComponent;
31  import java.awt.event.WindowAdapter;
32  import java.awt.event.WindowEvent;
33  import java.util.List;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * <p>SelectProgramUIHandler class.</p>
39   *
40   */
41  public class SelectProgramUIHandler extends AbstractDaliUIHandler<SelectProgramUIModel, SelectProgramUI> implements Cancelable {
42  
43      /** {@inheritDoc} */
44      @Override
45      public void beforeInit(final SelectProgramUI ui) {
46          super.beforeInit(ui);
47  
48          // create model and register to the JAXX context
49          final SelectProgramUIModel model = new SelectProgramUIModel();
50          ui.setContextValue(model);
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public void afterInit(final SelectProgramUI ui) {
56          initUI(ui);
57  
58          // listener on default close action = cancel
59          ui.addWindowListener(new WindowAdapter() {
60              @Override
61              public void windowClosing(WindowEvent e) {
62                  getModel().setTargetProgram(null);
63              }
64          });
65  
66          getModel().addPropertyChangeListener(evt -> {
67  
68              if (SelectProgramUIModel.PROPERTY_SOURCE_PROGRAM.equals(evt.getPropertyName())) {
69                  initBeanFilterableComboBox(getUI().getProgramCombo(), getAllowedPrograms(), getModel().getSourceProgram());
70                  updateMessageLabel();
71              } else if (SelectProgramUIModel.PROPERTY_SOURCE_STRATEGY.equals(evt.getPropertyName())) {
72                  updateMessageLabel();
73              }
74          });
75  
76      }
77  
78      private List<ProgramDTO> getAllowedPrograms() {
79  
80          Integer userId = getContext().getDataContext().getRecorderPersonId();
81          if (userId == null) {
82              return null; // not connected
83          }
84  
85          // return programs where current user is manager,
86          return getContext().getProgramStrategyService().getManagedProgramsByUser(userId);
87      }
88  
89      private void updateMessageLabel() {
90          if (getModel().getSourceProgram() != null && getModel().getSourceStrategy() != null) {
91              getUI().getMessageLabel().setText(t("dali.action.duplicate.strategy.selectProgram.message",
92                      decorate(getModel().getSourceStrategy()), decorate(getModel().getSourceProgram())));
93          }
94      }
95  
96      /**
97       * <p>valid.</p>
98       */
99      public void valid() {
100         closeDialog();
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public void cancel() {
106         getModel().setTargetProgram(null);
107         closeDialog();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     protected JComponent getComponentToFocus() {
113         return getUI().getProgramCombo();
114     }
115 
116 }