Lines 106-114
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 DocReference docRef; |
110 |
|
|
|
111 |
|
112 |
|
110 |
|
113 |
/** Non default MIME type used to editing */ |
111 |
/** Non default MIME type used to editing */ |
114 |
private String mimeType; |
112 |
private String mimeType; |
Lines 251-256
Link Here
|
251 |
final EditorKit kit () { |
249 |
final EditorKit kit () { |
252 |
return kit; |
250 |
return kit; |
253 |
} |
251 |
} |
|
|
252 |
|
253 |
/** Getter for the document we are associated with. |
254 |
*/ |
255 |
final StyledDocument doc () { |
256 |
return doc (true); |
257 |
} |
258 |
/** Getter for the document we are associated with. |
259 |
* @param wait whether to wait for the result or not |
260 |
*/ |
261 |
final StyledDocument doc (boolean wait) { |
262 |
Object o = docRef; |
263 |
if (o instanceof StyledDocument) { |
264 |
return (StyledDocument)o; |
265 |
} else { |
266 |
DocReference ref = (DocReference)o; |
267 |
return ref == null ? null : (StyledDocument)ref.get (wait); |
268 |
} |
269 |
} |
254 |
|
270 |
|
255 |
|
271 |
|
256 |
/** |
272 |
/** |
Lines 377-392
Link Here
|
377 |
// in spite of that the document is not yet fully read in |
393 |
// in spite of that the document is not yet fully read in |
378 |
|
394 |
|
379 |
kit = createEditorKit (); |
395 |
kit = createEditorKit (); |
380 |
if (doc == null) { |
396 |
StyledDocument styled = doc (); |
381 |
doc = createStyledDocument (kit); |
397 |
if (styled == null) { |
|
|
398 |
styled = createStyledDocument (kit); |
399 |
docRef = new DocReference (styled); |
382 |
} |
400 |
} |
383 |
final StyledDocument docToLoad = doc; |
|
|
384 |
|
401 |
|
385 |
// The thread nume should be: "Loading document " + env; // NOI18N |
402 |
// The thread nume should be: "Loading document " + env; // NOI18N |
386 |
prepareTask = RequestProcessor.getDefault().post(new Runnable () { |
403 |
class R implements Runnable { |
387 |
|
404 |
private StyledDocument docToLoad; |
388 |
private boolean runningInAtomicLock; |
405 |
private boolean runningInAtomicLock; |
389 |
|
406 |
|
|
|
407 |
public R (StyledDocument style) { |
408 |
this.docToLoad = style; |
409 |
} |
410 |
|
390 |
public void run () { |
411 |
public void run () { |
391 |
|
412 |
|
392 |
// Run the operations under atomic lock primarily due |
413 |
// Run the operations under atomic lock primarily due |
Lines 398-405
Link Here
|
398 |
|
419 |
|
399 |
// Add undoable listener after atomic change has finished |
420 |
// Add undoable listener after atomic change has finished |
400 |
synchronized (getLock()) { |
421 |
synchronized (getLock()) { |
401 |
if (doc == docToLoad) { // if document still valid |
422 |
if (doc() == docToLoad && docToLoad != null) { // if document still valid |
402 |
doc.addUndoableEditListener(getUndoRedo()); |
423 |
docToLoad.addUndoableEditListener(getUndoRedo()); |
403 |
} |
424 |
} |
404 |
} |
425 |
} |
405 |
|
426 |
|
Lines 413-419
Link Here
|
413 |
} |
434 |
} |
414 |
|
435 |
|
415 |
// Check whether the document to be loaded was not closed |
436 |
// Check whether the document to be loaded was not closed |
416 |
if (doc != docToLoad) { |
437 |
if (doc() != docToLoad) { |
417 |
return; // do not load closed document |
438 |
return; // do not load closed document |
418 |
} |
439 |
} |
419 |
|
440 |
|
Lines 432-438
Link Here
|
432 |
|
453 |
|
433 |
updateLineSet(true); |
454 |
updateLineSet(true); |
434 |
|
455 |
|
435 |
fireDocumentChange(doc, true); |
456 |
fireDocumentChange(docToLoad, true); |
436 |
|
457 |
|
437 |
clearDocument(); |
458 |
clearDocument(); |
438 |
} |
459 |
} |
Lines 445-451
Link Here
|
445 |
// assign before fireDocumentChange() as listener should be able to access getDocument() |
466 |
// assign before fireDocumentChange() as listener should be able to access getDocument() |
446 |
documentStatus = DOCUMENT_READY; |
467 |
documentStatus = DOCUMENT_READY; |
447 |
|
468 |
|
448 |
fireDocumentChange(doc, false); |
469 |
fireDocumentChange(docToLoad, false); |
449 |
|
470 |
|
450 |
// Confirm that whole loading succeeded |
471 |
// Confirm that whole loading succeeded |
451 |
targetStatus = DOCUMENT_READY; |
472 |
targetStatus = DOCUMENT_READY; |
Lines 456-462
Link Here
|
456 |
throw t; |
477 |
throw t; |
457 |
|
478 |
|
458 |
} finally { |
479 |
} finally { |
459 |
|
480 |
docToLoad = null; |
460 |
synchronized (getLock()) { |
481 |
synchronized (getLock()) { |
461 |
documentStatus = targetStatus; |
482 |
documentStatus = targetStatus; |
462 |
|
483 |
|
Lines 466-484
Link Here
|
466 |
|
487 |
|
467 |
} |
488 |
} |
468 |
} |
489 |
} |
469 |
}); |
490 |
} |
|
|
491 |
prepareTask = RequestProcessor.getDefault().post(new R (styled)); |
470 |
|
492 |
|
471 |
return prepareTask; |
493 |
return prepareTask; |
472 |
} |
494 |
} |
473 |
|
495 |
|
474 |
/** Clears the <code>doc</code> document. Helper method. */ |
496 |
/** Clears the <code>doc</code> document. Helper method. */ |
475 |
private void clearDocument() { |
497 |
private void clearDocument() { |
476 |
NbDocument.runAtomic(doc, new Runnable() { |
498 |
final StyledDocument style = doc (); |
|
|
499 |
if (style == null) return; |
500 |
|
501 |
NbDocument.runAtomic(style, new Runnable() { |
477 |
public void run() { |
502 |
public void run() { |
478 |
try { |
503 |
try { |
479 |
doc.removeDocumentListener(getListener()); |
504 |
style.removeDocumentListener(getListener()); |
480 |
doc.remove(0, doc.getLength()); // remove all text |
505 |
style.remove(0, style.getLength()); // remove all text |
481 |
doc.addDocumentListener(getListener()); |
506 |
style.addDocumentListener(getListener()); |
482 |
} catch(BadLocationException ble) { |
507 |
} catch(BadLocationException ble) { |
483 |
ErrorManager.getDefault().notify( |
508 |
ErrorManager.getDefault().notify( |
484 |
ErrorManager.INFORMATIONAL, ble); |
509 |
ErrorManager.INFORMATIONAL, ble); |
Lines 528-534
Link Here
|
528 |
|
553 |
|
529 |
case DOCUMENT_RELOADING: // proceed to DOCUMENT_READY |
554 |
case DOCUMENT_RELOADING: // proceed to DOCUMENT_READY |
530 |
case DOCUMENT_READY: |
555 |
case DOCUMENT_READY: |
531 |
return doc; |
556 |
return doc(); |
532 |
|
557 |
|
533 |
default: // loading |
558 |
default: // loading |
534 |
try { |
559 |
try { |
Lines 563-569
Link Here
|
563 |
// (possible only via LineListener->DocumentLine..). |
588 |
// (possible only via LineListener->DocumentLine..). |
564 |
// PENDING Needs to be tried to redesign DocumentLine to avoid this. |
589 |
// PENDING Needs to be tried to redesign DocumentLine to avoid this. |
565 |
if (LOCAL_LOAD_TASK.get() != null) { |
590 |
if (LOCAL_LOAD_TASK.get() != null) { |
566 |
return doc; |
591 |
return doc(); |
567 |
} |
592 |
} |
568 |
|
593 |
|
569 |
try { |
594 |
try { |
Lines 634-640
Link Here
|
634 |
// remember time of last save |
659 |
// remember time of last save |
635 |
lastSaveTime = System.currentTimeMillis(); |
660 |
lastSaveTime = System.currentTimeMillis(); |
636 |
|
661 |
|
637 |
notifyUnmodified (); |
662 |
doNotifyUnmodified (); |
638 |
|
663 |
|
639 |
} catch (BadLocationException ex) { |
664 |
} catch (BadLocationException ex) { |
640 |
ErrorManager.getDefault().notify(ex); |
665 |
ErrorManager.getDefault().notify(ex); |
Lines 861-866
Link Here
|
861 |
* @return <code>true</code> if document is loaded |
886 |
* @return <code>true</code> if document is loaded |
862 |
*/ |
887 |
*/ |
863 |
public boolean isDocumentLoaded() { |
888 |
public boolean isDocumentLoaded() { |
|
|
889 |
doc (); |
864 |
return loadTask != null; |
890 |
return loadTask != null; |
865 |
} |
891 |
} |
866 |
|
892 |
|
Lines 1022-1027
Link Here
|
1022 |
* by calling <tt>prepareDocument()</tt>. |
1048 |
* by calling <tt>prepareDocument()</tt>. |
1023 |
*/ |
1049 |
*/ |
1024 |
protected Task reloadDocument() { |
1050 |
protected Task reloadDocument() { |
|
|
1051 |
StyledDocument doc = doc (); |
1025 |
if (doc != null) { |
1052 |
if (doc != null) { |
1026 |
// UndoManager must be detached from document here because it will be attached in loadDocument() |
1053 |
// UndoManager must be detached from document here because it will be attached in loadDocument() |
1027 |
doc.removeUndoableEditListener (getUndoRedo ()); |
1054 |
doc.removeUndoableEditListener (getUndoRedo ()); |
Lines 1069-1075
Link Here
|
1069 |
) |
1096 |
) |
1070 |
); |
1097 |
); |
1071 |
|
1098 |
|
1072 |
notifyUnmodified (); |
1099 |
doNotifyUnmodified (); |
1073 |
updateLineSet(true); |
1100 |
updateLineSet(true); |
1074 |
} |
1101 |
} |
1075 |
}); |
1102 |
}); |
Lines 1134-1139
Link Here
|
1134 |
return sd; |
1161 |
return sd; |
1135 |
} |
1162 |
} |
1136 |
|
1163 |
|
|
|
1164 |
// |
1165 |
// Ensuring some behaviour is done when modified/unmodified |
1166 |
// |
1167 |
|
1168 |
private void doNotifyModified () { |
1169 |
notifyModified (); |
1170 |
docRef.hold (true); |
1171 |
} |
1172 |
|
1173 |
private void doNotifyUnmodified () { |
1174 |
notifyUnmodified (); |
1175 |
docRef.hold (false); |
1176 |
} |
1177 |
|
1178 |
|
1137 |
/** Notification method called when the document become unmodified. |
1179 |
/** Notification method called when the document become unmodified. |
1138 |
* Called after save or after reload of document. |
1180 |
* Called after save or after reload of document. |
1139 |
* <P> |
1181 |
* <P> |
Lines 1230-1236
Link Here
|
1230 |
private Runnable createUndoTask() { |
1272 |
private Runnable createUndoTask() { |
1231 |
return new Runnable() { |
1273 |
return new Runnable() { |
1232 |
public void run() { |
1274 |
public void run() { |
1233 |
StyledDocument sd = doc; |
1275 |
StyledDocument sd = doc (); |
1234 |
if(sd == null) { |
1276 |
if(sd == null) { |
1235 |
// #20883, doc can be null(!), doCloseDocument was faster. |
1277 |
// #20883, doc can be null(!), doCloseDocument was faster. |
1236 |
return; |
1278 |
return; |
Lines 1269-1275
Link Here
|
1269 |
/** Allows access to the document without any checking. |
1311 |
/** Allows access to the document without any checking. |
1270 |
*/ |
1312 |
*/ |
1271 |
final StyledDocument getDocumentHack () { |
1313 |
final StyledDocument getDocumentHack () { |
1272 |
return doc; |
1314 |
return doc (); |
1273 |
} |
1315 |
} |
1274 |
|
1316 |
|
1275 |
|
1317 |
|
Lines 1295-1300
Link Here
|
1295 |
|
1337 |
|
1296 |
Line.Set oldSet = lineSet; |
1338 |
Line.Set oldSet = lineSet; |
1297 |
|
1339 |
|
|
|
1340 |
StyledDocument doc = doc (); |
1298 |
if (doc == null || documentStatus == DOCUMENT_RELOADING) { |
1341 |
if (doc == null || documentStatus == DOCUMENT_RELOADING) { |
1299 |
lineSet = new EditorSupportLineSet.Closed(CloneableEditorSupport.this); |
1342 |
lineSet = new EditorSupportLineSet.Closed(CloneableEditorSupport.this); |
1300 |
} else { |
1343 |
} else { |
Lines 1424-1438
Link Here
|
1424 |
|
1467 |
|
1425 |
// notifies the support that |
1468 |
// notifies the support that |
1426 |
env ().removePropertyChangeListener(getListener()); |
1469 |
env ().removePropertyChangeListener(getListener()); |
1427 |
notifyUnmodified (); |
1470 |
doNotifyUnmodified (); |
1428 |
|
1471 |
|
|
|
1472 |
StyledDocument doc = doc (false); |
1429 |
if (doc != null) { |
1473 |
if (doc != null) { |
1430 |
getUndoRedo().discardAllEdits(); |
1474 |
getUndoRedo().discardAllEdits(); |
1431 |
doc.removeUndoableEditListener (getUndoRedo ()); |
1475 |
doc.removeUndoableEditListener (getUndoRedo ()); |
1432 |
doc.removeDocumentListener(getListener()); |
1476 |
doc.removeDocumentListener(getListener()); |
1433 |
} |
1477 |
} |
1434 |
|
1478 |
|
1435 |
if (positionManager != null) { |
1479 |
if (positionManager != null && doc != null) { |
1436 |
positionManager.documentClosed (); |
1480 |
positionManager.documentClosed (); |
1437 |
|
1481 |
|
1438 |
documentStatus = DOCUMENT_NO; |
1482 |
documentStatus = DOCUMENT_NO; |
Lines 1440-1446
Link Here
|
1440 |
} |
1484 |
} |
1441 |
|
1485 |
|
1442 |
documentStatus = DOCUMENT_NO; |
1486 |
documentStatus = DOCUMENT_NO; |
1443 |
doc = null; |
1487 |
docRef = null; |
1444 |
|
1488 |
|
1445 |
kit = null; |
1489 |
kit = null; |
1446 |
|
1490 |
|
Lines 1459-1465
Link Here
|
1459 |
return; // return if no document loaded |
1503 |
return; // return if no document loaded |
1460 |
} |
1504 |
} |
1461 |
|
1505 |
|
1462 |
d = doc; // used with reload dialog - should not be null |
1506 |
d = doc (); // used with reload dialog - should not be null |
|
|
1507 |
|
1508 |
if (d == null) { |
1509 |
// no document loaded |
1510 |
return; |
1511 |
} |
1463 |
} |
1512 |
} |
1464 |
|
1513 |
|
1465 |
if (!doReload && !reloadDialogOpened) { |
1514 |
if (!doReload && !reloadDialogOpened) { |
Lines 1785-1798
Link Here
|
1785 |
* @param ev event describing the action |
1834 |
* @param ev event describing the action |
1786 |
*/ |
1835 |
*/ |
1787 |
public void insertUpdate(DocumentEvent ev) { |
1836 |
public void insertUpdate(DocumentEvent ev) { |
1788 |
notifyModified (); |
1837 |
doNotifyModified (); |
1789 |
} |
1838 |
} |
1790 |
|
1839 |
|
1791 |
/** Gives notification that a portion of the document has been removed. |
1840 |
/** Gives notification that a portion of the document has been removed. |
1792 |
* @param ev event describing the action |
1841 |
* @param ev event describing the action |
1793 |
*/ |
1842 |
*/ |
1794 |
public void removeUpdate(DocumentEvent ev) { |
1843 |
public void removeUpdate(DocumentEvent ev) { |
1795 |
notifyModified (); |
1844 |
doNotifyModified (); |
1796 |
} |
1845 |
} |
1797 |
|
1846 |
|
1798 |
/** Listener to changes in the Env. |
1847 |
/** Listener to changes in the Env. |
Lines 1826-1831
Link Here
|
1826 |
/** Initialization of the document. |
1875 |
/** Initialization of the document. |
1827 |
*/ |
1876 |
*/ |
1828 |
public void run () { |
1877 |
public void run () { |
|
|
1878 |
StyledDocument doc = doc (); |
1829 |
// synchronized (getLock ()) { |
1879 |
// synchronized (getLock ()) { |
1830 |
/* Remove existing listener before running the loading task |
1880 |
/* Remove existing listener before running the loading task |
1831 |
* This should prevent firing of insertUpdate() during load (or reload) |
1881 |
* This should prevent firing of insertUpdate() during load (or reload) |
Lines 2029-2035
Link Here
|
2029 |
super.redo(); |
2079 |
super.redo(); |
2030 |
|
2080 |
|
2031 |
if (saveTime == lastSaveTime) { |
2081 |
if (saveTime == lastSaveTime) { |
2032 |
notifyUnmodified(); |
2082 |
doNotifyUnmodified(); |
2033 |
} |
2083 |
} |
2034 |
} |
2084 |
} |
2035 |
|
2085 |
|
Lines 2064-2072
Link Here
|
2064 |
super.undo(); |
2114 |
super.undo(); |
2065 |
|
2115 |
|
2066 |
if (saveTime == lastSaveTime) { |
2116 |
if (saveTime == lastSaveTime) { |
2067 |
notifyUnmodified(); |
2117 |
doNotifyUnmodified(); |
2068 |
} |
2118 |
} |
2069 |
} |
2119 |
} |
2070 |
|
2120 |
|
|
|
2121 |
} |
2122 |
|
2123 |
/** Special reference holding the document. |
2124 |
*/ |
2125 |
private final class DocReference extends java.lang.ref.SoftReference |
2126 |
implements Runnable { |
2127 |
/** hard reference to the document if marked as modified */ |
2128 |
private StyledDocument hard; |
2129 |
/** has finalization been run */ |
2130 |
private boolean finalized; |
2131 |
|
2132 |
public DocReference (StyledDocument doc) { |
2133 |
super (doc, org.openide.util.Utilities.activeReferenceQueue ()); |
2134 |
} |
2135 |
|
2136 |
/** Called to notify the reference that it should hold or not hard |
2137 |
* reference to the document. |
2138 |
* @param hard true if hard link should be hold |
2139 |
*/ |
2140 |
public void hold (boolean hard) { |
2141 |
if (hard) { |
2142 |
this.hard = (StyledDocument)super.get (); |
2143 |
} else { |
2144 |
this.hard = null; |
2145 |
} |
2146 |
} |
2147 |
|
2148 |
public void run () { |
2149 |
closeDocument (); |
2150 |
synchronized (this) { |
2151 |
finalized = true; |
2152 |
notifyAll (); |
2153 |
} |
2154 |
} |
2155 |
|
2156 |
public Object get () { |
2157 |
throw new IllegalStateException ("Not implemented exception"); // NOI18N |
2158 |
} |
2159 |
|
2160 |
/** Gets the document. |
2161 |
* @param wait true whether to wait for finalization to finish or not |
2162 |
*/ |
2163 |
public Object get (boolean wait) { |
2164 |
for (;;) { |
2165 |
Object ret = super.get (); |
2166 |
if (ret != null || !wait) { |
2167 |
return ret; |
2168 |
} |
2169 |
synchronized (this) { |
2170 |
if (!finalized) { |
2171 |
try { |
2172 |
wait (); |
2173 |
} catch (InterruptedException ex) { |
2174 |
// go on and try once more |
2175 |
} |
2176 |
} else { |
2177 |
return null; |
2178 |
} |
2179 |
} |
2180 |
} |
2181 |
} |
2182 |
|
2071 |
} |
2183 |
} |
2072 |
} |
2184 |
} |