1 19 20 package org.netbeans.lib.editor.util.swing; 21 22 import javax.swing.text.Element ; 23 import javax.swing.event.DocumentEvent ; 24 import javax.swing.undo.AbstractUndoableEdit ; 25 import javax.swing.undo.CannotUndoException ; 26 import javax.swing.undo.CannotRedoException ; 27 import org.netbeans.lib.editor.util.GapList; 28 29 35 36 public abstract class GapBranchElement implements Element { 37 38 protected static final Element [] EMPTY_ELEMENT_ARRAY = new Element [0]; 39 40 private GapList children; 41 42 public GapBranchElement() { 43 children = new GapList(); 44 } 45 46 public int getElementCount() { 47 return children.size(); 48 } 49 50 public Element getElement(int index) { 51 return (Element )children.get(index); 52 } 53 54 public void copyElements(int srcBegin, int srcEnd, Element dst[], int dstBegin) { 55 children.copyElements(srcBegin, srcEnd, dst, dstBegin); 56 } 57 58 88 public int getElementIndex(int offset) { 89 int low = 0; 90 int high = getElementCount() - 1; 91 92 if (high == -1) { return -1; 94 } 95 96 while (low <= high) { 97 int mid = (low + high) / 2; 98 int elemStartOffset = getElement(mid).getStartOffset(); 99 100 if (elemStartOffset < offset) { 101 low = mid + 1; 102 } else if (elemStartOffset > offset) { 103 high = mid - 1; 104 } else { return mid; 106 } 107 } 108 109 if (high < 0) { high = 0; 111 } 112 return high; 113 } 114 115 public boolean isLeaf() { 116 return false; 117 } 118 119 protected void replace(int index, int removeCount, Element [] addedElems) { 120 if (removeCount > 0) { 121 children.remove(index, removeCount); 122 } 123 if (addedElems != null) { 124 children.addArray(index, addedElems); 125 } 126 } 127 128 129 public String toString() { 130 return children.toString(); 131 } 132 133 public class Edit extends AbstractUndoableEdit 134 implements DocumentEvent.ElementChange { 135 136 private int index; 137 138 private Element [] childrenAdded; 139 140 private Element [] childrenRemoved; 141 142 public Edit(int index, Element [] childrenRemoved, Element [] childrenAdded) { 143 this.index = index; 144 this.childrenRemoved = childrenRemoved; 145 this.childrenAdded = childrenAdded; 146 } 147 148 public Element getElement() { 149 return GapBranchElement.this; 150 } 151 152 public int getIndex() { 153 return index; 154 } 155 156 public Element [] getChildrenRemoved() { 157 return childrenRemoved; 158 } 159 160 public Element [] getChildrenAdded() { 161 return childrenAdded; 162 } 163 164 public void undo() throws CannotUndoException { 165 super.undo(); 166 167 replace(index, childrenAdded.length, childrenRemoved); 168 169 Element [] tmp = childrenRemoved; 171 childrenRemoved = childrenAdded; 172 childrenAdded = tmp; 173 } 174 175 public void redo() throws CannotRedoException { 176 super.redo(); 177 178 Element [] tmp = childrenRemoved; 180 childrenRemoved = childrenAdded; 181 childrenAdded = tmp; 182 183 replace(index, childrenRemoved.length, childrenAdded); 184 } 185 186 } 187 188 193 public abstract class LastIndex { 194 195 private int lastReturnedElementIndex; 196 197 204 public int getElementIndex(int offset) { 205 int low = 0; 206 int high = getElementCount() - 1; 207 208 if (high == -1) { return -1; 210 } 211 212 int lastIndex = lastReturnedElementIndex; if (lastIndex >= low && lastIndex <= high) { 214 Element lastElem = getElement(lastIndex); 215 int lastElemStartOffset = lastElem.getStartOffset(); 216 if (offset >= lastElemStartOffset) { 217 int lastElemEndOffset = lastElem.getEndOffset(); 218 if (offset < lastElemEndOffset) { return lastIndex; 220 } else { low = lastIndex + 1; 222 } 223 } else { high = lastIndex - 1; 225 } 226 } 227 228 while (low <= high) { 229 int mid = (low + high) / 2; 230 int elemStartOffset = ((Element )children.get(mid)).getStartOffset(); 231 232 if (elemStartOffset < offset) { 233 low = mid + 1; 234 } else if (elemStartOffset > offset) { 235 high = mid - 1; 236 } else { lastReturnedElementIndex = mid; 238 return mid; 239 } 240 } 241 242 if (high < 0) { 243 high = 0; 244 } 245 lastReturnedElementIndex = high; 246 return high; 247 } 248 249 } 250 251 } 252 | Popular Tags |