1 19 20 package org.netbeans.editor.view.spi; 21 22 import java.awt.Graphics ; 23 import java.awt.Shape ; 24 import javax.swing.event.DocumentEvent ; 25 import javax.swing.text.AbstractDocument ; 26 import javax.swing.text.AttributeSet ; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.Element ; 30 import javax.swing.text.JTextComponent ; 31 import javax.swing.text.Position ; 32 import javax.swing.text.View ; 33 import javax.swing.text.ViewFactory ; 34 import org.netbeans.lib.editor.util.PriorityMutex; 35 36 43 44 public class LockView extends View { 45 46 private static final String PROPERTY_VIEW_HIERARCHY_MUTEX = "viewHierarchyMutex"; private static final String PROPERTY_FOLD_HIERARCHY_MUTEX = "foldHierarchyMutex"; 50 private View view; 51 52 private PriorityMutex mutex; 53 54 private AbstractDocument doc; 55 56 69 public static synchronized PriorityMutex getViewHierarchyMutex(JTextComponent component) { 70 PriorityMutex mutex = (PriorityMutex)component.getClientProperty(PROPERTY_FOLD_HIERARCHY_MUTEX); 72 if (mutex == null) { 73 mutex = new PriorityMutex(); 74 component.putClientProperty(PROPERTY_FOLD_HIERARCHY_MUTEX, mutex); 75 } 76 component.putClientProperty(PROPERTY_VIEW_HIERARCHY_MUTEX, mutex); 77 78 return mutex; 79 } 80 81 91 public static LockView get(View view) { 92 while (view != null && !(view instanceof LockView)) { 93 view = view.getParent(); 94 } 95 96 return (LockView)view; 97 } 98 99 public LockView(View view) { 100 super(null); 101 102 this.view = view; 103 } 105 106 public void setParent(View parent) { 107 108 View origParent = getParent(); 109 if (origParent != null && parent != null) { 110 119 throw new IllegalStateException ("Unexpected state occurred when" + " trying to set non-null parent to LockView with non-null" + " parent already set." ); 123 } 124 125 if (mutex == null && parent != null) { 128 JTextComponent c = (JTextComponent )parent.getContainer(); 129 if (c != null) { 130 mutex = getViewHierarchyMutex(c); 131 } 132 } 133 134 if (parent != null) { 135 Document maybeAbstractDoc = parent.getDocument(); 137 if (!(maybeAbstractDoc instanceof AbstractDocument )) { 138 150 throw new IllegalStateException ("Currently the LockView" + " is designed to work with AbstractDocument instances only." ); 153 } 154 155 188 this.doc = (AbstractDocument )maybeAbstractDoc; 189 } 190 191 194 this.doc.readLock(); 195 try { 196 lock(); 197 try { 198 199 setParentLocked(parent); 200 201 } finally { 202 unlock(); 203 } 204 205 } finally { 206 this.doc.readUnlock(); 207 } 208 } 209 210 protected void setParentLocked(View parent) { 211 if (parent == null && view != null) { 214 view.setParent(null); 215 } 216 217 super.setParent(parent); 218 219 if (parent != null && view != null) { 221 view.setParent(this); 222 } 223 } 224 225 228 public void setView(View v) { 229 lock(); 230 try { 231 232 if (view != null) { 233 view.setParent(null); 236 } 237 view = v; 238 if (view != null) { 239 view.setParent(this); 240 } 241 242 } finally { 243 unlock(); 244 } 245 } 246 247 public void lock() { 248 if (mutex != null) { 249 mutex.lock(); 250 } 251 } 252 253 public void unlock() { 254 mutex.unlock(); } 256 257 public boolean isPriorityThreadWaiting() { 258 return mutex.isPriorityThreadWaiting(); 259 } 260 261 271 public Thread getLockThread() { 272 return mutex.getLockThread(); 273 } 274 275 public void render(Runnable r) { 276 lock(); 277 try { 278 279 r.run(); 280 281 } finally { 282 unlock(); 283 } 284 } 285 286 291 public AttributeSet getAttributes() { 292 return null; 293 } 294 295 public float getPreferredSpan(int axis) { 296 lock(); 297 try { 298 299 if (view != null) { 300 return view.getPreferredSpan(axis); 301 } 302 return 10; 303 304 } finally { 305 unlock(); 306 } 307 } 308 309 public float getMinimumSpan(int axis) { 310 lock(); 311 try { 312 313 if (view != null) { 314 return view.getMinimumSpan(axis); 315 } 316 return 10; 317 318 } finally { 319 unlock(); 320 } 321 } 322 323 public float getMaximumSpan(int axis) { 324 lock(); 325 try { 326 327 if (view != null) { 328 return view.getMaximumSpan(axis); 329 } 330 return Integer.MAX_VALUE; 331 332 } finally { 333 unlock(); 334 } 335 } 336 337 public void preferenceChanged(View child, boolean width, boolean height) { 338 View parent = getParent(); 339 if (parent != null) { 340 parent.preferenceChanged(this, width, height); 341 } 342 } 343 344 public float getAlignment(int axis) { 345 lock(); 346 try { 347 348 if (view != null) { 349 return view.getAlignment(axis); 350 } 351 return 0; 352 353 } finally { 354 unlock(); 355 } 356 } 357 358 public void paint(Graphics g, Shape allocation) { 359 lock(); 360 try { 361 362 if (view != null) { 363 view.paint(g, allocation); 364 } 365 366 } finally { 367 unlock(); 368 } 369 } 370 371 public int getViewCount() { 372 return 1; 373 } 374 375 381 public View getView(int n) { 382 return view; 383 } 384 385 public int getViewIndex(int pos, Position.Bias b) { 386 return 0; 387 } 388 389 public Shape getChildAllocation(int index, Shape a) { 390 return a; 391 } 392 393 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 394 lock(); 395 try { 396 397 if (view != null) { 398 return view.modelToView(pos, a, b); 399 } 400 return null; 401 402 } finally { 403 unlock(); 404 } 405 } 406 407 public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException { 408 lock(); 409 try { 410 411 if (view != null) { 412 return view.modelToView(p0, b0, p1, b1, a); 413 } 414 return null; 415 416 } finally { 417 unlock(); 418 } 419 } 420 421 public int viewToModel(float x, float y, Shape a, Position.Bias [] bias) { 422 lock(); 423 try { 424 425 if (view != null) { 426 return view.viewToModel(x, y, a, bias); 427 } 428 return -1; 429 430 } finally { 431 unlock(); 432 } 433 } 434 435 public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a, 436 int direction, Position.Bias [] biasRet) throws BadLocationException { 437 438 lock(); 439 try { 440 441 if(view != null) { 442 return view.getNextVisualPositionFrom(pos, b, a, direction, biasRet); 443 } 444 return -1; 445 446 } finally { 447 unlock(); 448 } 449 } 450 451 public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) { 452 lock(); 453 try { 454 455 if (view != null) { 456 view.insertUpdate(e, a, f); 457 } 458 459 } finally { 460 unlock(); 461 } 462 } 463 464 public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) { 465 lock(); 466 try { 467 468 if (view != null) { 469 view.removeUpdate(e, a, f); 470 } 471 472 } finally { 473 unlock(); 474 } 475 } 476 477 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { 478 lock(); 479 try { 480 481 if (view != null) { 482 view.changedUpdate(e, a, f); 483 } 484 485 } finally { 486 unlock(); 487 } 488 } 489 490 public String getToolTipText(float x, float y, Shape allocation) { 491 lock(); 492 try { 493 494 return (view != null) 495 ? view.getToolTipText(x, y, allocation) 496 : null; 497 498 } finally { 499 unlock(); 500 } 501 } 502 503 public Document getDocument() { 504 return doc; 505 } 506 507 public int getStartOffset() { 508 if (view != null) { 509 return view.getStartOffset(); 510 } 511 Element elem = getElement(); 512 return (elem != null) ? elem.getStartOffset() : 0; 513 } 514 515 public int getEndOffset() { 516 if (view != null) { 517 return view.getEndOffset(); 518 } 519 Element elem = getElement(); 520 return (elem != null) ? elem.getEndOffset() : 0; 521 } 522 523 public Element getElement() { 524 if (view != null) { 525 return view.getElement(); 526 } 527 Document doc = getDocument(); 528 return (doc != null) ? doc.getDefaultRootElement() : null; 529 } 530 531 public View breakView(int axis, float len, Shape a) { 532 throw new Error ("Can't break lock view"); } 534 535 public int getResizeWeight(int axis) { 536 lock(); 537 try { 538 539 if (view != null) { 540 return view.getResizeWeight(axis); 541 } 542 return 0; 543 544 } finally { 545 unlock(); 546 } 547 } 548 549 public void setSize(float width, float height) { 550 lock(); 551 try { 552 553 if (view != null) { 554 view.setSize(width, height); 555 } 556 557 } finally { 558 unlock(); 559 } 560 } 561 562 } 563 | Popular Tags |