Skip to main content
summaryrefslogtreecommitdiffstats
blob: e5c9c29eed4207d2ab001c82125b7b98973dea72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*******************************************************************************
 * Copyright (c) 2001, 2006 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
 *******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.asd.design.connections;

import org.eclipse.draw2d.AbstractConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

public class CenteredConnectionAnchor extends AbstractConnectionAnchor
{
  public static final int TOP = 0;
  public static final int BOTTOM = 1;
  public static final int LEFT = 2;
  public static final int RIGHT = 3;
  
  // These two are custom for rectangles with header blocks
  public static final int HEADER_LEFT = 4;
  public static final int HEADER_RIGHT = 5;

  private int location;
  private int inset;
  private int offset = 0;
  
  public CenteredConnectionAnchor(IFigure owner, int location, int inset) {
    super(owner);
    this.location = location;
    this.inset = inset;
  }
  
  public CenteredConnectionAnchor(IFigure owner, int location, int inset, int offset) {
    this(owner, location, inset);
    this.offset = offset;
  }
  
  public Point getLocation(Point reference) {
    Rectangle r = getOwner().getBounds();
    int x, y;
    switch (location) {
      case TOP:
        x = r.right() - r.width / 2 + offset;
        y = r.y + inset;
        break;
      case BOTTOM:
        x = r.right() - r.width / 2 + offset;
        y = r.bottom() - inset;
        break;
      case LEFT:
        x = r.x + inset;
        y = r.bottom() - r.height / 2 + offset;
        break;
      case RIGHT:
        x = r.right() - inset;
        y = r.bottom() - r.height / 2 + offset;
        break;
      case HEADER_LEFT:
        x = r.x + inset;
        y = r.y + offset;
        break;
      case HEADER_RIGHT:
        x = r.right() - inset;
        y = r.y + offset;
        break;        
        
      default:
        // Something went wrong. Attach the anchor to the middle
        x = r.right() - r.width / 2;
        y = r.bottom() - r.height / 2;
    }
    Point p = new Point(x,y);

    getOwner().translateToAbsolute(p);
    return p;
  }
  
  public Point getReferencePoint() {
    return getLocation(null);
  }

}

Back to the top