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.io.Serializable;
22  import java.awt.Graphics;
23  import java.awt.Point;
24  import java.awt.Polygon;
25  import org.devaki.nextobjects.util.NOGraphicsUtil;
26  import org.devaki.nextobjects.workspace.models.ConceptualModel;
27  import org.devaki.nextobjects.workspace.models.objects.AssociationLink;
28  /***
29   * This class is responsible for drawing an Association Link
30   * in the Merise formalism
31   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
32   */
33  public class AssociationLinkView extends LineView implements Serializable
34  {
35      /* First location point */
36      private int associationPointType = SelectionPoint.NORTH;
37      /* Second location point */
38      private int entityPointType = SelectionPoint.NORTH;
39      /***
40       * Create a new 'AssociationLinkView' object
41       * @param pAssociationLink the contextual link
42       */
43      public AssociationLinkView(AssociationLink pAssociationLink)
44      {
45          super(pAssociationLink);
46          selectionPoints[0] =
47              ((AssociationLink) this.getBaseObject())
48                  .getParentClass()
49                  .getObjectView()
50                  .getSelectionPoint(0);
51          selectionPoints[1] =
52              ((AssociationLink) this.getBaseObject())
53                  .getChildClass()
54                  .getObjectView()
55                  .getSelectionPoint(0);
56      }
57      /***
58       * Return the surface on which the user can click to select the object
59       * @return the surface as polygon/shape
60       */
61      public Polygon getSurface()
62      {
63          Point p3 =
64              NOGraphicsUtil.getMiddle(
65                  selectionPoints[0].getPoint(),
66                  selectionPoints[1].getPoint());
67          int delta = 20;
68          int tmpX1 = (int) p3.getX();
69          int tmpY1 = (int) p3.getY();
70          int xi[] =
71              {
72                  (int) selectionPoints[0].getPoint().x,
73                  tmpX1 - delta,
74                  (int) selectionPoints[1].getPoint().x,
75                  tmpX1 + delta,
76                  selectionPoints[0].getPoint().x };
77          int yi[] =
78              {
79                  selectionPoints[0].getPoint().y,
80                  tmpY1 - delta,
81                  selectionPoints[1].getPoint().y,
82                  tmpY1 + delta,
83                  selectionPoints[1].getPoint().y };
84          return new Polygon(xi, yi, 4);
85      }
86      /***
87       * Set the entity point location
88       * @param pEntityPointType the entity point location
89       */
90      public void setEntityPointType(int pEntityPointType)
91      {
92          entityPointType = pEntityPointType;
93      }
94      /***
95       * Set the associatioon point location
96       * @param pAssociationPointType the location
97       */
98      public void setAssociationPointType(int pAssociationPointType)
99      {
100         associationPointType = pAssociationPointType;
101     }
102     /***
103      * Paint the association link
104      * @param g graphics context
105      * @task Implement getVisibleRectangle in ConceptualView and PhysicalView.
106      */
107     public void paint(Graphics g)
108     {
109         Point p0 =
110             ((ConceptualView) myObject.getMyModel().getPanel())
111                 .getRectangle()
112                 .getLocation();
113         Point p1 = selectionPoints[0].getPoint();
114         p1.translate(-p0.x, -p0.y);
115         Point p2 = selectionPoints[1].getPoint();
116         p2.translate(-p0.x, -p0.y);
117         // Define color
118         g.setColor(this.myStyle.getLineColor());
119         Point p3 = NOGraphicsUtil.getMiddle(p1, p2);
120         String strCard =
121             this.getCardString(((AssociationLink) this.myObject).getCard());
122         g.drawLine(
123             (int) p1.getX(),
124             (int) p1.getY(),
125             (int) p2.getX(),
126             (int) p2.getY());
127         g.drawString(strCard, (int) p3.getX(), (int) p3.getY());
128     }
129     /***
130      * Return the String association to the cardinality static definition
131      * @param Card the cardinality
132      * @return the cardinality as a string
133      */
134     private String getCardString(int Card)
135     {
136         String str = "";
137         switch (Card)
138         {
139             case ConceptualModel.ASSO_01 :
140                 str = "0,1";
141                 break;
142             case ConceptualModel.ASSO_11 :
143                 str = "1,1";
144                 break;
145             case ConceptualModel.ASSO_0N :
146                 str = "0,n";
147                 break;
148             case ConceptualModel.ASSO_1N :
149                 str = "1,n";
150                 break;
151         }
152         return str;
153     }
154     /***
155      * reset selection point
156      * Do nothing here.
157      */
158     public void resetSelectionPoint()
159     {
160     }
161 }