org.openide.text
Class CloneableEditorSupport

java.lang.Object
  |
  +--org.openide.windows.CloneableOpenSupport
        |
        +--org.openide.text.CloneableEditorSupport
Direct Known Subclasses:
DataEditorSupport

public abstract class CloneableEditorSupport
extends CloneableOpenSupport

Support for associating an editor and a Swing 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.


Inner Class Summary
static interface CloneableEditorSupport.Env
          Interface for providing data for the support and also locking the source of data.
 
Inner classes inherited from class org.openide.windows.CloneableOpenSupport
CloneableOpenSupport.Env
 
Field Summary
static String EDITOR_MODE
          Common name for editor mode.
 
Fields inherited from class org.openide.windows.CloneableOpenSupport
allEditors, env
 
Constructor Summary
CloneableEditorSupport(CloneableEditorSupport.Env env)
          Creates new CloneableEditorSupport attached to given environment.
 
Method Summary
 void addChangeListener(ChangeListener l)
          Adds a listener for status changes.
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 editor.
protected  CloneableTopComponent createCloneableTopComponent()
          A method to create a new component.
protected  EditorKit createEditorKit()
          Creates editor kit for this source.
 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.
 StyledDocument getDocument()
          Get the document.
 Line.Set getLineSet()
          Get the line set for all paragraphs in the document.
 JEditorPane[] getOpenedPanes()
           
protected  UndoRedo.Manager getUndoRedo()
          Getter for 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 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.
 StyledDocument openDocument()
          Get the document associated with this cookie.
 Task prepareDocument()
          Load the document into memory.
 void print()
          The implementation of @see org.openide.cookies.PrintCookie#print() method.
protected  Task reloadDocument()
          Reload the document in response to external modification.
 void removeChangeListener(ChangeListener l)
          Removes a listener for status changes.
 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.
protected  void updateTitles()
          Updates titles of all editors.
 
Methods inherited from class org.openide.windows.CloneableOpenSupport
close, edit, messageOpened, messageOpening, open, openCloneableTopComponent, view
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EDITOR_MODE

public static final String EDITOR_MODE
Common name for editor mode.
Constructor Detail

CloneableEditorSupport

public CloneableEditorSupport(CloneableEditorSupport.Env env)
Creates new CloneableEditorSupport attached to given environment.
Parameters:
env - environment that is source of all actions around the data object
Method Detail

messageSave

protected abstract String messageSave()
Constructs message that should be displayed when the data object is modified and is being closed.
Returns:
text to show to the user

messageName

protected abstract String messageName()
Constructs message that should be used to name the editor component.
Returns:
name of the editor

messageToolTip

protected abstract String messageToolTip()
Text to use as tooltip for component.
Returns:
text to show to the user

getUndoRedo

protected final UndoRedo.Manager getUndoRedo()
Getter for undo redo manager.

prepareDocument

public Task prepareDocument()
Load the document into memory. This is done in different thread. A task for the thread is returned so anyone may test whether the loading has been finished or is still in process.
Returns:
task for control over loading

openDocument

public StyledDocument openDocument()
                            throws IOException
Get the document associated with this cookie. It is an instance of Swing's 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.

Returns:
the styled document for this cookie that understands the guarded attribute
Throws:
IOException - if the document could not be loaded

getDocument

public StyledDocument getDocument()
Get the document. This method may be called before the document initialization (prepareTask) has been completed, in such a case the document must not be modified.
Returns:
document or null if it is not yet loaded

isModified

public boolean isModified()
Test whether the document is modified.
Returns:
true if the document is in memory and is modified; otherwise false

saveDocument

public void saveDocument()
                  throws IOException
Save the document in this thread. Create 'orig' document for the case that the save would fail.
Throws:
IOException - on I/O error

getOpenedPanes

public JEditorPane[] getOpenedPanes()

getLineSet

public Line.Set getLineSet()
Get the line set for all paragraphs in the document.
Returns:
positions of all paragraphs on last save

print

public void print()
The implementation of @see org.openide.cookies.PrintCookie#print() method.

createCloneableTopComponent

protected CloneableTopComponent createCloneableTopComponent()
Description copied from class: CloneableOpenSupport
A method to create a new component. Must be overridden in subclasses.
Overrides:
createCloneableTopComponent in class CloneableOpenSupport
Following copied from class: org.openide.windows.CloneableOpenSupport
Returns:
the cloneable top component for this support

canClose

protected boolean canClose()
Should test whether all data is saved, and if not, prompt the user to save.
Overrides:
canClose in class CloneableOpenSupport
Returns:
true if everything can be closed

