Skip to content

Commit ca253c1

Browse files
authored
Add dispose method to preview map view (#674)
* add dispose method to preview map view * local variable replace
1 parent 566bdf1 commit ca253c1

File tree

2 files changed

+83
-86
lines changed

2 files changed

+83
-86
lines changed

tiled_layers/export-tiles/src/main/java/com/esri/samples/export_tiles/ExportTilesSample.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public void start(Stage stage) {
119119
Layer layer = map.getBasemap().getBaseLayers().get(0);
120120
if (layer instanceof ArcGISTiledLayer) {
121121
ArcGISTiledLayer tiledLayer = (ArcGISTiledLayer) layer;
122-
123122
// create progress bar to show task progress
124123
var progressBar = new ProgressBar(0.0);
125124
progressBar.setVisible(false);
@@ -166,13 +165,15 @@ public void start(Stage stage) {
166165
preview.initOwner(mapView.getScene().getWindow());
167166
preview.setTitle("Preview");
168167
preview.setHeaderText("Exported to " + tileCache.getPath());
169-
MapView mapPreview = new MapView();
170-
mapPreview.setMinSize(400, 400);
168+
MapView previewMapView = new MapView();
169+
previewMapView.setMinSize(400, 400);
171170
ArcGISTiledLayer tiledLayerPreview = new ArcGISTiledLayer(tileCache);
172171
ArcGISMap previewMap = new ArcGISMap(new Basemap(tiledLayerPreview));
173-
mapPreview.setMap(previewMap);
174-
preview.getDialogPane().setContent(mapPreview);
172+
previewMapView.setMap(previewMap);
173+
preview.getDialogPane().setContent(previewMapView);
175174
preview.show();
175+
// dispose of the preview mapview's resources
176+
preview.setOnCloseRequest(event -> previewMapView.dispose());
176177

177178
} else {
178179
new Alert(Alert.AlertType.ERROR, exportTileCacheJob.getError().getAdditionalMessage()).show();

tiled_layers/export-vector-tiles/src/main/java/com/esri/samples/export_vector_tiles/ExportVectorTilesSample.java

Lines changed: 77 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.esri.arcgisruntime.geometry.Envelope;
4040
import com.esri.arcgisruntime.geometry.Point;
4141
import com.esri.arcgisruntime.layers.ArcGISVectorTiledLayer;
42-
import com.esri.arcgisruntime.layers.Layer;
4342
import com.esri.arcgisruntime.loadable.LoadStatus;
4443
import com.esri.arcgisruntime.mapping.ArcGISMap;
4544
import com.esri.arcgisruntime.mapping.Basemap;
@@ -49,17 +48,14 @@
4948
import com.esri.arcgisruntime.mapping.view.Graphic;
5049
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
5150
import com.esri.arcgisruntime.mapping.view.MapView;
52-
import com.esri.arcgisruntime.portal.PortalItem;
5351
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
54-
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesJob;
5552
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesParameters;
5653
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesResult;
5754
import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesTask;
5855

5956
public class ExportVectorTilesSample extends Application {
6057

6158
private MapView mapView;
62-
private PortalItem portalItem; // keep loadable in scope to avoid garbage collection
6359

6460
@Override
6561
public void start(Stage stage) {
@@ -138,85 +134,85 @@ public void start(Stage stage) {
138134
if (layer instanceof ArcGISVectorTiledLayer) {
139135
ArcGISVectorTiledLayer vectorTiledLayer = (ArcGISVectorTiledLayer) layer;
140136

141-
// when the button is clicked, export the tiles to a temporary file
142-
exportVectorTilesButton.setOnAction(e -> {
143-
try {
144-
// disable the button and show the progress bar
145-
exportVectorTilesButton.setDisable(true);
146-
progressBar.setVisible(true);
147-
148-
// create temporary files for the .vtpk file and style item resources
149-
File vtpkFile = File.createTempFile("tiles", ".vtpk");
150-
File resDir = Files.createTempDirectory("StyleItemResources").toFile();
151-
vtpkFile.deleteOnExit();
152-
resDir.deleteOnExit();
153-
154-
// create a new export vector tiles task
155-
var exportVectorTilesTask = new ExportVectorTilesTask(vectorTiledLayer.getUri());
156-
157-
// create parameters for the export vector tiles job
158-
double mapScale = mapView.getMapScale();
159-
// the max scale parameter is set to 10% of the map's scale to limit the
160-
// number of tiles exported to within the vector tiled layer's max tile export limit
161-
ListenableFuture<ExportVectorTilesParameters> exportVectorTilesParametersFuture = exportVectorTilesTask
162-
.createDefaultExportVectorTilesParametersAsync(downloadArea.getGeometry(), mapScale * 0.1);
163-
164-
exportVectorTilesParametersFuture.addDoneListener(() -> {
165-
try {
166-
var exportVectorTilesParameters = exportVectorTilesParametersFuture.get();
167-
168-
// create a job with the parameters
169-
var exportVectorTilesJob =
170-
exportVectorTilesTask.exportVectorTiles(exportVectorTilesParameters, vtpkFile.getAbsolutePath(), resDir.getAbsolutePath());
171-
172-
// start the job and wait for it to finish
173-
exportVectorTilesJob.start();
174-
exportVectorTilesJob.addProgressChangedListener(() -> progressBar.setProgress(exportVectorTilesJob.getProgress() / 100.0));
175-
exportVectorTilesJob.addJobDoneListener(() -> {
176-
177-
if (exportVectorTilesJob.getStatus() == Job.Status.SUCCEEDED) {
178-
// show preview of exported tiles in alert
179-
ExportVectorTilesResult tilesResult = exportVectorTilesJob.getResult();
180-
VectorTileCache tileCache = tilesResult.getVectorTileCache();
181-
ItemResourceCache resourceCache = tilesResult.getItemResourceCache();
182-
Alert preview = new Alert(Alert.AlertType.INFORMATION);
183-
preview.initOwner(mapView.getScene().getWindow());
184-
preview.setTitle("Preview");
185-
preview.setHeaderText("Exported tiles to " + tileCache.getPath() + "\nExported resources to " +
186-
resourceCache.getPath());
187-
MapView mapPreview = new MapView();
188-
mapPreview.setMinSize(400, 400);
189-
ArcGISVectorTiledLayer vectorTiledLayerPreview = new ArcGISVectorTiledLayer(tileCache, resourceCache);
190-
ArcGISMap previewMap = new ArcGISMap(new Basemap(vectorTiledLayerPreview));
191-
mapPreview.setMap(previewMap);
192-
preview.getDialogPane().setContent(mapPreview);
193-
preview.show();
194-
195-
} else {
196-
Alert alert = new Alert(Alert.AlertType.ERROR, exportVectorTilesJob.getError().getAdditionalMessage());
197-
alert.show();
198-
}
199-
200-
// reset the UI
137+
// when the button is clicked, export the tiles to a temporary file
138+
exportVectorTilesButton.setOnAction(e -> {
139+
try {
140+
// disable the button and show the progress bar
141+
exportVectorTilesButton.setDisable(true);
142+
progressBar.setVisible(true);
143+
144+
// create temporary files for the .vtpk file and style item resources
145+
File vtpkFile = File.createTempFile("tiles", ".vtpk");
146+
File resDir = Files.createTempDirectory("StyleItemResources").toFile();
147+
vtpkFile.deleteOnExit();
148+
resDir.deleteOnExit();
149+
150+
// create a new export vector tiles task
151+
var exportVectorTilesTask = new ExportVectorTilesTask(vectorTiledLayer.getUri());
152+
153+
// create parameters for the export vector tiles job
154+
double mapScale = mapView.getMapScale();
155+
// the max scale parameter is set to 10% of the map's scale to limit the
156+
// number of tiles exported to within the vector tiled layer's max tile export limit
157+
ListenableFuture<ExportVectorTilesParameters> exportVectorTilesParametersFuture = exportVectorTilesTask
158+
.createDefaultExportVectorTilesParametersAsync(downloadArea.getGeometry(), mapScale * 0.1);
159+
160+
exportVectorTilesParametersFuture.addDoneListener(() -> {
161+
try {
162+
var exportVectorTilesParameters = exportVectorTilesParametersFuture.get();
163+
164+
// create a job with the parameters
165+
var exportVectorTilesJob =
166+
exportVectorTilesTask.exportVectorTiles(exportVectorTilesParameters, vtpkFile.getAbsolutePath(), resDir.getAbsolutePath());
167+
168+
// start the job and wait for it to finish
169+
exportVectorTilesJob.start();
170+
exportVectorTilesJob.addProgressChangedListener(() -> progressBar.setProgress(exportVectorTilesJob.getProgress() / 100.0));
171+
exportVectorTilesJob.addJobDoneListener(() -> {
172+
173+
if (exportVectorTilesJob.getStatus() == Job.Status.SUCCEEDED) {
174+
// show preview of exported tiles in alert
175+
ExportVectorTilesResult tilesResult = exportVectorTilesJob.getResult();
176+
VectorTileCache tileCache = tilesResult.getVectorTileCache();
177+
ItemResourceCache resourceCache = tilesResult.getItemResourceCache();
178+
Alert preview = new Alert(Alert.AlertType.INFORMATION);
179+
preview.initOwner(mapView.getScene().getWindow());
180+
preview.setTitle("Preview");
181+
preview.setHeaderText("Exported tiles to " + tileCache.getPath() + "\nExported resources to " +
182+
resourceCache.getPath());
183+
MapView previewMapView = new MapView();
184+
previewMapView.setMinSize(400, 400);
185+
ArcGISVectorTiledLayer vectorTiledLayerPreview = new ArcGISVectorTiledLayer(tileCache, resourceCache);
186+
ArcGISMap previewMap = new ArcGISMap(new Basemap(vectorTiledLayerPreview));
187+
previewMapView.setMap(previewMap);
188+
preview.getDialogPane().setContent(previewMapView);
189+
preview.show();
190+
// dispose of the preview mapview's resources
191+
preview.setOnCloseRequest(event -> previewMapView.dispose());
192+
193+
} else {
194+
new Alert(Alert.AlertType.ERROR, exportVectorTilesJob.getError().getAdditionalMessage()).show();
195+
}
196+
197+
// reset the UI
198+
progressBar.setVisible(false);
199+
progressBar.setProgress(0);
200+
exportVectorTilesButton.setDisable(false);
201+
});
202+
203+
} catch (InterruptedException | ExecutionException ex) {
204+
new Alert(Alert.AlertType.ERROR, ex.getMessage()).show();
201205
progressBar.setVisible(false);
202206
progressBar.setProgress(0);
203-
exportVectorTilesButton.setDisable(false);
204-
});
205-
206-
} catch (InterruptedException | ExecutionException ex) {
207-
Alert alert = new Alert(Alert.AlertType.ERROR, ex.getMessage());
208-
alert.show();
209-
progressBar.setVisible(false);
210-
progressBar.setProgress(0);
211-
}
212-
});
213-
214-
} catch (IOException ex) {
215-
Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to create temporary file");
216-
alert.show();
217-
}
218-
});
219-
}
207+
}
208+
});
209+
210+
} catch (IOException ex) {
211+
Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to create temporary file");
212+
alert.show();
213+
}
214+
});
215+
}
220216

221217
} else {
222218
new Alert(Alert.AlertType.ERROR, "Map could not be loaded").show();

0 commit comments

Comments
 (0)