View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.components;
20  
21  import javax.swing.JTextArea;
22  import javax.swing.LookAndFeel;
23  import javax.swing.text.Highlighter;
24  
25  /***
26  * A custom JTextArea that looks and feels like a JLabel, but supports
27  * line wrapping.  This works a lot more like the IFC label component.
28  * NOTE: doesn't support icons (yet).
29  *
30  * @author michael@mpowers.net
31  * @author $Author: cgruber $
32  * @date $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $
33  * @revision $Revision: 904 $
34  */
35  public class MultiLineLabel extends JTextArea
36  {
37  	/***
38  	* Saves a reference to the original highlighter
39  	* to enable/disable text selection. 
40  	*/
41  	protected Highlighter originalHighlighter;
42  	
43  /*
44  * Creates a MultiLineLabel instance with an empty string for the title.
45  */
46      public MultiLineLabel()
47      {
48          super();
49  
50          // turn on wrapping and disable editing and highlighting
51  
52          setLineWrap(true);
53          setWrapStyleWord(true);
54          setEditable(false);
55  		setSelectable( false );
56      }
57  
58  /*
59  * Creates a MultiLineLabel instance with the specified text.
60  * @param text The specified text.
61  */
62      public MultiLineLabel( String text )
63      {
64          super( text );
65  
66          // turn on wrapping and disable editing and highlighting
67  
68          setLineWrap(true);
69          setWrapStyleWord(true);
70          setEditable(false);
71  		setSelectable( false );
72      }
73  
74  /*
75  * Overridden to look like a label.
76  * @param text The specified text.
77  */
78      public void updateUI()
79      {
80          // got the implementation idea from usenet
81  
82          super.updateUI();
83  
84          // turn on wrapping and disable editing and highlighting
85  
86          setLineWrap(true);
87          setWrapStyleWord(true);
88          setEditable(false);
89  		setSelectable( false );
90  
91          // Set the text area's border, colors and font to
92          // that of a label
93  
94          LookAndFeel.installBorder(this, "Label.border");
95  
96          LookAndFeel.installColorsAndFont(this,
97              "Label.background",
98              "Label.foreground",
99              "Label.font");
100     }
101 
102 /***
103 * Sets whether text is selectable.
104 * Default is non-selectable text.
105 */
106 	public void setSelectable( boolean selectable )
107 	{
108 		if ( selectable )
109 		{
110 			setHighlighter( originalHighlighter );
111 		}
112 		else
113 		{
114 			originalHighlighter = getHighlighter();
115 			setHighlighter( null );
116 		}
117 	}
118 	
119 /***
120 * Gets whether text is selectable.
121 * Default is non-selectable text.
122 */
123 	public boolean isSelectable()
124 	{
125 		return ( getHighlighter() != null );	
126 	}
127 
128 /***
129 * Overridden to return false.
130 */
131 	public boolean isFocusTraversable()
132 	{
133 		return false;
134 	}	
135 }