1 19 20 package org.netbeans.editor; 21 22 import java.awt.Insets ; 23 import java.awt.Color ; 24 import java.awt.Graphics ; 25 import java.awt.Shape ; 26 import java.awt.Rectangle ; 27 import javax.swing.text.View ; 28 import javax.swing.text.Element ; 29 import javax.swing.text.JTextComponent ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.event.DocumentEvent ; 32 33 40 41 public abstract class BaseView extends View { 42 43 44 protected static final int INSETS_TOP = 1; 45 46 47 protected static final int MAIN_AREA = 2; 48 49 50 protected static final int INSETS_BOTTOM = 4; 51 52 53 protected boolean packed; 54 55 61 protected int helperInd; 62 63 64 private JTextComponent component; 65 66 67 protected Insets insets; 68 69 70 private int startY = -1; 71 72 73 public BaseView(Element elem) { 74 super(elem); 75 } 76 77 78 public boolean isPacked() { 79 return packed; 80 } 81 82 83 public void setPacked(boolean packed) { 84 this.packed = packed; 85 } 86 87 88 public float getAlignment(int axis) { 89 return 0f; 90 } 91 92 public abstract void modelToViewDG(int pos, DrawGraphics dg) 93 throws BadLocationException ; 94 95 96 protected abstract int getYFromPos(int pos) throws BadLocationException ; 97 98 99 protected abstract int getPosFromY(int y); 100 101 protected abstract int getBaseX(int y); 102 103 110 protected abstract int getPaintAreas(Graphics g, int clipY, int clipHeight); 111 112 118 protected abstract void paintAreas(Graphics g, int clipY, int clipHeight, int paintAreas); 119 120 128 public void paint(Graphics g, Shape allocation) { 129 Rectangle 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 140 public JTextComponent getComponent() { 141 if (component == null) { 142 component = (JTextComponent )getContainer(); 143 } 144 return component; 145 } 146 147 148 public Insets getInsets() { 149 return insets; 150 } 151 152 156 protected void setHelperInd(int ind) { 157 helperInd = ind; 158 } 159 160 171 protected abstract int getViewStartY(BaseView view, int helperInd); 172 173 177 protected void invalidateStartY() { 178 startY = -1; 179 } 180 181 182 protected int getStartY() { 183 if (startY == -1) { BaseView v = (BaseView)getParent(); 185 if (v != null) { 186 startY = v.getViewStartY(this, helperInd); 187 } 188 } 189 return startY; 190 } 191 192 193 public abstract int getHeight(); 194 195 198 public abstract void updateMainHeight(); 199 200 201 public float getPreferredSpan(int axis) { 202 switch (axis) { 203 case Y_AXIS: 204 return getHeight(); 205 } 206 return 0f; 207 } 208 209 210 protected EditorUI getEditorUI() { 211 return ((BaseTextUI)getComponent().getUI()).getEditorUI(); 212 } 213 214 215 public void displayHierarchy() { 216 BaseView v = this; 218 while (v.getParent() != null) { 219 v = (BaseView)v.getParent(); 220 } 221 v.displayHierarchyHelper(this, 0, 0); 222 } 223 224 230 private void displayHierarchyHelper(View origView, int col, int index) { 231 StringBuffer buf = new StringBuffer (); 232 buf.append(((this == origView) ? "*" : " ")); for (int i = 0; i < col; i++) { 234 buf.append(' '); 235 } 236 buf.append('['); 237 buf.append(Integer.toString(index)); 238 buf.append("] "); 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 toString() { 251 return "BaseView=" + System.identityHashCode(this) + ", elem=" + getElement() + ", parent=" + System.identityHashCode(getParent()); 254 } 255 256 } 257 | Popular Tags |