View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.individualobservation;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import fr.ifremer.tutti.service.sampling.SamplingCodePrefix;
26  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
27  import jaxx.runtime.SwingUtil;
28  import jaxx.runtime.validator.swing.SwingValidator;
29  
30  import javax.swing.JComponent;
31  import java.awt.event.KeyAdapter;
32  import java.awt.event.KeyEvent;
33  
34  /**
35   * @author Kevin Morin - kmorin@codelutin.com
36   * @since 4.5
37   */
38  public class SampleCodeEditionPopupUIHandler extends AbstractTuttiUIHandler<SampleCodeEditionPopupUIModel, SampleCodeEditionPopupUI> {
39  
40      @Override
41      public void afterInit(SampleCodeEditionPopupUI ui) {
42          initUI(ui);
43  
44          ui.getSampleCodeField().getTextField().addKeyListener(new KeyAdapter() {
45  
46              @Override
47              public void keyReleased(KeyEvent e) {
48                  super.keyReleased(e);
49                  if (e.getKeyCode() == KeyEvent.VK_ENTER) {
50                      validate();
51                  }
52              }
53          });
54      }
55  
56      @Override
57      public void onCloseUI() {
58          getUI().dispose();
59      }
60  
61      @Override
62      public SwingValidator<SampleCodeEditionPopupUIModel> getValidator() {
63          return ui.getValidator();
64      }
65  
66      @Override
67      protected JComponent getComponentToFocus() {
68          return getUI().getSampleCodeField();
69      }
70  
71      public void open(SamplingCodePrefix prefix, Integer sampleCode) {
72          getModel().setSampleCodePrefix(prefix);
73          getModel().setSampleCode(sampleCode);
74          getModel().setValid(false);
75          getUI().pack();
76          SwingUtil.center(getContext().getMainUI(), ui);
77          getUI().setVisible(true);
78      }
79  
80      public void validate() {
81          if (getValidator().isValid()) {
82              getModel().setValid(true);
83              onCloseUI();
84          }
85      }
86  
87      public void cancel() {
88          getModel().setSampleCode(null);
89          onCloseUI();
90      }
91  }