Where in the JTextArea code is line/word wrapping handled?

Hi all--

I was making a custom renderer using a JTextArea, and was looking in the source code for JTextArea trying to figure out how it determines when to break a line given it's width and the current font...but I can't find it anywhere. I can find this function:

publicvoid setLineWrap(boolean wrap){

boolean old = this.wrap;

this.wrap = wrap;

firePropertyChange("lineWrap", old, wrap);

}

but it appears to simply set a private boolean called "wrap" that isn't in its superclass JTextComponent. So one can only imagine that it must somehow calculate where to wrap its text. But this code is no where to be found in JTextArea. In fact, the only thing I can find is that it fires this "lineWrap" property change, but I cannot find in either JTextArea or JTextComponent where this event is handled to recalculate where to draw the string and when to wrap it to a new line.

Mostly I'm just curious how it's done more than anything else. Anyone know where the missing code is to handle this?

[1259 byte] By [mr.v.a] at [2007-10-3 10:03:30]
# 1
Probably in PlainWrappedView.
camickra at 2007-7-15 5:22:29 > top of Java-index,Desktop,Core GUI APIs...
# 2
don't forget that swing uses the MVC pattern. JTextArea is just the controller component (or close enough). The view code would be in javax.swing.plaf.* package hierarchy. Start your way with javax.swing.plaf.basic.BasicTextAreaUI
dberanskya at 2007-7-15 5:22:29 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks for the clues...I'm going to go a searching...
mr.v.a at 2007-7-15 5:22:29 > top of Java-index,Desktop,Core GUI APIs...
# 4

>The view code would be in javax.swing.plaf.* package hierarchy.

dberansky-- Too deeply have I delved there, and awoke the nameless fear...

The BasicTextAreaUI was in javax.swing.plaf.basic.* as you said, and as camikr (somewhat dyslexically) pointed out in the method:

public View create(Element elem)

if wrap is set, it creates a WrappedPlainView that has the method

protected int calculateBreakPosition(int p0, int p1) {

which uses the Utilities class to calculate the break position given the metrics..

Thanks to you both. It will be many a year before I touch a line of that code =)

mr.v.a at 2007-7-15 5:22:29 > top of Java-index,Desktop,Core GUI APIs...
# 5

Okay I'm sorry, I got confused again.

So I was reading this article in the Swing tutorial.

http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#editorkits

and it says "Under the hood, text components use an EditorKit to tie the various pieces of the text component together. It provides the view factory, document, caret, and actions."

It also goes on to say that a JTextArea hides its editor kit. But if the EditorKit provides the view factory, actions, caret, etc, what does the PLAF/TextUI provide? Because I just got through finding out that the PLAF handles line wrapping. But isn't that more of an issue for the EditorKit to provide a proper view factory that knows how to display the data stored in the Document model

I guess my question is, for your average text component, is it the Editor Kit or instead is it the PLAF/UI component that's handling constructing the view and painting it?

mr.v.a at 2007-7-15 5:22:29 > top of Java-index,Desktop,Core GUI APIs...
# 6

Yeah, this stuff gets pretty twisted. The main entry point is the getViewFactory() method in the RootView member class of BasicTextUI. (RootView is just what it sounds like: the root of whatever View hierarchy finally gets built.) It first tries to get a ViewFactory from the EditorKit, and if that doesn't work, it returns the enclosing BasicTextUI object. If the component being constructed is a JTextPane or JEditorPane, the EditorKit will be a StyledEditorKit or a subclass thereof, which will provide a ViewFactory that can vary with the kind of document being displayed/edited. But in the case of JTextArea, it will be a DefaultEditorKit, which returns null from its getViewFactory() method, meaning the BasicTextUI object is expected to serve as the ViewFactory. BasicTextUI provides only a dummy implementation of the ViewFactory interface, but its subclass, BasicTextAreaUI, implements create(Element) to return either a PlainView or a WrappedPlainView, depending on the relevant setting the the JTextArea.

uncle_alicea at 2007-7-15 5:22:29 > top of Java-index,Desktop,Core GUI APIs...