KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > BaseView


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;
21
22 import java.awt.Insets JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Shape JavaDoc;
26 import java.awt.Rectangle JavaDoc;
27 import javax.swing.text.View JavaDoc;
28 import javax.swing.text.Element JavaDoc;
29 import javax.swing.text.JTextComponent JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.event.DocumentEvent JavaDoc;
32
33 /**
34 * Base abstract view serves as parent for both
35 * leaf and branch views.
36 *
37 * @author Miloslav Metelka
38 * @version 1.00
39 */

40
41 public abstract class BaseView extends View JavaDoc {
42
43     /** Top insets paint type. */
44     protected static final int INSETS_TOP = 1;
45
46     /** Main area paint type. */
47     protected static final int MAIN_AREA = 2;
48
49     /** Bottom insets paint type. */
50     protected static final int INSETS_BOTTOM = 4;
51
52     /** Is this view packed */
53     protected boolean packed;
54
55     /** Index of this view as child view in its parent.
56     * This gives the parent view a hint at which index it should
57     * search for this view. If this index is incorrect, the parent
58     * view searches the whole array of children. However this gives
59     * good optimization of child views search process.
60     */

61     protected int helperInd;
62
63     /** JTextComponent hosting this view */
64     private JTextComponent JavaDoc component;
65
66     /** Border insets of this view. Can be null */
67     protected Insets JavaDoc insets;
68
69     /** Start y coord. for this view */
70     private int startY = -1;
71
72     /** Construct new base view */
73     public BaseView(Element JavaDoc elem) {
74         super(elem);
75     }
76
77     /** Getter for packed flag */
78     public boolean isPacked() {
79         return packed;
80     }
81
82     /** Setter for packed flag */
83     public void setPacked(boolean packed) {
84         this.packed = packed;
85     }
86
87     /** Get aligment along an X_AXIS or Y_AXIS */
88     public float getAlignment(int axis) {
89         return 0f;
90     }
91
92     public abstract void modelToViewDG(int pos, DrawGraphics dg)
93     throws BadLocationException JavaDoc;
94
95     /** Get y-coord value from position */
96     protected abstract int getYFromPos(int pos) throws BadLocationException JavaDoc;
97
98     /** Get position when knowing y-coord */
99     protected abstract int getPosFromY(int y);
100
101     protected abstract int getBaseX(int y);
102
103     /** Returns binary composition of regions that should be painted.
104     * It can be binary composition of INSETS_TOP, MAIN_AREA and INSETS_BOTTOM.
105     *
106     * @param g Graphics to paint through
107     * @param clip clipping area of graphics object
108     *
109     */

110     protected abstract int getPaintAreas(Graphics JavaDoc g, int clipY, int clipHeight);
111
112     /** Paint either top insets, main area, or bottom insets.
113     *
114     * @param g Graphics to paint through
115     * @param clip clipping area of graphics object
116     * @param paintAreas binary composition of paint areas
117     */

118     protected abstract void paintAreas(Graphics JavaDoc g, int clipY, int clipHeight, int paintAreas);
119
120     /** It divides painting into three areas:
121     * INSETS_TOP, MAIN_AREA, INSETS_BOTTOM. It paints them sequentially
122     * using <CODE>paintArea()</CODE> method until it returns false.
123     * This implementation also supposes that <CODE>allocation</CODE> is
124     * instance of <CODE>Rectangle</CODE> to save object creations. The root
125     * view in TextUI implementation will take care to ensure child views will
126     * get rectangle instances.
127     */

128     public void paint(Graphics JavaDoc g, Shape JavaDoc allocation) {
129         Rectangle JavaDoc clip = g.getClipBounds();
130         if (clip.height < 0 || clip.width < 0) {
131             return;
132         }
133         int paintAreas = getPaintAreas(g, clip.y, clip.height);
134         if (paintAreas != 0) {
135             paintAreas(g, clip.y, clip.height, paintAreas);
136         }
137     }
138
139     /** Get component hosting this view. */
140     public JTextComponent JavaDoc getComponent() {
141         if (component == null) {
142             component = (JTextComponent JavaDoc)getContainer();
143         }
144         return component;
145     }
146
147     /** Get insets of this view. */
148     public Insets JavaDoc getInsets() {
149         return insets;
150     }
151
152     /** This function is used to correct information
153     * about which index this child view occupies in parent view's
154     * array of children. It is called by parent view.
155     */

156     protected void setHelperInd(int ind) {
157         helperInd = ind;
158     }
159
160     /** Get child view's y base value. This function is called by children
161     * of this view to get its y offset.
162     * @param view is child view of this view for which
163     * the offset should be computed.
164     * @param helperInd is index that child view has cached to ease
165     * the parent view to search for it in its children array.
166     * If this index is correct, parent uses it. If it's incorrect
167     * parent view searches through the whole array of its children
168     * to find the child. It then calls children's
169     * <CODE>setParentInd()</CODE> to correct its location index.
170     */

171     protected abstract int getViewStartY(BaseView view, int helperInd);
172
173     /** Informs the view that if it had cached start y of itself
174     * it should invalidate it as it is no longer valid and it should call
175     * <CODE>getViewStartY()</CODE> to get updated value.
176     */

177     protected void invalidateStartY() {
178         startY = -1;
179     }
180
181     /** Get y base value for this view. */
182     protected int getStartY() {
183         if (startY == -1) { // the value is invalid
184
BaseView v = (BaseView)getParent();
185             if (v != null) {
186                 startY = v.getViewStartY(this, helperInd);
187             }
188         }
189         return startY;
190     }
191
192     /** Get height of the view */
193     public abstract int getHeight();
194
195     /** Update height of main area as result of some important change.
196     * Propagate to child views if necessary.
197     */

198     public abstract void updateMainHeight();
199
200     /** Get preferred span over axis */
201     public float getPreferredSpan(int axis) {
202         switch (axis) {
203         case Y_AXIS:
204             return getHeight();
205         }
206         return 0f;
207     }
208
209     /** Get editor UI */
210     protected EditorUI getEditorUI() {
211         return ((BaseTextUI)getComponent().getUI()).getEditorUI();
212     }
213
214     /** Display view hierarchy on console. Current view is marked with asterisk. */
215     public void displayHierarchy() {
216         // find the root view
217
BaseView v = this;
218         while (v.getParent() != null) {
219             v = (BaseView)v.getParent();
220         }
221         v.displayHierarchyHelper(this, 0, 0);
222     }
223
224     /** Helper for displaying hierarchy - called recursively.
225     * @param origView view for which the displayHierarchy method was originally
226     * called. It is marked by asterisk.
227     * @param col column offset
228     * @param index index of this view in parent children[] array
229     */

230     private void displayHierarchyHelper(View JavaDoc origView, int col, int index) {
231         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
232         buf.append(((this == origView) ? "*" : " ")); // NOI18N
233
for (int i = 0; i < col; i++) {
234             buf.append(' ');
235         }
236         buf.append('[');
237         buf.append(Integer.toString(index));
238         buf.append("] "); // NOI18N
239
buf.append(this.toString());
240         System.out.println(buf);
241         int childrenCnt = getViewCount();
242         if (childrenCnt > 0) {
243             for (int i = 0; i < childrenCnt; i++) {
244                 ((BaseView)getView(i)).displayHierarchyHelper(origView, col + 1, i);
245             }
246         }
247
248     }
249
250     public String JavaDoc toString() {
251         return "BaseView=" + System.identityHashCode(this) // NOI18N
252
+ ", elem=" + getElement() + ", parent=" // NOI18N
253
+ System.identityHashCode(getParent());
254     }
255
256 }
257
Popular Tags