KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicTextAreaUI


1 /*
2  * @(#)BasicTextAreaUI.java 1.68 03/01/23
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.basic;
8
9 import java.beans.*;
10 import java.awt.*;
11 import java.awt.event.KeyEvent JavaDoc;
12 import java.awt.event.InputEvent JavaDoc;
13 import javax.swing.*;
14 import javax.swing.event.DocumentEvent JavaDoc;
15 import javax.swing.text.*;
16 import javax.swing.plaf.*;
17
18 /**
19  * Provides the look and feel for a plain text editor. In this
20  * implementation the default UI is extended to act as a simple
21  * view factory.
22  * <p>
23  * <strong>Warning:</strong>
24  * Serialized objects of this class will not be compatible with
25  * future Swing releases. The current serialization support is
26  * appropriate for short term storage or RMI between applications running
27  * the same version of Swing. As of 1.4, support for long term storage
28  * of all JavaBeans<sup><font size="-2">TM</font></sup>
29  * has been added to the <code>java.beans</code> package.
30  * Please see {@link java.beans.XMLEncoder}.
31  *
32  * @author Timothy Prinzing
33  * @version 1.68 01/23/03
34  */

35 public class BasicTextAreaUI extends BasicTextUI JavaDoc {
36     
37     /**
38      * Creates a UI for a JTextArea.
39      *
40      * @param ta a text area
41      * @return the UI
42      */

43     public static ComponentUI createUI(JComponent ta) {
44         return new BasicTextAreaUI JavaDoc();
45     }
46
47     /**
48      * Constructs a new BasicTextAreaUI object.
49      */

50     public BasicTextAreaUI() {
51     super();
52     }
53
54     /**
55      * Fetches the name used as a key to look up properties through the
56      * UIManager. This is used as a prefix to all the standard
57      * text properties.
58      *
59      * @return the name ("TextArea")
60      */

61     protected String JavaDoc getPropertyPrefix() {
62     return "TextArea";
63     }
64     
65     protected void installDefaults() {
66         super.installDefaults();
67         //the fix for 4785160 is undone
68
}
69
70     /**
71      * This method gets called when a bound property is changed
72      * on the associated JTextComponent. This is a hook
73      * which UI implementations may change to reflect how the
74      * UI displays bound properties of JTextComponent subclasses.
75      * This is implemented to rebuild the View when the
76      * <em>WrapLine</em> or the <em>WrapStyleWord</em> property changes.
77      *
78      * @param evt the property change event
79      */

80     protected void propertyChange(PropertyChangeEvent evt) {
81     if (evt.getPropertyName().equals("lineWrap") ||
82         evt.getPropertyName().equals("wrapStyleWord") ||
83         evt.getPropertyName().equals("tabSize")) {
84         // rebuild the view
85
modelChanged();
86     } else if ("editable".equals(evt.getPropertyName())) {
87         updateFocusTraversalKeys();
88     }
89     }
90
91
92     /**
93      * The method is overridden to take into account caret width.
94      *
95      * @param c the editor component
96      * @return the preferred size
97      * @throws IllegalArgumentException if invalid value is passed
98      *
99      * @since 1.5
100      */

101     public Dimension getPreferredSize(JComponent c) {
102         return super.getPreferredSize(c);
103         //the fix for 4785160 is undone
104
}
105                                                                           
106     /**
107      * The method is overridden to take into account caret width.
108      *
109      * @param c the editor component
110      * @return the minimum size
111      * @throws IllegalArgumentException if invalid value is passed
112      *
113      * @since 1.5
114      */

115     public Dimension getMinimumSize(JComponent c) {
116         return super.getMinimumSize(c);
117         //the fix for 4785160 is undone
118
}
119
120     /**
121      * Creates the view for an element. Returns a WrappedPlainView or
122      * PlainView.
123      *
124      * @param elem the element
125      * @return the view
126      */

127     public View create(Element elem) {
128     Document doc = elem.getDocument();
129     Object JavaDoc i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
130     if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
131         // build a view that support bidi
132
return createI18N(elem);
133     } else {
134         JTextComponent c = getComponent();
135         if (c instanceof JTextArea) {
136         JTextArea area = (JTextArea) c;
137         View v;
138         if (area.getLineWrap()) {
139             v = new WrappedPlainView(elem, area.getWrapStyleWord());
140         } else {
141             v = new PlainView(elem);
142         }
143         return v;
144         }
145     }
146     return null;
147     }
148
149     View createI18N(Element elem) {
150     String JavaDoc kind = elem.getName();
151     if (kind != null) {
152         if (kind.equals(AbstractDocument.ContentElementName)) {
153         return new PlainParagraph(elem);
154         } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
155         return new BoxView(elem, View.Y_AXIS);
156         }
157     }
158     return null;
159     }
160
161     /**
162      * Paragraph for representing plain-text lines that support
163      * bidirectional text.
164      */

