KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > wrapped > WrappedNames


1 package text.wrapped;
2
3 import text.*;
4
5 import java.util.*;
6 import java.awt.*;
7 import rero.util.*;
8
9 /* a special class for dealing with the formatting of /names output */
10
11 public class WrappedNames extends WrappedContainer
12 {
13    protected AttributedText[] rawData;
14    protected double percentage;
15    protected String JavaDoc[] users;
16
17    public WrappedNames(String JavaDoc text, String JavaDoc[] _users, double _percentage)
18    {
19       super(text);
20
21       users = _users;
22
23       calculateAttributes();
24
25       percentage = _percentage;
26    }
27
28    public void calculateAttributes()
29    {
30       rawData = new AttributedText[users.length];
31
32       for (int x = 0; x < users.length; x++)
33       {
34          AttributedString temp = AttributedString.CreateAttributedString(users[x]);
35          temp.assignWidths();
36          rawData[x] = temp.getAttributedText();
37       }
38    }
39
40    public void touch(int size)
41    {
42       int width = (int)(size * percentage);
43       if (width < maxSize && width > minSize)
44       {
45          return;
46       }
47
48       wrapped = wrapNames(width);
49       determineBounds(width);
50    }
51
52    public void reset()
53    {
54       super.reset();
55       calculateAttributes();
56    }
57
58    public AttributedText[] wrapNames(int width)
59    {
60       LinkedList values = new LinkedList();
61      
62       AttributedText head = rawData[0].cloneList();
63
64       AttributedText temp = head; // temp is essentially the tail of the new list for our wordwrapped line of text
65
while (temp.next != null) { temp = temp.next; }
66
67       int current = rawData[0].getWidth();
68  
69       for (int x = 1; x < rawData.length; x++)
70       {
71          current += rawData[x].getWidth();
72          
73          if (current > width)
74          {
75             values.add(head);
76             head = rawData[x].cloneList();
77             current = head.getWidth();
78             temp = head;
79             while (temp.next != null) { temp = temp.next; }
80          }
81          else
82          {
83             temp.next = rawData[x].cloneList();
84             while (temp.next != null) { temp = temp.next; }
85          }
86       }
87
88       values.add(head);
89
90       //
91
// convert the linked list to an array.
92
//
93
AttributedText returnValue[] = new AttributedText[values.size()];
94       int x = values.size() - 1;
95       ListIterator i = values.listIterator();
96       while (i.hasNext())
97       {
98          returnValue[x] = (AttributedText)i.next();
99          x--;
100       }
101
102       return returnValue;
103    }
104 }
105
Popular Tags