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
.
Added method to create guarded block on top of existing text.
APIs exposed and actually used on GuardedDocument in editor.lib
module is now declared in Editor Guarded Sections.
Clients may depend on Guarded Sections instead of on fading-away editor.lib module.
Dependency on openide.text
eliminated, new SPI GuardedRegionMarker
is added for Documents that are willing to style their contents according to guarded areas.
When this option is set, setting the content of a GuardedSection will pass the data through the given guarded writer and back through the given guarded reader, to ensure the result is the same as if it would be read from the disk.
Note that this new mode is not fully compatible with the original mode, e.g. all the set methods of all the GuardedSection classes will throw IllegalStateException if invoked inside the write&read part.
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 Apache Git repositories or in the GitHub 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.