isDocumentLoaded

public boolean isDocumentLoaded()
Test whether the document is in memory, or whether loading is still in progress.
Returns:
true if document is loaded

setMIMEType

public void setMIMEType(String s)
Set the MIME type for the document.
Parameters:
s - the new MIME type

addChangeListener

public void addChangeListener(ChangeListener l)
Adds a listener for status changes. An event is fired when the document is moved or removed from memory.
Parameters:
l - new listener

removeChangeListener

public void removeChangeListener(ChangeListener l)
Removes a listener for status changes.
Parameters:
l - listener to remove

createPositionRef

public final PositionRef createPositionRef(int offset,
                                           Position.Bias bias)
Create a position reference for the given offset. The position moves as the document is modified and reacts to closing and opening of the document.
Parameters:
offset - the offset to create position at
bias - the Position.Bias for new creating position.
Returns:
position reference for that offset

createCloneableEditor

protected CloneableEditor createCloneableEditor()
Allows subclasses to create their own version of editor.
Returns:
the CloneableEditor for this support

initializeCloneableEditor

protected void initializeCloneableEditor(CloneableEditor editor)
Initialize the editor. This method is called after the editor component is deserialized and also when the component is created. It allows the subclasses to annotate the component with icon, selected nodes, etc.
Parameters:
editor - the editor that has been created and should be annotated

createUndoRedoManager

protected UndoRedo.Manager createUndoRedoManager()
Create an undo/redo manager. This manager is then attached to the document, and listens to all changes made in it.

The default implementation simply uses UndoRedo.Manager.

Returns:
the undo/redo manager

saveFromKitToStream

protected void saveFromKitToStream(StyledDocument doc,
                                   EditorKit kit,
                                   OutputStream stream)
                            throws IOException,
                                   BadLocationException
Actually write file data to an output stream from an editor kit's document. Called during a file save by saveDocument().

The default implementation just calls EditorKit.write(...). Subclasses could override this to provide support for persistent guard blocks, for example.

Parameters:
doc - the document to write from
kit - the associated editor kit
stream - the open stream to write to
Throws:
IOException - if there was a problem writing the file
BadLocationException - should not normally be thrown
See Also:
loadFromStreamToKit(javax.swing.text.StyledDocument, java.io.InputStream, javax.swing.text.EditorKit)

loadFromStreamToKit

protected void loadFromStreamToKit(StyledDocument doc,
                                   InputStream stream,
                                   EditorKit kit)
                            throws IOException,
                                   BadLocationException
Actually read file data into an editor kit's document from an input stream. Called during a file load by prepareDocument().

The default implementation just calls EditorKit.read(...). Subclasses could override this to provide support for persistent guard blocks, for example.

Parameters:
doc - the document to read into
stream - the open stream to read from
kit - the associated editor kit
Throws:
IOException - if there was a problem reading the file
BadLocationException - should not normally be thrown
See Also:
saveFromKitToStream(javax.swing.text.StyledDocument, javax.swing.text.EditorKit, java.io.OutputStream)

reloadDocument

protected Task reloadDocument()
Reload the document in response to external modification.
Returns:
task that reloads the document. It can be also obtained by calling prepareDocument().

createEditorKit

protected EditorKit createEditorKit()
Creates editor kit for this source.
Returns:
editor kit

createStyledDocument

protected StyledDocument createStyledDocument(EditorKit kit)
Method that can be overriden by children to create empty styled document or attach additional document properties to it.
Parameters:
kit - the kit to use
Returns:
styled document to use

notifyUnmodified

protected void notifyUnmodified()
Notification method called when the document become unmodified. Called after save or after reload of document.

This implementation simply marks the associated environement unmodified and updates titles of all components.


notifyModified

protected boolean notifyModified()
Called when the document is being modified. The responsibility of this method is to inform the environment that its document is modified. Current implementation Just calls env.setModified (true) to notify it about modification.
Returns:
true if the environment accepted being marked as modified or false if it refused it and the document should still be unmodified

notifyClosed

protected void notifyClosed()
Method that is called when all components of the support are closed. The default implementation closes the document.

close

protected boolean close(boolean ask)
Closes all opened editors (if the user agrees) and flushes content of the document to the file.
Overrides:
close in class CloneableOpenSupport
Parameters:
ask - ask whether to save the document or not?
Returns:
false if the operation is cancelled

updateTitles

protected void updateTitles()
Updates titles of all editors.


Built on December 12 2001.  |  Portions Copyright 1997-2001 Sun Microsystems, Inc. All rights reserved.