Skip to content

Commit 90b7ec2

Browse files
committed
initial implementation
1 parent d93c5ec commit 90b7ec2

File tree

8 files changed

+617
-0
lines changed

8 files changed

+617
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.ButtonType;
22+
import javafx.scene.control.Dialog;
23+
import javafx.scene.control.TextField;
24+
25+
import com.esri.arcgisruntime.security.OAuthConfiguration;
26+
27+
/**
28+
* Custom dialog for getting an OAuthConfiguration.
29+
*/
30+
class AuthenticationDialog extends Dialog<OAuthConfiguration> {
31+
32+
@FXML
33+
private TextField portalURL;
34+
@FXML
35+
private TextField clientID;
36+
@FXML
37+
private TextField redirectURI;
38+
@FXML
39+
private ButtonType cancelButton;
40+
@FXML
41+
private ButtonType continueButton;
42+
43+
AuthenticationDialog() {
44+
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/create_and_save_map_auth_dialog.fxml"));
45+
loader.setRoot(this);
46+
loader.setController(this);
47+
48+
try {
49+
loader.load();
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
53+
54+
setResultConverter(dialogButton -> {
55+
try {
56+
return dialogButton == continueButton ? new OAuthConfiguration(portalURL.getText(), clientID.getText(),
57+
redirectURI.getText()) : null;
58+
} catch (Exception e) {
59+
return null;
60+
}
61+
});
62+
}
63+
64+
}
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.SelectionMode;
45+
import javafx.scene.control.TextArea;
46+
import javafx.scene.control.TextField;
47+
import javafx.util.StringConverter;
48+
49+
public class CreateAndSaveMapController {
50+
51+
52+
@FXML
53+
private MapView mapView;
54+
@FXML
55+
private TextField title;
56+
@FXML
57+
private TextField tags;
58+
@FXML
59+
private TextArea description;
60+
@FXML
61+
private ComboBox<PortalFolder> folderList;
62+
@FXML
63+
private ListView<Basemap> basemapList;
64+
@FXML
65+
private ListView<Layer> layersList;
66+
@FXML
67+
private Button saveButton;
68+
69+
private ArcGISMap map;
70+
private Portal portal;
71+
72+
@FXML
73+
private void initialize() {
74+
75+
// set basemap options
76+
basemapList.getItems().addAll(Basemap.createStreets(), Basemap.createImagery(), Basemap
77+
.createTopographic(), Basemap.createOceans());
78+
79+
// update basemap when selection changes
80+
basemapList.getSelectionModel().select(0);
81+
basemapList.getSelectionModel().selectedItemProperty().addListener(o ->
82+
map.setBasemap(basemapList.getSelectionModel().getSelectedItem())
83+
);
84+
85+
basemapList.setCellFactory(c -> new BasemapCell());
86+
87+
// create and set a map with the first basemap option
88+
map = new ArcGISMap(basemapList.getSelectionModel().getSelectedItem());
89+
mapView.setMap(map);
90+
91+
// set operational layer options
92+
String worldElevationService = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer";
93+
ArcGISMapImageLayer worldElevation = new ArcGISMapImageLayer(worldElevationService);
94+
worldElevation.loadAsync();
95+
96+
String worldCensusService = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer";
97+
ArcGISMapImageLayer worldCensus = new ArcGISMapImageLayer(worldCensusService);
98+
worldCensus.loadAsync();
99+
100+
layersList.getItems().addAll(worldElevation, worldCensus);
101+
layersList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
102+
103+
layersList.getSelectionModel().selectedItemProperty().addListener(o -> {
104+
map.getOperationalLayers().clear();
105+
map.getOperationalLayers().addAll(layersList.getSelectionModel().getSelectedItems());
106+
});
107+
108+
layersList.setCellFactory(c -> new LayerCell());
109+
110+
// set portal folder title converter
111+
folderList.setConverter(new StringConverter<PortalFolder>() {
112+
@Override
113+
public String toString(PortalFolder folder) {
114+
return folder.getTitle();
115+
}
116+
117+
@Override
118+
public PortalFolder fromString(String string) {
119+
return null;
120+
}
121+
});
122+
}
123+
124+
/**
125+
* Open a dialog to create and log into a portal.
126+
*/
127+
void authenticate() {
128+
129+
Dialog<OAuthConfiguration> authenticationDialog = new AuthenticationDialog();
130+
authenticationDialog.showAndWait().ifPresent(configuration -> {
131+
132+
AuthenticationManager.addOAuthConfiguration(configuration);
133+
134+
// setup the handler that will prompt an authentication challenge to the user
135+
AuthenticationManager.setAuthenticationChallengeHandler(new OAuthChallengeHandler());
136+
137+
portal = new Portal("http://" + configuration.getPortalUrl(), true);
138+
portal.addDoneLoadingListener(() -> {
139+
if (portal.getLoadStatus() == LoadStatus.LOADED) {
140+
try {
141+
PortalUserContent portalUserContent = portal.getUser().fetchContentAsync().get();
142+
List<PortalFolder> portalFolders = portalUserContent.getFolders();
143+
folderList.getItems().addAll(portalFolders);
144+
} catch (Exception e) {
145+
e.printStackTrace();
146+
}
147+
148+
saveButton.setDisable(false);
149+
150+
} else if (portal.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
151+
152+
// show alert message on error
153+
showMessage("Authentication failed", portal.getLoadError().getMessage(), Alert.AlertType.ERROR);
154+
}
155+
});
156+
157+
// loading the portal info of a secured resource
158+
// this will invoke the authentication challenge
159+
portal.loadAsync();
160+
});
161+
162+
}
163+
164+
/**
165+
* Shows a Basemap title in a ListView.
166+
*/
167+
private class BasemapCell extends ListCell<Basemap> {
168+
@Override
169+
protected void updateItem(Basemap basemap, boolean empty) {
170+
super.updateItem(basemap, empty);
171+
setText(empty || basemap == null ? null : basemap.getName());
172+
setGraphic(null);
173+
}
174+
}
175+
176+
/**
177+
* Shows a Layer title in a ListView.
178+
*/
179+
private class LayerCell extends ListCell<Layer> {
180+
@Override
181+
protected void updateItem(Layer layer, boolean empty) {
182+
super.updateItem(layer, empty);
183+
setText(empty || layer == null ? null : layer.getName());
184+
setGraphic(null);
185+
}
186+
}
187+
188+
/**
189+
* Save the map to the portal.
190+
*/
191+
@FXML
192+
private void saveMap() {
193+
try {
194+
ListenableFuture<PortalItem> result = map.saveAsAsync(portal, folderList.getSelectionModel().getSelectedItem(),
195+
title.getText(), Arrays.asList(tags.getText().split(",")), description.getText(), null, true);
196+
result.addDoneListener(() -> {
197+
try {
198+
PortalItem portalItem = result.get();
199+
showMessage("Save Successful", "Map titled " + title.getText() + " saved to portal item with id: " +
200+
portalItem.getItemId(), Alert.AlertType.INFORMATION);
201+
} catch (InterruptedException | ExecutionException e) {
202+
showMessage("Save Unscuccessful", e.getCause().getMessage(), Alert.AlertType.ERROR);
203+
}
204+
});
205+
} catch (Exception e) {
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)