Skip to content

Commit 96aade0

Browse files
JonLavitschie
andauthored
Update Graphic - Update code and improve UI (#507)
Co-authored-by: Tyler Schiewe <[email protected]>
1 parent 943f006 commit 96aade0

File tree

1 file changed

+54
-57
lines changed

1 file changed

+54
-57
lines changed

display_information/update-graphics/src/main/java/com/esri/samples/update_graphics/UpdateGraphicsSample.java

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package com.esri.samples.update_graphics;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import java.util.Optional;
22-
2319
import javafx.application.Application;
2420
import javafx.geometry.Insets;
2521
import javafx.geometry.Point2D;
@@ -38,11 +34,13 @@
3834
import javafx.scene.layout.VBox;
3935
import javafx.scene.paint.Paint;
4036
import javafx.stage.Stage;
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
import java.util.Optional;
4140

4241
import com.esri.arcgisruntime.concurrent.ListenableFuture;
4342
import com.esri.arcgisruntime.geometry.Point;
4443
import com.esri.arcgisruntime.geometry.SpatialReference;
45-
import com.esri.arcgisruntime.loadable.LoadStatus;
4644
import com.esri.arcgisruntime.mapping.ArcGISMap;
4745
import com.esri.arcgisruntime.mapping.Basemap;
4846
import com.esri.arcgisruntime.mapping.view.Graphic;
@@ -55,6 +53,9 @@ public class UpdateGraphicsSample extends Application {
5553

5654
private boolean isUpdateLocationActive;
5755
private List<SimpleMarkerSymbol> markers;
56+
private Button updateLocationButton;
57+
private Button updateDescriptionButton;
58+
private ComboBox<String> symbolBox;
5859

5960
private ArcGISMap map; // keep loadable in scope to avoid garbage collection
6061
private MapView mapView;
@@ -87,52 +88,23 @@ public void start(Stage stage) {
8788
// create a control panel
8889
VBox controlsVBox = new VBox(6);
8990
controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,
90-
Insets.EMPTY)));
91+
Insets.EMPTY)));
9192
controlsVBox.setPadding(new Insets(10.0));
9293
controlsVBox.setMaxSize(180, 150);
9394
controlsVBox.getStyleClass().add("panel-region");
9495

9596
// create buttons for user interaction
96-
Button updateLocationButton = new Button("Update Location");
97-
Button updateDescriptionButton = new Button("Update Description");
97+
updateLocationButton = new Button("Update Location");
98+
updateDescriptionButton = new Button("Update Description");
9899
updateLocationButton.setMaxWidth(Double.MAX_VALUE);
99100
updateDescriptionButton.setMaxWidth(Double.MAX_VALUE);
100101
updateLocationButton.setDisable(true);
101102
updateDescriptionButton.setDisable(true);
102103

