Class Script

  • All Implemented Interfaces:
    java.io.Serializable, OptionHandler
    Direct Known Subclasses:
    GroovyScript, JythonScript

    public abstract class Script
    extends java.lang.Object
    implements OptionHandler, java.io.Serializable
    A simple helper class for loading, saving scripts.
    Version:
    $Revision: 10222 $
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Script.ScriptThread
      The Thread for running a script.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String BACKUP_EXTENSION
      the backup extension.
    • Constructor Summary

      Constructors 
      Constructor Description
      Script()
      Initializes the script.
      Script​(javax.swing.text.Document doc)
      Initializes the script.
      Script​(javax.swing.text.Document doc, java.io.File file)
      Initializes the script.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void addScriptFinishedListener​(ScriptExecutionListener l)
      Adds the given listener to its internal list.
      void empty()
      Empties the document.
      java.lang.String getContent()
      Returns the content.
      abstract java.lang.String getDefaultExtension()
      Returns the default extension.
      java.io.File getFilename()
      Returns the current filename.
      abstract ExtensionFileFilter[] getFilters()
      Returns the extension filters for this type of script.
      java.lang.String getNewLine()
      Returns the new line string in use.
      java.lang.String[] getOptions()
      Gets the current settings of the script.
      boolean isModified()
      Returns whether the script is modified.
      boolean isRunning()
      Returns whether the script is still running.
      java.util.Enumeration<Option> listOptions()
      Returns an enumeration describing the available options.
      abstract Script.ScriptThread newThread​(java.lang.String[] args)
      Returns a new thread to execute.
      boolean open​(java.io.File file)
      Tries to open the file.
      void removeScriptFinishedListener​(ScriptExecutionListener l)
      Removes the given listener from its internal list.
      void run​(java.io.File file, java.lang.String[] args)
      Executes the script without loading it first.
      static void runScript​(Script script, java.lang.String[] args)
      Runs the specified script.
      boolean save()
      Saves the file under with the current filename.
      boolean saveAs​(java.io.File file)
      Saves the file under with the given filename (and updates the internal filename).
      void setContent​(java.lang.String value)
      Sets the content.
      void setOptions​(java.lang.String[] options)
      Parses a given list of options.
      void start​(java.lang.String[] args)
      Executes the script.
      void stop()
      Stops the execution of the script.
      java.lang.String toString()
      Returns the content as string.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • BACKUP_EXTENSION

        public static final java.lang.String BACKUP_EXTENSION
        the backup extension.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Script

        public Script()
        Initializes the script.
      • Script

        public Script​(javax.swing.text.Document doc)
        Initializes the script.
        Parameters:
        doc - the document to use as basis
      • Script

        public Script​(javax.swing.text.Document doc,
                      java.io.File file)
        Initializes the script. Automatically loads the specified file, if not null.
        Parameters:
        doc - the document to use as basis
        file - the file to load (if not null)
    • Method Detail

      • listOptions

        public java.util.Enumeration<Option> listOptions()
        Returns an enumeration describing the available options.
        Specified by:
        listOptions in interface OptionHandler
        Returns:
        an enumeration of all the available options
      • setOptions

        public void setOptions​(java.lang.String[] options)
                        throws java.lang.Exception
        Parses a given list of options.
        Specified by:
        setOptions in interface OptionHandler
        Parameters:
        options - the list of options as an array of strings
        Throws:
        java.lang.Exception - if an option is not supported
      • getOptions

        public java.lang.String[] getOptions()
        Gets the current settings of the script.
        Specified by:
        getOptions in interface OptionHandler
        Returns:
        an array of strings suitable for passing to setOptions
      • getFilters

        public abstract ExtensionFileFilter[] getFilters()
        Returns the extension filters for this type of script.
        Returns:
        the filters
      • getDefaultExtension

        public abstract java.lang.String getDefaultExtension()
        Returns the default extension. Gets automatically added to files if their name doesn't end with this.
        Returns:
        the default extension (incl. the dot)
        See Also:
        saveAs(File)
      • getFilename

        public java.io.File getFilename()
        Returns the current filename.
        Returns:
        the filename, null if no file loaded/saved
      • getNewLine

        public java.lang.String getNewLine()
        Returns the new line string in use.
        Returns:
        the new line string
      • isModified

        public boolean isModified()
        Returns whether the script is modified.
        Returns:
        true if the script is modified
      • getContent

        public java.lang.String getContent()
        Returns the content.
        Returns:
        the content or null in case of an error
      • setContent

        public void setContent​(java.lang.String value)
        Sets the content.
        Parameters:
        value - the new content
      • empty

        public void empty()
        Empties the document.
      • open

        public boolean open​(java.io.File file)
        Tries to open the file.
        Parameters:
        file - the file to open
        Returns:
        true if successfully read
      • save

        public boolean save()
        Saves the file under with the current filename.
        Returns:
        true if successfully written
      • saveAs

        public boolean saveAs​(java.io.File file)
        Saves the file under with the given filename (and updates the internal filename).
        Parameters:
        file - the filename to write the content to
        Returns:
        true if successfully written
      • newThread

        public abstract Script.ScriptThread newThread​(java.lang.String[] args)
        Returns a new thread to execute.
        Parameters:
        args - optional commandline arguments
        Returns:
        the new thread object
      • start

        public void start​(java.lang.String[] args)
                   throws java.lang.Exception
        Executes the script.
        Parameters:
        args - optional commandline arguments, can be null
        Throws:
        java.lang.Exception - if checks or execution fail
      • stop

        public void stop()
        Stops the execution of the script.
      • run

        public void run​(java.io.File file,
                        java.lang.String[] args)
        Executes the script without loading it first.
        Parameters:
        file - the script to execute
        args - the commandline parameters for the script
      • isRunning

        public boolean isRunning()
        Returns whether the script is still running.
        Returns:
        true if the script is still running
      • addScriptFinishedListener

        public void addScriptFinishedListener​(ScriptExecutionListener l)
        Adds the given listener to its internal list.
        Parameters:
        l - the listener to add
      • removeScriptFinishedListener

        public void removeScriptFinishedListener​(ScriptExecutionListener l)
        Removes the given listener from its internal list.
        Parameters:
        l - the listener to remove
      • toString

        public java.lang.String toString()
        Returns the content as string.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the current content
      • runScript

        public static void runScript​(Script script,
                                     java.lang.String[] args)
                              throws java.lang.Exception
        Runs the specified script. All options that weren't "consumed" (like "-s" for the script filename), will be used as commandline arguments for the actual script.
        Parameters:
        script - the script object to use
        args - the commandline arguments
        Throws:
        java.lang.Exception - if execution fails