KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > view > spi > ViewRenderingContext


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.view.spi;
21
22 import java.awt.Container JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.Shape JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.Caret JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import javax.swing.text.Position JavaDoc;
30 import javax.swing.text.Segment JavaDoc;
31 import javax.swing.text.TabExpander JavaDoc;
32 import javax.swing.text.View JavaDoc;
33
34 /**
35  * Context used for rendering of a view.
36  * It provides methods useful for measuring
37  * of the view contents.
38  * <br>
39  * It also provides information about additional highlights
40  * that the view should respect when painting
41  * a particular text.
42  *
43  * <p>
44  * A particular view can obtain an instance
45  * of <code>ViewRenderingContext</code> by using methods in
46  * {@link RenderingContextView}.
47  *
48  * <p>
49  * Only one thread at the time can safely use
50  * the rendering context. It does not have
51  * to be event dispatch thread.
52  *
53  * @author Miloslav Metelka
54  * @version 1.00
55  */

56
57 public abstract class ViewRenderingContext {
58     
59     /**
60      * Find instance of the <code>ViewRenderingContext</code>
61      * for the given view.
62      */

63     public static synchronized ViewRenderingContext obtainContext(View JavaDoc v) {
64        // return org.netbeans.lib.editor.view.PaintContextResolver.get(v);
65
return null;
66     }
67
68     public abstract float getSpan(View JavaDoc v, int p0, int p1, TabExpander JavaDoc e, float x);
69
70     public abstract float getHeight(View JavaDoc v);
71
72     public abstract float getAscent(View JavaDoc v);
73
74     public abstract float getDescent(View JavaDoc v);
75
76     /**
77      * Paint the glyphs representing the given range.
78      */

79         public abstract void paint(View JavaDoc v, Graphics JavaDoc g, Shape JavaDoc a, int p0, int p1);
80
81     /**
82      * Provides a mapping from the document model coordinate space
83      * to the coordinate space of the view mapped to it.
84      * This is shared by the broken views.
85      *
86      * @param pos the position to convert
87      * @param a the allocated region to render into
88      * @param rightToLeft true if the text is rendered right to left.
89      * @return the bounding box of the given position
90      * @exception BadLocationException if the given position does not represent a
91      * valid location in the associated document
92      * @see View#modelToView
93      */

94     public abstract Shape JavaDoc modelToView(View JavaDoc v,
95                       int pos, Position.Bias JavaDoc bias,
96                       Shape JavaDoc a) throws BadLocationException JavaDoc;
97
98     /**
99      * Provides a mapping from the view coordinate space to the logical
100      * coordinate space of the model.
101      *
102      * @param x the X coordinate
103      * @param y the Y coordinate
104      * @param a the allocated region to render into
105      * @param rightToLeft true if the text is rendered right to left
106      * @return the location within the model that best represents the
107      * given point of view
108      * @see View#viewToModel
109      */

110         public abstract int viewToModel(View JavaDoc v,
111                     float x, float y, Shape JavaDoc a,
112                     Position.Bias JavaDoc[] biasReturn);
113
114     /**
115      * Determines the model location that represents the
116      * maximum advance that fits within the given span.
117      * This could be used to break the given view. The result
118      * should be a location just shy of the given advance. This
119      * differs from viewToModel which returns the closest
120      * position which might be proud of the maximum advance.
121      *
122      * @param v the view to find the model location to break at.
123      * @param p0 the location in the model where the
124      * fragment should start it's representation >= 0.
125      * @param pos the graphic location along the axis that the
126      * broken view would occupy >= 0. This may be useful for
127      * things like tab calculations.
128      * @param len specifies the distance into the view
129      * where a potential break is desired >= 0.
130      * @return the maximum model location possible for a break.
131      * @see View#breakView
132      */

133         public abstract int getBoundedPosition(View JavaDoc v, int p0, float x, float len);
134
135     /**
136      * Provides a way to determine the next visually represented model
137      * location that one might place a caret. Some views may not be
138      * visible, they might not be in the same order found in the model, or
139      * they just might not allow access to some of the locations in the
140      * model.
141      *
142      * @param v the view to use
143      * @param pos the position to convert >= 0
144      * @param a the allocated region to render into
145      * @param direction the direction from the current position that can
146      * be thought of as the arrow keys typically found on a keyboard.
147      * This may be SwingConstants.WEST, SwingConstants.EAST,
148      * SwingConstants.NORTH, or SwingConstants.SOUTH.
149      * @return the location within the model that best represents the next
150      * location visual position.
151      * @exception BadLocationException
152      * @exception IllegalArgumentException for an invalid direction
153      */

154         public int getNextVisualPositionFrom(View JavaDoc v, int pos, Position.Bias JavaDoc b, Shape JavaDoc a,
155                          int direction,
156                          Position.Bias JavaDoc[] biasRet)
157         throws BadLocationException JavaDoc {
158
159         int startOffset = v.getStartOffset();
160         int endOffset = v.getEndOffset();
161         Segment JavaDoc text;
162         
163         switch (direction) {
164         case View.NORTH:
165         case View.SOUTH:
166                 if (pos != -1) {
167                     // Presumably pos is between startOffset and endOffset,
168
// since View is only one line, we won't contain
169
// the position to the nort/south, therefore return -1.
170
return -1;
171                 }
172                 Container JavaDoc container = v.getContainer();
173
174                 if (container instanceof JTextComponent JavaDoc) {
175                     Caret JavaDoc c = ((JTextComponent JavaDoc)container).getCaret();
176                     Point JavaDoc magicPoint;
177                     magicPoint = (c != null) ? c.getMagicCaretPosition() :null;
178
179                     if (magicPoint == null) {
180                         biasRet[0] = Position.Bias.Forward;
181                         return startOffset;
182                     }
183                     int value = v.viewToModel(magicPoint.x, 0f, a, biasRet);
184                     return value;
185                 }
186                 break;
187         case View.EAST:
188         if(startOffset == v.getDocument().getLength()) {
189             if(pos == -1) {
190             biasRet[0] = Position.Bias.Forward;
191             return startOffset;
192             }
193             // End case for bidi text where newline is at beginning
194
// of line.
195
return -1;
196         }
197         if(pos == -1) {
198             biasRet[0] = Position.Bias.Forward;
199             return startOffset;
200         }
201         if(pos == endOffset) {
202             return -1;
203         }
204         if(++pos == endOffset) {
205                     // Assumed not used in bidi text, GlyphPainter2 will
206
// override as necessary, therefore return -1.
207
return -1;
208         }
209         else {
210             biasRet[0] = Position.Bias.Forward;
211         }
212         return pos;
213         case View.WEST:
214         if(startOffset == v.getDocument().getLength()) {
215             if(pos == -1) {
216             biasRet[0] = Position.Bias.Forward;
217             return startOffset;
218             }
219             // End case for bidi text where newline is at beginning
220
// of line.
221
return -1;
222         }
223         if(pos == -1) {
224                     // Assumed not used in bidi text, GlyphPainter2 will
225
// override as necessary, therefore return -1.
226
biasRet[0] = Position.Bias.Forward;
227             return endOffset - 1;
228         }
229         if(pos == startOffset) {
230             return -1;
231         }
232         biasRet[0] = Position.Bias.Forward;
233         return (pos - 1);
234         default:
235         throw new IllegalArgumentException JavaDoc("Bad direction: " + direction); // NOI18N
236
}
237         return pos;
238
239     }
240
241 }
242
Popular Tags