103-
// when clicked allow user to move graphic's location
104-
updateLocationButton.setOnAction(e -> {
105-
if (selectedGraphic.isSelected()) {
106-
isUpdateLocationActive = true;
107-
}
108-
});
109-
110-
updateDescriptionButton.setOnAction(e -> {
111-
if (selectedGraphic.isSelected()) {
112-
// get attributes from selected graphic
113-
java.util.Map<String, Object> attributes = selectedGraphic.getAttributes();
114-
115-
// create input dialog
116-
TextInputDialog dialog = new TextInputDialog();
117-
dialog.setTitle(attributes.get("NAME").toString());
118-
dialog.setGraphic(null);
119-
dialog.setHeaderText(attributes.get("DESCRIPTION").toString());
120-
dialog.setContentText("New Description");
121-
122-
// set the graphic's description is text entered
123-
Optional<String> result = dialog.showAndWait();
124-
result.ifPresent(text -> {
125-
if (!text.isEmpty()) {
126-
attributes.put("DESCRIPTION", text);
127-
}
128-
});
129-
}
130-
});
131-
132104
// create section for combo box
133105
Label symbolLabel = new Label("Update Symbol:");
134106
symbolLabel.getStyleClass().add("panel-label");
135-
ComboBox<String> symbolBox = new ComboBox<>();
107+
symbolBox = new ComboBox<>();
136108
symbolBox.getItems().addAll("CIRCLE", "TRIANGLE", "CROSS", "DIAMOND");
137109
symbolBox.getSelectionModel().selectFirst();
138110
symbolBox.setMaxWidth(Double.MAX_VALUE);
@@ -148,37 +120,53 @@ public void start(Stage stage) {
148120
// add label, dropdown and buttons to the control panel
149121
controlsVBox.getChildren().addAll(updateLocationButton, updateDescriptionButton, symbolLabel, symbolBox);
150122

151-
// create a graphics overlay
152-
graphicsOverlay = new GraphicsOverlay();
153-
selectedGraphic = new Graphic();
154-
155123
// create a ArcGISMap with basemap light gray canvas
156124
map = new ArcGISMap(Basemap.Type.LIGHT_GRAY_CANVAS, 56.075844, -2.681572, 13);
157125

158-
// enable buttons when map view is done loading
159-
map.addDoneLoadingListener(() -> {
160-
if (map.getLoadStatus() == LoadStatus.LOADED) {
161-
symbolBox.setDisable(false);
162-
updateLocationButton.setDisable(false);
163-
updateDescriptionButton.setDisable(false);
164-
} else {
165-
Alert alert = new Alert(Alert.AlertType.ERROR, "Map Failed to Load!");
166-
alert.show();
167-
}
168-
});
169-
170126
// create a map view and set the map to it
171127
mapView = new MapView();
172128
mapView.setMap(map);
173129

130+
// create a graphics overlay
131+
graphicsOverlay = new GraphicsOverlay();
132+
174133
// add graphics overlay to the map view
175134
mapView.getGraphicsOverlays().add(graphicsOverlay);
176135

177136
// create default graphics for graphics overlay
178137
createGraphics();
179138

139+
// when clicked allow user to move graphic's location
140+
updateLocationButton.setOnAction(e -> {
141+
if (selectedGraphic.isSelected()) {
142+
isUpdateLocationActive = true;
143+
}
144+
});
145+
146+
updateDescriptionButton.setOnAction(e -> {
147+
if (selectedGraphic.isSelected()) {
148+
// get attributes from selected graphic
149+
java.util.Map<String, Object> attributes = selectedGraphic.getAttributes();
150+
151+
// create input dialog
152+
TextInputDialog dialog = new TextInputDialog();
153+
dialog.setTitle(attributes.get("NAME").toString());
154+
dialog.setGraphic(null);
155+
dialog.setHeaderText(attributes.get("DESCRIPTION").toString());
156+
dialog.setContentText("New Description");
157+
158+
// set the graphic's description is text entered
159+
Optional<String> result = dialog.showAndWait();
160+
result.ifPresent(text -> {
161+
if (!text.isEmpty()) {
162+
attributes.put("DESCRIPTION", text);
163+
}
164+
});
165+
}
166+
});
167+
180168
mapView.setOnMouseClicked(e -> {
181-
if (e.getButton() == MouseButton.PRIMARY) {
169+
if (e.getButton() == MouseButton.PRIMARY && e.isStillSincePress()) {
182170
// clear any selected graphic
183171
graphicsOverlay.clearSelection();
184172

@@ -202,10 +190,13 @@ public void start(Stage stage) {
202190
selectedGraphic.setSelected(true);
203191
String style = ((SimpleMarkerSymbol) selectedGraphic.getSymbol()).getStyle().toString();
204192
symbolBox.getSelectionModel().select(style);
193+
194+
enableUI(false);
195+
} else {
196+
enableUI(true);
205197
}
206198
} catch (Exception x) {
207-
// on any error, display the stack trace
208-
x.printStackTrace();
199+
new Alert(Alert.AlertType.ERROR, "Error identifying clicked graphic").show();
209200
}
210201
});
211202
}
@@ -269,6 +260,12 @@ private void createGraphics() {
269260
}
270261
}
271262

263+
private void enableUI(boolean enable) {
264+
updateDescriptionButton.setDisable(enable);
265+
updateLocationButton.setDisable(enable);
266+
symbolBox.setDisable(enable);
267+
}
268+
272269
/**
273270
* Stops and releases all resources used in application.
274271
*/

0 commit comments

Comments
 (0)