This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 38218
Collapse All | Expand All

(-)src/org/openide/text/CloneableEditorSupport.java (-24 / +54 lines)
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)
(-)src/org/openide/text/DocumentLine.java (-5 / +5 lines)
Lines 805-811 Link Here
805
        }
805
        }
806
806
807
        Set (StyledDocument doc, CloneableEditorSupport support) {
807
        Set (StyledDocument doc, CloneableEditorSupport support) {
808
            listener = new LineListener (doc, support);
808
            listener = new LineListener (support);
809
        }
809
        }
810
810
811
        
811
        
Lines 822-828 Link Here
822
                // revalidate all parts attached to this line
822
                // revalidate all parts attached to this line
823
                // that they are still part of the line
823
                // that they are still part of the line
824
                if(line instanceof DocumentLine) {
824
                if(line instanceof DocumentLine) {
825
                    ((DocumentLine)line).notifyChange(p0, this, listener.doc);
825
                    ((DocumentLine)line).notifyChange(p0, this, listener.support.doc ());
826
                }
826
                }
827
            }
827
            }
828
        }
828
        }
Lines 890-896 Link Here
890
        */
890
        */
891
        public Line getOriginal (int line) throws IndexOutOfBoundsException {
891
        public Line getOriginal (int line) throws IndexOutOfBoundsException {
892
            int newLine = listener.getLine (line);
892
            int newLine = listener.getLine (line);
893
            int offset = NbDocument.findLineOffset (listener.doc, newLine);
893
            int offset = NbDocument.findLineOffset (listener.support.doc (), newLine);
894
894
895
            return safelyRegisterLine(createLine(offset));
895
            return safelyRegisterLine(createLine(offset));
896
        }
896
        }
Lines 901-907 Link Here
901
        * @exception IndexOutOfBoundsException if <code>line</code> is invalid.
901
        * @exception IndexOutOfBoundsException if <code>line</code> is invalid.
902
        */
902
        */
903
        public Line getCurrent (int line) throws IndexOutOfBoundsException {
903
        public Line getCurrent (int line) throws IndexOutOfBoundsException {
904
            int offset = NbDocument.findLineOffset (listener.doc, line);
904
            int offset = NbDocument.findLineOffset (listener.support.doc (), line);
905
905
906
            return safelyRegisterLine(createLine(offset));
906
            return safelyRegisterLine(createLine(offset));
907
        }
907
        }
Lines 929-935 Link Here
929
                }
929
                }
930
            }
930
            }
931
            DocumentRenderer renderer = new DocumentRenderer ();
931
            DocumentRenderer renderer = new DocumentRenderer ();
932
            listener.doc.render (renderer);
932
            listener.support.doc ().render (renderer);
933
            return renderer.result;
933
            return renderer.result;
934
        }
934
        }
935
    }
935
    }
