1 22 23 package org.gjt.sp.jedit.textarea; 24 25 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 import org.gjt.sp.jedit.buffer.*; 32 34 class SelectionManager 35 { 36 List <Selection> selection; 39 40 SelectionManager(TextArea textArea) 42 { 43 this.textArea = textArea; 44 selection = new ArrayList <Selection>(); 45 } 47 52 int getSelectionCount() 53 { 54 return selection.size(); 55 } 57 62 public Selection[] getSelection() 63 { 64 return selection.toArray( 65 new Selection[selection.size()]); 66 } 68 73 void setSelection(Selection[] selection) 74 { 75 this.selection.clear(); 76 addToSelection(selection); 77 } 79 86 void addToSelection(Selection[] selection) 87 { 88 if(selection != null) 89 { 90 for(int i = 0; i < selection.length; i++) 91 { 92 Selection s = selection[i]; 93 if(s != null) 94 addToSelection(s); 95 } 96 } 97 } 99 void addToSelection(Selection addMe) 101 { 102 if(addMe.start > addMe.end) 103 { 104 throw new IllegalArgumentException (addMe.start 105 + " > " + addMe.end); 106 } 107 else if(addMe.start == addMe.end) 108 { 109 if(addMe instanceof Selection.Range) 110 return; 111 else if(addMe instanceof Selection.Rect) 112 { 113 if(((Selection.Rect)addMe).extraEndVirt == 0) 114 return; 115 } 116 } 117 118 Iterator <Selection> iter = selection.iterator(); 119 while(iter.hasNext()) 120 { 121 Selection s = iter.next(); 124 if(s.overlaps(addMe)) 125 { 126 addMe.start = Math.min(s.start,addMe.start); 127 addMe.end = Math.max(s.end,addMe.end); 128 iter.remove(); 129 } 130 } 131 132 addMe.startLine = textArea.getLineOfOffset(addMe.start); 133 addMe.endLine = textArea.getLineOfOffset(addMe.end); 134 135 boolean added = false; 136 137 for(int i = 0; i < selection.size(); i++) 138 { 139 Selection s = selection.get(i); 140 if(addMe.start < s.start) 141 { 142 selection.add(i,addMe); 143 added = true; 144 break; 145 } 146 } 147 148 if(!added) 149 selection.add(addMe); 150 151 textArea.invalidateLineRange(addMe.startLine,addMe.endLine); 152 } 154 159 void setSelection(Selection selection) 160 { 161 this.selection.clear(); 162 163 if(selection != null) 164 addToSelection(selection); 165 } 167 174 Selection getSelectionAtOffset(int offset) 175 { 176 if(selection != null) 177 { 178 for (Selection s : selection) 179 { 180 if(offset >= s.start && offset <= s.end) 181 return s; 182 } 183 } 184 185 return null; 186 } 188 193 void removeFromSelection(Selection sel) 194 { 195 selection.remove(sel); 196 } 198 211 void resizeSelection(int offset, int end, int extraEndVirt, 212 boolean rect) 213 { 214 boolean reversed = false; 215 if(end < offset) 216 { 217 int tmp = offset; 218 offset = end; 219 end = tmp; 220 reversed = true; 221 } 222 223 Selection newSel; 224 if(rect) 225 { 226 Selection.Rect rectSel = new Selection.Rect(offset,end); 227 if(reversed) 228 rectSel.extraStartVirt = extraEndVirt; 229 else 230 rectSel.extraEndVirt = extraEndVirt; 231 newSel = rectSel; 232 } 233 else 234 newSel = new Selection.Range(offset,end); 235 236 addToSelection(newSel); 237 } 239 249 int[] getSelectedLines() 250 { 251 252 Set <Integer > set = new TreeSet <Integer >(); 253 for (Selection s : selection) 254 { 255 int endLine = 256 s.end == textArea.getLineStartOffset(s.endLine) 257 ? s.endLine - 1 258 : s.endLine; 259 260 for(int j = s.startLine; j <= endLine; j++) 261 { 262 set.add(j); 263 } 264 } 265 266 267 int[] returnValue = new int[set.size()]; 268 int i = 0; 269 for (Integer line : set) 270 returnValue[i++] = line; 271 272 return returnValue; 273 } 275 void invertSelection() 277 { 278 Selection[] newSelection = new Selection[selection.size() + 1]; 279 int lastOffset = 0; 280 for(int i = 0; i < selection.size(); i++) 281 { 282 Selection s = selection.get(i); 283 newSelection[i] = new Selection.Range(lastOffset, 284 s.getStart()); 285 lastOffset = s.getEnd(); 286 } 287 newSelection[selection.size()] = new Selection.Range( 288 lastOffset,textArea.getBufferLength()); 289 setSelection(newSelection); 290 } 292 297 int[] getSelectionStartAndEnd(int screenLine, int physicalLine, 298 Selection s) 299 { 300 int start = textArea.getScreenLineStartOffset(screenLine); 301 int end = textArea.getScreenLineEndOffset(screenLine); 302 303 if(end <= s.start || start > s.end) 304 return null; 305 306 int selStartScreenLine; 307 if(textArea.displayManager.isLineVisible(s.startLine)) 308 selStartScreenLine = textArea.getScreenLineOfOffset(s.start); 309 else 310 selStartScreenLine = -1; 311 312 int selEndScreenLine; 313 if(textArea.displayManager.isLineVisible(s.endLine)) 314 selEndScreenLine = textArea.getScreenLineOfOffset(s.end); 315 else 316 selEndScreenLine = -1; 317 318 JEditBuffer buffer = textArea.getBuffer(); 319 320 int lineStart = buffer.getLineStartOffset(physicalLine); 321 int x1, x2; 322 323 if(s instanceof Selection.Rect) 324 { 325 start -= lineStart; 326 end -= lineStart; 327 328 Selection.Rect rect = (Selection.Rect)s; 329 int _start = rect.getStartColumn(buffer); 330 int _end = rect.getEndColumn(buffer); 331 332 int lineLen = buffer.getLineLength(physicalLine); 333 334 int[] total = new int[1]; 335 336 int rectStart = buffer.getOffsetOfVirtualColumn( 337 physicalLine,_start,total); 338 if(rectStart == -1) 339 { 340 x1 = (_start - total[0]) * textArea.charWidth; 341 rectStart = lineLen; 342 } 343 else 344 x1 = 0; 345 346 int rectEnd = buffer.getOffsetOfVirtualColumn( 347 physicalLine,_end,total); 348 if(rectEnd == -1) 349 { 350 x2 = (_end - total[0]) * textArea.charWidth; 351 rectEnd = lineLen; 352 } 353 else 354 x2 = 0; 355 356 if(end <= rectStart || start > rectEnd) 357 return null; 358 359 x1 = rectStart < start ? 0 360 : x1 + textArea.offsetToXY(physicalLine, 361 rectStart).x; 362 x2 = rectEnd > end ? textArea.getWidth() 363 : x2 + textArea.offsetToXY(physicalLine, 364 rectEnd).x; 365 } 366 else if(selStartScreenLine == selEndScreenLine 367 && selStartScreenLine != -1) 368 { 369 x1 = textArea.offsetToXY(physicalLine, 370 s.start - lineStart).x; 371 x2 = textArea.offsetToXY(physicalLine, 372 s.end - lineStart).x; 373 } 374 else if(screenLine == selStartScreenLine) 375 { 376 x1 = textArea.offsetToXY(physicalLine, 377 s.start - lineStart).x; 378 x2 = textArea.getWidth(); 379 } 380 else if(screenLine == selEndScreenLine) 381 { 382 x1 = 0; 383 x2 = textArea.offsetToXY(physicalLine, 384 s.end - lineStart).x; 385 } 386 else 387 { 388 x1 = 0; 389 x2 = textArea.getWidth(); 390 } 391 392 if(x1 < 0) 393 x1 = 0; 394 if(x2 < 0) 395 x2 = 0; 396 397 if(x1 == x2) 398 x2++; 399 400 return new int[] { x1, x2 }; 401 } 403 408 boolean insideSelection(int x, int y) 409 { 410 int offset = textArea.xyToOffset(x,y); 411 412 Selection s = textArea.getSelectionAtOffset(offset); 413 if(s == null) 414 return false; 415 416 int screenLine = textArea.getScreenLineOfOffset(offset); 417 if(screenLine == -1) 418 return false; 419 420 int[] selectionStartAndEnd = getSelectionStartAndEnd( 421 screenLine,textArea.getLineOfOffset(offset),s); 422 if(selectionStartAndEnd == null) 423 return false; 424 425 return x >= selectionStartAndEnd[0] 426 && x <= selectionStartAndEnd[1]; 427 } 429 private TextArea textArea; 430 } 431 | Popular Tags |