See: Description
Package | Description |
---|---|
org.netbeans.api.editor.guards | |
org.netbeans.spi.editor.guards | |
org.netbeans.spi.editor.guards.support |
GuardedSectionsAPI
The Guarded Sections module is supposed to operate over the Swing's StyledDocument
.
It allows clients to manipulate named guarded sections that prevents user to
modify the content. So if you like to create, modify or delete guarded sections the
GuardedSectionManager
is the best place where to start.
GuardedSectionsSPI
The module also allows to implement custom guarded section persistance in various content types like java, xml, ...
The easiest way is to subclass
AbstractGuardedSectionsProvider
.
In order to bind guarded sections to your editor see
GuardedSectionsFactory
.
In order to use proper encoding by guards impl it is necessary to
change GuardedSectionsProvider to accept encoding rather as java.nio.Charset
instance
than as a plain encoding name.
Reader createGuardedReader(InputStream stream, String encoding) throws UnsupportedEncodingException
replaced with
Reader createGuardedReader(InputStream stream, Charset charset)
Writer createGuardedWriter(OutputStream stream, String encoding) throws UnsupportedEncodingException
replaced with
Reader createGuardedReader(InputStream stream, Charset charset)
String sectionName = ...; StyledDocument doc = ...; GuardedSectionManager guards = GuardedSectionManager.getInstance(doc); GuardedSection g = guards.findSimpleSection(sectionName); guards.createSimpleSection("new_name", doc.createPosition(g.getEndPosition().getOffset() + 1));
StyledDocument doc = ...; GuardedSectionManager guards = GuardedSectionManager.getInstance(doc); GuardedSection g = guards.findSimpleSection("sectionName"); g.deleteSection();
CloneableEditorSupport
to provide
guarded sections you should implement the GuardedEditorSupport
interface.
private final class MyGuardedEditor implements GuardedEditorSupport { ... }Further implement reading and writing of existing sections.
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException { if (guardedEditor == null) { guardedEditor = new MyGuardedEditor(); // remember the provider String mimeType = ((CloneableEditorSupport.Env) this.env).getMimeType(); guardedProvider = GuardedSectionsFactory.find(mimeType).create(guardedEditor); } // load content to kit if (guardedProvider != null) { guardedEditor.setDocument(doc); Charset cs = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Reader reader = guardedProvider.createGuardedReader(stream, cs); try { kit.read(reader, doc, 0); } finally { reader.close(); } } else { kit.read(stream, doc, 0); } } protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException { if (guardedProvider != null) { Charset cs = FileEncodingQuery.getEncoding(this.getDataObject().getPrimaryFile()); Writer writer = guardedProvider.createGuardedWriter(stream, cs); try { kit.write(writer, doc, 0, doc.getLength()); } finally { writer.close(); } } else { kit.write(stream, doc, 0, doc.getLength()); } }Your module should also require a proper implementation. In case of java content add to your module manifest file:
OpenIDE-Module-Requires: org.netbeans.api.editor.guards.Java
|
|
|
The sources for the module are in the NetBeans Mercurial repositories.
A module using the Guarded Sections API should also require a proper implementation. Eg in case of java content add to your module manifest file:
OpenIDE-Module-Requires: org.netbeans.api.editor.guards.Java
A module implementing the Guarded Sections SPI should provide a token in the manifest file. Eg in case of java content add:
OpenIDE-Module-Provides: org.netbeans.api.editor.guards.Java
Read more about the implementation in the answers to architecture questions.
Built on March 18 2014. | Portions Copyright 1997-2014 Sun Microsystems, Inc. All rights reserved.