(-)src/org/openide/text/LineListener.java (-4 / +5 lines)
Lines 24-31 Link Here
24
    implements javax.swing.event.DocumentListener {
24
    implements javax.swing.event.DocumentListener {
25
    /** original count of lines */
25
    /** original count of lines */
26
    private int orig;
26
    private int orig;
27
    /** document to work with */
28
    public final StyledDocument doc;
29
    /** root element of all lines */
27
    /** root element of all lines */
30
    private Element root;
28
    private Element root;
31
    /** last tested amount of lines */
29
    /** last tested amount of lines */
Lines 38-45 Link Here
38
    CloneableEditorSupport support;
36
    CloneableEditorSupport support;
39
    
37
    
40
    /** Creates new LineListener */
38
    /** Creates new LineListener */
41
    public LineListener (StyledDocument doc, CloneableEditorSupport support) {
39
    public LineListener (CloneableEditorSupport support) {
42
        this.doc = doc;
40
        StyledDocument doc = support.doc ();
41
        
43
        this.struct = new LineStruct ();
42
        this.struct = new LineStruct ();
44
        root = NbDocument.findLineRootElement (doc);
43
        root = NbDocument.findLineRootElement (doc);
45
        orig = lines = root.getElementCount ();
44
        orig = lines = root.getElementCount ();
Lines 63-68 Link Here
63
        int delta = lines - elem;
62
        int delta = lines - elem;
64
        lines = elem;
63
        lines = elem;
65
64
65
        StyledDocument doc = support.doc ();
66
        int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ());
66
        int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ());
67
        
67
        
68
        if (delta > 0) {
68
        if (delta > 0) {
Lines 95-100 Link Here
95
        int delta = elem - lines;
95
        int delta = elem - lines;
96
        lines = elem;
96
        lines = elem;
97
97
98
        StyledDocument doc = support.doc ();
98
        int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ());
99
        int lineNumber = NbDocument.findLineNumber (doc, p0.getOffset ());
99
        
100
        
100
        if (delta > 0) {
101
        if (delta > 0) {
(-)src/org/openide/text/PositionRef.java (-14 / +9 lines)
Lines 192-200 Link Here
192
        /** support for the editor */
192
        /** support for the editor */
193
        transient private CloneableEditorSupport support;
193
        transient private CloneableEditorSupport support;
194
194
195
        /** the document for this manager or null if the manager is not in memory */
196
        transient private StyledDocument doc;
197
198
        static final long serialVersionUID =-4374030124265110801L;
195
        static final long serialVersionUID =-4374030124265110801L;
199
        /** Creates new manager
196
        /** Creates new manager
200
        * @param supp support to work with
197
        * @param supp support to work with
Lines 254-261 Link Here
254
        /** Converts all positions into document one.
251
        /** Converts all positions into document one.
255
        */
252
        */
256
        void documentOpened (StyledDocument doc) {
253
        void documentOpened (StyledDocument doc) {
257
            this.doc = doc;
258
259
            processPositions(true);
254
            processPositions(true);
260
        }
255
        }
261
256
Lines 264-271 Link Here
264
        */
259
        */
265
        void documentClosed () {
260
        void documentClosed () {
266
            processPositions(false);
261
            processPositions(false);
267
268
            doc = null;
269
        }
262
        }
270
        
263
        
271
        /** Puts/gets positions to/from memory. It also provides full
264
        /** Puts/gets positions to/from memory. It also provides full
Lines 502-508 Link Here
502
                        "Illegal PositionKind: " + pos + "[offset=" // NOI18N
495
                        "Illegal PositionKind: " + pos + "[offset=" // NOI18N
503
                        + offset + ",line=" // NOI18N
496
                        + offset + ",line=" // NOI18N
504
                        + line + ",column=" + column + "] in " // NOI18N
497
                        + line + ",column=" + column + "] in " // NOI18N
505
                        + doc + " used by " + support + "." // NOI18N
498
                        + support.doc() + " used by " + support + "." // NOI18N
506
                    );
499
                    );
507
                }
500
                }
508
                
501
                
Lines 555-561 Link Here
555
                        "Illegal OutKind[offset=" // NOI18N
548
                        "Illegal OutKind[offset=" // NOI18N
556
                        + offset + ",line=" // NOI18N
549
                        + offset + ",line=" // NOI18N
557
                        + line + ",column=" + column + "] in " // NOI18N
550
                        + line + ",column=" + column + "] in " // NOI18N
558
                        + doc + " used by " + support + "." // NOI18N
551
                        + support.doc () + " used by " + support + "." // NOI18N
559
                    );
552
                    );
560
                }
553
                }
561
                
554
                
Lines 594-600 Link Here
594
                        "Illegal OutKind[offset=" // NOI18N
587
                        "Illegal OutKind[offset=" // NOI18N
595
                        + offset + ",line=" // NOI18N
588
                        + offset + ",line=" // NOI18N
596
                        + line + ",column=" + column + "] in " // NOI18N
589
                        + line + ",column=" + column + "] in " // NOI18N
597
                        + doc + " used by " + support + "." // NOI18N
590
                        + support.doc () + " used by " + support + "." // NOI18N
598
                    );
591
                    );
599
                }
592
                }
600
                
593
                
Lines 616-622 Link Here
616
                if(offset < 0) {
609
                if(offset < 0) {
617
                    throw new IndexOutOfBoundsException(
610
                    throw new IndexOutOfBoundsException(
618
                        "Illegal OffsetKind[offset=" // NOI18N
611
                        "Illegal OffsetKind[offset=" // NOI18N
619
                        + offset + "] in " + doc + " used by " // NOI18N
612
                        + offset + "] in " + support.doc () + " used by " // NOI18N
620
                        + support + "." // NOI18N
613
                        + support + "." // NOI18N
621
                    );
614
                    );
622
                }
615
                }
Lines 650-656 Link Here
650
                if(offset < 0) {
643
                if(offset < 0) {
651
                    throw new IOException(
644
                    throw new IOException(
652
                        "Illegal OffsetKind[offset=" // NOI18N
645
                        "Illegal OffsetKind[offset=" // NOI18N
653
                        + offset + "] in " + doc + " used by " // NOI18N
646
                        + offset + "] in " + support.doc () + " used by " // NOI18N
654
                        + support + "." // NOI18N
647
                        + support + "." // NOI18N
655
                    );
648
                    );
656
                }
649
                }
Lines 675-681 Link Here
675
                    throw new IndexOutOfBoundsException(
668
                    throw new IndexOutOfBoundsException(
676
                        "Illegal LineKind[line=" // NOI18N
669
                        "Illegal LineKind[line=" // NOI18N
677
                        + line + ",column=" + column + "] in " // NOI18N
670
                        + line + ",column=" + column + "] in " // NOI18N
678
                        + doc + " used by " + support + "." // NOI18N
671
                        + support.doc () + " used by " + support + "." // NOI18N
679
                    );
672
                    );
680
                }
673
                }
681
                
674
                
Lines 731-737 Link Here
731
                    throw new IOException(
724
                    throw new IOException(
732
                        "Illegal LineKind[line=" // NOI18N
725
                        "Illegal LineKind[line=" // NOI18N
733
                        + line + ",column=" + column + "] in " // NOI18N
726
                        + line + ",column=" + column + "] in " // NOI18N
734
                        + doc + " used by " + support + "." // NOI18N
727
                        + support.doc () + " used by " + support + "." // NOI18N
735
                    );
728
                    );
736
                }
729
                }
737
                
730
                
Lines 856-861 Link Here
856
            }
849
            }
857
            
850
            
858
            void render() {
851
            void render() {
852
                StyledDocument doc = support.doc ();
859
                if (doc != null) {
853
                if (doc != null) {
860
                    doc.render(this);
854
                    doc.render(this);
861
                } else {
855
                } else {
Lines 902-907 Link Here
902
            }
896
            }
903
            
897
            
904
            public void run() {
898
            public void run() {
899
                StyledDocument doc = support.doc ();
905
                try {
900
                try {
906
                    switch (opCode) {
901
                    switch (opCode) {
907
                        case KIND_TO_MEMORY: {
902
                        case KIND_TO_MEMORY: {
(-)test/unit/src/org/openide/text/CloneableEditorSupportTest.java (+13 lines)
Lines 166-171 Link Here
166
        assertGC ("Document can dissapear", ref);
166
        assertGC ("Document can dissapear", ref);
167
    }
167
    }
168
168
169
    public void testDocumentBeGarbageCollectedWhenNotModifiedButOpened () throws Exception {
170
        content = "Ahoj\nMyDoc";
171
        javax.swing.text.Document doc = support.openDocument ();
172
        assertNotNull (doc);
173
        
174
        java.lang.ref.WeakReference ref = new java.lang.ref.WeakReference (doc);
175
        doc = null;
176
        
177
        assertGC ("Document can dissapear", ref);
178
179
        assertFalse ("Document is not loaded", support.isDocumentLoaded ());
180
        assertTrue ("Can be closed without problems", support.close ());
181
    }
169
    
182
    
170
    private void compareStreamWithString(InputStream is, String s) throws Exception{
183
    private void compareStreamWithString(InputStream is, String s) throws Exception{
171
        int i;
184
        int i;

Return to bug 38218