001    /*
002      Copyright (C) 2002-2003 Renaud Pawlak <renaud@aopsys.com>,
003                              Laurent Martelli <laurent@aopsys.com>
004    
005      This program is free software; you can redistribute it and/or modify
006      it under the terms of the GNU Lesser General Public License as
007      published by the Free Software Foundation; either version 2 of the
008      License, or (at your option) any later version.
009    
010      This program is distributed in the hope that it will be useful,
011      but WITHOUT ANY WARRANTY; without even the implied warranty of
012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013      GNU Lesser General Public License for more details.
014    
015      You should have received a copy of the GNU Lesser General Public
016      License along with this program; if not, write to the Free Software
017      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
018    */
019    
020    package org.objectweb.jac.ide.diagrams;
021    
022    import CH.ifa.draw.standard.AbstractLocator;
023    import CH.ifa.draw.standard.ChopBoxConnector;
024    import CH.ifa.draw.framework.Figure;
025    import java.awt.Point;
026    import java.awt.Rectangle;
027    import org.objectweb.jac.util.Log;
028    
029    public class AttachedTextLocator extends AbstractLocator {
030    
031       public Point locate(Figure owner,Figure locatedObject) {
032          //if(DiagramView.init) return new Point(0,0);
033          Log.trace("locator","locate text "+locatedObject+" owned by "+owner);
034    
035          LinkFigure f = (LinkFigure)owner;
036          Point center = owner.center();
037          
038          if (f.endFigure()==null 
039              || f.startFigure()==null
040              || locatedObject==null) 
041             return center;
042          
043          AttachedTextFigure text = (AttachedTextFigure)locatedObject;
044          
045          if (text.getType()==AttachedTextFigure.START_ROLE) {
046    
047             Log.trace("diagram",2,"locating end role");
048             ChopBoxConnector chopper = new ChopBoxConnector(f.endFigure());
049             return locate(chopper.findEnd(f),f.endFigure(),false);
050    
051          } else if (text.getType()==AttachedTextFigure.END_ROLE) {
052             
053             Log.trace("diagram",2,"locating start role");
054             ChopBoxConnector chopper = new ChopBoxConnector(f.startFigure());
055             return locate(chopper.findStart(f),f.startFigure(),true);
056    
057          } else if (text.getType()==AttachedTextFigure.START_CARDINALITY) {
058             
059             Log.trace("locator","locating end cardinality "+f.endFigure().center());
060             ChopBoxConnector chopper = new ChopBoxConnector(f.endFigure());
061             return locate(chopper.findEnd(f),f.endFigure(),true);
062    
063          } else if (text.getType()==AttachedTextFigure.END_CARDINALITY) { 
064             
065             Log.trace("locator","locating start cardinality "+f.startFigure().center());
066             ChopBoxConnector chopper = new ChopBoxConnector(f.startFigure());
067             return locate(chopper.findStart(f),f.startFigure(),false);
068    
069          } else if (text.getType()==AttachedTextFigure.NAME) {
070    
071             Rectangle r = owner.displayBox();
072             Rectangle r2 = locatedObject.displayBox();
073             Point p = owner.center();
074             if (r.getWidth()>r.getHeight()) {
075                return new Point(p.x, p.y+10); // hack
076             } else {
077                return new Point((int)(p.x-(r2.getWidth()/2)-5), p.y-10); // hack
078             }
079          }
080          return center;
081       }
082    
083       /**
084        * Locate attached text
085        * @param ep starting point (intersection of line with class)
086        * @param f class figure the text is attached to
087        * @param flip if true, flip sides
088        */
089       Point locate(Point ep, Figure f, boolean flip) {
090          Point result = null;
091          Point center = f.center();
092          double dY = (double)(ep.y - center.y);
093          double dX = (double)(ep.x - center.x);
094          double c = 20/Math.sqrt(dY*dY+dX*dX);
095          double dx = dX*c;
096          double dy = dY*c;
097          if (!flip) {
098             result = new Point((int)(ep.x+dx-dy),(int)(ep.y+dy+dx));
099          } else {
100             result = new Point((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