1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.metis.viewer;
18
19 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
20 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
21 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIIconId;
22 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButton;
23 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButtonFactory;
24 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
25 import io.github.tonywasher.joceanus.tethys.api.control.TethysUIControlFactory;
26 import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
27 import io.github.tonywasher.joceanus.tethys.api.control.TethysUISlider;
28 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
29 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
30 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBorderPaneManager;
31 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
32 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIPaneFactory;
33
34
35
36
37 public class MetisViewerControl
38 implements TethysUIComponent {
39
40
41
42 private static final String TEXT_ITEM = MetisViewerResource.VIEWER_SELECT_ITEM.getValue();
43
44
45
46
47 private static final String TEXT_OF = MetisViewerResource.VIEWER_SELECT_OF.getValue();
48
49
50
51
52 private static final char CHAR_BLANK = ' ';
53
54
55
56
57 private static final int ICON_SIZE = 24;
58
59
60
61
62 private static final int HGAP_SIZE = 10;
63
64
65
66
67 private final TethysUIBorderPaneManager thePane;
68
69
70
71
72 private final MetisViewerWindow theViewer;
73
74
75
76
77 private final TethysUIButton theParentButton;
78
79
80
81
82 private final TethysUIScrollButtonManager<MetisViewerMode> theModeButton;
83
84
85
86
87 private final TethysUIButton theNextButton;
88
89
90
91
92 private final TethysUIButton thePrevButton;
93
94
95
96
97 private final TethysUILabel theLabel;
98
99
100
101
102 private final TethysUISlider theSlider;
103
104
105
106
107 private final TethysUIBorderPaneManager theSliderPane;
108
109
110
111
112 private final TethysUIBoxPaneManager theButtonsPane;
113
114
115
116
117
118
119
120 protected MetisViewerControl(final TethysUIFactory<?> pFactory,
121 final MetisViewerWindow pViewer) {
122
123 theViewer = pViewer;
124
125
126 final TethysUIButtonFactory<?> myButtons = pFactory.buttonFactory();
127 theParentButton = myButtons.newButton();
128 configureButton(theParentButton, MetisViewerIcon.PARENT);
129 theParentButton.getEventRegistrar().addEventListener(e -> theViewer.handleParentPage());
130
131
132 theModeButton = myButtons.newScrollButton(MetisViewerMode.class);
133 theModeButton.getEventRegistrar().addEventListener(TethysUIEvent.NEWVALUE, e -> theViewer.handleMode(theModeButton.getValue()));
134
135
136 theNextButton = myButtons.newButton();
137 configureButton(theNextButton, MetisViewerIcon.NEXT);
138 theNextButton.getEventRegistrar().addEventListener(e -> theViewer.handleNextPage());
139
140
141 thePrevButton = myButtons.newButton();
142 configureButton(thePrevButton, MetisViewerIcon.PREV);
143 thePrevButton.getEventRegistrar().addEventListener(e -> theViewer.handlePreviousPage());
144
145
146 final TethysUIControlFactory myControls = pFactory.controlFactory();
147 theLabel = myControls.newLabel();
148
149
150 theSlider = myControls.newSlider();
151 theSlider.getEventRegistrar().addEventListener(e -> theViewer.handleExplicitPage(theSlider.getValue() + 1));
152
153
154 final TethysUIPaneFactory myPanes = pFactory.paneFactory();
155 final TethysUIBorderPaneManager myInnerPane = myPanes.newBorderPane();
156 myInnerPane.setCentre(theSlider);
157 myInnerPane.setEast(theNextButton);
158 myInnerPane.setWest(thePrevButton);
159
160
161 theSliderPane = myPanes.newBorderPane();
162 theSliderPane.setHGap(HGAP_SIZE);
163 theSliderPane.setCentre(myInnerPane);
164 theSliderPane.setWest(theLabel);
165
166
167 theButtonsPane = myPanes.newHBoxPane();
168 theButtonsPane.addNode(theParentButton);
169 theButtonsPane.addNode(theModeButton);
170
171
172 thePane = myPanes.newBorderPane();
173 thePane.setCentre(theSliderPane);
174 thePane.setWest(theButtonsPane);
175 setVisible(false);
176 }
177
178 @Override
179 public TethysUIComponent getUnderlying() {
180 return thePane;
181 }
182
183
184
185
186
187
188
189
190 private static <K extends Enum<K> & TethysUIIconId> void configureButton(final TethysUIButton pButton,
191 final K pId) {
192 pButton.setIconOnly();
193 pButton.setIcon(pId);
194 pButton.setIconSize(ICON_SIZE);
195 pButton.setNullMargins();
196 }
197
198
199
200
201
202
203 protected void updateState(final MetisViewerPage pPage) {
204
205 final boolean hasParent = pPage.hasParent();
206 theParentButton.setVisible(hasParent);
207 boolean isVisible = hasParent;
208
209
210 final MetisViewerMode myMode = pPage.getMode();
211 final boolean isList = !MetisViewerMode.CONTENTS.equals(myMode);
212
213
214 theModeButton.setValue(myMode);
215 final boolean isEnabled = pPage.hasMultiModes();
216 theModeButton.setEnabled(isEnabled);
217 if (isEnabled) {
218 buildModeMenu(pPage);
219 }
220 isVisible |= isEnabled;
221
222
223 final int mySize = pPage.getSize();
224 final int myPos = pPage.getItemNo();
225
226
227 if (isList
228 && (mySize > 1)) {
229
230 theSliderPane.setVisible(true);
231 isVisible = true;
232
233
234 final String myText = TEXT_ITEM
235 + CHAR_BLANK
236 + myPos
237 + CHAR_BLANK
238 + TEXT_OF
239 + CHAR_BLANK
240 + mySize;
241 theLabel.setText(myText);
242
243
244 theSlider.setMaximum(mySize - 1);
245 theSlider.setValue(myPos - 1);
246
247
248 theNextButton.setEnabled(pPage.hasNext());
249 thePrevButton.setEnabled(pPage.hasPrevious());
250
251
252 } else {
253
254 theSliderPane.setVisible(false);
255 }
256
257
258 setVisible(isVisible);
259 }
260
261
262
263
264
265
266 private void buildModeMenu(final MetisViewerPage pPage) {
267
268 final TethysUIScrollMenu<MetisViewerMode> myMenu = theModeButton.getMenu();
269 myMenu.removeAllItems();
270
271
272 for (MetisViewerMode myMode : MetisViewerMode.values()) {
273
274 if (pPage.validMode(myMode)) {
275
276 myMenu.addItem(myMode);
277 }
278 }
279 }
280 }