1313import javafx .scene .control .*;
1414import javafx .scene .image .ImageView ;
1515import javafx .scene .input .MouseEvent ;
16+ import javafx .scene .layout .GridPane ;
1617import javafx .scene .layout .HBox ;
18+ import javafx .scene .layout .Priority ;
19+ import javafx .scene .text .Text ;
1720import javafx .scene .text .TextAlignment ;
1821import javafx .stage .FileChooser ;
1922
@@ -42,6 +45,25 @@ private interface SupplierWithIO<T> {
4245 T getWithIO () throws IOException ;
4346 }
4447
48+ private class SourceDialog extends Dialog <ButtonType > {
49+ private final Text errorText = new Text ();
50+
51+ private SourceDialog (final Parent root , Control inputField ) {
52+ final GridPane gridContent = new GridPane ();
53+ gridContent .setMaxWidth (Double .MAX_VALUE );
54+ GridPane .setHgrow (inputField , Priority .ALWAYS );
55+ GridPane .setHgrow (errorText , Priority .NEVER );
56+ errorText .wrappingWidthProperty ().bind (inputField .widthProperty ());
57+ gridContent .add (errorText , 0 , 0 );
58+ gridContent .add (inputField , 0 , 1 );
59+
60+ getDialogPane ().setContent (gridContent );
61+ getDialogPane ().setStyle (root .getStyle ());
62+ getDialogPane ().getStylesheets ().addAll (root .getStylesheets ());
63+ getDialogPane ().getButtonTypes ().addAll (ButtonType .CANCEL , ButtonType .OK );
64+ }
65+ }
66+
4567 public AddSourceView (EventBus eventBus ) {
4668 this .eventBus = checkNotNull (eventBus , "Event Bus can not be null" );
4769
@@ -76,32 +98,28 @@ public AddSourceView(EventBus eventBus) {
7698 final Parent root = this .getScene ().getRoot ();
7799
78100 // Show a dialog for the user to pick a camera index
79- final Dialog <ButtonType > dialog = new Dialog <>();
80101 final Spinner <Integer > cameraIndex = new Spinner <Integer >(0 , Integer .MAX_VALUE , 0 );
102+ final SourceDialog dialog = new SourceDialog (root , cameraIndex );
81103
82104 dialog .setTitle ("Add Webcam" );
83105 dialog .setHeaderText ("Choose a camera" );
84106 dialog .setContentText ("index" );
85- dialog .getDialogPane ().setContent (cameraIndex );
86- dialog .getDialogPane ().getButtonTypes ().addAll (ButtonType .CANCEL , ButtonType .OK );
87- dialog .getDialogPane ().setStyle (root .getStyle ());
88- dialog .getDialogPane ().getStylesheets ().addAll (root .getStylesheets ());
89107
90108 // If the user clicks OK, add a new camera source
91109 loadCamera (dialog ,
92110 () -> new CameraSource (eventBus , cameraIndex .getValue ()).start (eventBus ),
93111 e -> {
94- // TODO: Indicate to user that the camera source was invalid
112+ dialog . errorText . setText ( e . getMessage ());
95113 });
96114 });
97115
98116 addButton ("Add IP\n Camera" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
99117 final Parent root = this .getScene ().getRoot ();
100118
101119 // Show a dialog for the user to pick a camera URL
102- final Dialog <ButtonType > dialog = new Dialog <>();
103120
104121 final TextField cameraAddress = new TextField ();
122+ final SourceDialog dialog = new SourceDialog (root , cameraAddress );
105123 cameraAddress .setPromptText ("Ex: http://10.1.90.11/mjpg/video.mjpg" );
106124 cameraAddress .textProperty ().addListener (observable -> {
107125 boolean validURL = true ;
@@ -119,17 +137,13 @@ public AddSourceView(EventBus eventBus) {
119137 dialog .setTitle ("Add IP Camera" );
120138 dialog .setHeaderText ("Enter the IP camera URL" );
121139 dialog .setContentText ("URL" );
122- dialog .getDialogPane ().setContent (cameraAddress );
123- dialog .getDialogPane ().getButtonTypes ().addAll (ButtonType .CANCEL , ButtonType .OK );
124140 dialog .getDialogPane ().lookupButton (ButtonType .OK ).setDisable (true );
125- dialog .getDialogPane ().setStyle (root .getStyle ());
126- dialog .getDialogPane ().getStylesheets ().addAll (root .getStylesheets ());
127141
128142 // If the user clicks OK, add a new camera source
129143 loadCamera (dialog ,
130144 () -> new CameraSource (eventBus , cameraAddress .getText ()).start (eventBus ),
131145 e -> {
132- // TODO: Indicate to user that the camera source was invalid
146+ dialog . errorText . setText ( e . getMessage ());
133147 });
134148 });
135149 }
0 commit comments