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 42638
Collapse All | Expand All

(-)manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.loaders
2
OpenIDE-Module: org.openide.loaders
3
OpenIDE-Module-Specification-Version: 6.1
3
OpenIDE-Module-Specification-Version: 6.2
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
5
5
(-)api/apichanges.xml (+18 lines)
Lines 83-88 Link Here
83
<!-- ACTUAL CHANGES BEGIN HERE: -->
83
<!-- ACTUAL CHANGES BEGIN HERE: -->
84
84
85
  <changes>
85
  <changes>
86
    <change id="FileEncodingQuery">
87
        <api name="loaders"/>
88
        <summary>DataEditorSupport uses FileEncodingQuery to read or write document in correct encoding</summary>
89
        <version major="6" minor="2"/>
90
        <date day="14" month="3" year="2007"/>
91
        <author login="tzezula"/>
92
        <compatibility semantic="compatible" />
93
        <description>
94
        <p>
95
        The DataEditorSupport overides the loadFromStreamToKit and saveFromKitToStream methods.
96
        In these methods it uses FileEncodingQuery to find out the encoding of the file, creates
97
        the Reader or Writer with obtained encoding and calls EditorKit.read or EditorKit.write.
98
        </p>
99
        </description>
100
        <class package="org.openide.text" name="DataEditorSupport"/>
101
        <issue number="42638"/>
102
    </change>
103
86
     <change id="scripting">
104
     <change id="scripting">
87
        <api name="loaders"/>
105
        <api name="loaders"/>
88
        <summary>Integration with scripting and templating</summary>
106
        <summary>Integration with scripting and templating</summary>
(-)nbproject/project.xml (+9 lines)
Lines 33-38 Link Here
33
                    </run-dependency>
33
                    </run-dependency>
34
                </dependency>
34
                </dependency>
35
                <dependency>
35
                <dependency>
36
                    <code-name-base>org.netbeans.modules.queries</code-name-base>
37
                    <build-prerequisite/>
38
                    <compile-dependency/>
39
                    <run-dependency>
40
                        <release-version>1</release-version>
41
                        <specification-version>1.9</specification-version>
42
                    </run-dependency>
43
                </dependency>
44
                <dependency>
36
                    <code-name-base>org.openide.actions</code-name-base>
45
                    <code-name-base>org.openide.actions</code-name-base>
37
                    <build-prerequisite/>
46
                    <build-prerequisite/>
38
                    <compile-dependency/>
47
                    <compile-dependency/>
(-)src/org/openide/text/DataEditorSupport.java (+30 lines)
Lines 23-32 Link Here
23
import java.beans.*;
23
import java.beans.*;
24
import java.io.*;
24
import java.io.*;
25
import java.lang.ref.Reference;
25
import java.lang.ref.Reference;
26
import java.nio.charset.Charset;
26
import java.util.*;
27
import java.util.*;
27
import java.util.logging.Level;
28
import java.util.logging.Level;
28
import java.util.logging.Logger;
29
import java.util.logging.Logger;
29
import javax.swing.text.*;
30
import javax.swing.text.*;
31
import org.netbeans.api.queries.FileEncodingQuery;
30
import org.netbeans.modules.openide.loaders.UIException;
32
import org.netbeans.modules.openide.loaders.UIException;
31
import org.openide.*;
33
import org.openide.*;
32
import org.openide.filesystems.*;
34
import org.openide.filesystems.*;
Lines 272-277 Link Here
272
        
274
        
273
        return super.canClose();
275
        return super.canClose();
274
    }
276
    }
277
    
278
    /**
279
     * @inheritDoc
280
     */
281
    @Override
282
    protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException {
283
        final Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
284
        final Reader r = new InputStreamReader (stream, c);
285
        try {
286
            kit.read(r, doc, 0);
287
        } finally {
288
            r.close();
289
        }
290
    }
291
292
    /**
293
     * @inheritDoc
294
     */
295
    @Override
296
    protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException {
297
        final Charset c = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile());
298
        final Writer w = new OutputStreamWriter (stream, c);
