X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram.connection%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fconnection%2Frendering%2FStyledRouteGraphRenderer.java;fp=bundles%2Forg.simantics.diagram.connection%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fconnection%2Frendering%2FStyledRouteGraphRenderer.java;h=b4e5594a6bddbfd7a386476a94fcbd49f651d228;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java b/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java new file mode 100644 index 000000000..b4e5594a6 --- /dev/null +++ b/bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/rendering/StyledRouteGraphRenderer.java @@ -0,0 +1,184 @@ +/******************************************************************************* + * Copyright (c) 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.connection.rendering; + +import gnu.trove.set.hash.THashSet; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.geom.Path2D; +import java.io.Serializable; +import java.util.List; + +import org.simantics.diagram.connection.RouteGraph; +import org.simantics.diagram.connection.RouteLine; +import org.simantics.diagram.connection.RoutePoint; +import org.simantics.diagram.connection.RouteTerminal; +import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle; + + +public class StyledRouteGraphRenderer implements IRouteGraphRenderer, Serializable { + + private static final BasicStroke GUIDE_LINE_STROKE = new BasicStroke(0.1f); + private static final Color GUIDE_LINE_COLOR = new Color(255,255,255); + + private static final long serialVersionUID = 1564960933064029020L; + + protected final ConnectionStyle style; + + // Caches to avoid creating new objects all the time during rendering + protected transient Path2D path; + protected transient THashSet branchPoints; + + public StyledRouteGraphRenderer(ConnectionStyle style) { + if (style == null) + throw new NullPointerException("null style"); + this.style = style; + } + + public ConnectionStyle getStyle() { + return style; + } + + @Override + public void render(Graphics2D g, RouteGraph rg) { + if (path == null) + path = new Path2D.Double(); + path.reset(); + rg.getPath2D(path); + style.drawPath(g, path, false); + + if (branchPoints == null) + branchPoints = new THashSet(); + branchPoints.clear(); + for(RouteLine line : rg.getLines()) { + renderLine(g, line, false); + collectBranchPoints(line, branchPoints); + } + for(RouteLine line : rg.getTransientLines()) { + renderLine(g, line, true); + collectBranchPoints(line, branchPoints); + } + + /*for(RouteTerminal terminal : rg.getTerminals()) + terminal.render(g);*/ + for(RoutePoint point : branchPoints) { + style.drawBranchPoint(g, point.getX(), point.getY()); + } + } + + private static void collectBranchPoints(RouteLine line, THashSet branchPoints) { + List points = line.getPoints(); + for(int i=1;i ady) { + if(adx > 4) + dx = 0.5*dx - Math.signum(dx); + else + dx *= 0.25; + path.moveTo(p1.getX()+dx, p1.getY()); + path.lineTo(p2.getX()-dx, p2.getY()); + } + else { + if(ady > 4) + dy = 0.5*dy - Math.signum(dy); + else + dy *= 0.25; + path.moveTo(p1.getX(), p1.getY()+dy); + path.lineTo(p2.getX(), p2.getY()-dy); + } + } + g.setStroke(GUIDE_LINE_STROKE); + g.setColor(GUIDE_LINE_COLOR); + g.draw(path); + } + + @Override + public int hashCode() { + return style.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + StyledRouteGraphRenderer other = (StyledRouteGraphRenderer) obj; + return style.equals(other.style); + } + +}