165     static class PlainParagraph extends ParagraphView {
166
167     PlainParagraph(Element elem) {
168         super(elem);
169         layoutPool = new LogicalView(elem);
170         layoutPool.setParent(this);
171     }
172
173         public void setParent(View parent) {
174             super.setParent(parent);
175             if (parent != null) {
176                 setPropertiesFromAttributes();
177             }
178         }
179
180         protected void setPropertiesFromAttributes() {
181         Component c = getContainer();
182         if ((c != null) && (! c.getComponentOrientation().isLeftToRight())) {
183         setJustification(StyleConstants.ALIGN_RIGHT);
184         } else {
185         setJustification(StyleConstants.ALIGN_LEFT);
186         }
187     }
188
189     /**
190      * Fetch the constraining span to flow against for
191      * the given child index.
192      */

193         public int getFlowSpan(int index) {
194         Component c = getContainer();
195         if (c instanceof JTextArea) {
196         JTextArea area = (JTextArea) c;
197         if (! area.getLineWrap()) {
198             // no limit if unwrapped
199
return Integer.MAX_VALUE;
200         }
201         }
202         return super.getFlowSpan(index);
203     }
204
205         protected SizeRequirements calculateMinorAxisRequirements(int axis,
206                                   SizeRequirements r) {
207         SizeRequirements req = super.calculateMinorAxisRequirements(axis, r);
208         Component c = getContainer();
209         if (c instanceof JTextArea) {
210         JTextArea area = (JTextArea) c;
211         if (! area.getLineWrap()) {
212             // min is pref if unwrapped
213
req.minimum = req.preferred;
214         } else {
215                     req.minimum = 0;
216                     req.preferred = getWidth();
217                     if (req.preferred == Integer.MAX_VALUE) {
218                         // We have been initially set to MAX_VALUE, but we
219
// don't want this as our preferred.
220
req.preferred = 100;
221                     }
222                 }
223         }
224         return req;
225     }
226
227         /**
228          * Sets the size of the view. If the size has changed, layout
229          * is redone. The size is the full size of the view including
230          * the inset areas.
231          *
232          * @param width the width >= 0
233          * @param height the height >= 0
234          */

235         public void setSize(float width, float height) {
236             if ((int) width != getWidth()) {
237                 preferenceChanged(null, true, true);
238             }
239             super.setSize(width, height);
240         }
241
242     /**
243      * This class can be used to represent a logical view for
244      * a flow. It keeps the children updated to reflect the state
245      * of the model, gives the logical child views access to the
246      * view hierarchy, and calculates a preferred span. It doesn't
247      * do any rendering, layout, or model/view translation.
248      */

249     static class LogicalView extends CompositeView {
250         
251         LogicalView(Element elem) {
252         super(elem);
253         }
254
255             protected int getViewIndexAtPosition(int pos) {
256         Element elem = getElement();
257         if (elem.getElementCount() > 0) {
258             return elem.getElementIndex(pos);
259         }
260         return 0;
261         }
262
263             protected boolean updateChildren(DocumentEvent.ElementChange JavaDoc ec,
264                          DocumentEvent JavaDoc e, ViewFactory f) {
265         return false;
266         }
267
268             protected void loadChildren(ViewFactory f) {
269         Element elem = getElement();
270         if (elem.getElementCount() > 0) {
271             super.loadChildren(f);
272         } else {
273             View v = new GlyphView(elem);
274             append(v);
275         }
276         }
277
278             public float getPreferredSpan(int axis) {
279                 if( getViewCount() != 1 )
280                     throw new Error JavaDoc("One child view is assumed.");
281                 
282         View v = getView(0);
283         return v.getPreferredSpan(axis);
284         }
285
286             /**
287              * Forward the DocumentEvent to the given child view. This
288              * is implemented to reparent the child to the logical view
289              * (the children may have been parented by a row in the flow
290              * if they fit without breaking) and then execute the superclass
291              * behavior.
292              *
293              * @param v the child view to forward the event to.
294              * @param e the change information from the associated document
295              * @param a the current allocation of the view
296              * @param f the factory to use to rebuild if the view has children
297              * @see #forwardUpdate
298              * @since 1.3
299              */

300             protected void forwardUpdateToView(View v, DocumentEvent JavaDoc e,
301                                                Shape a, ViewFactory f) {
302                 v.setParent(this);
303                 super.forwardUpdateToView(v, e, a, f);
304             }
305
306         // The following methods don't do anything useful, they
307
// simply keep the class from being abstract.
308

309             public void paint(Graphics g, Shape allocation) {
310         }
311
312             protected boolean isBefore(int x, int y, Rectangle alloc) {
313         return false;
314         }
315
316             protected boolean isAfter(int x, int y, Rectangle alloc) {
317         return false;
318         }
319
320             protected View getViewAtPoint(int x, int y, Rectangle alloc) {
321         return null;
322         }
323
324             protected void childAllocation(int index, Rectangle a) {
325         }
326         }
327     }
328
329 }
330
Popular Tags