1 package fr.ifremer.tutti.ui.swing.util.catches;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
44
45
46
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
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 }