Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 23 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
23
Dung lượng
270,34 KB
Nội dung
22.5 Views In our discussion of how Swing represents styled text, we haven't mentioned how it is actually drawn on the screen That's where the View classes come in They form most of the V part of the MVC architecture for text components and are responsible for rendering the text The way this works is a tree of View objects is created from the Document's Element tree (Examples of this are shown in Figure 22-11.) For DefaultStyledDocuments in a JTextPane, the View tree is modeled closely on the structure of the Element tree, with almost a one-to-one mapping from Elements to Views A PlainDocument in a JTextArea is handled more simply, with a single View object that paints the entire Element tree Figure 22-11 View trees created from Element trees Notice that the View trees have a root View above what could be considered the "natural" root This was done to ease the implementation of the other View classes, which can now all assume that they have a non-null parent in the View tree So that each child doesn't have to register as a DocumentListener, the root View also takes care of dispatching DocumentEvents to its children The actual type of the root View is a package-private inner class (You are free to create a View tree with any root you like, so this does not always need to be the case The implementations of the TextUI.getRootView( ) method that Swing provides do return a package-private View class.) Creation of the View tree is often handled by the text component's TextUI, but if its EditorKit returns a non-null value from its getViewFactory( ) method, this task is delegated to the factory This gives the more complex text components (such as JEditorPane, discussed in Chapter 23) a powerful tool for customizing their appearance 22.5.1 The ViewFactory Interface Implementations of the ViewFactory interface determine the kind of View object that needs to be created for each Element passed in to the factory We'll see more of the ViewFactory interface in the next chapter 22.5.1.1 Method public View create(Element elem) Return a View for the given Element 22.5.2 The View Class This abstract class is the base class for a score of View types (see Figure 22-12) and provides basic functionality for drawing part of a Document's content Before we cover the properties and methods of View, we should note a few potentially confusing details 22.5.2.1 Float coordinates The View classes follow the lead of the 2D API and use float values to specify x and y coordinates It is usually safe to convert these to ints if you prefer 22.5.2.2 Shape versus Rectangle Many View methods take arguments of type java.awt.Shape The Shape interface is implemented by many classes that represent geometric shapes, including java.awt.Rectangle Theoretically, a View class can take advantage of nonrectangular rendering areas, but the existing View classes treat Shape objects as Rectangles You can convert a Shape into a Rectangle (int coordinates) by calling someShape.getBounds( ), or you can convert a Shape into a Rectangle2D (float coordinates) by calling someShape.getBounds2D( ) 22.5.2.3 Span and allocation The term span refers to a distance (typically a float) in a particular direction while allocation refers to the area (a Shape) in which a View is drawn These terms are used frequently in the View source code and documentation, so we'll use them too 22.5.2.4 Axis An axis value should be either X_AXIS or Y_AXIS (int constants) View has several indexed properties that expect the index to be one of these two values (The exception is the view property getView(n) requests the nth child of the View.) 22.5.2.5 Bias Many View methods have a parameter of type Position.Bias (see the previous discussion) There are only two values for an object of this type: Bias.Forward and Bias.Backward The idea is that the addition of a Bias refines a Document offset somewhat Without a Bias, if a user clicks to an offset of 5, then you know the click was either on the right half of the 4th character or on the left half of the 5th The Bias parameter distinguishes between the two cases That said, Swing's handling of Position.Bias is spotty Many method implementations simply ignore the Bias parameter, and many callers blindly pass in Bias.Forward Should you decide to do the same, you would be in good company 22.5.2.6 Properties The View class defines the properties shown in Table 22-34 Table 22-34 View properties Property alignmenti Data type get is set float · Default value 0.5 attributes AttributeSet · From element container Container · From parent document Document · From element element Element · From constructor endOffset int · From element graphics1.3 Graphics · maximumSpani float · (resizeWeight