KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > MultilinePanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.ui;
21
22 import java.awt.Font JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import javax.swing.Box JavaDoc;
25 import javax.swing.BoxLayout JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28
29 /**
30  * Panel for displaying multiline labels.
31  * Also a title (emphasized first line of the text) may be specified.
32  * The text is parsed using <CODE>\n</CODE> as a delimiter.
33  * <P>
34  * The panel is built from several <TT>JLabel</TT>s, layed out vertically
35  * using {@link BoxLayout}. A vertical glue ({@link Box#createVerticalGlue()})
36  * is added on the bottom of the panel.
37  *
38  * @author Marian Petras
39  */

40 public final class MultilinePanel extends JPanel JavaDoc {
41     
42     /**
43      * Creates a panel containing the specified text.
44      *
45      * @param text text to be displayed
46      */

47     public MultilinePanel(String JavaDoc text) {
48         this(null, text);
49     }
50     
51     /**
52      * Creates a panel containing the specified title and text.
53      * The title will be displayed as an emphasized (bold) first
54      * line of text.
55      *
56      * @param title title of the dialog
57      * (if <CODE>null</CODE>, no title is displayed)
58      * @param text text to be displayed
59      */

60     public MultilinePanel(String JavaDoc title, String JavaDoc text) {
61         super();
62         setLayout(new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS));
63         //
64
if (title != null) {
65             JLabel JavaDoc label = new JLabel JavaDoc(title);
66             label.setFont(label.getFont().deriveFont(Font.BOLD));
67             add(label);
68         }
69         //
70
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(text, "\n", true); //NOI18N
71
boolean lastWasNewline = true;
72         for (int i = 0; tokenizer.hasMoreTokens(); ++i) {
73             String JavaDoc line = tokenizer.nextToken();
74             if ("\n".equals(line)) { //NOI18N
75
if (!lastWasNewline) {
76                     lastWasNewline = true;
77                     continue; //newline after text - end of line
78
}
79                 //two adjacent newlines - empty line
80
line = " "; //empty JLabels have zero height //NOI18N
81
}
82             else {
83                 lastWasNewline = false;
84             }
85             add(new JLabel JavaDoc(line));
86         }
87         add(Box.createVerticalGlue());
88     }
89     
90 }
91
Popular Tags