KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > wrapped > WrappedData


1 package text.wrapped;
2
3 import text.*;
4
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import java.util.*;
8
9 import rero.config.*;
10
11 public class WrappedData implements BoundedRangeModel
12 {
13     private static int WRAP_TOLERANCE = 2000;
14     private static int WRAP_TO = 1000;
15
16     static
17     {
18        WRAP_TOLERANCE = ClientState.getClientState().getInteger("ui.buffersize", ClientDefaults.ui_buffersize);
19        WRAP_TO = WRAP_TOLERANCE / 2;
20     }
21
22     protected WrappedContainer last = null; // last element in our linked list of wrapped text containers
23
protected WrappedContainer text = null; // paint from this point on..
24
protected WrappedContainer head = null; // head element in our linked list of wrapped text containers
25

26     protected int tempMax = 1;
27     protected int maxValue = 1;
28     protected int currentValue = 0;
29
30     protected boolean adjusting = false;
31
32     protected LinkedList listeners = new LinkedList();
33     protected ChangeEvent event;
34
35     protected int extent = 1;
36     protected int tempExtent = 1;
37
38     public WrappedData()
39     {
40         event = new ChangeEvent(this);
41     }
42
43     public ListIterator find(String JavaDoc text)
44     {
45         LinkedList values = new LinkedList();
46
47         int x = 0;
48
49         WrappedContainer temp = last;
50         while (temp != null)
51         {
52            if (temp.getText().toUpperCase().indexOf(text.toUpperCase()) > -1)
53            {
54                values.add(new Integer JavaDoc(x));
55            }
56
57            x++;
58            temp = temp.prev;
59         }
60
61         ListIterator i = values.listIterator();
62
63         while (i.hasNext())
64         {
65            int tempzz = ( (Integer JavaDoc)i.next() ).intValue();
66
67            if (tempzz >= currentValue)
68            {
69               i.previous();
70               break;
71            }
72         }
73
74         return i;
75     }
76
77     public void dirty()
78     {
79         WrappedContainer temp = last;
80         while (temp != null)
81         {
82            temp.reset(); // resets its parameters, tells it that we are now "dirty"
83
temp = temp.previous();
84         }
85     }
86
87     public void reset()
88     {
89         tempMax = 1;
90         maxValue = 1;
91         currentValue = 0;
92         extent = 1;
93         tempExtent = 1;
94         head = null;
95         last = null;
96         text = null;
97         fireChangeEvent();
98     }
99
100     public WrappedContainer getCurrentText()
101     {
102         return text;
103     }
104
105     public WrappedObject getTokenAt(int height, int pixelx, int pixely)
106     {
107        int baseline = height - 5; // gives us a 5 pixel buffer
108
// between the textbox and the textarea
109

110        int lineno = 0;
111        int desired = TextSource.translateToLineNumber(pixely);
112
113        WrappedContainer head = text;
114        AttributedText[] strings;
115
116        while (head != null && baseline > 0)
117        {
118            strings = head.getWrappedText();
119
120            for (int x = 0; strings != null && x < strings.length && baseline > 0; x++)
121            {
122               if (pixely >= (baseline - TextSource.fontMetrics.getHeight() - 2))
123               {
124                  return new WrappedObject(head.getText(), head.getTokenAt(strings[x], pixelx));
125               }
126               lineno++;
127               baseline -= (TextSource.fontMetrics.getHeight() + 2);
128            }
129
130            head = head.next();
131         }
132          
133         return null;
134     }
135
136     public WrappedObject getAttributesAt(int height, int pixelx, int pixely)
137     {
138        int baseline = height - 5; // gives us a 5 pixel buffer
139
// between the textbox and the textarea
140

141        int lineno = 0;
142        int desired = TextSource.translateToLineNumber(pixely);
143
144        WrappedContainer head = text;
145        AttributedText[] strings;
146
147        while (head != null && baseline > 0)
148        {
149            strings = head.getWrappedText();
150
151            for (int x = 0; x < strings.length && baseline > 0; x++)
152            {
153               if (pixely >= (baseline - TextSource.fontMetrics.getHeight() - 2))
154               {
155                  return new WrappedObject(head.getText(), head.getAttributedTextAt(strings[x], pixelx));
156               }
157               lineno++;
158               baseline -= (TextSource.fontMetrics.getHeight() + 2);
159            }
160
161            head = head.next();
162         }
163          
164         return null;
165     }
166
167     protected Runnable JavaDoc changeEventTask = new Runnable JavaDoc()
168     {
169         public void run()
170         {
171            fireChangeEvent();
172         }
173     };
174
175     public void fireChangeEvent()
176     {
177         ListIterator i = listeners.listIterator();
178         while (i.hasNext())
179         {
180            ChangeListener temp = (ChangeListener)i.next();
181            temp.stateChanged(event);
182         }
183     }
184
185     public void addChangeListener(ChangeListener x)
186     {
187         listeners.add(x);
188     }
189
190     public void removeChangeListener(ChangeListener x)
191     {
192         listeners.remove(x);
193     }
194
195     public void addText(WrappedContainer container)
196     {
197         boolean jump = true;
198
199         if (head == null)
200         {
201            head = container;
202            text = container;
203            last = container;
204         }
205         else
206         {
207            container.setNext(head);
208            head.setPrevious(container);
209
210            head = container;
211
212            if (text.previous() != head) // if the current text has a previous text, then we're scrolled up
213
{
214               maxValue++;
215            }
216            else if (!getValueIsAdjusting())
217            {
218               text = container;
219
220               if (maxValue > WRAP_TOLERANCE)
221               {
222                  while (maxValue > WRAP_TO && last.hasPrevious())
223                  {
224                     last = last.previous();
225                     maxValue--;
226                  }
227                  last.setNext(null);
228               }
229
230               currentValue = maxValue;
231               maxValue++;
232            }
233            else
234            {
235               tempMax++;
236            }
237         }
238
239         SwingUtilities.invokeLater(changeEventTask); // this is in place to prevent a case of deadlock
240
}
241
242     public int getExtent()
243     {
244         return 1;
245     }
246
247     public int getMaximum()
248     {
249         return maxValue;
250     }
251
252     public int getMinimum()
253     {
254         return 0;
255     }
256
257     public int getValue()
258     {
259         return currentValue;
260     }
261
262     public boolean getValueIsAdjusting()
263     {
264         return adjusting;
265     }
266
267     public void setExtent(int x)
268     {
269         // do a check on is adjusting if we're going to choose wether or not to update the extent.
270

271         extent = x;
272     }
273
274     public void setMaximum(int x)
275     {
276     }
277
278     public void setMinimum(int x)
279     {
280     }
281
282     public void setRangeProperties(int newValue, int extent, int min, int max, boolean adjusting)
283     {
284     }
285
286     public void setValue (int newValue)
287     {
288         if (head == null)
289             return;
290
291         if (currentValue > newValue)
292         {
293              // move down to new value using next values
294

295             while (currentValue > newValue && text.hasNext())
296             {
297                 text = text.next();
298                 currentValue--;
299             }
300         }
301         else if (currentValue < newValue)
302         {
303             // move up to new value using prev values;
304

305             while (currentValue < newValue && text.hasPrevious())
306             {
307                 text = text.previous();
308                 currentValue++;
309             }
310         }
311
312         currentValue = newValue;
313
314         if (currentValue >= maxValue)
315         {
316             currentValue = maxValue - 1;
317         }
318         if (currentValue < 0)
319         {
320             currentValue = 0;
321         }
322
323         //System.out.println(currentValue + " versus possible is: " +
324
//maxValue + " and extent is: " + extent);
325

326         fireChangeEvent();
327     }
328
329     public void setValueIsAdjusting(boolean b)
330     {
331         adjusting = b;
332         if (!b && tempMax > maxValue)
333         {
334            maxValue = tempMax;
335         }
336         else
337         {
338            tempMax = maxValue;
339         }
340     }
341
342     protected SelectionSpace selection;
343
344     public void setSelection(SelectionSpace select)
345     {
346         selection = select;
347     }
348
349     public SelectionSpace getSelection()
350     {
351         return selection;
352     }
353 }
354
355
356
Popular Tags