Lines 106-112
Link Here
|
106 |
private EditorKit kit; |
106 |
private EditorKit kit; |
107 |
|
107 |
|
108 |
/** document we work with */ |
108 |
/** document we work with */ |
109 |
private StyledDocument doc; |
109 |
private Object docOrReference; |
110 |
|
110 |
|
111 |
|
111 |
|
112 |
|
112 |
|
Lines 251-256
Link Here
|
251 |
final EditorKit kit () { |
251 |
final EditorKit kit () { |
252 |
return kit; |
252 |
return kit; |
253 |
} |
253 |
} |
|
|
254 |
|
255 |
/** Getter for the document we are associated with. |
256 |
*/ |
257 |
final StyledDocument doc () { |
258 |
Object o = docOrReference; |
259 |
if (o instanceof StyledDocument) { |
260 |
return (StyledDocument)o; |
261 |
} else { |
262 |
java.lang.ref.Reference ref = (java.lang.ref.Reference)o; |
263 |
return ref == null ? null : (StyledDocument)ref.get (); |
264 |
} |
265 |
} |
254 |
|
266 |
|
255 |
|
267 |
|
256 |
/** |
268 |
/** |
Lines 377-392
Link Here
|
377 |
// in spite of that the document is not yet fully read in |
389 |
// in spite of that the document is not yet fully read in |
378 |
|
390 |
|
379 |
kit = createEditorKit (); |
391 |
kit = createEditorKit (); |
380 |
if (doc == null) { |
392 |
StyledDocument styled = doc (); |
381 |
doc = createStyledDocument (kit); |
393 |
if (styled == null) { |
|
|
394 |
styled = createStyledDocument (kit); |
395 |
docOrReference = new java.lang.ref.WeakReference (styled); |
382 |
} |
396 |
} |
383 |
final StyledDocument docToLoad = doc; |
|
|
384 |
|
397 |
|
385 |
// The thread nume should be: "Loading document " + env; // NOI18N |
398 |
// The thread nume should be: "Loading document " + env; // NOI18N |
386 |
prepareTask = RequestProcessor.getDefault().post(new Runnable () { |
399 |
class R implements Runnable { |
387 |
|
400 |
private StyledDocument docToLoad; |
388 |
private boolean runningInAtomicLock; |
401 |
private boolean runningInAtomicLock; |
389 |
|
402 |
|
|
|
403 |
public R (StyledDocument style) { |
404 |
this.docToLoad = style; |
405 |
} |
406 |
|
390 |
public void run () { |
407 |
public void run () { |
391 |
|
408 |
|
392 |
// Run the operations under atomic lock primarily due |
409 |
// Run the operations under atomic lock primarily due |
Lines 398-405
Link Here
|
398 |
|
415 |
|
399 |
// Add undoable listener after atomic change has finished |
416 |
// Add undoable listener after atomic change has finished |
400 |
synchronized (getLock()) { |
417 |
synchronized (getLock()) { |
401 |
if (doc == docToLoad) { // if document still valid |
418 |
if (doc() == docToLoad && docToLoad != null) { // if document still valid |
402 |
doc.addUndoableEditListener(getUndoRedo()); |
419 |
docToLoad.addUndoableEditListener(getUndoRedo()); |
403 |
} |
420 |
} |
404 |
} |
421 |
} |
405 |
|
422 |
|
Lines 413-419
Link Here
|
413 |
} |
430 |
} |
414 |
|
431 |
|
415 |
// Check whether the document to be loaded was not closed |
432 |
// Check whether the document to be loaded was not closed |
416 |
if (doc != docToLoad) { |
433 |
if (doc() != docToLoad) { |
417 |
return; // do not load closed document |
434 |
return; // do not load closed document |
418 |
} |
435 |
} |
419 |
|
436 |
|
Lines 432-438
Link Here
|
432 |
|
449 |
|
433 |
updateLineSet(true); |
450 |
updateLineSet(true); |
434 |
|
451 |
|
435 |
fireDocumentChange(doc, true); |
452 |
fireDocumentChange(docToLoad, true); |
436 |
|
453 |
|
437 |
clearDocument(); |
454 |
clearDocument(); |
438 |
} |
455 |
} |
Lines 445-451
Link Here
|
445 |
// assign before fireDocumentChange() as listener should be able to access getDocument() |
462 |
// assign before fireDocumentChange() as listener should be able to access getDocument() |
446 |
documentStatus = DOCUMENT_READY; |
463 |
documentStatus = DOCUMENT_READY; |
447 |
|
464 |
|
448 |
fireDocumentChange(doc, false); |
465 |
fireDocumentChange(docToLoad, false); |
449 |
|
466 |
|
450 |
// Confirm that whole loading succeeded |
467 |
// Confirm that whole loading succeeded |
451 |
targetStatus = DOCUMENT_READY; |
468 |
targetStatus = DOCUMENT_READY; |
Lines 456-462
Link Here
|
456 |
throw t; |
473 |
throw t; |
457 |
|
474 |
|
458 |
} finally { |
475 |
} finally { |
459 |
|
476 |
docToLoad = null; |
460 |
synchronized (getLock()) { |
477 |
synchronized (getLock()) { |
461 |
documentStatus = targetStatus; |
478 |
documentStatus = targetStatus; |
462 |
|
479 |
|
Lines 466-484
Link Here
|
466 |
|
483 |
|
467 |
} |
484 |
} |
468 |
} |
485 |
} |
469 |
}); |
486 |
} |
|
|
487 |
prepareTask = RequestProcessor.getDefault().post(new R (styled)); |
470 |
|
488 |
|
471 |
return prepareTask; |
489 |
return prepareTask; |
472 |
} |
490 |
} |
473 |
|
491 |
|
474 |
/** Clears the <code>doc</code> document. Helper method. */ |
492 |
/** Clears the <code>doc</code> document. Helper method. */ |
475 |
private void clearDocument() { |
493 |
private void clearDocument() { |
476 |
NbDocument.runAtomic(doc, new Runnable() { |
494 |
final StyledDocument style = doc (); |
|
|
495 |
if (style == null) return; |
496 |
|
497 |
NbDocument.runAtomic(style, new Runnable() { |
477 |
public void run() { |
498 |
public void run() { |
478 |
try { |
499 |
try { |
479 |
doc.removeDocumentListener(getListener()); |
500 |
style.removeDocumentListener(getListener()); |
480 |
doc.remove(0, doc.getLength()); // remove all text |
501 |
style.remove(0, style.getLength()); // remove all text |
481 |
doc.addDocumentListener(getListener()); |
502 |
style.addDocumentListener(getListener()); |
482 |
} catch(BadLocationException ble) { |
503 |
} catch(BadLocationException ble) { |
483 |
ErrorManager.getDefault().notify( |
504 |
ErrorManager.getDefault().notify( |
484 |
ErrorManager.INFORMATIONAL, ble); |
505 |
ErrorManager.INFORMATIONAL, ble); |
Lines 528-534
Link Here
|
528 |
|
549 |
|
529 |
case DOCUMENT_RELOADING: // proceed to DOCUMENT_READY |
550 |
case DOCUMENT_RELOADING: // proceed to DOCUMENT_READY |
530 |
case DOCUMENT_READY: |
551 |
case DOCUMENT_READY: |
531 |
return doc; |
552 |
return doc(); |
532 |
|
553 |
|
533 |
default: // loading |
554 |
default: // loading |
534 |
try { |
555 |
try { |
Lines 563-569
Link Here
|
563 |
// (possible only via LineListener->DocumentLine..). |
584 |
// (possible only via LineListener->DocumentLine..). |
564 |
// PENDING Needs to be tried to redesign DocumentLine to avoid this. |
585 |
// PENDING Needs to be tried to redesign DocumentLine to avoid this. |
565 |
if (LOCAL_LOAD_TASK.get() != null) { |
586 |
if (LOCAL_LOAD_TASK.get() != null) { |
566 |
return doc; |
587 |
return doc(); |
567 |
} |
588 |
} |
568 |
|
589 |
|
569 |
try { |
590 |
try { |
Lines 861-867
Link Here
|
861 |
* @return <code>true</code> if document is loaded |
882 |
* @return <code>true</code> if document is loaded |
862 |
*/ |
883 |
*/ |
863 |
public boolean isDocumentLoaded() { |
884 |
public boolean isDocumentLoaded() { |
864 |
return loadTask != null; |
885 |
return loadTask != null && doc () != null; |
865 |
} |
886 |
} |
866 |
|
887 |
|
867 |
/** |
888 |
/** |
Lines 1022-1027
Link Here
|
1022 |
* by calling <tt>prepareDocument()</tt>. |
1043 |
* by calling <tt>prepareDocument()</tt>. |
1023 |
*/ |
1044 |
*/ |
1024 |
protected Task reloadDocument() { |
1045 |
protected Task reloadDocument() { |
|
|
1046 |
StyledDocument doc = doc (); |
1025 |
if (doc != null) { |
1047 |
if (doc != null) { |
1026 |
// UndoManager must be detached from document here because it will be attached in loadDocument() |
1048 |
// UndoManager must be detached from document here because it will be attached in loadDocument() |
1027 |
doc.removeUndoableEditListener (getUndoRedo ()); |
1049 |
doc.removeUndoableEditListener (getUndoRedo ()); |
Lines 1230-1236
Link Here
|
1230 |
private Runnable createUndoTask() { |
1252 |
private Runnable createUndoTask() { |
1231 |
return new Runnable() { |
1253 |
return new Runnable() { |
1232 |
public void run() { |
1254 |
public void run() { |
1233 |
StyledDocument sd = doc; |
1255 |
StyledDocument sd = doc (); |
1234 |
if(sd == null) { |
1256 |
if(sd == null) { |
1235 |
// #20883, doc can be null(!), doCloseDocument was faster. |
1257 |
// #20883, doc can be null(!), doCloseDocument was faster. |
1236 |
return; |
1258 |
return; |
Lines 1269-1275
Link Here
|
1269 |
/** Allows access to the document without any checking. |
1291 |
/** Allows access to the document without any checking. |
1270 |
*/ |
1292 |
*/ |
1271 |
final StyledDocument getDocumentHack () { |
1293 |
final StyledDocument getDocumentHack () { |
1272 |
return doc; |
1294 |
return doc (); |
1273 |
} |
1295 |
} |
1274 |
|
1296 |
|
1275 |
|
1297 |
|
Lines 1295-1300
Link Here
|
1295 |
|
1317 |
|
1296 |
Line.Set oldSet = lineSet; |
1318 |
Line.Set oldSet = lineSet; |
1297 |
|
1319 |
|
|
|
1320 |
StyledDocument doc = doc (); |
1298 |
if (doc == null || documentStatus == DOCUMENT_RELOADING) { |
1321 |
if (doc == null || documentStatus == DOCUMENT_RELOADING) { |
1299 |
lineSet = new EditorSupportLineSet.Closed(CloneableEditorSupport.this); |
1322 |
lineSet = new EditorSupportLineSet.Closed(CloneableEditorSupport.this); |
1300 |
} else { |
1323 |
} else { |
Lines 1426-1431
Link Here
|
1426 |
env ().removePropertyChangeListener(getListener()); |
1449 |
env ().removePropertyChangeListener(getListener()); |
1427 |
notifyUnmodified (); |
1450 |
notifyUnmodified (); |
1428 |
|
1451 |
|
|
|
1452 |
StyledDocument doc = doc (); |
1429 |
if (doc != null) { |
1453 |
if (doc != null) { |
1430 |
getUndoRedo().discardAllEdits(); |
1454 |
getUndoRedo().discardAllEdits(); |
1431 |
doc.removeUndoableEditListener (getUndoRedo ()); |
1455 |
doc.removeUndoableEditListener (getUndoRedo ()); |
Lines 1440-1446
Link Here
|
1440 |
} |
1464 |
} |
1441 |
|
1465 |
|
1442 |
documentStatus = DOCUMENT_NO; |
1466 |
documentStatus = DOCUMENT_NO; |
1443 |
doc = null; |
1467 |
docOrReference = null; |
1444 |
|
1468 |
|
1445 |
kit = null; |
1469 |
kit = null; |
1446 |
|
1470 |
|
Lines 1459-1465
Link Here
|
1459 |
return; // return if no document loaded |
1483 |
return; // return if no document loaded |
1460 |
} |
1484 |
} |
1461 |
|
1485 |
|
1462 |
d = doc; // used with reload dialog - should not be null |
1486 |
d = doc (); // used with reload dialog - should not be null |
|
|
1487 |
|
1488 |
if (d == null) { |
1489 |
// no document loaded |
1490 |
return; |
1491 |
} |
1463 |
} |
1492 |
} |
1464 |
|
1493 |
|
1465 |
if (!doReload && !reloadDialogOpened) { |
1494 |
if (!doReload && !reloadDialogOpened) { |
Lines 1826-1831
Link Here
|
1826 |
/** Initialization of the document. |
1855 |
/** Initialization of the document. |
1827 |
*/ |
1856 |
*/ |
1828 |
public void run () { |
1857 |
public void run () { |
|
|
1858 |
StyledDocument doc = doc (); |
1829 |
// synchronized (getLock ()) { |
1859 |
// synchronized (getLock ()) { |
1830 |
/* Remove existing listener before running the loading task |
1860 |
/* Remove existing listener before running the loading task |
1831 |
* This should prevent firing of insertUpdate() during load (or reload) |
1861 |
* This should prevent firing of insertUpdate() during load (or reload) |