Skip to content

Commit 9f9c9ba

Browse files
authored
Merge pull request #105 from Esri/tyle8552/create_and_save_map
Create and Save Map
2 parents 61b1c36 + f5aab10 commit 9f9c9ba

File tree

8 files changed

+637
-0
lines changed

8 files changed

+637
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 javafx.fxml.FXML;
20+
import javafx.fxml.FXMLLoader;
21+
import javafx.scene.control.Alert;
22+
import javafx.scene.control.ButtonType;
23+
import javafx.scene.control.Dialog;
24+
import javafx.scene.control.TextField;
25+
26+
import com.esri.arcgisruntime.security.OAuthConfiguration;
27+
28+
/**
29+
* Custom dialog for getting an OAuthConfiguration.
30+
*/
31+
class AuthenticationDialog extends Dialog<OAuthConfiguration> {
32+
33+
@FXML
34+
private TextField portalURL;
35+
@FXML
36+
private TextField clientID;
37+
@FXML
38+
private TextField redirectURI;
39+
@FXML
40+
private ButtonType cancelButton;
41+
@FXML
42+
private ButtonType continueButton;
43+
44+
AuthenticationDialog() {
45+
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/create_and_save_map_auth_dialog.fxml"));
46+
loader.setRoot(this);
47+
loader.setController(this);
48+
49+
setTitle("Authenticate");
50+
51+
try {
52+
loader.load();
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
}
56+
57+
setResultConverter(dialogButton -> {
58+
if (dialogButton == continueButton) {
59+
if (!portalURL.getText().equals("") && !clientID.getText().equals("") && !redirectURI.getText().equals("")) {
60+
try {
61+
return new OAuthConfiguration(portalURL.getText(), clientID.getText(), redirectURI.getText());
62+
} catch (Exception e) {
63+
Alert alert = new Alert(Alert.AlertType.ERROR);
64+
alert.setContentText(e.getMessage());
65+
alert.show();
66+
}
67+
} else {
68+
Alert alert = new Alert(Alert.AlertType.ERROR);
69+
alert.setContentText("missing credentials");
70+
alert.show();
71+
}
72+
}
73+
return null;
74+
});
75+
}
76+
77+
}
377 KB
Loading
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.io.IOException;
20+
21+
import javafx.application.Application;
22+
import javafx.fxml.FXMLLoader;
23+
import javafx.scene.Parent;
24+
import javafx.scene.Scene;
25+
import javafx.stage.Stage;
26+
27+
public class CreateAndSaveMapSample extends Application {
28+
29+
private static CreateAndSaveMapController controller;
30+
31+
@Override
32+
public void start(Stage stage) throws IOException {
33+
// set up the scene
34+
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/create_and_save_map.fxml"));
35+
Parent root = loader.load();
36+
controller = loader.getController();
37+
Scene scene = new Scene(root);
38+
39+
// set up the stage
40+
stage.setTitle("Create and Save Map Sample");
41+
stage.setWidth(800);
42+
stage.setHeight(700);
43+
stage.setScene(scene);
44+
stage.show();
45+
46+
controller.authenticate();
47+
}
48+
49+
/**
50+
* Stops and releases all resources used in application.
51+
*/
52+
@Override
53+
public void stop() {
54+
controller.terminate();
55+
}
56+
57+
/**
58+
* Opens and runs application.
59+
*
60+
* @param args arguments passed to this application
61+
*/
62+
public static void main(String[] args) {
63+
64+
Application.launch(args);
65+
}
66+
}

0 commit comments

Comments
 (0)