View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  package org.devaki.nextobjects.workspace.models.graphics;
21  import java.awt.Graphics;
22  import java.awt.Point;
23  import java.awt.Polygon;
24  import org.devaki.nextobjects.util.NOGraphicsUtil;
25  import org.devaki.nextobjects.workspace.models.objects.InheritanceLink;
26  /***
27   * The line-arrow representing an inheritance link.
28   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
29   */
30  public class InheritanceLinkView extends LineView
31  {
32      /*** Arrowhead sharpness */
33      public static final double theta = Math.toRadians(20);
34      /*** Arrowhead length */
35      public static final int size = 15;
36      /***
37       * Create a new <code>InheritanceLinkView</code> object
38       * @param pInheritanceLink the contextual association links
39       */
40      public InheritanceLinkView(InheritanceLink pInheritanceLink)
41      {
42          super(pInheritanceLink);
43      }
44      /***
45       * Return the surface on which the user can click to select the object
46       * @return the surface as polygon/shape
47       */
48      public Polygon getSurface()
49      {
50          Point p3 =
51              NOGraphicsUtil.getMiddle(
52                  selectionPoints[0].getPoint(),
53                  selectionPoints[1].getPoint());
54          int delta = 20;
55          int tmpX1 = (int) p3.getX();
56          int tmpY1 = (int) p3.getY();
57          int xi[] =
58              {
59                  (int) selectionPoints[0].getPoint().x,
60                  tmpX1 - delta,
61                  (int) selectionPoints[1].getPoint().x,
62                  tmpX1 + delta,
63                  selectionPoints[0].getPoint().x };
64          int yi[] =
65              {
66                  selectionPoints[0].getPoint().y,
67                  tmpY1 - delta,
68                  selectionPoints[1].getPoint().y,
69                  tmpY1 + delta,
70                  selectionPoints[1].getPoint().y };
71          return new Polygon(xi, yi, 4);
72      }
73      /***
74       * Paint method
75       * @param g Graphics context g
76       */
77      public void paint(Graphics g)
78      {
79          Point p0 =
80              ((BaseModelView) myObject.getMyModel().getPanel())
81                  .getRectangle()
82                  .getLocation();
83          Point p1 = selectionPoints[1].getPoint();
84          p1.translate(-p0.x, -p0.y);
85          Point p2 = selectionPoints[0].getPoint();
86          p2.translate(-p0.x, -p0.y);
87          int x1 = (int) p1.getX();
88          int y1 = (int) p1.getY();
89          int x2 = (int) p2.getX();
90          int y2 = (int) p2.getY();
91          // Set color from style
92          g.setColor(this.myStyle.getLineColor());
93          // calculate parrallels
94          double angle = Math.atan2(y2 - y1, x2 - x1) + Math.PI;
95          int x3 = (int) (x2 + Math.cos(angle - theta) * size / 2);
96          int y3 = (int) (y2 + Math.sin(angle - theta) * size / 2);
97          int x4 = (int) (x2 + Math.cos(angle + theta) * size / 2);
98          int y4 = (int) (y2 + Math.sin(angle + theta) * size / 2);
99          //g.drawLine(x5,y5,x3,y3);
100         g.drawLine(x1, y1, x2, y2);
101         // calculate points for arrowhead
102         x3 = (int) (x2 + Math.cos(angle - theta) * size);
103         y3 = (int) (y2 + Math.sin(angle - theta) * size);
104         x4 = (int) (x2 + Math.cos(angle + theta) * size);
105         y4 = (int) (y2 + Math.sin(angle + theta) * size);
106         int intXarr[] = { x2, x3, x4 };
107         int intYarr[] = { y2, y3, y4 };
108         // draw arrowhead
109         g.fillPolygon(intXarr, intYarr, 3);
110     }
111     /***
112      * Reset the selection points
113      * @param
114      */
115     public void resetSelectionPoint()
116     {
117     }
118     /***
119      * Return the Selection Points Array
120      * @return SelectionPoint[] the selection points array
121      */
122     public SelectionPoint[] getSelectionPoints()
123     {
124         return this.selectionPoints;
125     }
126     /***
127      * Return the Selection Point
128      * @param i the index
129      * @return SelectionPoint one of its selection point
130      */
131     public SelectionPoint getSelectionPoint(int i)
132     {
133         return this.selectionPoints[i];
134     }
135     /***
136      * Set the Selection Point
137      * @param point the point
138      * @param i the index
139      */
140     public void setSelectionPoint(SelectionPoint point, int i)
141     {
142         this.selectionPoints[i] = point;
143     }
144 }