1 19 20 package org.netbeans.api.editor.fold; 21 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import javax.swing.event.DocumentEvent ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Document ; 27 import javax.swing.text.JTextComponent ; 28 import org.netbeans.modules.editor.fold.ApiPackageAccessor; 29 import org.netbeans.modules.editor.fold.FoldHierarchyExecution; 30 import org.netbeans.modules.editor.fold.FoldOperationImpl; 31 32 87 88 public final class FoldHierarchy { 89 90 93 public static final FoldType ROOT_FOLD_TYPE = new FoldType("root-fold"); 95 private static boolean apiPackageAccessorRegistered; 96 97 static { 98 ensureApiAccessorRegistered(); 99 } 100 101 private static void ensureApiAccessorRegistered() { 102 if (!apiPackageAccessorRegistered) { 103 apiPackageAccessorRegistered = true; 104 ApiPackageAccessor.register(new ApiPackageAccessorImpl()); 105 } 106 } 107 108 111 private FoldHierarchyExecution execution; 112 113 122 public static synchronized FoldHierarchy get(JTextComponent component) { 123 return FoldHierarchyExecution.getOrCreateFoldHierarchy(component); 124 } 125 126 127 private FoldHierarchy(FoldHierarchyExecution execution) { 128 this.execution = execution; 130 } 131 132 146 public void render(Runnable r) { 147 lock(); 148 try { 149 r.run(); 150 } finally { 151 unlock(); 152 } 153 } 154 155 184 public void lock() { 185 execution.lock(); 186 } 187 188 192 public void unlock() { 193 execution.unlock(); 194 } 195 196 206 public void collapse(Fold f) { 207 collapse(Collections.singletonList(f)); 208 } 209 210 219 public void collapse(Collection c) { 220 execution.collapse(c); 221 } 222 223 233 public void expand(Fold f) { 234 expand(Collections.singletonList(f)); 235 } 236 237 246 public void expand(Collection c) { 247 execution.expand(c); 248 } 249 250 259 public void toggle(Fold f) { 260 if (f.isCollapsed()) { 261 expand(f); 262 } else { collapse(f); 264 } 265 } 266 267 272 public JTextComponent getComponent() { 273 return execution.getComponent(); 274 } 275 276 282 public Fold getRootFold() { 283 return execution.getRootFold(); 284 } 285 286 291 public void addFoldHierarchyListener(FoldHierarchyListener l) { 292 execution.addFoldHierarchyListener(l); 293 } 294 295 300 public void removeFoldHierarchyListener(FoldHierarchyListener l) { 301 execution.removeFoldHierarchyListener(l); 302 } 303 304 310 public String toString() { 311 return execution.toString(); 312 } 313 314 318 private static final class ApiPackageAccessorImpl extends ApiPackageAccessor { 319 320 public FoldHierarchy createFoldHierarchy(FoldHierarchyExecution execution) { 321 return new FoldHierarchy(execution); 322 } 323 324 public Fold createFold(FoldOperationImpl operation, 325 FoldType type, String description, boolean collapsed, 326 Document doc, int startOffset, int endOffset, 327 int startGuardedLength, int endGuardedLength, 328 Object extraInfo) 329 throws BadLocationException { 330 return new Fold(operation, type, description, collapsed, 331 doc, startOffset, endOffset, 332 startGuardedLength, endGuardedLength, 333 extraInfo 334 ); 335 } 336 337 public FoldHierarchyEvent createFoldHierarchyEvent(FoldHierarchy source, 338 Fold[] removedFolds, Fold[] addedFolds, FoldStateChange[] foldStateChanges, 339 int affectedStartOffset, int affectedEndOffset) { 340 return new FoldHierarchyEvent(source, removedFolds, addedFolds, 341 foldStateChanges, affectedStartOffset, affectedEndOffset); 342 } 343 344 public FoldStateChange createFoldStateChange(Fold fold) { 345 return new FoldStateChange(fold); 346 } 347 348 public void foldSetParent(Fold fold, Fold parent) { 349 fold.setParent(parent); 350 } 351 352 public void foldExtractToChildren(Fold fold, int index, int length, Fold targetFold) { 353 fold.extractToChildren(index, length, targetFold); 354 } 355 356 public Fold foldReplaceByChildren(Fold fold, int index) { 357 return fold.replaceByChildren(index); 358 } 359 360 public void foldSetCollapsed(Fold fold, boolean collapsed) { 361 fold.setCollapsed(collapsed); 362 } 363 364 public void foldSetDescription(Fold fold, String description) { 365 fold.setDescription(description); 366 } 367 368 public void foldSetStartOffset(Fold fold, Document doc, int startOffset) 369 throws BadLocationException { 370 fold.setStartOffset(doc, startOffset); 371 } 372 373 public void foldSetEndOffset(Fold fold, Document doc, int endOffset) 374 throws BadLocationException { 375 fold.setEndOffset(doc, endOffset); 376 } 377 378 public boolean foldIsStartDamaged(Fold fold) { 379 return fold.isStartDamaged(); 380 } 381 382 public boolean foldIsEndDamaged(Fold fold) { 383 return fold.isEndDamaged(); 384 } 385 386 public boolean foldIsExpandNecessary(Fold fold) { 387 return fold.isExpandNecessary(); 388 } 389 390 public void foldInsertUpdate(Fold fold, DocumentEvent evt) { 391 fold.insertUpdate(evt); 392 } 393 394 public void foldRemoveUpdate(Fold fold, DocumentEvent evt) { 395 fold.removeUpdate(evt); 396 } 397 398 public FoldOperationImpl foldGetOperation(Fold fold) { 399 return fold.getOperation(); 400 } 401 402 public int foldGetRawIndex(Fold fold) { 403 return fold.getRawIndex(); 404 } 405 406 public void foldSetRawIndex(Fold fold, int rawIndex) { 407 fold.setRawIndex(rawIndex); 408 } 409 410 public void foldUpdateRawIndex(Fold fold, int rawIndexDelta) { 411 fold.updateRawIndex(rawIndexDelta); 412 } 413 414 public Object foldGetExtraInfo(Fold fold) { 415 return fold.getExtraInfo(); 416 } 417 418 public void foldStateChangeCollapsedChanged(FoldStateChange fsc) { 419 fsc.collapsedChanged(); 420 } 421 422 public void foldStateChangeDescriptionChanged(FoldStateChange fsc) { 423 fsc.descriptionChanged(); 424 } 425 426 public void foldStateChangeStartOffsetChanged(FoldStateChange fsc, 427 int originalStartOffset) { 428 fsc.startOffsetChanged(originalStartOffset); 429 } 430 431 public void foldStateChangeEndOffsetChanged(FoldStateChange fsc, 432 int originalEndOffset) { 433 fsc.endOffsetChanged(originalEndOffset); 434 } 435 436 } 437 438 } 439 | Popular Tags |