Class TextEditorPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Printable, Serializable, EventListener, Accessible, DocumentListener, Scrollable, SyntaxConstants

public class TextEditorPane extends RSyntaxTextArea implements DocumentListener
An extension of RSyntaxTextArea that adds information about the file being edited, such as:
  • Its name and location.
  • Is it dirty?
  • Is it read-only?
  • The last time it was loaded or saved to disk (local files only).
  • The file's encoding on disk.
  • Easy access to the line separator.
Loading and saving is also built into the editor.

When saving UTF-8 files, whether a BOM is written is controlled by the UnicodeWriter class. Use UnicodeWriter.setWriteUtf8BOM(boolean) to toggle writing BOMs for UTF-8 files.

Both local and remote files (e.g. ftp) are supported. See the FileLocation class for more information.

Version:
1.0
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • FULL_PATH_PROPERTY

      public static final String FULL_PATH_PROPERTY
      Property change event fired when the file path this text area references is updated.
      See Also:
    • DIRTY_PROPERTY

      public static final String DIRTY_PROPERTY
      Property change event fired when the text area's dirty flag changes.
      See Also:
    • READ_ONLY_PROPERTY

      public static final String READ_ONLY_PROPERTY
      Property change event fired when the text area should be treated as read-only, and previously it should not, or vice-versa.
      See Also:
    • ENCODING_PROPERTY

      public static final String ENCODING_PROPERTY
      Property change event fired when the text area's encoding changes.
      See Also:
    • loc

      private FileLocation loc
      The location of the file being edited.
    • charSet

      private String charSet
      The charset to use when reading or writing this file.
    • readOnly

      private boolean readOnly
      Whether the file should be treated as read-only.
    • dirty

      private boolean dirty
      Whether the file is dirty.
    • lastSaveOrLoadTime

      private long lastSaveOrLoadTime
      The last time this file was modified on disk, for local files. For remote files, this value should always be LAST_MODIFIED_UNKNOWN.
    • LAST_MODIFIED_UNKNOWN

      public static final long LAST_MODIFIED_UNKNOWN
      The value returned by getLastSaveOrLoadTime() for remote files.
      See Also:
    • DEFAULT_FILE_NAME

      private static final String DEFAULT_FILE_NAME
      The default name given to files if none is specified in a constructor.
      See Also:
  • Constructor Details

    • TextEditorPane

      public TextEditorPane()
      Constructor. The file will be given a default name.
    • TextEditorPane

      public TextEditorPane(int textMode)
      Constructor. The file will be given a default name.
      Parameters:
      textMode - Either INSERT_MODE or OVERWRITE_MODE.
    • TextEditorPane

      public TextEditorPane(int textMode, boolean wordWrapEnabled)
      Creates a new TextEditorPane. The file will be given a default name.
      Parameters:
      textMode - Either INSERT_MODE or OVERWRITE_MODE.
      wordWrapEnabled - Whether to use word wrap in this pane.
    • TextEditorPane

      public TextEditorPane(int textMode, boolean wordWrapEnabled, FileLocation loc) throws IOException
      Creates a new TextEditorPane.
      Parameters:
      textMode - Either INSERT_MODE or OVERWRITE_MODE.
      wordWrapEnabled - Whether to use word wrap in this pane.
      loc - The location of the text file being edited. If this value is null, a file named "Untitled.txt" in the current directory is used.
      Throws:
      IOException - If an IO error occurs reading the file at loc. This of course won't happen if loc is null.
    • TextEditorPane

      public TextEditorPane(int textMode, boolean wordWrapEnabled, FileLocation loc, String defaultEnc) throws IOException
      Creates a new TextEditorPane.
      Parameters:
      textMode - Either INSERT_MODE or OVERWRITE_MODE.
      wordWrapEnabled - Whether to use word wrap in this pane.
      loc - The location of the text file being edited. If this value is null, a file named "Untitled.txt" in the current directory is used. This file is displayed as empty even if it actually exists.
      defaultEnc - The default encoding to use when opening the file, if the file is not Unicode. If this value is null, a system default value is used.
      Throws:
      IOException - If an IO error occurs reading the file at loc. This of course won't happen if loc is null.
  • Method Details

    • changedUpdate

      public void changedUpdate(DocumentEvent e)
      Callback for when styles in the current document change. This method is never called.
      Specified by:
      changedUpdate in interface DocumentListener
      Parameters:
      e - The document event.
    • getDefaultEncoding

      private static String getDefaultEncoding()
      Returns the default encoding for this operating system.
      Returns:
      The default encoding.
    • getEncoding

      public String getEncoding()
      Returns the encoding to use when reading or writing this file.
      Returns:
      The encoding.
      See Also:
    • getFileFullPath

      public String getFileFullPath()
      Returns the full path to this document.
      Returns:
      The full path to the document.
    • getFileName

      public String getFileName()
      Returns the file name of this document.
      Returns:
      The file name.
    • getLastSaveOrLoadTime

      public long getLastSaveOrLoadTime()
      Returns the timestamp for when this file was last loaded or saved by this editor pane. If the file has been modified on disk by another process after it was loaded into this editor pane, this method will not return the actual file's last modified time.

      For remote files, this method will always return LAST_MODIFIED_UNKNOWN.

      Returns:
      The timestamp when this file was last loaded or saved by this editor pane, if it is a local file, or LAST_MODIFIED_UNKNOWN if it is a remote file.
      See Also:
    • getLineSeparator

      public Object getLineSeparator()
      Returns the line separator used when writing this file (e.g. "\n", "\r\n", or "\r").

      Note that this value is an Object and not a String as that is the way the Document interface defines its property values. If you always use setLineSeparator(String) to modify this value, then the value returned from this method will always be a String.

      Returns:
      The line separator. If this value is null, then the system default line separator is used (usually the value of System.getProperty("line.separator")).
      See Also:
    • init

      private void init(FileLocation loc, String defaultEnc) throws IOException
      Initializes this editor with the specified file location.
      Parameters:
      loc - The file location. If this is null, a default location is used and an empty file is displayed.
      defaultEnc - The default encoding to use when opening the file, if the file is not Unicode. If this value is null, a system default value is used.
      Throws:
      IOException - If an IO error occurs reading from loc. If loc is null, this cannot happen.
    • insertUpdate

      public void insertUpdate(DocumentEvent e)
      Callback for when text is inserted into the document.
      Specified by:
      insertUpdate in interface DocumentListener
      Parameters:
      e - Information on the insertion.
    • isDirty

      public boolean isDirty()
      Returns whether the text in this editor has unsaved changes.
      Returns:
      Whether the text has unsaved changes.
      See Also:
    • isLocal

      public boolean isLocal()
      Returns whether this file is a local file.
      Returns:
      Whether this is a local file.
    • isLocalAndExists

      public boolean isLocalAndExists()
      Returns whether this is a local file that already exists.
      Returns:
      Whether this is a local file that already exists.
    • isModifiedOutsideEditor

      public boolean isModifiedOutsideEditor()
      Returns whether the text file has been modified outside this editor since the last load or save operation. Note that if this is a remote file, this method will always return false.

      This method may be used by applications to implement a reloading feature, where the user is prompted to reload a file if it has been modified since their last open or save.

      Returns:
      Whether the text file has been modified outside this editor.
      See Also:
    • isReadOnly

      public boolean isReadOnly()
      Returns whether the text area should be treated as read-only.
      Returns:
      Whether the text area should be treated as read-only.
      See Also:
    • load

      public void load(FileLocation loc) throws IOException
      Loads the specified file in this editor. This method fires a property change event of type FULL_PATH_PROPERTY.

      The file will be checked for a BOM; if one is found, the proper Unicode flavor is used to load the file. If not, the system default encoding is assumed.

      Parameters:
      loc - The location of the file to load. This cannot be null.
      Throws:
      IOException - If an IO error occurs.
      See Also:
    • load

      public void load(FileLocation loc, Charset defaultEnc) throws IOException
      Loads the specified file in this editor. This method fires a property change event of type FULL_PATH_PROPERTY.
      Parameters:
      loc - The location of the file to load. This cannot be null.
      defaultEnc - The encoding to use when loading/saving the file. This encoding will only be used if the file is not Unicode. If this value is null, the system default encoding is used.
      Throws:
      IOException - If an IO error occurs.
      See Also:
    • load

      public void load(FileLocation loc, String defaultEnc) throws IOException
      Loads the specified file in this editor. This method fires a property change event of type FULL_PATH_PROPERTY.
      Parameters:
      loc - The location of the file to load. This cannot be null.
      defaultEnc - The encoding to use when loading/saving the file. This encoding will only be used if the file is not Unicode. If this value is null, the system default encoding is used.
      Throws:
      IOException - If an IO error occurs.
      See Also:
    • reload

      public void reload() throws IOException
      Reloads this file from disk. The file must exist for this operation to not throw an exception.

      The file's "dirty" state will be set to false after this operation. If this is a local file, its "last modified" time is updated to reflect that of the actual file.

      Note that if the file has been modified on disk, and is now a Unicode encoding when before it wasn't (or if it is a different Unicode now), this will cause this TextEditorPane's encoding to change. Otherwise, the file's encoding will stay the same.

      Throws:
      IOException - If the file does not exist, or if an IO error occurs reading the file.
      See Also:
    • removeUpdate

      public void removeUpdate(DocumentEvent e)
      Called whenever text is removed from this editor.
      Specified by:
      removeUpdate in interface DocumentListener
      Parameters:
      e - The document event.
    • save

      public void save() throws IOException
      Saves the file in its current encoding.

      The text area's "dirty" state is set to false, and if this is a local file, its "last modified" time is updated.

      Throws:
      IOException - If an IO error occurs.
      See Also:
    • saveAs

      public void saveAs(FileLocation loc) throws IOException
      Saves this file in a new local location. This method fires a property change event of type FULL_PATH_PROPERTY.
      Parameters:
      loc - The location to save to.
      Throws:
      IOException - If an IO error occurs.
      See Also:
    • saveImpl

      private void saveImpl(FileLocation loc) throws IOException
      Saves the text in this editor to the specified location.
      Parameters:
      loc - The location to save to.
      Throws:
      IOException - If an IO error occurs.
    • setDirty

      public void setDirty(boolean dirty)
      Sets whether this text in this editor has unsaved changes. This fires a property change event of type DIRTY_PROPERTY.

      Applications will usually have no need to call this method directly; the only time you might have a need to call this method directly is if you have to initialize an instance of TextEditorPane with content that does not come from a file. TextEditorPane automatically sets its own dirty flag when its content is edited, when its encoding is changed, or when its line ending property is changed. It is cleared whenever load(), reload(), save(), or saveAs() are called.

      Parameters:
      dirty - Whether the text has been modified.
      See Also:
    • setDocument

      public void setDocument(Document doc)
      Sets the document for this editor.
      Overrides:
      setDocument in class RSyntaxTextArea
      Parameters:
      doc - The new document.
    • setEncoding

      public void setEncoding(String encoding)
      Sets the encoding to use when reading or writing this file. This method sets the editor's dirty flag when the encoding is changed, and fires a property change event of type ENCODING_PROPERTY.
      Parameters:
      encoding - The new encoding.
      Throws:
      UnsupportedCharsetException - If the encoding is not supported.
      NullPointerException - If encoding is null.
      See Also:
    • setLineSeparator

      public void setLineSeparator(String separator)
      Sets the line separator sequence to use when this file is saved (e.g. "\n", "\r\n" or "\r"). Besides parameter checking, this method is preferred over getDocument().putProperty() because it sets the editor's dirty flag when the line separator is changed.
      Parameters:
      separator - The new line separator.
      Throws:
      NullPointerException - If separator is null.
      IllegalArgumentException - If separator is not one of "\n", "\r\n" or "\r".
      See Also:
    • setLineSeparator

      public void setLineSeparator(String separator, boolean setDirty)
      Sets the line separator sequence to use when this file is saved (e.g. "\n", "\r\n" or "\r"). Besides parameter checking, this method is preferred over getDocument().putProperty() because can set the editor's dirty flag when the line separator is changed.
      Parameters:
      separator - The new line separator.
      setDirty - Whether the dirty flag should be set if the line separator is changed.
      Throws:
      NullPointerException - If separator is null.
      IllegalArgumentException - If separator is not one of "\n", "\r\n" or "\r".
      See Also:
    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Sets whether this text area should be treated as read-only. This fires a property change event of type READ_ONLY_PROPERTY.
      Parameters:
      readOnly - Whether the document is read-only.
      See Also:
    • syncLastSaveOrLoadTimeToActualFile

      public void syncLastSaveOrLoadTimeToActualFile()
      Syncs this text area's "last saved or loaded" time to that of the file being edited, if that file is local and exists. If the file is remote or is local but does not yet exist, nothing happens.

      You normally do not have to call this method, as the "last saved or loaded" time for TextEditorPanes is kept up-to-date internally during such operations as save(), reload(), etc.

      See Also: