24
24
import java .util .Optional ;
25
25
import java .util .function .Consumer ;
26
26
import java .util .function .Predicate ;
27
-
28
27
import javafx .application .Platform ;
28
+ import javafx .event .ActionEvent ;
29
29
import javafx .event .EventHandler ;
30
30
import javafx .scene .Parent ;
31
31
import javafx .scene .control .Button ;
32
32
import javafx .scene .control .ButtonBar ;
33
33
import javafx .scene .control .ButtonType ;
34
- import javafx .scene .control .ContentDisplay ;
35
34
import javafx .scene .control .Control ;
36
35
import javafx .scene .control .Dialog ;
36
+ import javafx .scene .control .MenuButton ;
37
+ import javafx .scene .control .MenuItem ;
37
38
import javafx .scene .control .Spinner ;
38
39
import javafx .scene .control .TextField ;
39
40
import javafx .scene .image .ImageView ;
40
- import javafx .scene .input .MouseEvent ;
41
41
import javafx .scene .layout .GridPane ;
42
- import javafx .scene .layout .HBox ;
43
42
import javafx .scene .layout .Priority ;
44
43
import javafx .scene .text .Text ;
45
- import javafx .scene .text .TextAlignment ;
46
44
import javafx .stage .FileChooser ;
47
45
import javafx .stage .FileChooser .ExtensionFilter ;
48
46
52
50
* construct that source. As an example, the image file source results in a file picker that the
53
51
* user can use to browse for an image.
54
52
*/
55
- public class AddSourceView extends HBox {
53
+ public class AddSourceButton extends MenuButton {
56
54
57
55
@ VisibleForTesting
58
56
static final String SOURCE_DIALOG_STYLE_CLASS = "source-dialog" ;
59
57
private final EventBus eventBus ;
60
58
61
- private final Button webcamButton ;
62
- private final Button ipcamButton ;
59
+ private final MenuItem webcamButton ;
60
+ private final MenuItem ipcamButton ;
61
+ private final MenuItem httpButton ;
63
62
private Optional <Dialog > activeDialog = Optional .empty ();
64
63
65
64
@ Inject
66
- AddSourceView (EventBus eventBus ,
65
+ AddSourceButton (EventBus eventBus ,
67
66
MultiImageFileSource .Factory multiImageSourceFactory ,
68
67
ImageFileSource .Factory imageSourceFactory ,
69
68
CameraSource .Factory cameraSourceFactory ,
70
69
HttpSource .Factory httpSourceFactory ) {
70
+ super ("Add Source" );
71
71
this .eventBus = eventBus ;
72
-
73
- this .setFillHeight (true );
74
-
75
- addButton ("Add\n Image(s)" ,
76
- getClass ().getResource ("/edu/wpi/grip/ui/icons/add-image.png" ),
77
- mouseEvent -> {
72
+
73
+ addMenuItem ("Image(s)" ,
74
+ getClass ().getResource ("/edu/wpi/grip/ui/icons/add-image.png" ), mouseEvent -> {
78
75
// Show a file picker so the user can open one or more images from disk
79
76
final FileChooser fileChooser = new FileChooser ();
80
77
fileChooser .setTitle ("Open an image" );
@@ -120,14 +117,12 @@ public class AddSourceView extends HBox {
120
117
}
121
118
});
122
119
123
- webcamButton = addButton (
124
- "Add\n Webcam" ,
125
- getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ),
126
- mouseEvent -> {
120
+ webcamButton = addMenuItem ("Webcam" ,
121
+ getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
127
122
final Parent root = this .getScene ().getRoot ();
128
123
129
124
// Show a dialog for the user to pick a camera index
130
- final Spinner <Integer > cameraIndex = new Spinner <Integer >(0 , Integer .MAX_VALUE , 0 );
125
+ final Spinner <Integer > cameraIndex = new Spinner <>(0 , Integer .MAX_VALUE , 0 );
131
126
final SourceDialog dialog = new SourceDialog (root , cameraIndex );
132
127
133
128
dialog .setTitle ("Add Webcam" );
@@ -144,15 +139,11 @@ public class AddSourceView extends HBox {
144
139
cameraSource .initialize ();
145
140
return cameraSource ;
146
141
},
147
- e -> {
148
- dialog .errorText .setText (e .getMessage ());
149
- });
142
+ e -> dialog .errorText .setText (e .getMessage ()));
150
143
});
151
144
152
- ipcamButton = addButton (
153
- "Add IP\n Camera" ,
154
- getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ),
155
- mouseEvent -> {
145
+ ipcamButton = addMenuItem ("IP Camera" ,
146
+ getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
156
147
final Parent root = this .getScene ().getRoot ();
157
148
158
149
// Show a dialog for the user to pick a camera URL
@@ -197,8 +188,8 @@ public class AddSourceView extends HBox {
197
188
e -> dialog .errorText .setText (e .getMessage ()));
198
189
});
199
190
200
- addButton ( "Add \n HTTP source" , getClass (). getResource ( "/edu/wpi/grip/ui/icons/publish.png" ) ,
201
- mouseEvent -> {
191
+ httpButton = addMenuItem ( "HTTP" ,
192
+ getClass (). getResource ( "/edu/wpi/grip/ui/icons/publish.png" ), mouseEvent -> {
202
193
final Parent root = this .getScene ().getRoot ();
203
194
// Show a dialog to pick the server path images will be uploaded on
204
195
final String imageRoot = GripServer .IMAGE_UPLOAD_PATH + "/" ;
@@ -252,31 +243,34 @@ private void loadCamera(Dialog<ButtonType> dialog, SupplierWithIO<CameraSource>
252
243
/**
253
244
* Add a new button for adding a source. This method takes care of setting the event handler.
254
245
*/
255
- private Button addButton (String text , URL graphicURL , EventHandler <? super MouseEvent >
256
- onMouseClicked ) {
246
+ private MenuItem addMenuItem (String text , URL graphicURL , EventHandler <ActionEvent >
247
+ onActionEvent ) {
257
248
final ImageView graphic = new ImageView (graphicURL .toString ());
258
249
graphic .setFitWidth (DPIUtility .SMALL_ICON_SIZE );
259
250
graphic .setFitHeight (DPIUtility .SMALL_ICON_SIZE );
260
251
261
- final Button button = new Button (text , graphic );
262
- button .setTextAlignment (TextAlignment .CENTER );
263
- button .setContentDisplay (ContentDisplay .TOP );
264
- button .setOnMouseClicked (onMouseClicked );
252
+ final MenuItem menuItem = new MenuItem (" " + text , graphic );
253
+ menuItem .setOnAction (onActionEvent );
265
254
266
- this . getChildren ().add (button );
267
- return button ;
255
+ getItems ().add (menuItem );
256
+ return menuItem ;
268
257
}
269
258
270
259
@ VisibleForTesting
271
- Button getWebcamButton () {
260
+ MenuItem getWebcamButton () {
272
261
return webcamButton ;
273
262
}
274
263
275
264
@ VisibleForTesting
276
- Button getIpcamButton () {
265
+ MenuItem getIpcamButton () {
277
266
return ipcamButton ;
278
267
}
279
268
269
+ @ VisibleForTesting
270
+ MenuItem getHttpButton () {
271
+ return httpButton ;
272
+ }
273
+
280
274
@ VisibleForTesting
281
275
void closeDialogs () {
282
276
activeDialog .ifPresent (dialog -> {
@@ -291,7 +285,7 @@ void closeDialogs() {
291
285
}
292
286
293
287
public interface Factory {
294
- AddSourceView create ();
288
+ AddSourceButton create ();
295
289
}
296
290
297
291
private static class SourceDialog extends Dialog <ButtonType > {
0 commit comments