1 19 20 package org.netbeans.modules.editor.fold; 21 22 import org.netbeans.api.editor.fold.Fold; 23 import org.netbeans.modules.editor.fold.ApiPackageAccessor; 24 import org.netbeans.lib.editor.util.GapList; 25 26 28 35 36 public final class FoldChildren extends GapList { 37 38 41 private static final int INITIAL_INDEX_GAP_LENGTH 42 = Integer.MAX_VALUE >> 1; 43 44 47 Fold parent; 48 49 52 private int indexGapIndex; 53 54 60 private int indexGapLength; 61 62 public FoldChildren(Fold parent) { 63 this.parent = parent; 64 indexGapLength = INITIAL_INDEX_GAP_LENGTH; 65 } 66 67 73 public int getFoldCount() { 74 return size(); 75 } 76 77 83 public Fold getFold(int index) { 84 return (Fold)get(index); 85 } 86 87 public int getFoldIndex(Fold child) { 88 int index = getTranslatedFoldIndex(ApiPackageAccessor.get().foldGetRawIndex(child)); 89 if (index < 0 || index >= getFoldCount() || getFold(index) != child) { 90 index = -1; 91 } 92 return index; 93 } 94 95 public Fold[] foldsToArray(int index, int length) { 96 Fold[] folds = new Fold[length]; 97 copyItems(index, index + length, folds, 0); 98 return folds; 99 } 100 101 104 public void insert(int index, Fold fold) { 105 moveIndexGap(index); 106 insertImpl(index, fold); 108 } 109 110 113 public void insert(int index, Fold[] folds) { 114 moveIndexGap(index); 115 insertImpl(index, folds); 116 } 117 118 public void remove(int index, int length) { 119 moveIndexGap(index + length); 120 for (int i = index + length - 1; i >= index; i--) { 121 ApiPackageAccessor.get().foldSetParent(getFold(i), null); 122 } 123 super.remove(index, length); 124 indexGapLength += length; 125 indexGapIndex -= length; 126 } 127 128 136 public FoldChildren extractToChildren(int index, int length, Fold fold) { 137 FoldChildren foldChildren = new FoldChildren(fold); 138 if (length == 1) { 139 Fold insertFold = getFold(index); 140 remove(index, length); foldChildren.insert(0, insertFold); 142 } else { 143 Fold[] insertFolds = foldsToArray(index, length); 144 remove(index, length); foldChildren.insert(0, insertFolds); 146 } 147 148 insertImpl(index, fold); 150 151 return foldChildren; 152 } 153 154 public void replaceByChildren(int index, FoldChildren children) { 155 remove(index, 1); 156 157 if (children != null) { 158 int childCount = children.getFoldCount(); 160 insertImpl(index, children, 0, childCount); 162 } 163 } 164 165 private void insertImpl(int index, FoldChildren children, 166 int childIndex, int childCount) { 167 168 switch (childCount) { 169 case 0: break; 171 172 case 1: insertImpl(index, children.getFold(childIndex)); 174 break; 175 176 default: Fold[] folds = children.foldsToArray(childIndex, childCount); 178 insertImpl(index, folds); 179 break; 180 } 181 } 182 183 private void insertImpl(int index, Fold fold) { 184 indexGapLength--; 185 indexGapIndex++; 186 ApiPackageAccessor api = ApiPackageAccessor.get(); 187 api.foldSetRawIndex(fold, index); 188 api.foldSetParent(fold, parent); 189 add(index, fold); 190 } 191 192 private void insertImpl(int index, Fold[] folds) { 193 ApiPackageAccessor api = ApiPackageAccessor.get(); 194 int foldsLength = folds.length; 195 indexGapLength -= foldsLength; 196 indexGapIndex += foldsLength; 197 for (int i = foldsLength - 1; i >= 0; i--) { 198 Fold fold = folds[i]; 199 api.foldSetRawIndex(fold, index + i); 200 api.foldSetParent(fold, parent); 201 } 202 addArray(index, folds); 203 } 204 205 private int getTranslatedFoldIndex(int rawIndex) { 206 if (rawIndex >= indexGapLength) { 207 rawIndex -= indexGapLength; 208 } 209 return rawIndex; 210 } 211 212 private void moveIndexGap(int index) { 213 if (index != indexGapIndex) { 214 ApiPackageAccessor api = ApiPackageAccessor.get(); 215 int gapLen = indexGapLength; if (index < indexGapIndex) { for (int i = indexGapIndex - 1; i >= index; i--) { 218 api.foldUpdateRawIndex(getFold(i), +gapLen); 219 } 220 221 } else { for (int i = indexGapIndex; i < index; i++) { 223 api.foldUpdateRawIndex(getFold(i), -gapLen); 224 } 225 } 226 indexGapIndex = index; 227 } 228 } 229 230 } 231 | Popular Tags |