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  
22  import java.awt.Graphics;
23  import java.awt.Font;
24  import java.awt.Dimension;
25  import java.util.StringTokenizer;
26  import java.awt.FontMetrics;
27  import java.awt.Color;
28  import javax.swing.UIManager;
29  import org.devaki.nextobjects.constants.CstGraphics;
30  import org.devaki.nextobjects.workspace.models.objects.BaseObject;
31  
32  /***
33   * The labels view are like postits
34   * @author eflorent
35   */
36  public class LabelView extends ClassView
37  {
38  
39   /***
40    * construct a LabelView
41    * @param pObject the base label
42    */
43   public LabelView(BaseObject pObject)
44   {
45    super(pObject);
46    this.setSize(CstGraphics.DEFAULT_LABEL_SIZE);
47    oldRectangle.setSize(this.getSize());
48    oldRectangle.setLocation(this.getLocation());
49   }
50  
51   /*** 
52    * Paint the "cartouche"
53    * @param g the graphic context
54    */
55   public void paint(Graphics g)
56   {
57    /*
58     * final :
59     *    ___
60     *  /|      |
61     * I____|
62     * 
63     */
64    g.setColor(Color.YELLOW);
65    g.fillRect(0, 0, this.getSize().width, this.getSize().height);
66    // g.setColor(CstGraphics.DEFAULT_LABEL_COLOR);
67    g.setColor(Color.BLACK);
68    g.drawRect(0, 0, this.getSize().width - 1, this.getSize().height - 1);
69    g.drawRect(0, 0, 15, 15);
70    g.setColor(CstGraphics.MODEL_BACKGROUND_COLOR);
71    g.fillRect(0, 0, 15, 15);
72    g.setColor(Color.BLACK);
73    g.drawLine(0, 15, 15, 0);
74    printText(g);
75   }
76  
77   /***
78    * Draw the texts
79    * @param g the graphic context
80    */
81  
82   public void printText(Graphics g)
83   {
84    g.setColor(Color.BLACK);
85    this.metriques = g.getFontMetrics(g.getFont());
86    g.setFont(
87     new Font(
88      ((Font) UIManager.get("Label.font")).getName(),
89      Font.BOLD,
90      ((Font) UIManager.get("Label.font")).getSize()));
91    int line = 1;
92    int margin = 5;
93    //add an empty space for postit(c) corner.
94    StringBuffer sb = new StringBuffer("    ");
95    FontMetrics fm = g.getFontMetrics();
96  
97    StringTokenizer st = new StringTokenizer(myObject.getName());
98    while (st.hasMoreTokens())
99    {
100    String nextword = st.nextToken();
101    if (fm.stringWidth(sb.toString() + nextword) + margin
102     < this.getSize().width)
103    {
104     sb.append(nextword);
105     sb.append(' ');
106    } else if (sb.length() == 0)
107    {
108     g.drawString(nextword, margin, line * fm.getHeight());
109     line++;
110    } else
111    {
112     g.drawString(sb.toString(), margin, line * fm.getHeight());
113     sb = new StringBuffer(nextword + " ");
114     line++;
115    }
116 
117   }
118   if (sb.length() > 0)
119   {
120    g.drawString(sb.toString(), margin, line * fm.getHeight());
121    line++;
122   }
123   sb.delete(0, sb.length());
124   st = new StringTokenizer(myObject.getNotes());
125 
126   g.setFont(
127    new Font(
128     ((Font) UIManager.get("Label.font")).getName(),
129     Font.PLAIN,
130     ((Font) UIManager.get("Label.font")).getSize()));
131 
132   while (st.hasMoreTokens())
133   {
134    String nextword = st.nextToken();
135    if (fm.stringWidth(sb.toString() + nextword) + margin
136     < this.getSize().width)
137    {
138     sb.append(nextword);
139     sb.append(' ');
140    } else if (sb.length() == 0)
141    {
142     g.drawString(nextword, margin, line * fm.getHeight());
143     line++;
144    } else
145    {
146     g.drawString(sb.toString(), margin, line * fm.getHeight());
147     sb = new StringBuffer(nextword + " ");
148     line++;
149    }
150 
151   }
152   if (sb.length() > 0)
153   {
154    g.drawString(sb.toString(), margin, line * fm.getHeight());
155    line++;
156   }
157  }
158  public Dimension autoResize(boolean actIt)
159  {
160   if (actIt) 
161   {
162   this.setSize(CstGraphics.DEFAULT_LABEL_SIZE);
163   }
164   return CstGraphics.DEFAULT_LABEL_SIZE;
165  }
166 }