299
        try {
300
            kit.write(w, doc, 0, doc.getLength());
301
        } finally {
302
            w.close();
303
        }
304
    }        
275
305
276
    /** Saves document. Overrides superclass method, adds checking
306
    /** Saves document. Overrides superclass method, adds checking
277
     * for read-only property of saving file and warns user in that case. */
307
     * for read-only property of saving file and warns user in that case. */
(-)test/unit/src/org/openide/text/DataEditorSupportTest.java (+45 lines)
Lines 25-30 Link Here
25
import java.io.IOException;
25
import java.io.IOException;
26
import java.io.OutputStream;
26
import java.io.OutputStream;
27
import java.lang.reflect.Method;
27
import java.lang.reflect.Method;
28
import java.nio.charset.Charset;
28
import java.util.Collections;
29
import java.util.Collections;
29
import java.util.HashSet;
30
import java.util.HashSet;
30
import java.util.Set;
31
import java.util.Set;
Lines 38-43 Link Here
38
39
39
import org.netbeans.junit.NbTestCase;
40
import org.netbeans.junit.NbTestCase;
40
import org.netbeans.junit.NbTestSuite;
41
import org.netbeans.junit.NbTestSuite;
42
import org.netbeans.spi.queries.FileEncodingQueryImplementation;
41
import org.openide.actions.*;
43
import org.openide.actions.*;
42
import org.openide.cookies.CloseCookie;
44
import org.openide.cookies.CloseCookie;
43
import org.openide.cookies.EditCookie;
45
import org.openide.cookies.EditCookie;
Lines 228-233 Link Here
228
        env.fileLock.releaseLock();
230
        env.fileLock.releaseLock();
229
    }
231
    }
230
    
232
    
233
    public void testFileEncodingQuery () throws Exception {
234
        DES des = support();
235
        FileEncodingQueryImpl.getDefault().reset();
236
        StyledDocument doc = des.openDocument();
237
        assertEquals(des.getDataObject().getPrimaryFile(),FileEncodingQueryImpl.getDefault().getFile());
238
        FileEncodingQueryImpl.getDefault().reset();
239
        doc.insertString(doc.getLength(), " Added text.", null);
240
        des.saveDocument();        
241
        assertEquals(des.getDataObject().getPrimaryFile(),FileEncodingQueryImpl.getDefault().getFile());
242
    }
243
    
231
    /** File object that let us know what is happening and delegates to certain
244
    /** File object that let us know what is happening and delegates to certain
232
     * instance variables of the test.
245
     * instance variables of the test.
233
     */
246
     */
Lines 392-397 Link Here
392
        }
405
        }
393
        
406
        
394
    }
407
    }
408
    
409
    private static final class FileEncodingQueryImpl extends FileEncodingQueryImplementation {
410
        
411
        private static FileEncodingQueryImpl instance;
412
        
413
        private FileObject file;
414
        
415
        private FileEncodingQueryImpl () {
416
            
417
        }
418
            
419
        public Charset getEncoding(FileObject file) {
420
            this.file = file;
421
            return Charset.defaultCharset();
422
        }
423
        
424
        public void reset () {
425
            this.file = null;
426
        }
427
        
428
        public FileObject getFile () {
429
            return this.file;
430
        }
431
        
432
        public synchronized static FileEncodingQueryImpl getDefault () {
433
            if (instance == null) {
434
                instance = new FileEncodingQueryImpl ();
435
            }
436
            return instance;
437
        }                
438
    }
395
439
396
    public static final class Lkp extends org.openide.util.lookup.AbstractLookup  {
440
    public static final class Lkp extends org.openide.util.lookup.AbstractLookup  {
397
        public Lkp () {
441
        public Lkp () {
Lines 402-407 Link Here
402
            super (ic);
446
            super (ic);
403
            
447
            
404
            ic.add (new Pool ());
448
            ic.add (new Pool ());
449
            ic.add (FileEncodingQueryImpl.getDefault());
405
        }
450
        }
406
        
451
        
407
    } // end of Lkp
452
    } // end of Lkp

Return to bug 42638