View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling;
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.ui.swing.util.AbstractTuttiUIHandler;
26  import jaxx.runtime.SwingUtil;
27  import jaxx.runtime.validator.swing.SwingValidator;
28  
29  import javax.swing.JComponent;
30  import java.awt.event.KeyAdapter;
31  import java.awt.event.KeyEvent;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * @author Kevin Morin - kmorin@codelutin.com
37   * @since 4.5
38   */
39  public class MinSizePopupUIHandler extends AbstractTuttiUIHandler<MinSizePopupUIModel, MinSizePopupUI> {
40  
41      @Override
42      public void afterInit(MinSizePopupUI ui) {
43          initUI(ui);
44  
45          ui.getMinSizeField().getTextField().addKeyListener(new KeyAdapter() {
46  
47              @Override
48              public void keyReleased(KeyEvent e) {
49                  super.keyReleased(e);
50                  if (e.getKeyCode() == KeyEvent.VK_ENTER) {
51                      validate();
52                  }
53              }
54          });
55      }
56  
57      @Override
58      public void onCloseUI() {
59          getUI().dispose();
60      }
61  
62      @Override
63      public SwingValidator<MinSizePopupUIModel> getValidator() {
64          return getUI().getValidator();
65      }
66  
67      @Override
68      protected JComponent getComponentToFocus() {
69          return getUI().getMinSizeField();
70      }
71  
72      public void open(int minMinSize, Integer maxMinSize) {
73          getModel().setMinMinSize(minMinSize);
74          getModel().setMaxMinSize(maxMinSize);
75          getModel().setValid(false);
76  
77          String message;
78          if (maxMinSize != null) {
79              message = t("tutti.cpsEditor.dialog.minSize.message", minMinSize, maxMinSize);
80  
81          } else {
82              message = t("tutti.cpsEditor.dialog.minSize.message.infinite", minMinSize);
83          }
84          getUI().getMessage().setText(message);
85  
86          getUI().pack();
87          SwingUtil.center(getContext().getMainUI(), ui);
88          getUI().setVisible(true);
89      }
90  
91      public void validate() {
92          if (getValidator().isValid()) {
93              getModel().setValid(true);
94              onCloseUI();
95          }
96      }
97  
98      public void cancel() {
99          getModel().setMinSize(null);
100         onCloseUI();
101     }
102 }