Skip to contentMethod: getRootCause(Throwable)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * blueMarine III: Semantic DAM
5: * http://tidalwave.it/projects/bluemarine3
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/bluemarine3-src
22: * git clone https://github.com/tidalwave-it/bluemarine3-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.bluemarine3.mapviewer.impl;
27:
28: import jakarta.annotation.Nonnull;
29: import jakarta.annotation.PostConstruct;
30: import java.util.Optional;
31: import java.nio.file.Path;
32: import org.springframework.stereotype.Component;
33: import it.tidalwave.bluemarine3.mapviewer.spi.MapViewerPresentation;
34: import it.tidalwave.bluemarine3.mapviewer.spi.MapViewerPresentation.Bindings;
35: import it.tidalwave.dam.ui.message.EntitySelectedEvent;
36: import it.tidalwave.geo.Coordinates;
37: import it.tidalwave.geo.GeoTrack;
38: import it.tidalwave.geo.RectangularArea;
39: import it.tidalwave.ui.core.BoundProperty;
40: import it.tidalwave.ui.core.message.PanelShowRequest;
41: import it.tidalwave.ui.core.message.PanelShownNotification;
42: import it.tidalwave.ui.core.message.PowerOnEvent;
43: import it.tidalwave.ui.core.role.UserAction;
44: import it.tidalwave.util.PreferencesHandler;
45: import it.tidalwave.util.annotation.VisibleForTesting;
46: import it.tidalwave.messagebus.MessageBus;
47: import it.tidalwave.messagebus.annotation.ListensTo;
48: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
49: import lombok.RequiredArgsConstructor;
50: import lombok.extern.slf4j.Slf4j;
51: import static it.tidalwave.ui.core.UserNotification.notification;
52: import static it.tidalwave.ui.core.role.Displayable._Displayable_;
53:
54: /***************************************************************************************************************************************************************
55: *
56: * @stereotype PresentationControl
57: * @author Fabrizio Giudici
58: *
59: **************************************************************************************************************************************************************/
60: @Component @SimpleMessageSubscriber @RequiredArgsConstructor @Slf4j
61: public class DefaultMapViewerPresentationControl
62: {
63: private static final Path CACHE_PATH = Path.of("cache/maps");
64:
65: private static final Coordinates DEFAULT_COORDINATES = Coordinates.of(44.4, 8.95);
66:
67: private static final int DEFAULT_ZOOM = 17;
68:
69: @Nonnull
70: private final MapViewerPresentation presentation;
71:
72: @Nonnull
73: private final MessageBus messageBus;
74:
75: @Nonnull
76: private final PreferencesHandler preferencesHandler;
77:
78: private final Bindings bindings = Bindings.builder()
79: .zoomIn(UserAction.of(this::zoomIn))
80: .zoomOut(UserAction.of(this::zoomOut))
81: .reframe(UserAction.of(this::reframe))
82: .zoom(new BoundProperty<>(7.0))
83: .mouseCoordinates(new BoundProperty<>(Coordinates.of(0, 0)))
84: .build();
85:
86: @Nonnull
87: @VisibleForTesting private Optional<RectangularArea> area = Optional.empty();
88:
89: /***********************************************************************************************************************************************************
90: *
91: **********************************************************************************************************************************************************/
92: @PostConstruct
93: @VisibleForTesting void initialize()
94: {
95: final var cacheFolder = preferencesHandler.getAppFolder().resolve(CACHE_PATH);
96: bindings.reframe.enabled().set(false);
97: presentation.initialize(bindings, cacheFolder);
98: bindings.mouseCoordinates.addPropertyChangeListener(evt -> renderCoordinates((Coordinates)evt.getNewValue()));
99: }
100:
101: /***********************************************************************************************************************************************************
102: *
103: **********************************************************************************************************************************************************/
104: @VisibleForTesting void onPowerOn (@ListensTo final PowerOnEvent event)
105: {
106: log.debug("onPowerOn({})", event);
107: presentation.renderMapAt(DEFAULT_COORDINATES, DEFAULT_ZOOM); // FIXME
108: }
109:
110: /***********************************************************************************************************************************************************
111: * When a previously hidden panel is shown, its size might change; hence it might need a refresh.
112: **********************************************************************************************************************************************************/
113: @VisibleForTesting void onPanelShown (@ListensTo final PanelShownNotification event)
114: {
115: if (event.getTarget() == presentation)
116: {
117: log.debug("onPanelShown({})", event);
118: area.ifPresent(presentation::fitTo);
119: }
120: }
121:
122: /***********************************************************************************************************************************************************
123: *
124: **********************************************************************************************************************************************************/
125: @VisibleForTesting void onTrackSelected (@ListensTo final EntitySelectedEvent event)
126: {
127: try
128: {
129: event.getEntity(GeoTrack.class).ifPresent(track ->
130: {
131: log.info("onTrackSelected({})", event);
132: presentation.showWaiting();
133: messageBus.publish(new PanelShowRequest(presentation));
134: final var area = track.getBoundingArea();
135: this.area = Optional.of(area);
136: presentation.renderTrack(track, area);
137: presentation.hideWaiting();
138: bindings.reframe.enabled().set(true);
139: });
140: }
141: catch (RuntimeException e)
142: {
143: log.error("onTrackSelected()", e);
144: area = Optional.empty();
145: presentation.removeTrack();
146: presentation.hideWaiting();
147: bindings.reframe.enabled().set(false);
148: presentation.notifyError(notification().withCaption(getClass(), "error")
149: .withText(getClass(), "cannotOpenFile", getMessage(e)));
150: }
151: }
152:
153: /***********************************************************************************************************************************************************
154: *
155: **********************************************************************************************************************************************************/
156: private void renderCoordinates (@Nonnull final Coordinates coordinates)
157: {
158: presentation.renderCoordinates(coordinates.as(_Displayable_).getDisplayName());
159: }
160:
161: /***********************************************************************************************************************************************************
162: *
163: **********************************************************************************************************************************************************/
164: private void zoomIn()
165: {
166: bindings.zoom.set(bindings.zoom.get() + 1);
167: }
168:
169: /***********************************************************************************************************************************************************
170: *
171: **********************************************************************************************************************************************************/
172: private void zoomOut()
173: {
174: bindings.zoom.set(bindings.zoom.get() - 1);
175: }
176:
177: /***********************************************************************************************************************************************************
178: *
179: **********************************************************************************************************************************************************/
180: private void reframe()
181: {
182: log.info("reframe()");
183: area.ifPresent(presentation::fitTo);
184: }
185:
186: /***********************************************************************************************************************************************************
187: *
188: **********************************************************************************************************************************************************/
189: @Nonnull
190: private static String getMessage (@Nonnull final Throwable e)
191: {
192: final var rootCause = getRootCause(e);
193: return Optional.ofNullable(rootCause.getLocalizedMessage()).orElse(rootCause.getClass().getSimpleName());
194: }
195:
196: /***********************************************************************************************************************************************************
197: *
198: **********************************************************************************************************************************************************/
199: @Nonnull
200: private static Throwable getRootCause (@Nonnull final Throwable e)
201: {
202:• return e.getCause() == null ? e : getRootCause(e.getCause());
203: }
204: }