org.simantics.maps.debug,
org.simantics.maps.eclipse,
org.simantics.maps.sg,
+ org.simantics.maps.sg.commands,
org.simantics.maps.tile,
org.simantics.maps.wms
Import-Package: org.simantics.scenegraph.g2d
*******************************************************************************/
package org.simantics.maps.eclipse;
+import java.awt.Color;
import java.awt.geom.AffineTransform;
import org.simantics.g2d.canvas.Hints;
import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
import org.simantics.maps.sg.MapNode;
import org.simantics.maps.sg.MapScaleNode;
+import org.simantics.maps.sg.commands.MapCommands;
import org.simantics.scenegraph.g2d.G2DParentNode;
import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
import org.simantics.scenegraph.g2d.events.command.CommandEvent;
*/
public static final Key KEY_MAP_ENABLED = new KeyOf(Boolean.class);
+ public static final Key KEY_MAP_BACKGROUND_COLOR = new KeyOf(Object.class);
+
public static final double ZOOM_IN_LIMIT = 10000000.0;
public static final double ZOOM_OUT_LIMIT = 10.0;
public void addedToContext(ICanvasContext ctx) {
super.addedToContext(ctx);
getHintStack().addKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
+ getHintStack().addKeyHintListener(getThread(), KEY_MAP_BACKGROUND_COLOR, mapListener);
}
@Override
public void removedFromContext(ICanvasContext ctx) {
getHintStack().removeKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
+ getHintStack().removeKeyHintListener(getThread(), KEY_MAP_BACKGROUND_COLOR, mapListener);
super.removedFromContext(ctx);
}
updateNode();
setDirty();
return true;
+ } else if (e.command.equals( MapCommands.MAP_BACKGROUND_COLOR_CHANGE )) {
+ ICanvasContext context = (ICanvasContext) e.getContext();
+ Color s = context.getHintStack().getHint(MapCommands.KEY_MAP_BACKGROUND_COLOR);
+ setBackgroundColor(s);
+ setDirty();
+ return true;
} else if (e.command.equals( Commands.MAP_DISABLE )) {
setEnabled(false);
updateNode();
protected void updateNode() {
node.setEnabled(isPaintingEnabled());
+ node.setBackgroundColor(getBackgroundColor());
}
boolean isPaintingEnabled() {
private void enablePainting() {
setHint(Hints.KEY_DISABLE_PAINTING, false);
}
+
+ private void setBackgroundColor(Color backgroundColor) {
+ setHint(KEY_MAP_BACKGROUND_COLOR, backgroundColor);
+ }
+
+ private Color getBackgroundColor() {
+ return getHint(KEY_MAP_BACKGROUND_COLOR);
+ }
}
private final int VIEWBOX_QUIET_TIME = 500;
protected Boolean enabled = true;
+ protected Color backgroundColor;
enum TileState {
NOT_LOADED,
this.enabled = enabled;
}
+ @SyncField("backgroundColor")
+ public void setBackgroundColor(Color color) {
+ this.backgroundColor = color;
+ }
+
@Override
public void render(Graphics2D g2d) {
- if (!enabled)
- return;
+
AffineTransform ot = g2d.getTransform();
g2d.transform(transform);
Rectangle2D b = (Rectangle2D)((Rectangle)g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS)).getBounds2D(); // getClipBounds is not accurate enough, use original clipbounds and scale here
Rectangle2D viewbox = new Rectangle2D.Double(offsetX/scaleX, offsetY/scaleY, b.getWidth()/sp.getWidth(), b.getHeight()/sp.getHeight()); //g.getClipBounds();
- if(viewbox == null) return; // FIXME
-
- double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE;
- int level = 0;
- double tileSize = 360 * MAP_SCALE*2;
- while (level < MAX_TILE_LEVEL) {
- double ratio = smallerViewboxDimension / tileSize;
- if (ratio >= 0.85) {
- break;
+
+ if (enabled) {
+
+ double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE;
+ int level = 0;
+ double tileSize = 360 * MAP_SCALE*2;
+ while (level < MAX_TILE_LEVEL) {
+ double ratio = smallerViewboxDimension / tileSize;
+ if (ratio >= 0.85) {
+ break;
+ }
+ tileSize *= 0.5;
+ level++;
}
- tileSize *= 0.5;
- level++;
- }
- /*
- * To convert y-coordinates to map coordinates in ruler, use:
- * double val = (y-offsetY)/scaleY;
- * val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
- * String str = formatValue(val);
- */
-
- double minx = Math.min(180, Math.max(viewbox.getMinX(), -180));
- double maxx = Math.min(180, Math.max(viewbox.getMaxX(), -180));
-
- double miny = Math.min(360, Math.max(viewbox.getMinY()+180, 0));
- double maxy = Math.min(360, Math.max(viewbox.getMaxY()+180, 0));
-
- g2d.setTransform(new AffineTransform());
- g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-
- int levels = (1 << level);
-
- // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
- int left = (int)Math.floor( (minx + 180) / 360 * (1<<level) );
- int right = (int)Math.floor( (maxx + 180) / 360 * (1<<level) );
- int top = (int)Math.floor(miny / 360 * (1<<level));// (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(miny)) + 1 / Math.cos(Math.toRadians(miny))) / Math.PI) / 2 * (1<<level) );
- int bottom = (int)Math.floor(maxy / 360 * (1<<level));// (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(maxy)) + 1 / Math.cos(Math.toRadians(maxy))) / Math.PI) / 2 * (1<<level) );
-
- double tsx = 360 / (double)levels; // Size of tile on zoom level
- for(int tx = left; tx <= right; tx++) {
- if(tx < 0 || tx >= levels) continue;
- for(int ty = top; ty <= bottom; ty++) {
- if(ty < 0 || ty >= levels) continue;
- TileKey tile = new TileKey(level, tx, ty);
- double y = (double)ty - (double)levels/2; // In level 0 we have only one tile
- paintTile(tileCache, g2d, tr, tile, tx*tsx-180, y*tsx, tsx);
+ /*
+ * To convert y-coordinates to map coordinates in ruler, use:
+ * double val = (y-offsetY)/scaleY;
+ * val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
+ * String str = formatValue(val);
+ */
+
+ double minx = Math.min(180, Math.max(viewbox.getMinX(), -180));
+ double maxx = Math.min(180, Math.max(viewbox.getMaxX(), -180));
+
+ double miny = Math.min(360, Math.max(viewbox.getMinY()+180, 0));
+ double maxy = Math.min(360, Math.max(viewbox.getMaxY()+180, 0));
+
+ g2d.setTransform(new AffineTransform());
+ g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+
+ int levels = (1 << level);
+
+ // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
+ int left = (int)Math.floor( (minx + 180) / 360 * (1<<level) );
+ int right = (int)Math.floor( (maxx + 180) / 360 * (1<<level) );
+ int top = (int)Math.floor(miny / 360 * (1<<level));// (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(miny)) + 1 / Math.cos(Math.toRadians(miny))) / Math.PI) / 2 * (1<<level) );
+ int bottom = (int)Math.floor(maxy / 360 * (1<<level));// (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(maxy)) + 1 / Math.cos(Math.toRadians(maxy))) / Math.PI) / 2 * (1<<level) );
+
+ double tsx = 360 / (double)levels; // Size of tile on zoom level
+ for(int tx = left; tx <= right; tx++) {
+ if(tx < 0 || tx >= levels) continue;
+ for(int ty = top; ty <= bottom; ty++) {
+ if(ty < 0 || ty >= levels) continue;
+ TileKey tile = new TileKey(level, tx, ty);
+ double y = (double)ty - (double)levels/2; // In level 0 we have only one tile
+ paintTile(tileCache, g2d, tr, tile, tx*tsx-180, y*tsx, tsx);
+ }
+ }
+ // g.dispose();
+
+ // Check if transform has changed
+ transformChanged(tr, level);
+ } else {
+ if (backgroundColor != null) {
+ g2d.setTransform(new AffineTransform());
+ Rectangle2D controlBounds = (Rectangle2D) g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS);
+ Color color = g2d.getColor();
+ g2d.setColor(backgroundColor);
+ g2d.fill(controlBounds);
+ g2d.setColor(color);
}
}
-// g.dispose();
-
- // Check if transform has changed
- transformChanged(tr, level);
-
g2d.setTransform(ot);
}
--- /dev/null
+package org.simantics.maps.sg.commands;
+
+import java.awt.Color;
+
+import org.simantics.scenegraph.g2d.events.command.Command;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
+
+public final class MapCommands {
+
+ public static final Key KEY_MAP_BACKGROUND_COLOR = new KeyOf(Color.class, "MAP_BACKGROUND_COLOR");
+ public static final Command MAP_BACKGROUND_COLOR_CHANGE = new Command("changeMapBackgroundColorChange");
+
+}
package org.simantics.district.network.ui;
+import java.awt.Color;
import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import org.simantics.datatypes.literal.RGB;
import org.simantics.db.ReadGraph;
-import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UnaryRead;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.procedure.Listener;
import org.simantics.diagram.ui.DiagramModelHints;
import org.simantics.g2d.participant.ZoomToAreaHandler;
import org.simantics.maps.MapScalingTransform;
import org.simantics.maps.eclipse.MapPainter;
+import org.simantics.maps.sg.commands.MapCommands;
import org.simantics.modeling.ui.diagramEditor.DiagramViewer;
import org.simantics.scenegraph.g2d.events.command.CommandEvent;
import org.simantics.scenegraph.g2d.events.command.Commands;
super.loadPageSettings(ctx);
// this might be the wrong place to start such listening but at least
// super.loadPageSettings() does async-db-operations
- 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();
+ }
}
}
public boolean canExecute(IEclipseContext popupContext, @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
@SuppressWarnings("unchecked")
Collection<IElement> elements = (Collection<IElement>) popupContext.get(SetFocusableDynamicMenuContribution.FOCUSABLE_ELEMENTS);
- return !elements.isEmpty();
+ return elements != null && !elements.isEmpty();
}
@Execute
Resource edge = graph.newResource();
graph.claim(edge, L0.InstanceOf, DN.Edge);
- graph.claim(edge, DN.HasMapping, mapping);
+ graph.claim(edge, DN.HasMapping, null, mapping);
OrderedSetUtils.addFirst(graph, composite, edge);
graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge);
graph.claim(vertex, L0.InstanceOf, DN.Vertex);
graph.claimLiteral(vertex, DIA.HasLocation, coords);
- graph.claim(vertex, DN.HasMapping, mapping);
+ graph.claim(vertex, DN.HasMapping, null, mapping);
OrderedSetUtils.add(graph, composite, vertex);
graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex);
return current != null ? current : true;
}
- public static void changeMapBackgroundColor(WriteGraph graph, Resource resource, Integer integer) throws DatabaseException {
+ public static void changeMapBackgroundColor(WriteGraph graph, Resource diagram, Integer integer) throws DatabaseException {
DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
- graph.claimLiteral(resource, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
+ graph.claimLiteral(diagram, DN.Diagram_backgroundColor, integer, Bindings.getBindingUnchecked(RGB.Integer.class));
}
public static Boolean trackChangesEnabled(ReadGraph graph, Resource diagram) throws DatabaseException {
- return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
+ if (diagram != null && graph.hasStatement(diagram)) {
+ return Boolean.TRUE.equals(graph.getPossibleRelatedValue(diagram,
DistrictNetworkResource.getInstance(graph).Diagram_trackChangesEnabled));
+ } else {
+ return false;
+ }
+ }
+
+ public static RGB.Integer backgroundColor(ReadGraph graph, Resource diagram) throws DatabaseException {
+ return graph.getPossibleRelatedValue(diagram,
+ DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
+ Bindings.getBindingUnchecked(RGB.Integer.class));
}
}