View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.species;
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 java.awt.Component;
34  import java.awt.event.WindowAdapter;
35  import java.awt.event.WindowEvent;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Created on 2/8/14.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.2
44   */
45  public class EnterMelagWeightUIHandler extends AbstractTuttiUIHandler<TuttiUIContext, EnterMelagWeightUI> {
46  
47      @Override
48      public void onCloseUI() {
49      }
50  
51      @Override
52      public SwingValidator<TuttiUIContext> getValidator() {
53          return null;
54      }
55  
56      @Override
57      protected JComponent getComponentToFocus() {
58          return null;
59      }
60  
61      @Override
62      public void afterInit(EnterMelagWeightUI ui) {
63  
64          // add a auto-close action
65          ui.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
66                  getConfig().getShortcutClosePopup(), "close");
67  
68          ui.addWindowListener(new WindowAdapter() {
69  
70              @Override
71              public void windowClosed(WindowEvent e) {
72                  Component ui = (Component) e.getSource();
73                  JAXXUtil.destroy(ui);
74              }
75          });
76          initUI(ui);
77  
78  
79      }
80  
81      protected Float openAndGetWeightValue(WeightUnit weightUnit) {
82  
83          ui.setWeightUnit(weightUnit);
84          SwingUtil.center(getContext().getMainUI(), ui);
85          ui.pack();
86          ui.getEditor().requestFocusInWindow();
87          ui.setVisible(true);
88          Number weight = ui.getEditor().getModel().getNumberValue();
89          return weight == null ? null : weight.floatValue();
90  
91      }
92  
93      protected String getTile(WeightUnit weightUnit) {
94          return t("tutti.createSpeciesMelag.title", weightUnit.getShortLabel());
95      }
96  
97      protected String getMessage(WeightUnit weightUnit) {
98          return t("tutti.createSpeciesMelag.message", weightUnit.getShortLabel());
99      }
100 }