Sketch

processing.app

## Class Sketch

- Object
- - Sketch

-     public class Sketch
      extends Object

  Stores information about files in the current sketch.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Sketch(String path, Editor editor)` path is location of the main .pde file, because this is also simplest to use when opening the file from the finder/explorer.
    `Sketch(String path, Mode mode)` Used by the command-line version to create a sketch object.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `addFile(File sourceFile)` Add a file to the sketch.
    `static String` | `checkName(String origName)` Convert to sanitized name and alert the user if changes were made.
    `void` | `ensureExistence()` Make sure the sketch hasn't been moved or deleted by some nefarious user.
    `SketchCode[]` | `getCode()`
    `SketchCode` | `getCode(int index)`
    `int` | `getCodeCount()`
    `File` | `getCodeFolder()` Returns the location of the sketch's code folder.
    `int` | `getCodeIndex(SketchCode who)`
    `SketchCode` | `getCurrentCode()`
    `int` | `getCurrentCodeIndex()`
    `File` | `getDataFolder()` Returns the location of the sketch's data folder.
    `File` | `getFolder()` Returns the sketch folder.
    `File` | `getMainFile()` Returns a File object for the main .pde file for this sketch.
    `String` | `getMainFilePath()` Returns path to the main .pde file for this sketch.
    `String` | `getMainProgram()`
    `Mode` | `getMode()`
    `String` | `getName()` Returns the name of this sketch.
    `void` | `handleAddFile()` Prompt the user for a new file to the sketch, then call the other addFile() function to actually add it.
    `void` | `handleDeleteCode()` Remove a piece of code from the sketch and from the disk.
    `void` | `handleNewCode()` Handler for the New Code menu option.
    `void` | `handleNextCode()` Move to the next tab.
    `void` | `handlePrevCode()` Move to the previous tab.
    `void` | `handleRenameCode()` Handler for the Rename Code menu option.
    `boolean` | `hasCodeFolder()`
    `boolean` | `hasDataFolder()`
    `boolean` | `isModified()`
    `boolean` | `isReadOnly()` Returns true if this is a read-only sketch.
    `static boolean` | `isSanitaryName(String name)` Return true if the name is valid for a Processing sketch.
    `boolean` | `isUntitled()`
    `File` | `makeTempFolder()` Create a temporary folder that includes the sketch's name in its title.
    `File` | `prepareCodeFolder()` Create the code folder if it does not exist already.
    `File` | `prepareDataFolder()` Create the data folder if it does not exist already.
    `void` | `reload()` Reload the current sketch.
    `static String` | `sanitizeName(String origName)` Produce a sanitized name that fits our standards for likely to work.
    `boolean` | `save()` Save all code in the current sketch.
    `void` | `setCurrentCode(int which)` Change what file is currently being edited.
    `void` | `setModified(boolean state)` Sets the modified value for the code in the frontmost tab.
    `void` | `setUntitled(boolean untitled)`

    - ### Methods inherited from class Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Sketch

          public Sketch(String path,
                Mode mode)

      Used by the command-line version to create a sketch object.
      Parameters:
        `path` - location of the main .pde file
        `mode` - what flavor of sketch we're dealing with.

    - #### Sketch

          public Sketch(String path,
                Editor editor)
                 throws IOException

      path is location of the main .pde file, because this is also simplest to use when opening the file from the finder/explorer.
      Throws:
        `IOException`

  - ### Method Detail

    - #### reload

          public void reload()

      Reload the current sketch. Used to update the text area when an external editor is in use.

    - #### handleNewCode

          public void handleNewCode()

      Handler for the New Code menu option.

    - #### handleRenameCode

          public void handleRenameCode()

      Handler for the Rename Code menu option.

    - #### handleDeleteCode

          public void handleDeleteCode()

      Remove a piece of code from the sketch and from the disk.

    - #### handlePrevCode

          public void handlePrevCode()

      Move to the previous tab.

    - #### handleNextCode

          public void handleNextCode()

      Move to the next tab.

    - #### setModified

          public void setModified(boolean state)

      Sets the modified value for the code in the frontmost tab.

    - #### isModified

          public boolean isModified()

    - #### save

          public boolean save()
                       throws IOException

      Save all code in the current sketch. This just forces the files to save in place, so if it's an untitled (un-saved) sketch, saveAs() should be called instead. (This is handled inside Editor.handleSave()).
      Throws:
        `IOException`

    - #### handleAddFile

          public void handleAddFile()

      Prompt the user for a new file to the sketch, then call the other addFile() function to actually add it.

    - #### addFile

          public boolean addFile(File sourceFile)

      Add a file to the sketch.

      .pde or .java files will be added to the sketch folder.
      .jar, .class, .dll, .jnilib, and .so files will all be added to the "code" folder.
      All other files will be added to the "data" folder.

      If they don't exist already, the "code" or "data" folder will be created.

      Returns:
        true if successful.

    - #### setCurrentCode

          public void setCurrentCode(int which)

      Change what file is currently being edited. Changes the current tab index.
      1. store the String for the text of the current file.
      2. retrieve the String for the text of the new file.
      3. change the text that's visible in the text area

    - #### makeTempFolder

          public File makeTempFolder()

      Create a temporary folder that includes the sketch's name in its title.

    - #### ensureExistence

          public void ensureExistence()

      Make sure the sketch hasn't been moved or deleted by some nefarious user. If they did, try to re-create it and save. Only checks to see if the main folder is still around, but not its contents.

    - #### isReadOnly

          public boolean isReadOnly()

      Returns true if this is a read-only sketch. Used for the examples directory, or when sketches are loaded from read-only volumes or folders without appropriate permissions.

    - #### getName

          public String getName()

      Returns the name of this sketch. (The pretty name of the main tab.)

    - #### getMainFile

          public File getMainFile()

      Returns a File object for the main .pde file for this sketch.

    - #### getMainFilePath

          public String getMainFilePath()

      Returns path to the main .pde file for this sketch.

    - #### getFolder

          public File getFolder()

      Returns the sketch folder.

    - #### getDataFolder

          public File getDataFolder()

      Returns the location of the sketch's data folder. (It may not exist yet.)

    - #### hasDataFolder

          public boolean hasDataFolder()

    - #### prepareDataFolder

          public File prepareDataFolder()

      Create the data folder if it does not exist already. As a convenience, it also returns the data folder, since it's likely about to be used.

    - #### getCodeFolder

          public File getCodeFolder()

      Returns the location of the sketch's code folder. (It may not exist yet.)

    - #### hasCodeFolder

          public boolean hasCodeFolder()

    - #### prepareCodeFolder

          public File prepareCodeFolder()

      Create the code folder if it does not exist already. As a convenience, it also returns the code folder, since it's likely about to be used.

    - #### getCode

          public SketchCode[] getCode()

    - #### getCodeCount

          public int getCodeCount()

    - #### getCode

          public SketchCode getCode(int index)

    - #### getCodeIndex

          public int getCodeIndex(SketchCode who)

    - #### getCurrentCode

          public SketchCode getCurrentCode()

    - #### getCurrentCodeIndex

          public int getCurrentCodeIndex()

    - #### getMainProgram

          public String getMainProgram()

    - #### setUntitled

          public void setUntitled(boolean untitled)

    - #### isUntitled

          public boolean isUntitled()

    - #### checkName

          public static String checkName(String origName)

      Convert to sanitized name and alert the user if changes were made.

    - #### isSanitaryName

          public static boolean isSanitaryName(String name)

      Return true if the name is valid for a Processing sketch.

    - #### sanitizeName

          public static String sanitizeName(String origName)

      Produce a sanitized name that fits our standards for likely to work.

      Java classes have a wider range of names that are technically allowed (supposedly any Unicode name) than what we support. The reason for going more narrow is to avoid situations with text encodings and converting during the process of moving files between operating systems, i.e. uploading from a Windows machine to a Linux server, or reading a FAT32 partition in OS X and using a thumb drive.

      This helper function replaces everything but A-Z, a-z, and 0-9 with underscores. Also disallows starting the sketch name with a digit or underscore.

      In Processing 2.0, sketches can no longer begin with an underscore, because these aren't valid class names on Android.

    - #### getMode

          public Mode getMode()