- sessionContext.getSession().asyncRequest(new UniqueRead<Boolean>() {
-
- @Override
- public Boolean perform(ReadGraph graph) throws DatabaseException {
- return DistrictNetworkUtil.drawMapEnabled(graph, getInputResource());
- }
- }, new Listener<Boolean>() {
-
- @Override
- public void execute(Boolean result) {
- canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), result ? Commands.MAP_ENABLE : Commands.MAP_DISABLE));
- }
-
- @Override
- public void exception(Throwable t) {
- LOGGER.error("Could not listen draw map for {}", getInputResource(), t);
- }
-
- @Override
- public boolean isDisposed() {
- return DistrictDiagramViewer.this.isDisposed();
- }
- });
+ setupDrawMapEnabled();
+ setupBackgroundColor();
+ }
+
+ private void setupDrawMapEnabled() {
+ sessionContext.getSession().asyncRequest(new DrawMapEnabledRequest(getInputResource()), new DrawMapEnabledListener(
+ result -> canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), result ? Commands.MAP_ENABLE : Commands.MAP_DISABLE)),
+ () -> DistrictDiagramViewer.this.isDisposed()));
+ }
+
+ private void setupBackgroundColor() {
+ sessionContext.getSession().asyncRequest(new MapBackgroundColorRequest(getInputResource()), new MapBackgroundColorListener(
+ result -> queueBackgroundColorChangeEvent(result),
+ () -> DistrictDiagramViewer.this.isDisposed()));
+ }
+
+ private void queueBackgroundColorChangeEvent(RGB.Integer result) {
+ if (result != null) {
+ Color backgroundColor = new Color(result.red, result.green, result.blue);
+ canvasContext.getDefaultHintContext().setHint(MapCommands.KEY_MAP_BACKGROUND_COLOR, backgroundColor);
+ canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), MapCommands.MAP_BACKGROUND_COLOR_CHANGE));
+ }
+ }
+
+ private static class DrawMapEnabledRequest extends UnaryRead<Resource, Boolean> {
+
+ public DrawMapEnabledRequest(Resource diagram) {
+ super(diagram);
+ }
+
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ return DistrictNetworkUtil.drawMapEnabled(graph, parameter);
+ }
+ }
+
+ private static class DrawMapEnabledListener implements Listener<Boolean> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DrawMapEnabledListener.class);
+
+ private Consumer<Boolean> callback;
+ private Supplier<Boolean> isDisposed;
+
+ public DrawMapEnabledListener(Consumer<Boolean> callback, Supplier<Boolean> isDisposed) {
+ this.callback = callback;
+ this.isDisposed = isDisposed;
+ }
+
+ @Override
+ public void execute(Boolean result) {
+ callback.accept(result);
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ LOGGER.error("Could not listen if draw map is enabled", t);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return isDisposed.get();
+ }
+ }
+
+ private static class MapBackgroundColorRequest extends UnaryRead<Resource, RGB.Integer> {
+
+ public MapBackgroundColorRequest(Resource diagram) {
+ super(diagram);
+ }
+
+ @Override
+ public RGB.Integer perform(ReadGraph graph) throws DatabaseException {
+ return DistrictNetworkUtil.backgroundColor(graph, parameter);
+ }
+ }
+
+ private static class MapBackgroundColorListener implements Listener<RGB.Integer> {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DrawMapEnabledListener.class);
+
+ private Consumer<RGB.Integer> callback;
+ private Supplier<Boolean> isDisposed;
+
+ public MapBackgroundColorListener(Consumer<RGB.Integer> callback, Supplier<Boolean> isDisposed) {
+ this.callback = callback;
+ this.isDisposed = isDisposed;
+ }
+
+ @Override
+ public void execute(RGB.Integer result) {
+ callback.accept(result);
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ LOGGER.error("Could not listen map background color", t);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return isDisposed.get();
+ }