View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.catches;
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.type.WeightUnit;
26  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
27  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
28  import jaxx.runtime.JAXXUtil;
29  import jaxx.runtime.SwingUtil;
30  import jaxx.runtime.validator.swing.SwingValidator;
31  
32  import javax.swing.JComponent;
33  import javax.swing.JRootPane;
34  import javax.swing.KeyStroke;
35  import java.awt.Component;
36  import java.awt.event.KeyEvent;
37  import java.awt.event.WindowAdapter;
38  import java.awt.event.WindowEvent;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Created on 2/8/14.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 3.2
47   */
48  public class EnterWeightUIHandler extends AbstractTuttiUIHandler<TuttiUIContext, EnterWeightUI> {
49  
50      public static final String CANCEL_ACTION = "cancelAction";
51  
52      public static final String VALIDATE_ACTION = "validateAction";
53  
54      @Override
55      public void onCloseUI() {
56      }
57  
58      @Override
59      public SwingValidator<TuttiUIContext> getValidator() {
60          return null;
61      }
62  
63      @Override
64      protected JComponent getComponentToFocus() {
65          return null;
66      }
67  
68      @Override
69      public void afterInit(EnterWeightUI ui) {
70  
71          initUI(ui);
72  
73          JRootPane rootPane = ui.getRootPane();
74  
75          // add a auto-close action
76  
77          rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), VALIDATE_ACTION);
78          rootPane.getActionMap().put(VALIDATE_ACTION, ui.getValidateButton().getAction());
79  
80          rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_ACTION);
81          rootPane.getActionMap().put(CANCEL_ACTION, ui.getCancelButton().getAction());
82  
83  
84          ui.addWindowListener(new WindowAdapter() {
85  
86              @Override
87              public void windowClosed(WindowEvent e) {
88                  Component ui = (Component) e.getSource();
89                  JAXXUtil.destroy(ui);
90              }
91          });
92  
93          initUI(ui);
94  
95  
96      }
97  
98      protected Float openAndGetWeightValue(String weightLabel, Float weight, WeightUnit weightUnit) {
99          ui.setWeightLabel(weightLabel);
100         ui.setOriginalWeight(weight);
101         ui.setWeightUnit(weightUnit);
102         SwingUtil.center(getContext().getMainUI(), ui);
103         ui.pack();
104         ui.getEditor().requestFocusInWindow();
105         ui.setVisible(true);
106         Number enteredWeight = ui.getEditor().getModel().getNumberValue();
107         return enteredWeight == null ? null : enteredWeight.floatValue();
108 
109     }
110 
111     protected String getTitle(String weightLabel, WeightUnit weightUnit) {
112         return weightUnit.decorateLabel(t("tutti.catches.enterWeight.title", weightLabel));
113     }
114 
115 }