+ setupDrawMapEnabled();
+ setupBackgroundColor();
+ setupColoringObjects();
+ setupColorBarOptions();
+ setupSizingObjects();
+ setupSizeBarOptions();
+
+ // add listeners
+ DistrictDiagramViewerListener[] listeners = Activator.getInstance().getDistrictDiagramViewerListeners();
+ if (listeners != null) {
+ for (DistrictDiagramViewerListener listener : listeners) {
+ listener.diagramLoaded(getRuntime(), canvasContext);
+ }
+ }
+ }
+
+ @Override
+ public void dispose() {
+ DistrictDiagramViewerListener[] listeners = Activator.getInstance().getDistrictDiagramViewerListeners();
+ if (listeners != null) {
+ Resource runtime = getRuntime();
+ for (DistrictDiagramViewerListener listener : listeners) {
+ listener.diagramDisposed(runtime, canvasContext);
+ }
+ }
+ super.dispose();
+ }
+
+ 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 void setupColoringObjects() {
+ sessionContext.getSession().asyncRequest(new ColoringObjectsRequest(getInputResource()), new ColoringObjectsListener(
+ result -> queueColoringObjectsChangeEvent(result),
+ () -> DistrictDiagramViewer.this.isDisposed()));
+ }
+
+
+ private void setupColorBarOptions() {
+ sessionContext.getSession().asyncRequest(new ColorBarOptionsRequest(getInputResource()), new ColorBarOptionsListener(
+ result -> queueColorBarOptionsChangeEvent(result),
+ () -> DistrictDiagramViewer.this.isDisposed()));
+ }
+
+ private void setupSizingObjects() {
+ sessionContext.getSession().asyncRequest(new SizingObjectsRequest(getInputResource()), new SizingObjectsListener(
+ result -> queueSizingObjectsChangeEvent(result),
+ () -> DistrictDiagramViewer.this.isDisposed()));
+ }
+
+
+ private void setupSizeBarOptions() {
+ sessionContext.getSession().asyncRequest(new SizeBarOptionsRequest(getInputResource()), new SizeBarOptionsListener(
+ result -> queueSizeBarOptionsChangeEvent(result),
+ () -> DistrictDiagramViewer.this.isDisposed()));
+ }
+
+ public static final Key KEY_MAP_COLOR_BAR_OPTIONS = new KeyOf(ColorBarOptions.class, "colorBarOptions");
+ public static final Command MAP_COLOR_BAR_OPTIONS_CHANGE = new Command("colorBarOptionsChange");
+ public static final Key KEY_MAP_SIZE_BAR_OPTIONS = new KeyOf(SizeBarOptions.class, "sizeBarOptions");
+ public static final Command MAP_SIZE_BAR_OPTIONS_CHANGE = new Command("sizeBarOptionsChange");
+
+ public static final Key KEY_MAP_COLORING_OBJECTS = new KeyOf(Map.class, "coloringObjects");
+ public static final Command MAP_COLORING_OBJECTS_CHANGE = new Command("coloringObjectsChange");
+
+ public static final Key KEY_MAP_SIZING_OBJECTS = new KeyOf(Map.class, "sizingObjects");
+ public static final Command MAP_SIZING_OBJECTS_CHANGE = new Command("sizingObjectsChange");
+
+
+ private void queueColoringObjectsChangeEvent(Map<String, DynamicColorContribution> result) {
+ queueEventInternal(KEY_MAP_COLORING_OBJECTS, MAP_COLORING_OBJECTS_CHANGE, result);
+ }
+
+ private void queueColorBarOptionsChangeEvent(ColorBarOptions result) {
+ queueEventInternal(KEY_MAP_COLOR_BAR_OPTIONS, MAP_COLOR_BAR_OPTIONS_CHANGE, result);
+ }
+
+ private void queueSizingObjectsChangeEvent(Map<String, DynamicSizeContribution> result) {
+ queueEventInternal(KEY_MAP_SIZING_OBJECTS, MAP_SIZING_OBJECTS_CHANGE, result);
+ }
+
+ private void queueSizeBarOptionsChangeEvent(SizeBarOptions result) {
+ queueEventInternal(KEY_MAP_SIZE_BAR_OPTIONS, MAP_SIZE_BAR_OPTIONS_CHANGE, result);
+ }
+
+ private void queueEventInternal(Key key, Command command, Object result) {
+ if (result != null && !canvasContext.isDisposed()) {
+ canvasContext.getThreadAccess().asyncExec(() -> {
+ canvasContext.getDefaultHintContext().setHint(key, result);
+ canvasContext.getEventQueue().queueEvent(new CommandEvent(canvasContext, System.currentTimeMillis(), command));
+ });
+ } else {
+ LOGGER.info("Result is either null or canvasContext is disposed", String.valueOf(result));
+ }
+ }
+
+ 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;
+
+ private Boolean lastResult;
+
+ public DrawMapEnabledListener(Consumer<Boolean> callback, Supplier<Boolean> isDisposed) {
+ this.callback = callback;
+ this.isDisposed = isDisposed;
+ }
+
+ @Override
+ public void execute(Boolean result) {
+ // Minor optimization
+ if (!Objects.equals(lastResult, result)) {
+ lastResult = 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(MapBackgroundColorListener.class);
+
+ private Consumer<RGB.Integer> callback;
+ private Supplier<Boolean> isDisposed;
+
+ private RGB.Integer lastResult;