KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > gui > FlowLabel


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.upgrade.gui;
25
26 import java.awt.*;
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.text.BreakIterator JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 public class FlowLabel extends Canvas
34 {
35
36     private String JavaDoc labelText;
37     private Vector JavaDoc textVector;
38     private transient Object JavaDoc textVectorLock;
39     private static Font labelFont = null;
40     private static String JavaDoc addAttr[] = {
41         "<bold>", "<b>", "<italics>", "<i>", "<tt>", "<br>", "<p>"
42     };
43     private static int attrBold = 3;
44     private static int attrItalics = 12;
45     private static int attrTt = 16;
46     private static int attrBreak;
47     private static int attrParagraph;
48     private static int attrFlowControlOnly;
49     private static String JavaDoc removeAttr[] = {
50         "</bold>", "</b>", "</italics>", "</i>", "</tt>", "</br>", "</p>"
51     };
52     private transient Dimension currentSize;
53     private transient Object JavaDoc currentSizeLock;
54     private transient Vector JavaDoc paintVector;
55     private transient Object JavaDoc paintVectorLock;
56     private int horizontalMargin;
57     private int verticalMargin;
58     private transient boolean resizePerformed;
59     static final long serialVersionUID = 0xc821ccd1154af049L;
60
61     public FlowLabel()
62     {
63         labelText = "";
64         textVector = new Vector JavaDoc();
65         textVectorLock = new Object JavaDoc();
66         currentSize = new Dimension(0, 0);
67         currentSizeLock = new Object JavaDoc();
68         paintVector = new Vector JavaDoc();
69         paintVectorLock = new Object JavaDoc();
70         horizontalMargin = 10;
71         verticalMargin = 5;
72         resizePerformed = false;
73     }
74
75     public FlowLabel(String JavaDoc s)
76     {
77         labelText = "";
78         textVector = new Vector JavaDoc();
79         textVectorLock = new Object JavaDoc();
80         currentSize = new Dimension(0, 0);
81         currentSizeLock = new Object JavaDoc();
82         paintVector = new Vector JavaDoc();
83         paintVectorLock = new Object JavaDoc();
84         horizontalMargin = 10;
85         verticalMargin = 5;
86         resizePerformed = false;
87         labelText = s;
88         textVector = createTextVector();
89     }
90
91     private Vector JavaDoc createPaintVector(Dimension dimension)
92     {
93         Vector JavaDoc vector = new Vector JavaDoc();
94         try
95         {
96             Dimension dimension1 = new Dimension(dimension);
97             dimension1.width -= horizontalMargin * 2;
98             dimension1.height -= verticalMargin * 2;
99             int i = 0;
100             boolean flag = false;
101             Vector JavaDoc vector1 = new Vector JavaDoc();
102             String JavaDoc s = new String JavaDoc();
103             Enumeration JavaDoc enumeration = null;
104             boolean flag1 = false;
105             int i1 = 0;
106             boolean flag2 = false;
107             int k1 = 0;
108             Font font = getFont();
109             font = new Font(font.getName(), 0, font.getSize());
110             FontMetrics fontmetrics = getToolkit().getFontMetrics(font);
111             synchronized(textVector)
112             {
113                 enumeration = textVector.elements();
114             }
115             while(enumeration.hasMoreElements())
116             {
117                 Object JavaDoc obj = enumeration.nextElement();
118                 if(obj instanceof Integer JavaDoc)
119                 {
120                     int l1 = ((Integer JavaDoc)obj).intValue();
121                     if((l1 & attrBreak) != 0)
122                     {
123                         vector.addElement(vector1);
124                         vector1 = new Vector JavaDoc();
125                         int j = i1;
126                         k1 = 0;
127                         i = 0;
128                     } else
129                     if((l1 & attrParagraph) != 0)
130                     {
131                         vector.addElement(vector1);
132                         vector1 = new Vector JavaDoc();
133                         vector.addElement(vector1);
134                         vector1 = new Vector JavaDoc();
135                         int k = i1;
136                         k1 = 0;
137                         i = 0;
138                     }
139                     font = getNewFont(font, ((Integer JavaDoc)obj).intValue());
140                     fontmetrics = getToolkit().getFontMetrics(font);
141                     vector1.addElement(obj);
142                 } else
143                 {
144                     String JavaDoc s1 = (String JavaDoc)obj;
145                     int l = 0;
146                     for(i1 = 0; i1 < s1.length() - 1;)
147                     {
148                         if(k1 == 0)
149                         {
150                             l = skipWhitespace(s1, l);
151                             i1 = getNextWordIndex(s1, i1);
152                             k1++;
153                         }
154                         for(int j1 = getNextWordIndex(s1, i1); j1 <= s1.length() && fontmetrics.stringWidth(s1.substring(l, j1)) + i < dimension1.width;)
155                         {
156                             i1 = j1;
157                             j1 = getNextWordIndex(s1, i1);
158                             k1++;
159                         }
160
161                         if(i1 >= s1.length() - 1)
162                         {
163                             i = fontmetrics.stringWidth(s1.substring(l, i1)) + i;
164                             vector1.addElement(s1.substring(l, i1));
165                         } else
166                         {
167                             vector1.addElement(s1.substring(l, i1));
168                             vector.addElement(vector1);
169                             vector1 = new Vector JavaDoc();
170                             l = i1;
171                             k1 = 0;
172                             i = 0;
173                         }
174                     }
175
176                 }
177             }
178             if(vector1.size() > 0)
179             {
180                 vector.addElement(vector1);
181             }
182         }
183         catch(Exception JavaDoc _ex)
184         {
185             return createPaintVector(dimension);
186         }
187         return vector;
188     }
189
190     private Vector JavaDoc createTextVector()
191     {
192         Vector JavaDoc vector = new Vector JavaDoc();
193         int i = 0;
194         int j = 0;
195         StringBuffer JavaDoc stringbuffer = new StringBuffer JavaDoc();
196         boolean flag = false;
197         while(i < labelText.length())
198         {
199             boolean flag1 = false;
200             while(i < labelText.length() && labelText.charAt(i) == '<')
201             {
202                 for(int k = 0; k < addAttr.length; k++)
203                 {
204                     if(labelText.length() >= i + addAttr[k].length() && labelText.substring(i, i + addAttr[k].length()).equalsIgnoreCase(addAttr[k]))
205                     {
206                         j |= 1 << k;
207                         i += addAttr[k].length();
208                         flag1 = true;
209                     }
210                     if(labelText.length() >= i + removeAttr[k].length() && labelText.substring(i, i + removeAttr[k].length()).equalsIgnoreCase(removeAttr[k]))
211                     {
212                         j &= 255 - (1 << k);
213                         i += removeAttr[k].length();
214                         flag1 = (1 << k & attrFlowControlOnly) == 0;
215                     }
216                 }
217
218                 if(!flag1)
219                 {
220                     break;
221                 }
222             }
223             if(flag1)
224             {
225                 int l = stringbuffer.length();
226                 if(l > 0)
227                 {
228                     vector.addElement(stringbuffer.toString());
229                     stringbuffer = new StringBuffer JavaDoc();
230                 }
231                 vector.addElement(new Integer JavaDoc(j));
232                 j &= ~attrFlowControlOnly;
233             } else
234             {
235                 if(labelText.charAt(i) == '\n')
236                 {
237                     if(stringbuffer.length() > 0)
238                     {
239                         vector.addElement(stringbuffer.toString());
240                         stringbuffer = new StringBuffer JavaDoc();
241                     }
242                     vector.addElement(new Integer JavaDoc(attrBreak));
243                     i++;
244                 }
245                 if(i < labelText.length())
246                 {
247                     stringbuffer.append(labelText.charAt(i));
248                     i++;
249                 }
250             }
251         }
252         if(stringbuffer.length() > 0)
253         {
254             vector.addElement(stringbuffer.toString());
255         }
256         return vector;
257     }
258
259     public void forceResize()
260     {
261         if(!resizePerformed)
262         {
263             resizePerformed = true;
264             invalidate();
265             Container container;
266             for(container = getParent(); container != null && container.getParent() != null; container = container.getParent()) { }
267             container.validate();
268         }
269     }
270
271     public String JavaDoc getContentString()
272     {
273         String JavaDoc s = "";
274         Vector JavaDoc vector = createTextVector();
275         for(Enumeration JavaDoc enumeration = vector.elements(); enumeration.hasMoreElements();)
276         {
277             Object JavaDoc obj = enumeration.nextElement();
278             if(obj instanceof String JavaDoc)
279             {
280                 s = s + obj;
281             }
282         }
283
284         return s;
285     }
286
287     public Dimension getMinimumSize()
288     {
289         return getPreferredSize();
290     }
291
292     private Font getNewFont(Font font, int i)
293     {
294         if(font != null)
295         {
296             if(labelFont == null && (i & attrTt) != 0)
297             {
298                 labelFont = font;
299                 font = new Font("Monospaced", 0, font.getSize());
300                 if(font == null)
301                 {
302                     font = labelFont;
303                     labelFont = null;
304                 }
305             } else
306             if(labelFont != null && (i & attrTt) == 0)
307             {
308                 font = labelFont;
309                 labelFont = null;
310             }
311             font = new Font(font.getName(), ((i & attrBold) == 0 ? 0 : 1) | ((i & attrItalics) == 0 ? 0 : 2), font.getSize());
312         }
313         return font;
314     }
315
316     private int getNextWordIndex(String JavaDoc s, int i)
317     {
318         int j;
319         for(j = i + 1; j < s.length() && Character.isWhitespace(s.charAt(j)); j++) { }
320         if(j < s.length())
321         {
322             BreakIterator JavaDoc breakiterator = BreakIterator.getLineInstance();
323             breakiterator.setText(s.substring(j, s.length()));
324             int k = breakiterator.first();
325             k = breakiterator.next();
326             if(k != -1)
327             {
328                 if(s.charAt((k + j) - 1) == '.')
329                 {
330                     BreakIterator JavaDoc breakiterator1 = BreakIterator.getSentenceInstance();
331                     breakiterator1.setText(s);
332                     int l;
333                     for(l = breakiterator1.first(); l != -1 && l < k + j; l = breakiterator1.next()) { }
334                     if(l != k + j)
335                     {
336                         return getNextWordIndex(s, k + j);
337                     }
338                 }
339                 return k + j;
340             }
341         }
342         return j;
343     }
344
345     public Dimension getPreferredSize()
346     {
347         Container container = getParent();
348         Dimension dimension = container.getSize();
349         Dimension dimension1 = new Dimension(1, 1);
350         if(dimension.width == 0)
351         {
352             dimension = new Dimension(400, 200);
353         }
354         Insets insets = container.getInsets();
355         try
356         {
357             Font font = getNewFont(getFont(), 0);
358             FontMetrics fontmetrics = getToolkit().getFontMetrics(font);
359             dimension1 = new Dimension(dimension.width - insets.right - insets.left, dimension.height - insets.top - insets.bottom);
360             synchronized(currentSizeLock)
361             {
362                 synchronized(paintVectorLock)
363                 {
364                     Vector JavaDoc vector = createPaintVector(dimension1);
365                     dimension1.height = verticalMargin * 2 + fontmetrics.getHeight() * vector.size();
366                 }
367             }
368         }
369         catch(Error JavaDoc _ex)
370         {
371             return getPreferredSize();
372         }
373         return dimension1;
374     }
375
376     public void paint(Graphics g)
377     {
378         Dimension dimension = getSize();
379         synchronized(currentSizeLock)
380         {
381             if(currentSize.width != dimension.width)
382             {
383                 sizeChanged(dimension);
384             }
385         }
386         Dimension dimension1 = getPreferredSize();
387         if(dimension.height < dimension1.height && !resizePerformed)
388         {
389             forceResize();
390             return;
391         }
392         g.setClip(0, 0, dimension.width, dimension.height);
393         g.setColor(getForeground());
394         Font font = getFont();
395         font = new Font(font.getName(), 0, font.getSize());
396         FontMetrics fontmetrics = getToolkit().getFontMetrics(font);
397         g.setFont(font);
398         int i = horizontalMargin;
399         int j = fontmetrics.getMaxAscent() + verticalMargin;
400         synchronized(paintVectorLock)
401         {
402             for(int k = 0; k < paintVector.size(); k++)
403             {
404                 Vector JavaDoc vector = (Vector JavaDoc)paintVector.elementAt(k);
405                 for(int l = 0; l < vector.size(); l++)
406                 {
407                     Object JavaDoc obj2 = vector.elementAt(l);
408                     if(obj2 instanceof Integer JavaDoc)
409                     {
410                         font = getNewFont(font, ((Integer JavaDoc)obj2).intValue());
411                         fontmetrics = getToolkit().getFontMetrics(font);
412                         g.setFont(font);
413                     }
414                     if(obj2 instanceof String JavaDoc)
415                     {
416                         try
417                         {
418                             g.drawString((String JavaDoc)obj2, i, j);
419                             i += fontmetrics.stringWidth((String JavaDoc)obj2);
420                         }
421                         catch(Exception JavaDoc _ex)
422                         {
423                             repaint();
424                         }
425                     }
426                 }
427
428                 i = horizontalMargin;
429                 j += fontmetrics.getHeight();
430             }
431
432         }
433     }
434
435     private void readObject(ObjectInputStream JavaDoc objectinputstream)
436         throws IOException JavaDoc
437     {
438         try
439         {
440             objectinputstream.defaultReadObject();
441         }
442         catch(ClassNotFoundException JavaDoc classnotfoundexception)
443         {
444             classnotfoundexception.printStackTrace();
445         }
446         finally
447         {
448             textVectorLock = new Object JavaDoc();
449             paintVectorLock = new Object JavaDoc();
450             paintVector = new Vector JavaDoc();
451             currentSize = new Dimension(0, 0);
452             currentSizeLock = new Object JavaDoc();
453         }
454     }
455
456     public void setHgap(int i)
457     {
458         horizontalMargin = i;
459     }
460
461     public void setText(String JavaDoc s)
462     {
463         labelText = s;
464         synchronized(textVectorLock)
465         {
466             textVector = createTextVector();
467         }
468         Dimension dimension = getSize();
469         if(dimension.width > 0 || dimension.height > 0)
470         {
471             synchronized(paintVectorLock)
472             {
473                 paintVector = createPaintVector(dimension);
474             }
475         }
476         if(isShowing())
477         {
478             invalidate();
479             repaint();
480         }
481     }
482
483     public void setVgap(int i)
484     {
485         verticalMargin = i;
486     }
487
488     public void sizeChanged(Dimension dimension)
489     {
490         synchronized(currentSizeLock)
491         {
492             Dimension dimension1 = currentSize;
493             if(!dimension.equals(dimension1))
494             {
495                 try
496                 {
497                     Graphics g = getGraphics();
498                     g.setColor(getBackground());
499                     g.fillRect(0, 0, dimension1.width + 1, dimension1.height + 1);
500                 }
501                 catch(Exception JavaDoc _ex) { }
502             }
503             if(dimension.width != dimension1.width)
504             {
505                 synchronized(paintVectorLock)
506                 {
507                     paintVector = createPaintVector(dimension);
508                 }
509             }
510             currentSize = dimension;
511             resizePerformed = false;
512         }
513     }
514
515     private int skipWhitespace(String JavaDoc s, int i)
516     {
517         int j;
518         for(j = i; j < s.length() && Character.isWhitespace(s.charAt(j)); j++) { }
519         return j;
520     }
521
522     static
523     {
524         attrBreak = 32;
525         attrParagraph = 64;
526         attrFlowControlOnly = attrBreak | attrParagraph;
527     }
528 }
529
Popular Tags