Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: iiuf/awt/MultiLineLabel.java


1   package iiuf.awt;
2   
3   import java.awt.*;
4   import java.util.*;
5   
6   /**
7      This example is from the book _Java in a Nutshell_ by David Flanagan.
8      Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
9      You may study, use, modify, and distribute this example for any purpose.
10     This example is provided WITHOUT WARRANTY either expressed or implied.
11  
12     Copyright (c) 1996 O'Reilly & Associates.
13     
14     @author David Flanagan, $Author: ohitz $
15     @version $Revision: 1.1 $
16  */
17  public class MultiLineLabel extends Canvas {
18    public static final int LEFT = 0; // Alignment constants
19    public static final int CENTER = 1;
20    public static final int RIGHT = 2;
21    /** @serial The lines of text to display. */
22    protected String[] lines;
23    /** @serial The number of lines. */
24    protected int num_lines;        
25    /** @serial Left and right margins. */
26    protected int margin_width;      
27    /** @serial Top and bottom margins. */
28    protected int margin_height;    
29    /** @serial Total height of the font. */
30    protected int line_height;       
31    /** @serial Font height above baseline. */
32    protected int line_ascent;       
33    /** @serial How wide each line is. */
34    protected int[] line_widths;    
35    /** @serial The width of the widest line. */
36    protected int max_width;          
37    /** @serial The alignment of the text. */
38    protected int alignment = LEFT;  
39    
40    // This method breaks a specified label up into an array of lines.
41    // It uses the StringTokenizer utility class.
42    protected void newLabel(String label) {
43      StringTokenizer t = new StringTokenizer(label, "\n");
44      num_lines = t.countTokens();
45      lines = new String[num_lines];
46      line_widths = new int[num_lines];
47      for(int i = 0; i < num_lines; i++) lines[i] = t.nextToken();
48    }
49    
50    // This method figures out how the font is, and how wide each
51    // line of the label is, and how wide the widest line is.
52    protected void measure() {
53      FontMetrics fm = this.getFontMetrics(this.getFont());
54      // If we don't have font metrics yet, just return.
55      if (fm == null) return;
56      
57      line_height = fm.getHeight();
58      line_ascent = fm.getAscent();
59      max_width = 0;
60      for(int i = 0; i < num_lines; i++) {
61        line_widths[i] = fm.stringWidth(lines[i]);
62        if (line_widths[i] > max_width) max_width = line_widths[i];
63      }
64    }
65    
66    // Here are four versions of the cosntrutor.
67    // Break the label up into separate lines, and save the other info.
68    public MultiLineLabel(String label, int margin_width, int margin_height,
69        int alignment) {
70      newLabel(label);
71      this.margin_width = margin_width;
72      this.margin_height = margin_height;
73      this.alignment = alignment;
74    }
75    public MultiLineLabel(String label, int margin_width, int margin_height) {
76      this(label, margin_width, margin_height, LEFT);
77    }
78    public MultiLineLabel(String label, int alignment) {
79      this(label, 10, 10, alignment);
80    }
81    public MultiLineLabel(String label) {
82      this(label, 10, 10, LEFT);
83    }
84    
85    // Methods to set the various attributes of the component
86    public void setLabel(String label) {
87      newLabel(label);
88      measure();
89      repaint();
90    }
91    public void setFont(Font f) {
92      super.setFont(f);
93      measure();
94      repaint();
95    }
96    public void setForeground(Color c) { 
97      super.setForeground(c); 
98      repaint(); 
99    }
100   public void setAlignment(int a) { alignment = a; repaint(); }
101   public void setMarginWidth(int mw) { margin_width = mw; repaint(); }
102   public void setMarginHeight(int mh) { margin_height = mh; repaint(); }
103   public int getAlignment() { return alignment; }
104   public int getMarginWidth() { return margin_width; }
105   public int getMarginHeight() { return margin_height; }
106   
107   // This method is invoked after our Canvas is first created
108   // but before it can actually be displayed.  After we've
109   // invoked our superclass's addNotify() method, we have font
110   // metrics and can successfully call measure() to figure out
111   // how big the label is.
112   public void addNotify() { super.addNotify(); measure(); }
113   
114   // This method is called by a layout manager when it wants to
115   // know how big we'd like to be.  
116   public Dimension getPreferredSize() {
117     return new Dimension(max_width + 2* margin_width, 
118        num_lines * line_height + 2*margin_height);
119   }
120   
121   // This method is called when the layout manager wants to know
122   // the bare minimum amount of space we need to get by.
123   public Dimension getMinimumSize() {
124     return new Dimension(max_width, num_lines * line_height);
125   }
126   
127   // This method draws the label (applets use the same method).
128   // Note that it handles the margins and the alignment, but that
129   // it doesn't have to worry about the color or font--the superclass
130   // takes care of setting those in the Graphics object we're passed.
131   public void paint(Graphics g) {
132     int x, y;
133     Dimension d = this.getSize();
134     y = line_ascent + (d.height - num_lines * line_height)/2;
135     for(int i = 0; i < num_lines; i++, y += line_height) {
136       switch(alignment) {
137       case LEFT:
138   x = margin_width; break;
139       case CENTER:
140       default:
141   x = (d.width - line_widths[i])/2; break;
142       case RIGHT:
143   x = d.width - margin_width - line_widths[i]; break;
144       }
145       g.drawString(lines[i], x, y);
146     }
147   }
148 
149   public void setText(String text) {
150     setLabel(text);
151   }
152 }
153 /*
154   $Log: MultiLineLabel.java,v $
155   Revision 1.1  2002/07/11 09:20:36  ohitz
156   Initial checkin
157 
158   Revision 1.4  2001/01/04 16:28:29  schubige
159   Header update for 2001 and DIUF
160 
161   Revision 1.3  2000/11/20 17:36:56  schubige
162   tinja project ide
163 
164   Revision 1.2  1999/11/26 09:14:29  schubige
165   intermediate commit
166 
167   Revision 1.1  1999/11/26 08:51:16  schubige
168   *** empty log message ***
169 
170   Revision 1.3  1999/09/03 15:50:08  schubige
171   Changed to new header & log conventions.
172   
173 */