2020import java .util .List ;
2121import java .util .concurrent .ExecutionException ;
2222
23+ import javafx .fxml .FXML ;
24+ import javafx .scene .control .Alert ;
25+ import javafx .scene .control .Button ;
26+ import javafx .scene .control .ComboBox ;
27+ import javafx .scene .control .ListCell ;
28+ import javafx .scene .control .ListView ;
29+ import javafx .scene .control .ProgressIndicator ;
30+ import javafx .scene .control .SelectionMode ;
31+ import javafx .scene .control .TextArea ;
32+ import javafx .scene .control .TextField ;
33+ import javafx .util .StringConverter ;
34+
2335import com .esri .arcgisruntime .concurrent .ListenableFuture ;
2436import com .esri .arcgisruntime .layers .ArcGISMapImageLayer ;
2537import com .esri .arcgisruntime .layers .Layer ;
3446import com .esri .arcgisruntime .security .AuthenticationManager ;
3547import com .esri .arcgisruntime .security .OAuthConfiguration ;
3648
37- import javafx .fxml .FXML ;
38- import javafx .scene .control .Alert ;
39- import javafx .scene .control .Button ;
40- import javafx .scene .control .ComboBox ;
41- import javafx .scene .control .Dialog ;
42- import javafx .scene .control .ListCell ;
43- import javafx .scene .control .ListView ;
44- import javafx .scene .control .ProgressIndicator ;
45- import javafx .scene .control .SelectionMode ;
46- import javafx .scene .control .TextArea ;
47- import javafx .scene .control .TextField ;
48- import javafx .util .StringConverter ;
49-
5049public class CreateAndSaveMapController {
5150
52-
5351 @ FXML private MapView mapView ;
5452 @ FXML private TextField title ;
5553 @ FXML private TextField tags ;
@@ -72,9 +70,8 @@ private void initialize() {
7270
7371 // update basemap when selection changes
7472 basemapList .getSelectionModel ().select (0 );
75- basemapList .getSelectionModel ().selectedItemProperty ().addListener (o ->
76- map .setBasemap (basemapList .getSelectionModel ().getSelectedItem ())
77- );
73+ basemapList .getSelectionModel ().selectedItemProperty ()
74+ .addListener (o -> map .setBasemap (basemapList .getSelectionModel ().getSelectedItem ()));
7875
7976 basemapList .setCellFactory (c -> new BasemapCell ());
8077
@@ -83,7 +80,8 @@ private void initialize() {
8380 mapView .setMap (map );
8481
8582 // set operational layer options
86- String worldElevationService = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer" ;
83+ String worldElevationService =
84+ "http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer" ;
8785 ArcGISMapImageLayer worldElevation = new ArcGISMapImageLayer (worldElevationService );
8886 worldElevation .loadAsync ();
8987
@@ -103,6 +101,7 @@ private void initialize() {
103101
104102 // set portal folder title converter
105103 folderList .setConverter (new StringConverter <PortalFolder >() {
104+
106105 @ Override
107106 public String toString (PortalFolder folder ) {
108107 return folder .getTitle ();
@@ -125,34 +124,37 @@ void authenticate() {
125124 authenticationDialog .setOnCloseRequest (r -> {
126125
127126 OAuthConfiguration configuration = authenticationDialog .getResult ();
128- AuthenticationManager .addOAuthConfiguration (configuration );
129-
130- // setup the handler that will prompt an authentication challenge to the user
131- AuthenticationManager .setAuthenticationChallengeHandler (new OAuthChallengeHandler ());
132-
133- portal = new Portal ("http://" + configuration .getPortalUrl (), true );
134- portal .addDoneLoadingListener (() -> {
135- if (portal .getLoadStatus () == LoadStatus .LOADED ) {
136- try {
137- PortalUserContent portalUserContent = portal .getUser ().fetchContentAsync ().get ();
138- List <PortalFolder > portalFolders = portalUserContent .getFolders ();
139- folderList .getItems ().addAll (portalFolders );
140- } catch (Exception e ) {
141- e .printStackTrace ();
127+ // check authentication went through
128+ if (configuration != null ) {
129+ AuthenticationManager .addOAuthConfiguration (configuration );
130+
131+ // setup the handler that will prompt an authentication challenge to the user
132+ AuthenticationManager .setAuthenticationChallengeHandler (new OAuthChallengeHandler ());
133+
134+ portal = new Portal ("http://" + configuration .getPortalUrl (), true );
135+ portal .addDoneLoadingListener (() -> {
136+ if (portal .getLoadStatus () == LoadStatus .LOADED ) {
137+ try {
138+ PortalUserContent portalUserContent = portal .getUser ().fetchContentAsync ().get ();
139+ List <PortalFolder > portalFolders = portalUserContent .getFolders ();
140+ folderList .getItems ().addAll (portalFolders );
141+ } catch (Exception e ) {
142+ e .printStackTrace ();
143+ }
144+
145+ saveButton .setDisable (false );
146+
147+ } else if (portal .getLoadStatus () == LoadStatus .FAILED_TO_LOAD ) {
148+
149+ // show alert message on error
150+ showMessage ("Authentication failed" , portal .getLoadError ().getMessage (), Alert .AlertType .ERROR );
142151 }
152+ });
143153
144- saveButton .setDisable (false );
145-
146- } else if (portal .getLoadStatus () == LoadStatus .FAILED_TO_LOAD ) {
147-
148- // show alert message on error
149- showMessage ("Authentication failed" , portal .getLoadError ().getMessage (), Alert .AlertType .ERROR );
150- }
151- });
152-
153- // loading the portal info of a secured resource
154- // this will invoke the authentication challenge
155- portal .loadAsync ();
154+ // loading the portal info of a secured resource
155+ // this will invoke the authentication challenge
156+ portal .loadAsync ();
157+ }
156158 });
157159
158160 }
@@ -161,6 +163,7 @@ void authenticate() {
161163 * Shows a Basemap title in a ListView.
162164 */
163165 private class BasemapCell extends ListCell <Basemap > {
166+
164167 @ Override
165168 protected void updateItem (Basemap basemap , boolean empty ) {
166169 super .updateItem (basemap , empty );
@@ -173,6 +176,7 @@ protected void updateItem(Basemap basemap, boolean empty) {
173176 * Shows a Layer title in a ListView.
174177 */
175178 private class LayerCell extends ListCell <Layer > {
179+
176180 @ Override
177181 protected void updateItem (Layer layer , boolean empty ) {
178182 super .updateItem (layer , empty );
0 commit comments