Skip to content

Package: Tile

Tile

nameinstructionbranchcomplexitylinemethod
Tile(TileCache, TileSource, URI, int, int)
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
lambda$setImageByBitmap$1(Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$setImageByPath$0(Path, CountDownLatch)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
setImageByBitmap(Object)
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 3
75%
M: 0 C: 1
100%
setImageByPath(Path)
M: 19 C: 33
63%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 5 C: 8
62%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * MapView: a JavaFX map renderer for tile-based servers
5: * http://tidalwave.it/projects/mapview
6: *
7: * Copyright (C) 2024 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/mapview-src
22: * git clone https://github.com/tidalwave-it/mapview-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.mapview.javafx.impl;
27:
28: import jakarta.annotation.Nonnull;
29: import jakarta.annotation.Nullable;
30: import java.util.Optional;
31: import java.util.concurrent.CountDownLatch;
32: import java.util.concurrent.TimeUnit;
33: import java.nio.file.Path;
34: import java.net.URI;
35: import javafx.scene.image.Image;
36: import javafx.scene.image.ImageView;
37: import javafx.application.Platform;
38: import it.tidalwave.mapview.TileSource;
39: import it.tidalwave.mapview.impl.TileCache;
40: import it.tidalwave.mapview.impl.AbstractTile;
41: import lombok.Getter;
42: import lombok.extern.slf4j.Slf4j;
43:
44: /***************************************************************************************************************************************************************
45: *
46: * This class represents a single tile for rendering a map. It basically wraps a bitmap that is available on the internet at a specific URL, and it has the
47: * capability of being loaded in background and rendering some temporary icons while the process has not terminated.
48: *
49: * @author Fabrizio Giudici
50: *
51: **************************************************************************************************************************************************************/
52: @Getter @Slf4j
53: public class Tile extends ImageView implements AbstractTile
54: {
55: public static final int CREATION_TIMEOUT = 2000;
56: /** The source of tile bitmaps. */
57: @Nonnull
58: private final TileSource source;
59:
60: /** The URL of this tile. */
61: private final URI uri;
62:
63: /** The zoom level this tile belongs to. */
64: private final int zoom;
65:
66: /***********************************************************************************************************************************************************
67: * Creates a new tile and submits it to the cache for downloading.
68: * @param tileCache the tile cache
69: * @param source the tile source
70: * @param uri the URL of the tile
71: * @param size the size of the tile
72: * @param zoom the zoom level of this tile
73: **********************************************************************************************************************************************************/
74: @SuppressWarnings("this-escape")
75: protected Tile (@Nonnull final TileCache tileCache, @Nonnull final TileSource source, @Nonnull final URI uri, final int size, final int zoom)
76: {
77: this.source = source;
78: this.uri = uri;
79: this.zoom = zoom;
80: setFitWidth(size);
81: setFitHeight(size);
82: tileCache.loadTileInBackground(this);
83: }
84:
85: /***********************************************************************************************************************************************************
86: * {@inheritDoc}
87: **********************************************************************************************************************************************************/
88: @Override @Nonnull
89: public Optional<Object> setImageByPath (@Nullable final Path path)
90: {
91:• if (path == null)
92: {
93: setImageByBitmap(null);
94: }
95:• else if (Platform.isFxApplicationThread())
96: {
97: setImage(new Image(path.toUri().toString()));
98: }
99: else
100: {
101: final var latch = new CountDownLatch(1);
102: Platform.runLater(() ->
103: {
104: setImage(new Image(path.toUri().toString()));
105: latch.countDown();
106: });
107:
108: try
109: {
110:• if (!latch.await(CREATION_TIMEOUT, TimeUnit.MILLISECONDS))
111: {
112: log.error("Time-out while setting the image");
113: }
114: }
115: catch (InterruptedException e)
116: {
117: log.error("Timeout when loading " + path + " for " + uri, e);
118: Thread.currentThread().interrupt();
119: }
120: }
121:
122: return Optional.ofNullable(getImage());
123: }
124:
125: /***********************************************************************************************************************************************************
126: * {@inheritDoc}
127: **********************************************************************************************************************************************************/
128: @Override
129: public void setImageByBitmap (@Nullable final Object image)
130: {
131:• if (Platform.isFxApplicationThread())
132: {
133: setImage((Image)image);
134: }
135: else
136: {
137: Platform.runLater(() -> setImage((Image)image));
138: }
139: }
140:
141: /***********************************************************************************************************************************************************
142: * {@inheritDoc}
143: **********************************************************************************************************************************************************/
144: @Override @Nonnull
145: public String toString()
146: {
147: return super.toString() + " - " + uri;
148: }
149: }