KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsTextUI


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

7
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Graphics JavaDoc;
12 import java.awt.Rectangle JavaDoc;
13 import java.awt.Shape JavaDoc;
14 import javax.swing.plaf.basic.*;
15 import javax.swing.*;
16 import javax.swing.plaf.TextUI JavaDoc;
17 import javax.swing.plaf.UIResource JavaDoc;
18 import javax.swing.text.*;
19
20 /**
21  * Windows text rendering.
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 appropriate
26  * for short term storage or RMI between applications running the same
27  * version of Swing. A future release of Swing will provide support for
28  * long term persistence.
29  */

30 public abstract class WindowsTextUI extends BasicTextUI {
31     /**
32      * Creates the object to use for a caret. By default an
33      * instance of WindowsCaret is created. This method
34      * can be redefined to provide something else that implements
35      * the InputPosition interface or a subclass of DefaultCaret.
36      *
37      * @return the caret object
38      */

39     protected Caret createCaret() {
40         return new WindowsCaret();
41     }
42
43     /* public */
44     static LayeredHighlighter.LayerPainter WindowsPainter = new WindowsHighlightPainter(null);
45
46     /* public */
47     static class WindowsCaret extends DefaultCaret
48              implements UIResource JavaDoc {
49     /**
50      * Gets the painter for the Highlighter.
51      *
52      * @return the painter
53      */

54     protected Highlighter.HighlightPainter getSelectionPainter() {
55         return WindowsTextUI.WindowsPainter;
56     }
57     }
58
59     /* public */
60     static class WindowsHighlightPainter extends
61              DefaultHighlighter.DefaultHighlightPainter {
62     WindowsHighlightPainter(Color JavaDoc c) {
63         super(c);
64     }
65
66     // --- HighlightPainter methods ---------------------------------------
67

68         /**
69          * Paints a highlight.
70          *
71          * @param g the graphics context
72          * @param offs0 the starting model offset >= 0
73          * @param offs1 the ending model offset >= offs1
74          * @param bounds the bounding box for the highlight
75          * @param c the editor
76          */

77         public void paint(Graphics JavaDoc g, int offs0, int offs1, Shape JavaDoc bounds, JTextComponent c) {
78         Rectangle JavaDoc alloc = bounds.getBounds();
79         try {
80         // --- determine locations ---
81
TextUI JavaDoc mapper = c.getUI();
82         Rectangle JavaDoc p0 = mapper.modelToView(c, offs0);
83         Rectangle JavaDoc p1 = mapper.modelToView(c, offs1);
84
85         // --- render ---
86
Color JavaDoc color = getColor();
87
88         if (color == null) {
89             g.setColor(c.getSelectionColor());
90         }
91         else {
92             g.setColor(color);
93         }
94         boolean firstIsDot = false;
95         boolean secondIsDot = false;
96         if (c.isEditable()) {
97             int dot = c.getCaretPosition();
98             firstIsDot = (offs0 == dot);
99             secondIsDot = (offs1 == dot);
100         }
101         if (p0.y == p1.y) {
102             // same line, render a rectangle
103
Rectangle JavaDoc r = p0.union(p1);
104             if (r.width > 0) {
105             if (firstIsDot) {
106                 r.x++;
107                 r.width--;
108             }
109             else if (secondIsDot) {
110                 r.width--;
111             }
112             }
113             g.fillRect(r.x, r.y, r.width, r.height);
114         } else {
115             // different lines
116
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
117             if (firstIsDot && p0ToMarginWidth > 0) {
118             p0.x++;
119             p0ToMarginWidth--;
120             }
121             g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
122             if ((p0.y + p0.height) != p1.y) {
123             g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
124                    p1.y - (p0.y + p0.height));
125             }
126             if (secondIsDot && p1.x > alloc.x) {
127             p1.x--;
128             }
129             g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
130         }
131         } catch (BadLocationException e) {
132         // can't render
133
}
134     }
135
136     // --- LayerPainter methods ----------------------------
137
/**
138          * Paints a portion of a highlight.
139          *
140          * @param g the graphics context
141          * @param offs0 the starting model offset >= 0
142          * @param offs1 the ending model offset >= offs1
143          * @param bounds the bounding box of the view, which is not
144      * necessarily the region to paint.
145          * @param c the editor
146      * @param view View painting for
147      * @return region drawing occured in
148          */

149     public Shape JavaDoc paintLayer(Graphics JavaDoc g, int offs0, int offs1,
150                 Shape JavaDoc bounds, JTextComponent c, View view) {
151         Color JavaDoc color = getColor();
152
153         if (color == null) {
154         g.setColor(c.getSelectionColor());
155         }
156         else {
157         g.setColor(color);
158         }
159         boolean firstIsDot = false;
160         boolean secondIsDot = false;
161         if (c.isEditable()) {
162         int dot = c.getCaretPosition();
163         firstIsDot = (offs0 == dot);
164         secondIsDot = (offs1 == dot);
165         }
166         if (offs0 == view.getStartOffset() &&
167         offs1 == view.getEndOffset()) {
168         // Contained in view, can just use bounds.
169
Rectangle JavaDoc alloc;
170         if (bounds instanceof Rectangle JavaDoc) {
171             alloc = (Rectangle JavaDoc)bounds;
172         }
173         else {
174             alloc = bounds.getBounds();
175         }
176         if (firstIsDot && alloc.width > 0) {
177             g.fillRect(alloc.x + 1, alloc.y, alloc.width - 1,
178                    alloc.height);
179         }
180         else if (secondIsDot && alloc.width > 0) {
181             g.fillRect(alloc.x, alloc.y, alloc.width - 1,
182                    alloc.height);
183         }
184         else {
185             g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
186         }
187         return alloc;
188         }
189         else {
190         // Should only render part of View.
191
try {
192             // --- determine locations ---
193
Shape JavaDoc shape = view.modelToView(offs0, Position.Bias.Forward,
194                                                    offs1,Position.Bias.Backward,
195                                                    bounds);
196                     Rectangle JavaDoc r = (shape instanceof Rectangle JavaDoc) ?
197                                   (Rectangle JavaDoc)shape : shape.getBounds();
198             if (firstIsDot && r.width > 0) {
199             g.fillRect(r.x + 1, r.y, r.width - 1, r.height);
200             }
201             else if (secondIsDot && r.width > 0) {
202             g.fillRect(r.x, r.y, r.width - 1, r.height);
203             }
204             else {
205             g.fillRect(r.x, r.y, r.width, r.height);
206             }
207                     return r;
208         } catch (BadLocationException e) {
209             // can't render
210
}
211         }
212         // Only if exception
213
return null;
214     }
215
216     }
217
218 }
219
220
Popular Tags