KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > AttachedTextLocator


1 /*
2   Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>,
3                           Laurent Martelli <laurent@aopsys.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 */

19
20 package org.objectweb.jac.ide.diagrams;
21
22 import CH.ifa.draw.standard.AbstractLocator;
23 import CH.ifa.draw.standard.ChopBoxConnector;
24 import CH.ifa.draw.framework.Figure;
25 import java.awt.Point JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import org.objectweb.jac.util.Log;
28
29 public class AttachedTextLocator extends AbstractLocator {
30
31    public Point JavaDoc locate(Figure owner,Figure locatedObject) {
32       //if(DiagramView.init) return new Point(0,0);
33
Log.trace("locator","locate text "+locatedObject+" owned by "+owner);
34
35       LinkFigure f = (LinkFigure)owner;
36       Point JavaDoc center = owner.center();
37       
38       if (f.endFigure()==null
39           || f.startFigure()==null
40           || locatedObject==null)
41          return center;
42       
43       AttachedTextFigure text = (AttachedTextFigure)locatedObject;
44       
45       if (text.getType()==AttachedTextFigure.START_ROLE) {
46
47          Log.trace("diagram",2,"locating end role");
48          ChopBoxConnector chopper = new ChopBoxConnector(f.endFigure());
49          return locate(chopper.findEnd(f),f.endFigure(),false);
50
51       } else if (text.getType()==AttachedTextFigure.END_ROLE) {
52          
53          Log.trace("diagram",2,"locating start role");
54          ChopBoxConnector chopper = new ChopBoxConnector(f.startFigure());
55          return locate(chopper.findStart(f),f.startFigure(),true);
56
57       } else if (text.getType()==AttachedTextFigure.START_CARDINALITY) {
58          
59          Log.trace("locator","locating end cardinality "+f.endFigure().center());
60          ChopBoxConnector chopper = new ChopBoxConnector(f.endFigure());
61          return locate(chopper.findEnd(f),f.endFigure(),true);
62
63       } else if (text.getType()==AttachedTextFigure.END_CARDINALITY) {
64          
65          Log.trace("locator","locating start cardinality "+f.startFigure().center());
66          ChopBoxConnector chopper = new ChopBoxConnector(f.startFigure());
67          return locate(chopper.findStart(f),f.startFigure(),false);
68
69       } else if (text.getType()==AttachedTextFigure.NAME) {
70
71          Rectangle JavaDoc r = owner.displayBox();
72          Rectangle JavaDoc r2 = locatedObject.displayBox();
73          Point JavaDoc p = owner.center();
74          if (r.getWidth()>r.getHeight()) {
75             return new Point JavaDoc(p.x, p.y+10); // hack
76
} else {
77             return new Point JavaDoc((int)(p.x-(r2.getWidth()/2)-5), p.y-10); // hack
78
}
79       }
80       return center;
81    }
82
83    /**
84     * Locate attached text
85     * @param ep starting point (intersection of line with class)
86     * @param f class figure the text is attached to
87     * @param flip if true, flip sides
88     */

89    Point JavaDoc locate(Point JavaDoc ep, Figure f, boolean flip) {
90       Point JavaDoc result = null;
91       Point JavaDoc center = f.center();
92       double dY = (double)(ep.y - center.y);
93       double dX = (double)(ep.x - center.x);
94       double c = 20/Math.sqrt(dY*dY+dX*dX);
95       double dx = dX*c;
96       double dy = dY*c;
97       if (!flip) {
98          result = new Point JavaDoc((int)(ep.x+dx-dy),(int)(ep.y+dy+dx));
99       } else {
100          result = new Point JavaDoc((int)(ep.x+dx+dy),(int)(ep.y+dy-dx));
101       }
102       Log.trace("locator","ep="+ep+" center="+center+
103                 " dx,dy="+(int)dx+","+(int)dy+" -> "+result);
104       return result;
105    }
106 }
107
108
109
Popular Tags