44import java .awt .Color ;
55import java .awt .Dimension ;
66import java .awt .Font ;
7+ import java .awt .FontMetrics ;
78import java .awt .event .TextListener ;
89import java .awt .im .InputMethodRequests ;
910
@@ -27,6 +28,106 @@ public void isAWT() {
2728
2829 private int verticalScrollBarPolicy ;
2930
31+ /**
32+ * Create and display both vertical and horizontal scrollbars.
33+ *
34+ * @since JDK1.1
35+ */
36+ public static final int SCROLLBARS_BOTH = 0 ;
37+
38+ /**
39+ * Create and display vertical scrollbar only.
40+ *
41+ * @since JDK1.1
42+ */
43+ public static final int SCROLLBARS_VERTICAL_ONLY = 1 ;
44+
45+ /**
46+ * Create and display horizontal scrollbar only.
47+ *
48+ * @since JDK1.1
49+ */
50+ public static final int SCROLLBARS_HORIZONTAL_ONLY = 2 ;
51+
52+ /**
53+ * Do not create or display any scrollbars for the text area.
54+ *
55+ * @since JDK1.1
56+ */
57+ public static final int SCROLLBARS_NONE = 3 ;
58+
59+ public TextArea (int rows , int cols ) {
60+ this (null , rows , cols , SCROLLBARS_BOTH );
61+ }
62+
63+ public TextArea () {
64+ this (null , 0 , 0 , SCROLLBARS_BOTH );
65+ }
66+
67+ public TextArea (String text ) {
68+ this (text , 0 , 0 , SCROLLBARS_BOTH );
69+ }
70+
71+ public TextArea (String text , int rows , int cols ) {
72+ this (text , rows , cols , SCROLLBARS_BOTH );
73+ }
74+
75+ public TextArea (String text , int rows , int columns , int scrollbars ) {
76+ super (text , rows < 0 ? 0 : rows , columns < 0 ? 0 : columns );
77+ setWrapStyleWord (false );
78+ setLineWrap (false );
79+ switch (scrollbars ) {
80+ case SCROLLBARS_BOTH :
81+ setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_AS_NEEDED );
82+ setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_AS_NEEDED );
83+ break ;
84+ case SCROLLBARS_VERTICAL_ONLY :
85+ setLineWrap (true );
86+ setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_AS_NEEDED );
87+ setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
88+ break ;
89+ case SCROLLBARS_HORIZONTAL_ONLY :
90+ setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_NEVER );
91+ setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_AS_NEEDED );
92+ break ;
93+ case SCROLLBARS_NONE :
94+ setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_NEVER );
95+ setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
96+ break ;
97+ }
98+ }
99+
100+ public int getVerticalScrollBarPolicy () {
101+ return verticalScrollBarPolicy ;
102+ }
103+
104+ private void setVerticalScrollBarPolicy (int policy ) {
105+ int old = verticalScrollBarPolicy ;
106+ verticalScrollBarPolicy = policy ;
107+ firePropertyChange ("verticalScrollBarPolicy" , old , policy );
108+ revalidate ();
109+ repaint ();
110+ }
111+
112+ public int getHorizontalScrollBarPolicy () {
113+ return horizontalScrollBarPolicy ;
114+ }
115+
116+ private void setHorizontalScrollBarPolicy (int policy ) {
117+ int old = horizontalScrollBarPolicy ;
118+ horizontalScrollBarPolicy = policy ;
119+ firePropertyChange ("horizontalScrollBarPolicy" , old , policy );
120+ revalidate ();
121+ repaint ();
122+ }
123+
124+ public int getScrollbarVisibility () {
125+ boolean v = (getVerticalScrollBarPolicy () != ScrollPaneConstants .VERTICAL_SCROLLBAR_NEVER );
126+ boolean h = (getHorizontalScrollBarPolicy () != ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
127+ return (v && h ? SCROLLBARS_BOTH
128+ : v ? SCROLLBARS_VERTICAL_ONLY : h ? SCROLLBARS_HORIZONTAL_ONLY : SCROLLBARS_NONE );
129+ }
130+
30131 public synchronized void addTextListener (TextListener l ) {
31132 if (l == null ) {
32133 return ;
@@ -67,11 +168,24 @@ public Dimension getPreferredSize() {
67168 @ Override
68169 @ Deprecated
69170 public Dimension preferredSize () {
70- synchronized (getTreeLock ()) {
171+ // synchronized (getTreeLock()) {
71172 return ((super .rows > 0 ) && (super .columns > 0 )) ? preferredSize (super .rows , super .columns ) : super .preferredSize ();
72173 }
73- }
174+ // }
74175
176+ @ Override
177+ protected int getColumnWidth () {
178+
179+ //A column is an approximate average character width that is platform-dependent.
180+ //
181+ // We will call that "n" -- it is pretty close
182+ //
183+ if (columnWidth == 0 ) {
184+ FontMetrics metrics = getFontMetrics (getFont ());
185+ columnWidth = metrics .charWidth ('n' );
186+ }
187+ return columnWidth ;
188+ }
75189 /**
76190 * Determines the minimum size of a text area with the specified number of rows
77191 * and columns.
@@ -185,159 +299,6 @@ public InputMethodRequests getInputMethodRequests() {
185299// return super.getCaretPosition();
186300// }
187301//
188- /**
189- * Create and display both vertical and horizontal scrollbars.
190- *
191- * @since JDK1.1
192- */
193- public static final int SCROLLBARS_BOTH = 0 ;
194-
195- /**
196- * Create and display vertical scrollbar only.
197- *
198- * @since JDK1.1
199- */
200- public static final int SCROLLBARS_VERTICAL_ONLY = 1 ;
201-
202- /**
203- * Create and display horizontal scrollbar only.
204- *
205- * @since JDK1.1
206- */
207- public static final int SCROLLBARS_HORIZONTAL_ONLY = 2 ;
208-
209- /**
210- * Do not create or display any scrollbars for the text area.
211- *
212- * @since JDK1.1
213- */
214- public static final int SCROLLBARS_NONE = 3 ;
215-
216- public TextArea (int rows , int cols ) {
217- this (null , rows , cols , SCROLLBARS_BOTH );
218- }
219-
220- public TextArea () {
221- this (null , 0 , 0 , SCROLLBARS_BOTH );
222- }
223-
224- public TextArea (String text ) {
225- this (text , 0 , 0 , SCROLLBARS_BOTH );
226- }
227-
228- public TextArea (String text , int rows , int cols ) {
229- this (text , rows , cols , SCROLLBARS_BOTH );
230- }
231-
232- public TextArea (String text , int rows , int columns , int scrollbars ) {
233- super (text , rows < 0 ? 0 : rows , columns < 0 ? 0 : columns );
234- switch (scrollbars ) {
235- case SCROLLBARS_BOTH :
236- setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_AS_NEEDED );
237- setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_AS_NEEDED );
238- break ;
239- case SCROLLBARS_VERTICAL_ONLY :
240- setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_AS_NEEDED );
241- setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
242- break ;
243- case SCROLLBARS_HORIZONTAL_ONLY :
244- setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_NEVER );
245- setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_AS_NEEDED );
246- break ;
247- case SCROLLBARS_NONE :
248- setVerticalScrollBarPolicy (ScrollPaneConstants .VERTICAL_SCROLLBAR_NEVER );
249- setHorizontalScrollBarPolicy (ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
250- break ;
251- }
252- }
253-
254- /**
255- * Returns the vertical scroll bar policy value.
256- * @return the <code>verticalScrollBarPolicy</code> property
257- * @see #setVerticalScrollBarPolicy
258- */
259- public int getVerticalScrollBarPolicy () {
260- return verticalScrollBarPolicy ;
261- }
262-
263-
264- /**
265- * Determines when the vertical scrollbar appears in the scrollpane.
266- * Legal values are:
267- * <ul>
268- * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED</code>
269- * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER</code>
270- * <li><code>ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS</code>
271- * </ul>
272- *
273- * @param policy one of the three values listed above
274- * @exception IllegalArgumentException if <code>policy</code>
275- * is not one of the legal values shown above
276- * @see #getVerticalScrollBarPolicy
277- *
278- * @beaninfo
279- * preferred: true
280- * bound: true
281- * description: The scrollpane vertical scrollbar policy
282- * enum: VERTICAL_SCROLLBAR_AS_NEEDED ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
283- * VERTICAL_SCROLLBAR_NEVER ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER
284- * VERTICAL_SCROLLBAR_ALWAYS ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
285- */
286- private void setVerticalScrollBarPolicy (int policy ) {
287- int old = verticalScrollBarPolicy ;
288- verticalScrollBarPolicy = policy ;
289- firePropertyChange ("verticalScrollBarPolicy" , old , policy );
290- revalidate ();
291- repaint ();
292- }
293-
294-
295- /**
296- * Returns the horizontal scroll bar policy value.
297- * @return the <code>horizontalScrollBarPolicy</code> property
298- * @see #setHorizontalScrollBarPolicy
299- */
300- public int getHorizontalScrollBarPolicy () {
301- return horizontalScrollBarPolicy ;
302- }
303-
304-
305- /**
306- * Determines when the horizontal scrollbar appears in the scrollpane.
307- * The options are:<ul>
308- * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED</code>
309- * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER</code>
310- * <li><code>ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS</code>
311- * </ul>
312- *
313- * @param policy one of the three values listed above
314- * @exception IllegalArgumentException if <code>policy</code>
315- * is not one of the legal values shown above
316- * @see #getHorizontalScrollBarPolicy
317- *
318- * @beaninfo
319- * preferred: true
320- * bound: true
321- * description: The scrollpane scrollbar policy
322- * enum: HORIZONTAL_SCROLLBAR_AS_NEEDED ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
323- * HORIZONTAL_SCROLLBAR_NEVER ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
324- * HORIZONTAL_SCROLLBAR_ALWAYS ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
325- */
326- private void setHorizontalScrollBarPolicy (int policy ) {
327- int old = horizontalScrollBarPolicy ;
328- horizontalScrollBarPolicy = policy ;
329- firePropertyChange ("horizontalScrollBarPolicy" , old , policy );
330- revalidate ();
331- repaint ();
332- }
333-
334- public int getScrollbarVisibility () {
335- boolean v = (getVerticalScrollBarPolicy () != ScrollPaneConstants .VERTICAL_SCROLLBAR_NEVER );
336- boolean h = (getHorizontalScrollBarPolicy () != ScrollPaneConstants .HORIZONTAL_SCROLLBAR_NEVER );
337- return (v && h ? SCROLLBARS_BOTH
338- : v ? SCROLLBARS_VERTICAL_ONLY : h ? SCROLLBARS_HORIZONTAL_ONLY : SCROLLBARS_NONE );
339- }
340-
341302 @ Override
342303 public void setCaretPosition (int pos ) {
343304 super .setCaretPosition (pos );
0 commit comments