diff org.eclipse.draw2d/src/org/eclipse/draw2d/EllipseAnchor.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/EllipseAnchor.d	Sat Mar 14 18:23:29 2009 +0100
@@ -0,0 +1,72 @@
+/*******************************************************************************
+ * 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:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module org.eclipse.draw2d.EllipseAnchor;
+
+import java.lang.all;
+
+import org.eclipse.draw2d.geometry.Point;
+import org.eclipse.draw2d.geometry.Rectangle;
+import org.eclipse.draw2d.AbstractConnectionAnchor;
+import org.eclipse.draw2d.IFigure;
+
+/**
+ * Similar to a {@link org.eclipse.draw2d.ChopboxAnchor}, except this anchor is located on
+ * the ellipse defined by the owners bounding box.
+ * @author Alex Selkov
+ * Created 31.08.2002 23:11:43
+ */
+public class EllipseAnchor : AbstractConnectionAnchor {
+
+/**
+ * @see org.eclipse.draw2d.AbstractConnectionAnchor#AbstractConnectionAnchor()
+ */
+public this() { }
+
+/**
+ * @see org.eclipse.draw2d.AbstractConnectionAnchor#AbstractConnectionAnchor(IFigure)
+ */
+public this(IFigure owner) {
+    super(owner);
+}
+
+/**
+ * Returns a point on the ellipse (defined by the owner's bounding box) where the
+ * connection should be anchored.
+ * @see org.eclipse.draw2d.ConnectionAnchor#getLocation(Point)
+ */
+public Point getLocation(Point reference) {
+    Rectangle r = Rectangle.SINGLETON;
+    r.setBounds(getOwner().getBounds());
+    r.translate(-1, -1);
+    r.resize(1, 1);
+    getOwner().translateToAbsolute(r);
+
+    Point ref_ = r.getCenter().negate().translate(reference);
+
+    if (ref_.x is 0)
+        return new Point(reference.x, (ref_.y > 0) ? r.bottom() : r.y);
+    if (ref_.y is 0)
+        return new Point((ref_.x > 0) ? r.right() : r.x, reference.y);
+
+    float dx = (ref_.x > 0) ? 0.5f : -0.5f;
+    float dy = (ref_.y > 0) ? 0.5f : -0.5f;
+
+    // ref.x, ref.y, r.width, r.height !is 0 => safe to proceed
+
+    float k = cast(float)(ref_.y * r.width) / (ref_.x * r.height);
+    k = k * k;
+
+    return r.getCenter().translate(cast(int)(r.width * dx / Math.sqrt(1 + k)),
+                                    cast(int)(r.height * dy / Math.sqrt(1 + 1 / k)));
+}
+}