11package edu .wpi .grip .ui .pipeline ;
22
3+ import com .google .common .annotations .VisibleForTesting ;
34import com .google .common .eventbus .EventBus ;
45import com .google .inject .Inject ;
56import edu .wpi .grip .core .events .SourceAddedEvent ;
2728import java .net .URISyntaxException ;
2829import java .net .URL ;
2930import java .util .List ;
31+ import java .util .Optional ;
3032import java .util .function .Consumer ;
3133import java .util .function .Predicate ;
3234
3739 */
3840public class AddSourceView extends HBox {
3941
42+ @ VisibleForTesting
43+ static final String SOURCE_DIALOG_STYLE_CLASS = "source-dialog" ;
4044 private final EventBus eventBus ;
4145 private final MultiImageFileSource .Factory multiImageSourceFactory ;
4246 private final ImageFileSource .Factory imageSourceFactory ;
4347 private final CameraSource .Factory cameraSourceFactory ;
4448
49+ private final Button webcamButton ;
50+ private final Button ipcamButton ;
51+ private Optional <Dialog > activeDialog = Optional .empty ();
52+
4553 @ FunctionalInterface
4654 private interface SupplierWithIO <T > {
4755 T getWithIO () throws IOException ;
4856 }
4957
50- private class SourceDialog extends Dialog <ButtonType > {
58+ private static class SourceDialog extends Dialog <ButtonType > {
5159 private final Text errorText = new Text ();
5260
5361 private SourceDialog (final Parent root , Control inputField ) {
5462 super ();
63+
64+ this .getDialogPane ().getStyleClass ().add (SOURCE_DIALOG_STYLE_CLASS );
65+
5566 final GridPane gridContent = new GridPane ();
5667 gridContent .setMaxWidth (Double .MAX_VALUE );
5768 GridPane .setHgrow (inputField , Priority .ALWAYS );
@@ -112,7 +123,7 @@ public interface Factory {
112123 }
113124 });
114125
115- addButton ("Add\n Webcam" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
126+ webcamButton = addButton ("Add\n Webcam" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
116127 final Parent root = this .getScene ().getRoot ();
117128
118129 // Show a dialog for the user to pick a camera index
@@ -123,6 +134,8 @@ public interface Factory {
123134 dialog .setHeaderText ("Choose a camera" );
124135 dialog .setContentText ("index" );
125136
137+ dialog .getDialogPane ().lookupButton (ButtonType .OK ).requestFocus ();
138+
126139 // If the user clicks OK, add a new camera source
127140 loadCamera (dialog ,
128141 () -> {
@@ -135,7 +148,7 @@ public interface Factory {
135148 });
136149 });
137150
138- addButton ("Add IP\n Camera" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
151+ ipcamButton = addButton ("Add IP\n Camera" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
139152 final Parent root = this .getScene ().getRoot ();
140153
141154 // Show a dialog for the user to pick a camera URL
@@ -181,6 +194,7 @@ public interface Factory {
181194 */
182195 private void loadCamera (Dialog <ButtonType > dialog , SupplierWithIO <CameraSource > cameraSourceSupplier , Consumer <IOException > failureCallback ) {
183196 assert Platform .isFxApplicationThread () : "Should only run in FX thread" ;
197+ activeDialog = Optional .of (dialog );
184198 dialog .showAndWait ().filter (Predicate .isEqual (ButtonType .OK )).ifPresent (result -> {
185199 try {
186200 // Will try to create the camera with the values from the supplier
@@ -192,13 +206,14 @@ private void loadCamera(Dialog<ButtonType> dialog, SupplierWithIO<CameraSource>
192206 loadCamera (dialog , cameraSourceSupplier , failureCallback );
193207 }
194208 });
209+ activeDialog = Optional .empty ();
195210 }
196211
197212
198213 /**
199214 * Add a new button for adding a source. This method takes care of setting the event handler.
200215 */
201- private void addButton (String text , URL graphicURL , EventHandler <? super MouseEvent > onMouseClicked ) {
216+ private Button addButton (String text , URL graphicURL , EventHandler <? super MouseEvent > onMouseClicked ) {
202217 final ImageView graphic = new ImageView (graphicURL .toString ());
203218 graphic .setFitWidth (DPIUtility .SMALL_ICON_SIZE );
204219 graphic .setFitHeight (DPIUtility .SMALL_ICON_SIZE );
@@ -209,5 +224,29 @@ private void addButton(String text, URL graphicURL, EventHandler<? super MouseEv
209224 button .setOnMouseClicked (onMouseClicked );
210225
211226 this .getChildren ().add (button );
227+ return button ;
228+ }
229+
230+ @ VisibleForTesting
231+ Button getWebcamButton () {
232+ return webcamButton ;
233+ }
234+
235+ @ VisibleForTesting
236+ Button getIpcamButton () {
237+ return ipcamButton ;
238+ }
239+
240+ @ VisibleForTesting
241+ void closeDialogs () {
242+ activeDialog .ifPresent (dialog -> {
243+ for ( ButtonType bt : dialog .getDialogPane ().getButtonTypes () ) {
244+ if ( bt .getButtonData () == ButtonBar .ButtonData .CANCEL_CLOSE ) {
245+ Button cancelButton = ( Button ) dialog .getDialogPane ().lookupButton ( bt );
246+ Platform .runLater (() -> cancelButton .fire ());
247+ break ;
248+ }
249+ }
250+ });
212251 }
213252}
0 commit comments