11package edu .wpi .grip .ui .pipeline ;
22
3- import com .google .common .annotations .VisibleForTesting ;
4- import com .google .common .eventbus .EventBus ;
5- import com .google .inject .Inject ;
6- import com .google .common .net .InetAddresses ;
73import edu .wpi .grip .core .events .SourceAddedEvent ;
84import edu .wpi .grip .core .events .UnexpectedThrowableEvent ;
95import edu .wpi .grip .core .sources .CameraSource ;
106import edu .wpi .grip .core .sources .ImageFileSource ;
117import edu .wpi .grip .core .sources .MultiImageFileSource ;
128import edu .wpi .grip .ui .util .DPIUtility ;
139import edu .wpi .grip .ui .util .SupplierWithIO ;
10+
11+ import com .google .common .annotations .VisibleForTesting ;
12+ import com .google .common .eventbus .EventBus ;
13+ import com .google .common .net .InetAddresses ;
14+ import com .google .inject .Inject ;
15+
16+ import java .io .File ;
17+ import java .io .IOException ;
18+ import java .net .MalformedURLException ;
19+ import java .net .URISyntaxException ;
20+ import java .net .URL ;
21+ import java .util .List ;
22+ import java .util .Optional ;
23+ import java .util .function .Consumer ;
24+ import java .util .function .Predicate ;
25+
1426import javafx .application .Platform ;
1527import javafx .event .EventHandler ;
1628import javafx .scene .Parent ;
17- import javafx .scene .control .*;
29+ import javafx .scene .control .Button ;
30+ import javafx .scene .control .ButtonBar ;
31+ import javafx .scene .control .ButtonType ;
32+ import javafx .scene .control .ContentDisplay ;
33+ import javafx .scene .control .Control ;
34+ import javafx .scene .control .Dialog ;
35+ import javafx .scene .control .Spinner ;
36+ import javafx .scene .control .TextField ;
1837import javafx .scene .image .ImageView ;
1938import javafx .scene .input .MouseEvent ;
2039import javafx .scene .layout .GridPane ;
2544import javafx .stage .FileChooser ;
2645import javafx .stage .FileChooser .ExtensionFilter ;
2746
28- import java .io .File ;
29- import java .io .IOException ;
30- import java .net .MalformedURLException ;
31- import java .net .URISyntaxException ;
32- import java .net .URL ;
33- import java .util .List ;
34- import java .util .Optional ;
35- import java .util .function .Consumer ;
36- import java .util .function .Predicate ;
37-
3847/**
39- * A box of buttons that let the user add different kinds of {@link edu.wpi.grip.core.Source Source}s. Depending on which button is pressed,
40- * a different dialog is presented for the user to construct that source. As an example, the image file source results
41- * in a file picker that the user can use to browse for an image.
48+ * A box of buttons that let the user add different kinds of {@link edu.wpi.grip.core.Source Source}s. Depending on
49+ * which button is pressed, a different dialog is presented for the user to construct that source. As an example, the
50+ * image file source results in a file picker that the user can use to browse for an image.
4251 */
4352public class AddSourceView extends HBox {
4453
@@ -111,114 +120,113 @@ public interface Factory {
111120
112121 final List <File > imageFiles = fileChooser .showOpenMultipleDialog (this .getScene ().getWindow ());
113122
114- if (imageFiles == null ) return ;
123+ if (imageFiles == null ) {
124+ return ;
125+ }
115126
116127 // Add a new source for each image .
117128 if (imageFiles .size () == 1 ) {
118129 try {
119130 final ImageFileSource imageFileSource = imageSourceFactory .create (imageFiles .get (0 ));
120131 imageFileSource .initialize ();
121132 eventBus .post (new SourceAddedEvent (imageFileSource ));
122- } catch (IOException e ) {
123- eventBus .post (new UnexpectedThrowableEvent (e , "The image selected was invalid" ));
133+ } catch (IOException exception ) {
134+ eventBus .post (new UnexpectedThrowableEvent (exception , "The image selected was invalid" ));
124135 }
125136 } else {
126137 try {
127138 final MultiImageFileSource multiImageFileSource = multiImageSourceFactory .create (imageFiles );
128139 multiImageFileSource .initialize ();
129140 eventBus .post (new SourceAddedEvent (multiImageFileSource ));
130- } catch (IOException e ) {
131- eventBus .post (new UnexpectedThrowableEvent (e , "One of the images selected was invalid" ));
141+ } catch (IOException exception ) {
142+ eventBus .post (new UnexpectedThrowableEvent (exception , "One of the images selected was invalid" ));
132143 }
133144 }
134145 });
135146
136- webcamButton = addButton ("Add\n Webcam" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
137- final Parent root = this .getScene ().getRoot ();
138-
139- // Show a dialog for the user to pick a camera index
140- final Spinner <Integer > cameraIndex = new Spinner <Integer >(0 , Integer .MAX_VALUE , 0 );
141- final SourceDialog dialog = new SourceDialog (root , cameraIndex );
142-
143- dialog .setTitle ("Add Webcam" );
144- dialog .setHeaderText ("Choose a camera" );
145- dialog .setContentText ("index" );
146-
147- dialog .getDialogPane ().lookupButton (ButtonType .OK ).requestFocus ();
148-
149- // If the user clicks OK, add a new camera source
150- loadCamera (dialog ,
151- () -> {
152- final CameraSource cameraSource = cameraSourceFactory .create (cameraIndex .getValue ());
153- cameraSource .initialize ();
154- return cameraSource ;
155- },
156- e -> {
157- dialog .errorText .setText (e .getMessage ());
158- });
159- });
147+ webcamButton = addButton ("Add\n Webcam" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ),
148+ mouseEvent -> {
149+ final Parent root = this .getScene ().getRoot ();
160150
161- ipcamButton = addButton ("Add IP\n Camera" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ), mouseEvent -> {
162- final Parent root = this .getScene ().getRoot ();
151+ // Show a dialog for the user to pick a camera index
152+ final Spinner <Integer > cameraIndex = new Spinner <Integer >(0 , Integer .MAX_VALUE , 0 );
153+ final SourceDialog dialog = new SourceDialog (root , cameraIndex );
163154
164- // Show a dialog for the user to pick a camera URL
155+ dialog .setTitle ("Add Webcam" );
156+ dialog .setHeaderText ("Choose a camera" );
157+ dialog .setContentText ("index" );
165158
166- final TextField cameraAddress = new TextField ();
167- final SourceDialog dialog = new SourceDialog (root , cameraAddress );
168- cameraAddress .setPromptText ("Ex: http://10.1.90.11/mjpg/video.mjpg" );
169- cameraAddress .textProperty ().addListener (observable -> {
170- boolean validURL = true ;
159+ dialog .getDialogPane ().lookupButton (ButtonType .OK ).requestFocus ();
171160
172- try {
173- new URL (cameraAddress .getText ()).toURI ();
174- } catch (MalformedURLException e ) {
175- validURL = false ;
176- if (InetAddresses .isInetAddress (cameraAddress .getText ()) || cameraAddress .getText ().endsWith (".local" )) {
177- cameraAddress .setText ("http://" + cameraAddress .getText ());
178- validURL = true ;
161+ // If the user clicks OK, add a new camera source
162+ loadCamera (dialog , () -> {
163+ final CameraSource cameraSource = cameraSourceFactory .create (cameraIndex .getValue ());
164+ cameraSource .initialize ();
165+ return cameraSource ;
166+ }, e -> { dialog .errorText .setText (e .getMessage ()); });
167+ });
168+
169+ ipcamButton = addButton ("Add IP\n Camera" , getClass ().getResource ("/edu/wpi/grip/ui/icons/add-webcam.png" ),
170+ mouseEvent -> {
171+ final Parent root = this .getScene ().getRoot ();
172+
173+ // Show a dialog for the user to pick a camera URL
174+ final TextField cameraAddress = new TextField ();
175+ final SourceDialog dialog = new SourceDialog (root , cameraAddress );
176+ cameraAddress .setPromptText ("Ex: http://10.1.90.11/mjpg/video.mjpg" );
177+ cameraAddress .textProperty ().addListener (observable -> {
178+ boolean validUrl = true ;
179+
180+ try {
181+ new URL (cameraAddress .getText ()).toURI ();
182+ } catch (MalformedURLException exception ) {
183+ validUrl = false ;
184+ if (InetAddresses .isInetAddress (cameraAddress .getText ())
185+ || cameraAddress .getText ().endsWith (".local" )) {
186+ cameraAddress .setText ("http://" + cameraAddress .getText ());
187+ validUrl = true ;
188+ }
189+ } catch (URISyntaxException exception ) {
190+ validUrl = false ;
179191 }
180- } catch (URISyntaxException e ) {
181- validURL = false ;
182- }
183192
184- // Enable the "OK" button only if the user has entered a valid URL
185- dialog .getDialogPane ().lookupButton (ButtonType .OK ).setDisable (!validURL );
193+ // Enable the "OK" button only if the user has entered a valid URL
194+ dialog .getDialogPane ().lookupButton (ButtonType .OK ).setDisable (!validUrl );
195+ });
196+
197+ dialog .setTitle ("Add IP Camera" );
198+ dialog .setHeaderText ("Enter the IP camera URL" );
199+ dialog .setContentText ("URL" );
200+ dialog .getDialogPane ().lookupButton (ButtonType .OK ).setDisable (true );
201+
202+ // If the user clicks OK, add a new camera source
203+ loadCamera (dialog , () -> {
204+ final CameraSource cameraSource = cameraSourceFactory .create (cameraAddress .getText ());
205+ cameraSource .initialize ();
206+ return cameraSource ;
207+ }, e -> { dialog .errorText .setText (e .getMessage ()); });
186208 });
187-
188- dialog .setTitle ("Add IP Camera" );
189- dialog .setHeaderText ("Enter the IP camera URL" );
190- dialog .setContentText ("URL" );
191- dialog .getDialogPane ().lookupButton (ButtonType .OK ).setDisable (true );
192-
193- // If the user clicks OK, add a new camera source
194- loadCamera (dialog ,
195- () -> {
196- final CameraSource cameraSource = cameraSourceFactory .create (cameraAddress .getText ());
197- cameraSource .initialize ();
198- return cameraSource ;
199- },
200- e -> {
201- dialog .errorText .setText (e .getMessage ());
202- });
203- });
204209 }
205210
206211 /**
212+ * Load a new camera source.
213+ *
207214 * @param dialog The dialog to load the camera with
208215 * @param cameraSourceSupplier The supplier that will create the camera
209216 * @param failureCallback The handler for when the camera source supplier throws an IO Exception
210217 */
211- private void loadCamera (Dialog <ButtonType > dialog , SupplierWithIO <CameraSource > cameraSourceSupplier , Consumer <IOException > failureCallback ) {
218+ private void loadCamera (Dialog <ButtonType > dialog , SupplierWithIO <CameraSource > cameraSourceSupplier ,
219+ Consumer <IOException > failureCallback ) {
212220 assert Platform .isFxApplicationThread () : "Should only run in FX thread" ;
213221 activeDialog = Optional .of (dialog );
214222 dialog .showAndWait ().filter (Predicate .isEqual (ButtonType .OK )).ifPresent (result -> {
215223 try {
216224 // Will try to create the camera with the values from the supplier
217225 final CameraSource source = cameraSourceSupplier .getWithIO ();
218226 eventBus .post (new SourceAddedEvent (source ));
219- } catch (IOException e ) {
227+ } catch (IOException exception ) {
220228 // This will run it again with the new values retrieved by the supplier
221- failureCallback .accept (e );
229+ failureCallback .accept (exception );
222230 loadCamera (dialog , cameraSourceSupplier , failureCallback );
223231 }
224232 });
@@ -229,8 +237,8 @@ private void loadCamera(Dialog<ButtonType> dialog, SupplierWithIO<CameraSource>
229237 /**
230238 * Add a new button for adding a source. This method takes care of setting the event handler.
231239 */
232- private Button addButton (String text , URL graphicURL , EventHandler <? super MouseEvent > onMouseClicked ) {
233- final ImageView graphic = new ImageView (graphicURL .toString ());
240+ private Button addButton (String text , URL graphicUrl , EventHandler <? super MouseEvent > onMouseClicked ) {
241+ final ImageView graphic = new ImageView (graphicUrl .toString ());
234242 graphic .setFitWidth (DPIUtility .SMALL_ICON_SIZE );
235243 graphic .setFitHeight (DPIUtility .SMALL_ICON_SIZE );
236244
0 commit comments