public abstract class CloneableEditorSupport extends CloneableOpenSupport
Document
.
Can be assigned as a cookie to any editable data object.
This class is abstract, so any subclass has to provide implementation
for abstract method (usually for generating of messages) and also
provide environment CloneableEditorSupport.Env
to give this support access to
input/output streams, mime type and other features of edited object.
This class implements methods of the interfaces
EditorCookie
, OpenCookie
,
EditCookie
,
ViewCookie
, LineCookie
,
CloseCookie
, and PrintCookie
but does not implement
those interfaces. It is up to the subclass to decide which interfaces
really implement and which not.
This class supports collecting multiple edits into a group which is treated
as a single edit by undo/redo. Send CloneableEditorSupport.BEGIN_COMMIT_GROUP
and
CloneableEditorSupport.END_COMMIT_GROUP
to UndoableEditListener. These must always be paired.
Send CloneableEditorSupport.MARK_COMMIT_GROUP
to commit accumulated edits and to continue
accumulating.
Modifier and Type | Class and Description |
---|---|
static interface |
CloneableEditorSupport.Env
Interface for providing data for the support and also
locking the source of data.
|
static interface |
CloneableEditorSupport.Pane
Describes one existing editor.
|
Modifier and Type | Field and Description |
---|---|
static UndoableEdit |
BEGIN_COMMIT_GROUP
Start a group of edits which will be committed as a single edit
for purpose of undo/redo.
|
static String |
EDITOR_MODE
Common name for editor mode.
|
static UndoableEdit |
END_COMMIT_GROUP
End a group of edits.
|
static UndoableEdit |
MARK_COMMIT_GROUP
Any coalesced edits become a commit-group and a new commit-group
is started.
|
allEditors, env
Constructor and Description |
---|
CloneableEditorSupport(CloneableEditorSupport.Env env)
Creates new CloneableEditorSupport attached to given environment.
|
CloneableEditorSupport(CloneableEditorSupport.Env env,
Lookup l)
Creates new CloneableEditorSupport attached to given environment.
|
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(ChangeListener l)
Deprecated.
Deprecated since 3.40. Use
CloneableEditorSupport.addPropertyChangeListener(java.beans.PropertyChangeListener) instead.
See also EditorCookie.Observable . |
void |
addPropertyChangeListener(PropertyChangeListener l)
Add a PropertyChangeListener to the listener list.
|
protected void |
afterRedirect(CloneableOpenSupport redirectedTo) |
protected boolean |
asynchronousOpen()
Controls behavior of method open.
|
protected boolean |
canClose()
Should test whether all data is saved, and if not, prompt the user
to save.
|
protected boolean |
close(boolean ask)
Closes all opened editors (if the user agrees) and
flushes content of the document to the file.
|
protected CloneableEditor |
createCloneableEditor()
Allows subclasses to create their own version
of
CloneableEditor component. |
protected CloneableTopComponent |
createCloneableTopComponent()
Prepares document, creates and initializes
new
CloneableEditor component. |
protected EditorKit |
createEditorKit()
Creates editor kit for this source.
|
protected CloneableEditorSupport.Pane |
createPane()
Creates and initializes
new
CloneableEditor component. |
PositionRef |
createPositionRef(int offset,
Position.Bias bias)
Create a position reference for the given offset.
|
protected StyledDocument |
createStyledDocument(EditorKit kit)
Method that can be overriden by children to create empty
styled document or attach additional document properties to it.
|
protected UndoRedo.Manager |
createUndoRedoManager()
Create an undo/redo manager.
|
protected String |
documentID()
Constructs the ID used for persistence of opened editors.
|
protected void |
firePropertyChange(String propertyName,
Object oldValue,
Object newValue)
Report a bound property update to any registered listeners.
|
StyledDocument |
getDocument()
Get the document.
|
static EditorKit |
getEditorKit(String mimePath)
Gets an
EditorKit from Netbeans registry. |
InputStream |
getInputStream()
Returns an InputStream which reads the current data from this editor, taking into
account the encoding of the file.
|
Line.Set |
getLineSet()
Get the line set for all paragraphs in the document.
|
JEditorPane[] |
getOpenedPanes()
Gets editor panes opened by this support.
|
protected UndoRedo.Manager |
getUndoRedo()
Gets the undo redo manager.
|
protected void |
initializeCloneableEditor(CloneableEditor editor)
Initialize the editor.
|
boolean |
isDocumentLoaded()
Test whether the document is in memory, or whether loading is still in progress.
|
boolean |
isModified()
Test whether the document is modified.
|
protected void |
loadFromStreamToKit(StyledDocument doc,
InputStream stream,
EditorKit kit)
Actually read file data into an editor kit's document from an input stream.
|
protected String |
messageHtmlName()
Constructs message that should be used to name the editor component
in html fashion, with possible coloring, text styles etc.
|
protected String |
messageLine(Line line)
Computes display name for a line produced
by this
CloneableEditorSupport.getLineSet() . |
protected abstract String |
messageName()
Constructs message that should be used to name the editor component.
|
protected abstract String |
messageSave()
Constructs message that should be displayed when the data object
is modified and is being closed.
|
protected abstract String |
messageToolTip()
Text to use as tooltip for component.
|
protected void |
notifyClosed()
Method that is called when all components of the support are
closed.
|
protected boolean |
notifyModified()
Called when the document is being modified.
|
protected void |
notifyUnmodified()
Notification method called when the document become unmodified.
|
void |
open()
Overrides superclass method, first processes document preparation.
|
protected CloneableEditorSupport.Pane |
openAt(PositionRef pos,
int column)
Forcibly create one editor component.
|
StyledDocument |
openDocument()
Get the document associated with this cookie.
|
Task |
prepareDocument()
Load the document into memory.
|
void |
print()
A printing implementation suitable for
PrintCookie . |
protected Task |
reloadDocument()
Reload the document in response to external modification.
|
void |
removeChangeListener(ChangeListener l)
Deprecated.
Deprecated since 3.40. Use
CloneableEditorSupport.removePropertyChangeListener(java.beans.PropertyChangeListener) instead.
See also EditorCookie.Observable . |
void |
removePropertyChangeListener(PropertyChangeListener l)
Remove a PropertyChangeListener from the listener list.
|
void |
saveDocument()
Save the document in this thread.
|
protected void |
saveFromKitToStream(StyledDocument doc,
EditorKit kit,
OutputStream stream)
Actually write file data to an output stream from an editor kit's document.
|
void |
setMIMEType(String s)
Set the MIME type for the document.
|
String |
toString() |
protected void |
updateTitles()
Updates titles of all editors.
|
protected Component |
wrapEditorComponent(Component editorComponent)
Wraps the editor component in a custom component, allowing for creating
more complicated user interfaces which contain the editor UI in
an arbitrary place.
|
close, edit, messageOpened, messageOpening, openCloneableTopComponent, view
public static final String EDITOR_MODE
public static final UndoableEdit BEGIN_COMMIT_GROUP
public static final UndoableEdit END_COMMIT_GROUP
public static final UndoableEdit MARK_COMMIT_GROUP
public CloneableEditorSupport(CloneableEditorSupport.Env env)
env
- environment that is source of all actions around the
data objectpublic CloneableEditorSupport(CloneableEditorSupport.Env env, Lookup l)
env
- environment that is source of all actions around the
data objectl
- the context that will be passed to each Line produced
by this support's Line.Set. The line will return it from Line.getLookup
callprotected abstract String messageSave()
protected abstract String messageName()
protected String messageHtmlName()
protected String documentID()
protected abstract String messageToolTip()
protected String messageLine(Line line)
CloneableEditorSupport.getLineSet()
. The default
implementation reuses messageName and line number of the line.line
- the line object to compute display name forprotected final UndoRedo.Manager getUndoRedo()
protected boolean asynchronousOpen()
CloneableEditorSupport.open()
public void open()
open
in class CloneableOpenSupport
CloneableEditorSupport.prepareDocument()
public final void addPropertyChangeListener(PropertyChangeListener l)
EditorCookie.Observable
.l
- the PropertyChangeListener to be addedpublic final void removePropertyChangeListener(PropertyChangeListener l)
EditorCookie.Observable
.l
- the PropertyChangeListener to be removedprotected final void firePropertyChange(String propertyName, Object oldValue, Object newValue)
propertyName
- the programmatic name of the property that was changed.oldValue
- rhe old value of the property.newValue
- the new value of the property.public Task prepareDocument()
public StyledDocument openDocument() throws IOException
StyledDocument
but it should
also understand the NetBeans NbDocument.GUARDED
to
prevent certain lines from being edited by the user.
If the document is not loaded the method blocks until it is.
Method will throw UserQuestionException
exception
if file size is too big. This exception could be caught and
its method UserQuestionException.confirmed()
can be used for confirmation. You need to call CloneableEditorSupport.openDocument()
}
one more time after confirmation.
IOException
- if the document could not be loadedpublic StyledDocument getDocument()
prepareTask
)
has been completed, in such a case the document must not be modified.null
if it is not yet loadedpublic boolean isModified()
true
if the document is in memory and is modified;
otherwise false
public void saveDocument() throws IOException
IOException
- on I/O errorpublic JEditorPane[] getOpenedPanes()
EditorCookie.getOpenedPanes()
protected void afterRedirect(CloneableOpenSupport redirectedTo)
afterRedirect
in class CloneableOpenSupport
public Line.Set getLineSet()
public void print()
PrintCookie
.protected CloneableTopComponent createCloneableTopComponent()
CloneableEditor
component.
Typically do not override this method.
For creating your own CloneableEditor
type component
override CloneableEditorSupport.createCloneableEditor()
method.createCloneableTopComponent
in class CloneableOpenSupport
CloneableEditor
for this supportprotected CloneableEditorSupport.Pane createPane()
CloneableEditor
component.
Typically do not override this method (unless you are dealing with
multiviews).
For creating your own CloneableEditor
type component
override CloneableEditorSupport.createCloneableEditor()
method.CloneableEditorSupport.Pane
for this supportprotected Component wrapEditorComponent(Component editorComponent)
The default implementation merely returns the passed
editorComponent
parameter.
editorComponent
- the component containing the editor
(usually not the JEditorPane, but some its ancestor).editorComponent
or
editorComponent
itself.protected boolean canClose()
canClose
in class CloneableOpenSupport
true
if everything can be closedpublic boolean isDocumentLoaded()
true
if document is loadedpublic void setMIMEType(String s)
s
- the new MIME type@Deprecated public void addChangeListener(ChangeListener l)
CloneableEditorSupport.addPropertyChangeListener(java.beans.PropertyChangeListener)
instead.
See also EditorCookie.Observable
.l
- new listener@Deprecated public void removeChangeListener(ChangeListener l)
CloneableEditorSupport.removePropertyChangeListener(java.beans.PropertyChangeListener)
instead.
See also EditorCookie.Observable
.l
- listener to removepublic final PositionRef createPositionRef(int offset, Position.Bias bias)
offset
- the offset to create position atbias
- the Position.Bias for new creating position.protected CloneableEditor createCloneableEditor()
CloneableEditor
component.CloneableEditor
for this supportprotected void initializeCloneableEditor(CloneableEditor editor)
editor
- the editor that has been created and should be annotatedprotected UndoRedo.Manager createUndoRedoManager()
The default implementation uses improved UndoRedo.Manager
,
with support for various extensions (including CloneableEditorSupport.BEGIN_COMMIT_GROUP
and CloneableEditorSupport.END_COMMIT_GROUP
). It is not wise to override this
method without delegating to super.createUndoRedoManager
.
public InputStream getInputStream() throws IOException
CloneableEditorSupport.saveFromKitToStream(javax.swing.text.StyledDocument, javax.swing.text.EditorKit, java.io.OutputStream)
.InputStream
will contain the same data as if the file
was written out to the CloneableEditorSupport.Env
(usually disk). So it will contain
guarded block markers etc. If the document is not loaded,
then the InputStream
will be taken from the CloneableEditorSupport.Env
.IOException
- if saving the document to a virtual stream or other IO operation failsprotected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException
CloneableEditorSupport.saveDocument()
.
The default implementation just calls EditorKit.write(...)
.
Subclasses could override this to provide support for persistent guard blocks, for example.
doc
- the document to write fromkit
- the associated editor kitstream
- the open stream to write toIOException
- if there was a problem writing the fileBadLocationException
- should not normally be thrownCloneableEditorSupport.loadFromStreamToKit(javax.swing.text.StyledDocument, java.io.InputStream, javax.swing.text.EditorKit)
protected void loadFromStreamToKit(StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException
CloneableEditorSupport.prepareDocument()
.
The default implementation just calls EditorKit.read(...)
.
Subclasses could override this to provide support for persistent guard blocks, for example.
doc
- the document to read intostream
- the open stream to read fromkit
- the associated editor kitIOException
- if there was a problem reading the fileBadLocationException
- should not normally be thrownCloneableEditorSupport.saveFromKitToStream(javax.swing.text.StyledDocument, javax.swing.text.EditorKit, java.io.OutputStream)
protected Task reloadDocument()
public static EditorKit getEditorKit(String mimePath)
EditorKit
from Netbeans registry. The method looks
in the MimeLookup
for EditorKit
s registered for
the mime-path passed in and returns the first one it finds. If there is
no EditorKit
registered for the mime-path it will fall back
to the 'text/plain' EditorKit
and eventually to its own
default kit.
A mime-path is a concatenation of one or more mime-types allowing to address fragments of text with a different mime-type than the mime-type of a document that contains those fragments. As an example you can use a JSP page containing a java scriplet. The JSP page is a document of 'text/x-jsp' mime-type, while the mime-type of the java scriplet is 'text/x-java'. When accessing settings or services such as an 'EditorKit' for java scriplets embedded in a JSP page the scriplet's mime-path 'text/x-jsp/text/x-java' should be used.
If you are trying to get an 'EditorKit' for the whole document you can simply pass in the document's mime-type (e.g. 'text/x-java'). For the main document its mime-type and mime-path are the same.
mimePath
- The mime-path to find an EditorKit
for.EditorKit
implementation registered for the given mime-path.MimeLookup
protected EditorKit createEditorKit()
protected StyledDocument createStyledDocument(EditorKit kit)
kit
- the kit to useprotected void notifyUnmodified()
This implementation simply marks the associated environement unmodified and updates titles of all components.
protected boolean notifyModified()
protected void notifyClosed()
protected boolean close(boolean ask)
close
in class CloneableOpenSupport
ask
- ask whether to save the document or not?false
if the operation is cancelledprotected void updateTitles()
protected final CloneableEditorSupport.Pane openAt(PositionRef pos, int column)
pos
- where to place the caretcolumn
- where to place the caretnull
editor