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.Cursor;
23  import java.awt.Dimension;
24  import java.awt.Font;
25  import java.awt.FontMetrics;
26  import java.awt.Graphics2D;
27  import java.awt.Point;
28  import java.awt.Polygon;
29  import java.awt.Rectangle;
30  import javax.swing.UIManager;
31  import org.devaki.nextobjects.workspace.models.columns.Column;
32  import org.devaki.nextobjects.workspace.models.objects.BaseObject;
33  import org.devaki.nextobjects.workspace.models.objects.BaseClass;
34  import org.devaki.nextobjects.constants.CstGraphics;
35  import org.devaki.nextobjects.workspace.models.styles.ClassStyle;
36  /***
37   * This class is responsible for drawing all classes-like objects
38   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
39   */
40  public abstract class ClassView extends ObjectView implements Serializable
41  {
42      /*** The style */
43      protected ClassStyle myStyle;
44      /*** Font metrics */
45      protected FontMetrics metriques;
46      protected Font font =
47          new Font(
48              ((Font) UIManager.get("Label.font")).getName(),
49              Font.PLAIN,
50              ((Font) UIManager.get("Label.font")).getSize());
51      protected Font fontI =
52          new Font(
53              ((Font) UIManager.get("Label.font")).getName(),
54              Font.ITALIC,
55              ((Font) UIManager.get("Label.font")).getSize());
56      /*** Size and location */
57      public Rectangle rectangle = new Rectangle();
58      /*** Old size and location */
59      protected Rectangle oldRectangle = new Rectangle();
60      /***
61       * Construct a new <code>ObjectView</code> object
62       * @param pObject the context object
63       */
64      public ClassView(BaseObject pObject)
65      {
66          super(pObject);
67          this.selectionPoints = new SelectionPoint[8];
68          for (int i = 0; i < 8; i++)
69          {
70              this.selectionPoints[i] = new SelectionPoint(0, 0);
71          }
72          this.myStyle = new ClassStyle(this);
73      }
74      /***
75       * Construct a new <code>ObjectView</code> object
76       */
77      public ClassView()
78      {
79          super();
80          new Font(
81              ((Font) UIManager.get("Label.font")).getName(),
82              Font.PLAIN,
83              ((Font) UIManager.get("Label.font")).getSize());
84          this.selectionPoints = new SelectionPoint[8];
85          for (int i = 0; i < 8; i++)
86          {
87              this.selectionPoints[i] = new SelectionPoint(0, 0);
88          }
89          this.myStyle = new ClassStyle(this);
90      }
91      /***
92       * Returns a computed gravity center.
93       * @return the middle point
94       */
95      public Point getMiddlePoint()
96      {
97          return new Point(
98              (int) (this.getLocation().getLocation().getX()
99                  + (this.getSize().getWidth() / 2)),
100             (int) (this.getLocation().getY()
101                 + (this.getSize().getHeight() / 2)));
102     }
103     /***
104      * Get the style
105      * @return the style
106      */
107     public ClassStyle getStyle()
108     {
109         return this.myStyle;
110     }
111     /***
112      * Define the style
113      * @param pStyle the context style
114      */
115     public void setStyle(ClassStyle pStyle)
116     {
117         this.myStyle = pStyle;
118     }
119     /***
120      * Get the location
121      * @return the location
122      */
123     public Point getLocation()
124     {
125         return this.rectangle.getLocation();
126     }
127     /***
128      * Set the location
129      * @param p the location
130      */
131     public void setLocation(Point p)
132     {
133         this.rectangle.setLocation(p);
134     }
135     /***
136      * Get the size
137      * @return the dimension
138      */
139     public Dimension getSize()
140     {
141         return this.rectangle.getSize();
142     }
143     /***
144      * Define the size
145      * @param pDimension the new dimension
146      */
147     public void setSize(Dimension pDimension)
148     {
149         this.oldRectangle.setSize(this.rectangle.getSize());
150         this.rectangle.setSize(pDimension);
151         this.resetSelectionPoint();
152     }
153     /***
154      * Get the old Rectangle
155      * @return the old rectangle
156      */
157     public Rectangle getOldrectangle()
158     {
159         return this.oldRectangle;
160     }
161     /***
162      * Set the old location
163      * @param p the old location
164      */
165     public void setOldLocation(Point p)
166     {
167         this.oldRectangle.setLocation(p);
168     }
169     /***
170      * Set the old size
171      * @param pDimension the old size
172      */
173     public void setOldSize(Dimension pDimension)
174     {
175         this.oldRectangle.setSize(pDimension);
176     }
177     /***
178     * ResetSelectionPoint
179     */
180     public void resetSelectionPoint()
181     {
182         // North-West point
183         this.selectionPoints[Cursor.NW_RESIZE_CURSOR
184             - 4].setLocation(this.getLocation());
185         // North point
186         this.selectionPoints[Cursor.N_RESIZE_CURSOR
187             - 4].setLocation(
188                 new Point(
189                     ((int) (this.getLocation().getX()
190                         + (this.getSize().getWidth() / 2))),
191                     ((int) (this.getLocation().getY()))));
192         // North-East point
193         this.selectionPoints[Cursor.NE_RESIZE_CURSOR
194             - 4].setLocation(
195                 new Point(
196                     ((int) (this.getLocation().getX()
197                         + this.getSize().getWidth())),
198                     (int) this.getLocation().getY()));
199         // West point
200         this.selectionPoints[Cursor.W_RESIZE_CURSOR
201             - 4].setLocation(
202                 new Point(
203                     (int) this.getLocation().getX(),
204                     (
205                         (int) ((this.getLocation().getY()
206                             + (this.getSize().getHeight() / 2))))));
207         // East point
208         this.selectionPoints[Cursor.E_RESIZE_CURSOR
209             - 4].setLocation(
210                 new Point(
211                     (int) ((this.getLocation().getX()
212                         + this.getSize().getWidth())),
213                     (int) ((this.getLocation().getY()
214                         + (this.getSize().getHeight() / 2)))));
215         // South-West point
216         this.selectionPoints[Cursor.SW_RESIZE_CURSOR
217             - 4].setLocation(
218                 new Point(
219                     (int) this.getLocation().getX(),
220                     ((int) (this.getLocation().getY()
221                         + this.getSize().getHeight()))));
222         // South point
223         this.selectionPoints[Cursor.S_RESIZE_CURSOR
224             - 4].setLocation(
225                 new Point(
226                     (int) (this.getLocation().getX()
227                         + (this.getSize().getWidth() / 2)),
228                     ((int) (this.getLocation().getY()
229                         + this.getSize().getHeight()))));
230         // South-East point
231         this.selectionPoints[Cursor.SE_RESIZE_CURSOR
232             - 4].setLocation(
233                 new Point(
234                     (int) (this.getLocation().getX()
235                         + this.getSize().getWidth()),
236                     ((int) (this.getLocation().getY()
237                         + this.getSize().getHeight()))));
238     }
239     /***
240      * Get the surface on which the user can click to select the object
241      * @return the shape
242      */
243     public Polygon getSurface()
244     {
245         int xi[] =
246             {
247                 (int) this.getLocation().getX() - 5,
248                 (int) (this.getLocation().getX()
249                     + this.getSize().getWidth()
250                     + 5),
251                 (int) (this.getLocation().getX()
252                     + this.getSize().getWidth()
253                     + 5),
254                 (int) this.getLocation().getX() - 5 };
255         int yi[] =
256             {
257                 (int) this.getLocation().getY() - 5,
258                 (int) this.getLocation().getY() - 5,
259                 (int) (this.getLocation().getY() + this.getSize().getHeight())
260                     + 5,
261                 (int) (this.getLocation().getY() + this.getSize().getHeight())
262                     + 5 };
263         return new Polygon(xi, yi, 4);
264     }
265     /***
266      * Return the optimal surface of the object
267      * Optionaly resize the object.
268      * @param act_it really set the size or just return
269      * @return the dimension
270      */
271     public Dimension autoResize(boolean act_it)
272     {
273         String[] fieldList = new String[myObject.getData().size() + 1];
274         Dimension dim = null;
275         int x = myObject.getName().length();
276         fieldList[0] = this.getBaseObject().getCode();
277         for (int i = 0; i < fieldList.length - 1; i++)
278         {
279             fieldList[i] =
280                 this.getBaseObject().getData().elementAt(i).toString();
281             if (x < fieldList[i].length())
282             {
283                 x = fieldList[i].length();
284             }
285         }
286         x = 15 + (x * 7);
287         int y = 25 + (fieldList.length * 13);
288         if (fieldList.length == 1)
289         {
290             dim = new Dimension(x, CstGraphics.DEFAULT_ASSOCIATION_SIZE.height);
291         }
292         else
293         {
294             dim = new Dimension(x, y);
295         }
296         if (act_it)
297         {
298             this.setSize(dim);
299         }
300         return dim;
301     }
302     /***
303      * Paint the text common in all ClassViews
304      * @param g2 the graphics g
305      * @param x optional offset X
306      * @param y optional offset y
307      */
308     public void printColumns(Graphics2D g2, int x, int y)
309     {
310         if (((BaseClass) this.getBaseObject()).getSkipSql()
311             || ((BaseClass) this.getBaseObject()).getAbstractClass())
312         {
313             g2.setFont(fontI);
314         }
315         else
316         {
317             g2.setFont(font);
318         }
319         
320         this.metriques = g2.getFontMetrics(g2.getFont());
321         int yt = this.metriques.getMaxAscent() + 1 ;
322         int xt = this.metriques.stringWidth(myObject.getName());
323         if (xt < this.getSize().getWidth() - 2)
324         {
325             // Center name
326             xt = ((int) ((this.getSize().getWidth() - 1) - xt) / 2);
327         }
328         else
329         {
330             // The name width is bigger than the object width so place him
331             // 3 pixels close to the left of the object
332             xt = 3;
333         }       
334         g2.drawString(this.myObject.getName(), x + xt, y + yt);
335         /*** Draw a line between the name and the data **/
336         g2.drawLine(
337             x + 1,
338             y + yt + 4,
339             x + (int) this.getSize().getWidth() - 1,
340             y + yt + 4);
341         /*** Data **/
342         // Paint column names
343         int maxName = 0;
344         for (int i = 0; i < this.myObject.getData().size(); i++)
345         {
346             Column col=(Column) this.myObject.getData().elementAt(i);
347             
348 
349 
350             // Get the longest column name
351             if (xt > maxName)
352                 maxName = xt;         
353 
354             // If column is primary key, underline it
355             if (col.isPk())
356             {
357                 xt =
358                     this.metriques.stringWidth(
359                         col.toString());
360                 g2.drawLine(
361                     x + 10,
362                     y + (yt * (i + 2)) + 6,
363                     x + xt + 8,
364                     y + (yt * (i + 2)) + 6);                
365             }
366             g2.drawString(col.toString(), x + 10, y + (yt * (i + 2)) + 4);
367         }
368         /*** Draw a line between the data and the functions **/
369         g2.drawLine(
370             x + 1,
371             (int) y + (yt * (this.myObject.getData().size() + 2)),
372             (int) (x + this.getSize().getWidth() - 1),
373             (int) y + (yt * (this.myObject.getData().size() + 2)));
374     }
375 }