1 19 20 package org.netbeans.lib.editor.view; 21 22 import javax.swing.text.AbstractDocument ; 23 import javax.swing.text.View ; 24 import org.netbeans.editor.view.spi.FlyView; 25 import org.netbeans.editor.view.spi.LockView; 26 import org.netbeans.editor.view.spi.ViewLayoutState; 27 import org.netbeans.editor.view.spi.ViewUtilities; 28 29 40 41 public class SimpleViewLayoutState implements ViewLayoutState { 42 43 47 private static final int X_MAJOR_AXIS_BIT = 1; 48 49 53 private static final int MAJOR_AXIS_PREFERENCE_CHANGED_BIT = 2; 54 55 59 private static final int MINOR_AXIS_PREFERENCE_CHANGED_BIT = 4; 60 61 66 private static final int VIEW_SIZE_INVALID_BIT = 8; 67 68 protected static final int LAST_USED_BIT = VIEW_SIZE_INVALID_BIT; 69 70 74 private static final int ANY_INVALID 75 = MAJOR_AXIS_PREFERENCE_CHANGED_BIT 76 | MINOR_AXIS_PREFERENCE_CHANGED_BIT 77 | VIEW_SIZE_INVALID_BIT; 78 79 80 private int statusBits; 82 private View view; 84 private double layoutMajorAxisRawOffset; 86 private int viewRawIndex; 88 private float layoutMajorAxisPreferredSpan; 91 private float layoutMinorAxisPreferredSpan; 94 private float layoutMinorAxisAlignment; 96 97 public SimpleViewLayoutState(View v) { 98 100 view = v; 101 } 102 103 public final View getView() { 104 return view; 105 } 106 107 public boolean isFlyweight() { 108 return false; 109 } 110 111 public ViewLayoutState selectLayoutMajorAxis(int axis) { 112 114 if (axis == View.X_AXIS) { 115 setStatusBits(X_MAJOR_AXIS_BIT); 116 } else { clearStatusBits(X_MAJOR_AXIS_BIT); 118 } 119 120 return this; 121 } 122 123 protected final boolean isXMajorAxis() { 124 return isStatusBitsNonZero(X_MAJOR_AXIS_BIT); 125 } 126 127 protected final int getMajorAxis() { 128 return isXMajorAxis() ? View.X_AXIS : View.Y_AXIS; 129 } 130 131 protected final int getMinorAxis() { 132 return isXMajorAxis() ? View.Y_AXIS : View.X_AXIS; 133 } 134 135 protected final ViewLayoutState.Parent getLayoutStateParent() { 136 View parent = getView().getParent(); 137 return (parent instanceof ViewLayoutState.Parent) 138 ? ((ViewLayoutState.Parent)parent) 139 : null; 140 } 141 142 public void updateLayout() { 143 View parent = view.getParent(); 144 if (parent == null) { return; 146 } 147 148 while (!isLayoutValid()) { doUpdateLayout(parent); 150 } 151 } 152 153 protected void doUpdateLayout(View parent) { 154 ViewLayoutState.Parent lsParent = (parent instanceof ViewLayoutState.Parent) 155 ? (ViewLayoutState.Parent)parent 156 : null; 157 158 if (isStatusBitsNonZero(MINOR_AXIS_PREFERENCE_CHANGED_BIT)) { clearStatusBits(MINOR_AXIS_PREFERENCE_CHANGED_BIT); 161 162 int minorAxis = getMinorAxis(); 163 if (minorAxisUpdateLayout(minorAxis)) { 164 if (lsParent != null) { 165 lsParent.minorAxisPreferenceChanged(this); 166 } 167 } 168 } 169 170 if (isStatusBitsNonZero(MAJOR_AXIS_PREFERENCE_CHANGED_BIT)) { clearStatusBits(MAJOR_AXIS_PREFERENCE_CHANGED_BIT); 173 174 float oldSpan = getLayoutMajorAxisPreferredSpanFloat(); 175 float newSpan = view.getPreferredSpan(getMajorAxis()); 176 setLayoutMajorAxisPreferredSpan(newSpan); 177 double majorAxisSpanDelta = newSpan - oldSpan; 178 if (majorAxisSpanDelta != 0 && lsParent != null) { 179 lsParent.majorAxisPreferenceChanged(this, majorAxisSpanDelta); 180 } 181 } 182 183 if (isStatusBitsNonZero(VIEW_SIZE_INVALID_BIT)) { 185 clearStatusBits(VIEW_SIZE_INVALID_BIT); 186 187 if (lsParent != null) { 188 float width; 189 float height; 190 float majorAxisSpan = (float)getLayoutMajorAxisPreferredSpan(); 191 float minorAxisSpan = lsParent.getMinorAxisSpan(this); 192 if (isXMajorAxis()) { width = majorAxisSpan; 194 height = minorAxisSpan; 195 } else { 196 width = minorAxisSpan; 197 height = majorAxisSpan; 198 } 199 200 view.setSize(width, height); 201 } 202 } 203 204 updateLayout(); 206 } 207 208 protected boolean minorAxisUpdateLayout(int minorAxis) { 209 boolean minorAxisPreferenceChanged = false; 210 float val; 211 212 val = view.getPreferredSpan(minorAxis); 213 if (val != getLayoutMinorAxisPreferredSpan()) { 214 setLayoutMinorAxisPreferredSpan(val); 215 minorAxisPreferenceChanged = true; 216 } 217 218 val = view.getAlignment(getMinorAxis()); 219 if (val != getLayoutMinorAxisAlignment()) { 220 setLayoutMinorAxisAlignment(val); 221 minorAxisPreferenceChanged = true; 222 } 223 224 return minorAxisPreferenceChanged; 225 } 226 227 public int getViewRawIndex() { 228 return viewRawIndex; 229 } 230 231 public void setViewRawIndex(int viewRawIndex) { 232 this.viewRawIndex = viewRawIndex; 233 } 234 235 public double getLayoutMajorAxisRawOffset() { 236 return layoutMajorAxisRawOffset; 237 } 238 239 public void setLayoutMajorAxisRawOffset(double layoutMajorAxisRawOffset) { 240 this.layoutMajorAxisRawOffset = layoutMajorAxisRawOffset; 241 } 242 243 public double getLayoutMajorAxisPreferredSpan() { 244 return layoutMajorAxisPreferredSpan; 245 } 246 247 public float getLayoutMajorAxisPreferredSpanFloat() { 248 return layoutMajorAxisPreferredSpan; 249 } 250 251 protected void setLayoutMajorAxisPreferredSpan(float layoutMajorAxisPreferredSpan) { 252 this.layoutMajorAxisPreferredSpan = layoutMajorAxisPreferredSpan; 253 } 254 255 public float getLayoutMinorAxisPreferredSpan() { 256 return layoutMinorAxisPreferredSpan; 257 } 258 259 protected void setLayoutMinorAxisPreferredSpan(float layoutMinorAxisPreferredSpan) { 260 this.layoutMinorAxisPreferredSpan = layoutMinorAxisPreferredSpan; 261 } 262 263 public float getLayoutMinorAxisMinimumSpan() { 264 return getLayoutMinorAxisPreferredSpan(); 265 } 266 267 public float getLayoutMinorAxisMaximumSpan() { 268 return getLayoutMinorAxisPreferredSpan(); 269 } 270 271 public float getLayoutMinorAxisAlignment() { 272 return layoutMinorAxisAlignment; 273 } 274 275 public void setLayoutMinorAxisAlignment(float layoutMinorAxisAlignment) { 276 this.layoutMinorAxisAlignment = layoutMinorAxisAlignment; 277 } 278 279 public void viewPreferenceChanged(boolean width, boolean height) { 280 if (isXMajorAxis()) { if (width) { 282 setStatusBits(MAJOR_AXIS_PREFERENCE_CHANGED_BIT); } 284 if (height) { 285 setStatusBits(MINOR_AXIS_PREFERENCE_CHANGED_BIT); } 287 } else { 288 if (width) { 289 setStatusBits(MINOR_AXIS_PREFERENCE_CHANGED_BIT); } 291 if (height) { 292 setStatusBits(MAJOR_AXIS_PREFERENCE_CHANGED_BIT); } 294 } 295 setStatusBits(VIEW_SIZE_INVALID_BIT); } 297 298 public void markViewSizeInvalid() { 299 setStatusBits(VIEW_SIZE_INVALID_BIT); 300 } 301 302 public boolean isLayoutValid() { 303 return !isStatusBitsNonZero(ANY_INVALID); 304 } 305 306 protected final int getStatusBits(int bits) { 307 return (statusBits & bits); 308 } 309 310 protected final boolean isStatusBitsNonZero(int bits) { 311 return (getStatusBits(bits) != 0); 312 } 313 314 protected final void setStatusBits(int bits) { 315 statusBits |= bits; 316 } 317 318 protected final void clearStatusBits(int bits) { 319 statusBits &= ~bits; 320 } 321 322 } 323 | Popular Tags |