1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.tethys.swing.field;
18
19 import io.github.tonywasher.joceanus.oceanus.event.OceanusEvent;
20 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
21 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
22 import io.github.tonywasher.joceanus.tethys.api.field.TethysUIDataEditField.TethysUIScrollButtonField;
23 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
24 import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
25 import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
26 import io.github.tonywasher.joceanus.tethys.swing.button.TethysUISwingScrollButtonManager;
27 import io.github.tonywasher.joceanus.tethys.swing.menu.TethysUISwingScrollMenu;
28
29 import javax.swing.JButton;
30 import javax.swing.JLabel;
31 import javax.swing.SwingConstants;
32 import java.awt.Rectangle;
33 import java.util.function.Consumer;
34
35
36
37
38
39
40 public final class TethysUISwingScrollButtonField<T>
41 extends TethysUISwingDataTextField<T>
42 implements TethysUIScrollButtonField<T> {
43
44
45
46 private final TethysUISwingScrollButtonManager<T> theManager;
47
48
49
50
51 private Consumer<TethysUIScrollMenu<T>> theConfigurator;
52
53
54
55
56 private final JButton theButton;
57
58
59
60
61 private boolean isCellEditing;
62
63
64
65
66
67
68
69 TethysUISwingScrollButtonField(final TethysUICoreFactory<?> pFactory,
70 final Class<T> pClazz) {
71 this(pFactory, pClazz, new JLabel());
72 }
73
74
75
76
77
78
79
80
81 private TethysUISwingScrollButtonField(final TethysUICoreFactory<?> pFactory,
82 final Class<T> pClazz,
83 final JLabel pLabel) {
84 this(pFactory, pFactory.buttonFactory().newScrollButton(pClazz), pLabel);
85 }
86
87
88
89
90
91
92
93
94 private TethysUISwingScrollButtonField(final TethysUICoreFactory<?> pFactory,
95 final TethysUIScrollButtonManager<T> pManager,
96 final JLabel pLabel) {
97
98 super(pFactory, TethysUISwingNode.getComponent(pManager), pLabel);
99
100
101 theManager = (TethysUISwingScrollButtonManager<T>) pManager;
102 theButton = getEditControl();
103
104
105 pManager.getEventRegistrar().addEventListener(this::handleEvent);
106 theManager.getMenu().getEventRegistrar().addEventListener(TethysUIEvent.WINDOWCLOSED, e -> haltCellEditing());
107
108
109 theConfigurator = p -> {
110 };
111 }
112
113 @Override
114 public T getCastValue(final Object pValue) {
115 return theManager.getValueClass().cast(pValue);
116 }
117
118
119
120
121
122
123 private void handleEvent(final OceanusEvent<TethysUIEvent> pEvent) {
124 switch (pEvent.getEventId()) {
125 case NEWVALUE:
126 setValue(theManager.getValue());
127 fireEvent(TethysUIEvent.NEWVALUE, pEvent.getDetails());
128 break;
129 case EDITFOCUSLOST:
130 haltCellEditing();
131 break;
132 default:
133 break;
134 }
135 }
136
137 @Override
138 public void setMenuConfigurator(final Consumer<TethysUIScrollMenu<T>> pConfigurator) {
139 theConfigurator = pConfigurator;
140 theManager.setMenuConfigurator(theConfigurator);
141 }
142
143 @Override
144 public JButton getEditControl() {
145 return (JButton) super.getEditControl();
146 }
147
148 @Override
149 public void setValue(final T pValue) {
150
151 super.setValue(pValue);
152
153
154 theManager.setValue(pValue);
155 getLabel().setText(theButton.getText());
156 }
157
158 @Override
159 public void startCellEditing(final Rectangle pCell) {
160 isCellEditing = true;
161 final TethysUISwingScrollMenu<T> myMenu = theManager.getMenu();
162 theConfigurator.accept(myMenu);
163 if (!myMenu.isEmpty()) {
164 myMenu.showMenuAtPosition(pCell, SwingConstants.BOTTOM);
165 } else {
166 haltCellEditing();
167 }
168 }
169
170
171
172
173 private void haltCellEditing() {
174 if (isCellEditing) {
175 fireEvent(TethysUIEvent.EDITFOCUSLOST, this);
176 }
177 isCellEditing = false;
178 }
179
180 @Override
181 public TethysUISwingScrollButtonField<T> cloneField(final JLabel pLabel) {
182 return new TethysUISwingScrollButtonField<>(getGuiFactory(), theManager.getValueClass(), pLabel);
183 }
184 }