Index: src/org/eclipse/draw2d/PolylineConnection.java =================================================================== RCS file: /cvsroot/tools/org.eclipse.gef/plugins/org.eclipse.draw2d/src/org/eclipse/draw2d/PolylineConnection.java,v retrieving revision 1.19 diff -u -r1.19 PolylineConnection.java --- src/org/eclipse/draw2d/PolylineConnection.java 1 Dec 2005 20:21:06 -0000 1.19 +++ src/org/eclipse/draw2d/PolylineConnection.java 23 Jun 2009 16:37:10 -0000 @@ -278,6 +278,15 @@ } /** + * Sets the decoration to be used along the {@link Connection}. + * @param dec the new decoration + * @since 3.5 + */ +public void setDecoration(RotatableDecoration dec) { + add(dec, new PolylineLabelLocator(this)); +} + +/** * Sets the anchor to be used at the end of the polyline connection. Removes this listener * from the old anchor and adds it to the new anchor. * @param anchor the new target anchor Index: src/org/eclipse/draw2d/PolylineLabelLocator.java =================================================================== RCS file: src/org/eclipse/draw2d/PolylineLabelLocator.java diff -N src/org/eclipse/draw2d/PolylineLabelLocator.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/draw2d/PolylineLabelLocator.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2000, 2005 IBM Corporation and others. + * 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: + * Manuel Selva - initial API and implementation + *******************************************************************************/ +package org.eclipse.draw2d; + +import org.eclipse.draw2d.geometry.PointList; + +/** + * Locator used to place a {@link PolylineLabelDecoration} on a + * {@link Connection}. + * + * @since 3.5 + */ +public class PolylineLabelLocator implements Locator { + + private Connection connection; + + /** + * Constructs a LabelLocator associated with passed connection. + * + * @param connection + * The connection associated with the locator + */ + public PolylineLabelLocator(Connection connection) { + this.connection = connection; + } + + /** + * Relocates the passed in figure (which must be a + * {@link PolylineLabelDecoration}) at the start of the connection. + * + * @param target + * The PolylineLabelDecoration to relocate + */ + public void relocate(IFigure target) { + + PointList points = connection.getPoints(); + PolylineLabelDecoration dec = (PolylineLabelDecoration) target; + + dec.setLocation(points.getPoint(1)); + dec.setReferencePoint(points.getPoint(0)); + } +} Index: src/org/eclipse/draw2d/PolylineLabelDecoration.java =================================================================== RCS file: src/org/eclipse/draw2d/PolylineLabelDecoration.java diff -N src/org/eclipse/draw2d/PolylineLabelDecoration.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/draw2d/PolylineLabelDecoration.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,160 @@ +/******************************************************************************* + * Copyright (c) 2000, 2005 IBM Corporation and others. + * 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: + * Manuel Selva - initial API and implementation + *******************************************************************************/ +package org.eclipse.draw2d; + +import org.eclipse.swt.graphics.Image; + +import org.eclipse.draw2d.geometry.Dimension; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.Rectangle; + +/** + * A decorative label Figure intended to be placed on a {@link PolylineConnection}. + * + * @since 3.5 + */ +public class PolylineLabelDecoration extends Label implements + RotatableDecoration { + + private double angle; + private Point location = new Point(); + private Point refPoint = new Point(); + + public PolylineLabelDecoration() { + super(); + } + + public PolylineLabelDecoration(Image i) { + super(i); + } + + public PolylineLabelDecoration(String s) { + super(s); + } + + public PolylineLabelDecoration(String s, Image i) { + super(s, i); + } + + public Rectangle getBounds() { + if (bounds == null) { + int xDiff = refPoint.x - location.x; + int yDiff = refPoint.y - location.y; + int x, y, width, height; + if (xDiff >= 0) { + width = xDiff; + x = location.x; + } else { + width = -xDiff; + x = refPoint.x; + } + if (yDiff >= 0) { + height = yDiff; + y = location.y; + } else { + height = -yDiff; + y = refPoint.y; + } + bounds = new Rectangle(x, y, width, height); + } + return bounds; + } + + public void setLocation(Point p) { + bounds = null; + location.setLocation(p); + } + + public void setReferencePoint(Point ref) { + Point pt = Point.SINGLETON; + refPoint.setLocation(ref); + pt.setLocation(ref); + pt.negate().translate(location); + setRotation(Math.atan2(pt.y, pt.x)); + } + + private void setRotation(double angle) { + bounds = null; + this.angle = Math.toDegrees(angle); + } + + protected Point getIconLocation() { + double linkLength = Math.sqrt(bounds.width * bounds.width + + bounds.height * bounds.height); + int y = 0;//-getIconSize().height; + int x; + switch (getTextAlignment()) { + case LEFT: + x = 0; + break; + case RIGHT: + x = (int) (linkLength - getIconSize().width); + break; + default: + x = (int) (linkLength / 2 - getIconSize().width / 2); + } + if (x < 0) { + x = 0; + } + return new Point(x, y); + } + + protected Point getTextLocation() { + Dimension textDimension = getTextSize(); + double linkLength = Math.sqrt((bounds.width + getIconSize().width) * 2 + + (bounds.height + getIconSize().height) * 2); + int y = -textDimension.height; + int x; + switch (getTextAlignment()) { + case LEFT: + x = 0; + break; + case RIGHT: + x = (int) (linkLength - textDimension.width); + break; + default: + x = (int) (linkLength / 2 - textDimension.width / 2); + } + if (x < getIconSize().width + 5) { + x = getIconSize().width + 5; + } + return new Point(x, y); + } + + protected void paintFigure(Graphics graphics) { + + // Draws the text + graphics.pushState(); + graphics.setClip(graphics.getClip(new Rectangle()).expand(50, 50)); + graphics.translate(bounds.x, bounds.y); + if (angle < 0) { + graphics.translate(0, getBounds().height); + } + graphics.rotate((float) angle); + + if (isOpaque()) + graphics.fillRectangle(getBounds()); + if (getBorder() instanceof AbstractBackground) + ((AbstractBackground) getBorder()).paintBackground(this, graphics, + NO_INSETS); + if (getIcon() != null) + graphics.drawImage(getIcon(), getIconLocation()); + if (!isEnabled()) { + graphics.translate(1, 1); + graphics.setForegroundColor(ColorConstants.buttonLightest); + graphics.drawText(getSubStringText(), getTextLocation()); + graphics.translate(-1, -1); + graphics.setForegroundColor(ColorConstants.buttonDarker); + } + graphics.drawText(getSubStringText(), getTextLocation()); + graphics.popState(); + } +}