1 19 20 package org.netbeans.lib.editor.view; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import javax.swing.text.View ; 25 import org.netbeans.editor.view.spi.ViewLayoutState; 26 27 35 36 class GapLineViewChildren extends GapBoxViewChildren { 37 38 41 private float maxMinorAxisPrefAscent; 43 47 private int maxMinorAxisPrefDescentChildIndex = -1; 49 52 private float maxMinorAxisPrefDescent; 54 55 56 private RowList rowList; 58 59 GapLineViewChildren(GapBoxView view) { 60 super(view); 61 } 62 63 67 public void minorAxisLayout() { 68 71 int childCount = getChildCount(); 72 int maxMinorPrefAscentIndex = -1; 73 int maxMinorPrefDescentIndex = -1; 74 float maxMinorPrefAscentValue = 0f; 75 float maxMinorPrefDescentValue = 0f; 76 77 for (int i = 0; i < childCount; i++) { 78 ViewLayoutState child = getChild(i); 79 float span = child.getLayoutMinorAxisPreferredSpan(); 80 float alignment = child.getLayoutMinorAxisAlignment(); 81 float ascent = span * alignment; 82 float descent = span - ascent; 83 84 if (ascent > maxMinorPrefAscentValue) { 85 maxMinorPrefAscentIndex = i; 86 maxMinorPrefAscentValue = ascent; 87 } 88 89 if (descent > maxMinorPrefDescentValue) { 90 maxMinorPrefDescentIndex = i; 91 maxMinorPrefDescentValue = descent; 92 } 93 94 } 95 96 float maxMinorPrefSpanValue = maxMinorPrefAscentValue + maxMinorPrefDescentValue; 97 setMinorAxisPreferredSpan(maxMinorPrefSpanValue); 98 setMaxMinorAxisPrefAscent(maxMinorPrefAscentValue); 99 setMaxMinorAxisPrefDescent(maxMinorPrefDescentValue); 100 setMaxMinorAxisPrefAscentChildIndex(maxMinorPrefAscentIndex); 101 setMaxMinorAxisPrefDescentChildIndex(maxMinorPrefDescentIndex); 102 } 103 104 private float getPrefAscent(ViewLayoutState child) { 105 float span = child.getLayoutMinorAxisPreferredSpan(); 106 float alignment = child.getLayoutMinorAxisAlignment(); 107 return span * alignment; 108 } 109 110 private float getMaxMinorAxisPrefAscent() { 111 return maxMinorAxisPrefAscent; 112 } 113 114 private void setMaxMinorAxisPrefAscent(float maxMinorAxisPrefAscent) { 115 this.maxMinorAxisPrefAscent = maxMinorAxisPrefAscent; 116 } 117 118 private int getMaxMinorAxisPrefAscentChildIndex() { 119 return super.getMaxMinorAxisPreferredSpanChildIndex(); 121 } 122 123 private void setMaxMinorAxisPrefAscentChildIndex(int maxMinorAxisPrefAscentChildIndex) { 124 super.setMaxMinorAxisPreferredSpanChildIndex(maxMinorAxisPrefAscentChildIndex); 126 } 127 128 private float getPrefDescent(ViewLayoutState child) { 129 float span = child.getLayoutMinorAxisPreferredSpan(); 130 float alignment = child.getLayoutMinorAxisAlignment(); 131 return span * (1 - alignment); 132 } 133 134 private float getMaxMinorAxisPrefDescent() { 135 return maxMinorAxisPrefDescent; 136 } 137 138 private void setMaxMinorAxisPrefDescent(float maxMinorAxisPrefDescent) { 139 this.maxMinorAxisPrefDescent = maxMinorAxisPrefDescent; 140 } 141 142 private int getMaxMinorAxisPrefDescentChildIndex() { 143 return maxMinorAxisPrefDescentChildIndex; 144 } 145 146 private void setMaxMinorAxisPrefDescentChildIndex(int maxMinorAxisPrefDescentChildIndex) { 147 this.maxMinorAxisPrefDescentChildIndex = maxMinorAxisPrefDescentChildIndex; 148 } 149 150 int getMaxMinorAxisPrefSpanChildIndex() { 151 throw new IllegalStateException ("Should never be called"); } 153 154 void setMaxMinorAxisPrefSpanChildIndex(int maxMinorAxisPrefSpanChildIndex) { 155 throw new IllegalStateException ("Should never be called"); } 157 158 protected void replaceUpdateIndexes(int index, int removeLength, int insertLength, 159 int neighborIndex, int neighborIndexAfterReplace, ViewLayoutState neighbor) { 160 161 boolean minorAxisChanged = false; 162 int endRemoveIndex = index + removeLength; 163 168 180 181 182 int ind = getMaxMinorAxisPrefAscentChildIndex(); 183 if (ind >= endRemoveIndex) { 184 setMaxMinorAxisPrefAscentChildIndex(ind + insertLength - removeLength); 185 } else if (ind >= index) { 186 float neighborAscent = getPrefAscent(neighbor); 187 if (neighbor == null || neighborAscent < getMaxMinorAxisPrefAscent()) { 188 minorAxisChanged = true; 189 } 190 setMaxMinorAxisPrefAscentChildIndex(neighborIndexAfterReplace); 191 } 192 193 ind = getMaxMinorAxisPrefDescentChildIndex(); 194 if (ind >= endRemoveIndex) { 195 setMaxMinorAxisPrefDescentChildIndex(ind + insertLength - removeLength); 196 } else if (ind >= index) { 197 float neighborDescent = getPrefDescent(neighbor); 198 if (neighbor == null || neighborDescent < getMaxMinorAxisPrefDescent()) { 199 minorAxisChanged = true; 200 } 201 setMaxMinorAxisPrefDescentChildIndex(neighborIndexAfterReplace); 202 } 203 204 if (minorAxisChanged) { 205 view.markMinorAxisPreferenceChanged(); 206 } 207 } 208 209 protected void minorAxisPreferenceChanged(ViewLayoutState child, int childIndex) { 210 boolean minorAxisChanged = false; 211 212 float span = child.getLayoutMinorAxisPreferredSpan(); 213 float alignment = child.getLayoutMinorAxisAlignment(); 214 float ascent = span * alignment; 215 float descent = span - ascent; 216 217 if (getMaxMinorAxisPrefAscentChildIndex() == -1 218 || ascent > getMaxMinorAxisPrefAscent() 219 ) { 220 setMaxMinorAxisPrefAscent(ascent); 221 setMaxMinorAxisPrefAscentChildIndex(childIndex); 222 minorAxisChanged = true; 223 } 224 225 if (getMaxMinorAxisPrefDescentChildIndex() == -1 226 || descent > getMaxMinorAxisPrefDescent() 227 ) { 228 setMaxMinorAxisPrefDescent(descent); 229 setMaxMinorAxisPrefDescentChildIndex(childIndex); 230 minorAxisChanged = true; 231 } 232 233 234 if (minorAxisChanged) { 235 setMinorAxisPreferredSpan(getMaxMinorAxisPrefAscent() 236 + getMaxMinorAxisPrefDescent()); 237 238 view.markMinorAxisPreferenceChanged(); 239 } 240 } 241 242 public float getChildMinorAxisOffset(int childIndex) { 243 ViewLayoutState child = getChild(childIndex); 244 float minorAxisSpan = view.getMinorAxisAssignedSpan(); 245 float childMinorMaxSpan = child.getLayoutMinorAxisMaximumSpan(); 246 if (childMinorMaxSpan < minorAxisSpan) { 247 float align = child.getLayoutMinorAxisAlignment(); 249 float baseline = getMaxMinorAxisPrefAscent(); 251 float span = child.getLayoutMinorAxisPreferredSpan(); 252 float alignment = child.getLayoutMinorAxisAlignment(); 253 float ascent = span * alignment; 254 return baseline - ascent; 255 } 256 257 return 0f; 258 } 259 260 private int getRowCount() { 261 return (rowList != null) ? rowList.size() : 0; 262 } 263 264 private Row getRow(int rowIndex) { 265 return (Row)rowList.get(rowIndex); 266 } 267 268 276 private int getRowIndex(int childIndex) { 277 int low = 0; 279 284 int high = rowList.lastValidRowIndex; 285 286 while (low <= high) { 287 int mid = (low + high) / 2; 288 Row midRow = getRow(mid); 289 290 if (midRow.endChildIndex <= childIndex) { low = mid + 1; 292 } else if (midRow.startChildIndex > childIndex) { high = mid - 1; 294 } else { 295 return mid; 297 } 298 } 299 300 return low; } 302 303 310 class Row { 311 312 319 int startChildIndex; 320 321 325 int endChildIndex; 326 327 333 float beforeStartChildMajorOffset; 334 335 339 float minorAxisOffset; 340 341 344 float minorAxisSpan; 345 346 } 347 348 class RowList extends ArrayList { 349 350 int lastValidRowIndex; 351 352 } 353 354 } 355 | Popular Tags |