|
| 1 | +/* |
| 2 | + * Copyright 2016 Esri. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.samples.map.create_and_save_map; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | + |
| 23 | +import com.esri.arcgisruntime.concurrent.ListenableFuture; |
| 24 | +import com.esri.arcgisruntime.layers.ArcGISMapImageLayer; |
| 25 | +import com.esri.arcgisruntime.layers.Layer; |
| 26 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 27 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 28 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 29 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 30 | +import com.esri.arcgisruntime.portal.Portal; |
| 31 | +import com.esri.arcgisruntime.portal.PortalFolder; |
| 32 | +import com.esri.arcgisruntime.portal.PortalItem; |
| 33 | +import com.esri.arcgisruntime.portal.PortalUserContent; |
| 34 | +import com.esri.arcgisruntime.security.AuthenticationManager; |
| 35 | +import com.esri.arcgisruntime.security.OAuthConfiguration; |
| 36 | + |
| 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 | + |
| 50 | +public class CreateAndSaveMapController { |
| 51 | + |
| 52 | + |
| 53 | + @FXML private MapView mapView; |
| 54 | + @FXML private TextField title; |
| 55 | + @FXML private TextField tags; |
| 56 | + @FXML private TextArea description; |
| 57 | + @FXML private ComboBox<PortalFolder> folderList; |
| 58 | + @FXML private ListView<Basemap> basemapList; |
| 59 | + @FXML private ListView<Layer> layersList; |
| 60 | + @FXML private Button saveButton; |
| 61 | + @FXML private ProgressIndicator progress; |
| 62 | + |
| 63 | + private ArcGISMap map; |
| 64 | + private Portal portal; |
| 65 | + |
| 66 | + @FXML |
| 67 | + private void initialize() { |
| 68 | + |
| 69 | + // set basemap options |
| 70 | + basemapList.getItems().addAll(Basemap.createStreets(), Basemap.createImagery(), Basemap |
| 71 | + .createTopographic(), Basemap.createOceans()); |
| 72 | + |
| 73 | + // update basemap when selection changes |
| 74 | + basemapList.getSelectionModel().select(0); |
| 75 | + basemapList.getSelectionModel().selectedItemProperty().addListener(o -> |
| 76 | + map.setBasemap(basemapList.getSelectionModel().getSelectedItem()) |
| 77 | + ); |
| 78 | + |
| 79 | + basemapList.setCellFactory(c -> new BasemapCell()); |
| 80 | + |
| 81 | + // create and set a map with the first basemap option |
| 82 | + map = new ArcGISMap(basemapList.getSelectionModel().getSelectedItem()); |
| 83 | + mapView.setMap(map); |
| 84 | + |
| 85 | + // set operational layer options |
| 86 | + String worldElevationService = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer"; |
| 87 | + ArcGISMapImageLayer worldElevation = new ArcGISMapImageLayer(worldElevationService); |
| 88 | + worldElevation.loadAsync(); |
| 89 | + |
| 90 | + String worldCensusService = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer"; |
| 91 | + ArcGISMapImageLayer worldCensus = new ArcGISMapImageLayer(worldCensusService); |
| 92 | + worldCensus.loadAsync(); |
| 93 | + |
| 94 | + layersList.getItems().addAll(worldElevation, worldCensus); |
| 95 | + layersList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); |
| 96 | + |
| 97 | + layersList.getSelectionModel().selectedItemProperty().addListener(o -> { |
| 98 | + map.getOperationalLayers().clear(); |
| 99 | + map.getOperationalLayers().addAll(layersList.getSelectionModel().getSelectedItems()); |
| 100 | + }); |
| 101 | + |
| 102 | + layersList.setCellFactory(c -> new LayerCell()); |
| 103 | + |
| 104 | + // set portal folder title converter |
| 105 | + folderList.setConverter(new StringConverter<PortalFolder>() { |
| 106 | + @Override |
| 107 | + public String toString(PortalFolder folder) { |
| 108 | + return folder.getTitle(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public PortalFolder fromString(String string) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Open a dialog to create and log into a portal. |
| 120 | + */ |
| 121 | + void authenticate() { |
| 122 | + |
| 123 | + AuthenticationDialog authenticationDialog = new AuthenticationDialog(); |
| 124 | + authenticationDialog.show(); |
| 125 | + authenticationDialog.setOnCloseRequest(r -> { |
| 126 | + |
| 127 | + 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(); |
| 142 | + } |
| 143 | + |
| 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(); |
| 156 | + }); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Shows a Basemap title in a ListView. |
| 162 | + */ |
| 163 | + private class BasemapCell extends ListCell<Basemap> { |
| 164 | + @Override |
| 165 | + protected void updateItem(Basemap basemap, boolean empty) { |
| 166 | + super.updateItem(basemap, empty); |
| 167 | + setText(empty || basemap == null ? null : basemap.getName()); |
| 168 | + setGraphic(null); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Shows a Layer title in a ListView. |
| 174 | + */ |
| 175 | + private class LayerCell extends ListCell<Layer> { |
| 176 | + @Override |
| 177 | + protected void updateItem(Layer layer, boolean empty) { |
| 178 | + super.updateItem(layer, empty); |
| 179 | + setText(empty || layer == null ? null : layer.getName()); |
| 180 | + setGraphic(null); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Save the map to the portal. |
| 186 | + */ |
| 187 | + @FXML |
| 188 | + private void saveMap() { |
| 189 | + progress.setVisible(true); |
| 190 | + try { |
| 191 | + ListenableFuture<PortalItem> result = map.saveAsAsync(portal, folderList.getSelectionModel().getSelectedItem(), |
| 192 | + title.getText(), Arrays.asList(tags.getText().split(",")), description.getText(), null, true); |
| 193 | + result.addDoneListener(() -> { |
| 194 | + try { |
| 195 | + PortalItem portalItem = result.get(); |
| 196 | + showMessage("Save Successful", "Map titled " + title.getText() + " saved to portal item with id: " + |
| 197 | + portalItem.getItemId(), Alert.AlertType.INFORMATION); |
| 198 | + } catch (InterruptedException | ExecutionException e) { |
| 199 | + showMessage("Save Unscuccessful", e.getCause().getMessage(), Alert.AlertType.ERROR); |
| 200 | + } finally { |
| 201 | + progress.setVisible(false); |
| 202 | + } |
| 203 | + }); |
| 204 | + } catch (Exception e) { |
| 205 | + progress.setVisible(false); |
| 206 | + showMessage("Could not save map", e.getMessage(), Alert.AlertType.ERROR); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Display an alert to the user with the specified information. |
| 212 | + * @param title alert title |
| 213 | + * @param description alert content description |
| 214 | + * @param type alert type |
| 215 | + */ |
| 216 | + private void showMessage(String title, String description, Alert.AlertType type) { |
| 217 | + Alert alert = new Alert(type); |
| 218 | + alert.setTitle(title); |
| 219 | + alert.setContentText(description); |
| 220 | + alert.show(); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Stops and releases all resources used in application. |
| 225 | + */ |
| 226 | + void terminate() { |
| 227 | + |
| 228 | + if (mapView != null) { |
| 229 | + mapView.dispose(); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | +} |
0 commit comments