processing.app
Class Sketch

java.lang.Object
  extended by processing.app.Sketch

public class Sketch
extends java.lang.Object

Stores information about files in the current sketch


Constructor Summary
Sketch(Editor editor, java.lang.String path)
          path is location of the main .pde file, because this is also simplest to use when opening the file from the finder/explorer.
 
Method Summary
 boolean addFile(java.io.File sourceFile)
          Add a file to the sketch.
 java.lang.String build(java.lang.String buildPath)
          Preprocess and compile all the code for this sketch.
static java.lang.String checkName(java.lang.String origName)
          Convert to sanitized name and alert the user if changes were made.
 boolean exportApplet(java.lang.String appletPath)
          Handle export to applet.
 boolean exportApplication(java.lang.String destPath, int exportPlatform)
          Export to application without GUI.
 boolean exportApplicationPrompt()
           
 java.lang.String getAppletClassName2()
           
 java.lang.String getClassPath()
           
 SketchCode[] getCode()
           
 SketchCode getCode(int index)
           
 int getCodeCount()
           
 java.io.File getCodeFolder()
          Returns the location of the sketch's code folder.
 int getCodeIndex(SketchCode who)
           
 SketchCode getCurrentCode()
           
 java.io.File getDataFolder()
          Returns the location of the sketch's data folder.
 java.lang.String getDefaultExtension()
          Returns the default extension for this editor setup.
 java.lang.String[] getExtensions()
          Returns a String[] array of proper extensions.
 java.io.File getFolder()
          Returns the sketch folder.
 java.lang.String getLibraryPath()
           
 java.lang.String getMainFilePath()
          Returns path to the main .pde file for this sketch.
 java.lang.String getName()
          Returns the name of this sketch.
 java.io.File getPrimaryFile()
          Returns a file object for the primary .pde 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 hasDefaultExtension(SketchCode code)
          True if the specified code has the default file extension.
 boolean hideExtension(java.lang.String what)
          True if the specified extension should be hidden when shown on a tab.
 void importLibrary(java.lang.String jarPath)
          Add import statements to the current tab for all of packages inside the specified jar file.
 boolean isDefaultExtension(java.lang.String what)
          True if the specified extension is the default file extension.
 boolean isModified()
           
 boolean isReadOnly()
          Returns true if this is a read-only sketch.
static boolean isSanitaryName(java.lang.String name)
          Return true if the name is valid for a Processing sketch.
 boolean isUntitled()
           
 java.io.File prepareCodeFolder()
          Create the code folder if it does not exist already.
 java.io.File prepareDataFolder()
          Create the data folder if it does not exist already.
 java.lang.String preprocess(java.lang.String buildPath)
          Build all the code for this sketch.
static java.lang.String sanitizeName(java.lang.String origName)
          Produce a sanitized name that fits our standards for likely to work.
 boolean save()
          Save all code in the current sketch.
static java.lang.String scrubComments(java.lang.String what)
          Replace all commented portions of a given String as spaces.
 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 u)
           
 boolean validExtension(java.lang.String what)
          Check this extension (no dots, please) against the list of valid extensions.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Sketch

public Sketch(Editor editor,
              java.lang.String path)
       throws java.io.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:
java.io.IOException
Method Detail

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 java.io.IOException
Save all code in the current sketch.

Throws:
java.io.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(java.io.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.

importLibrary

public void importLibrary(java.lang.String jarPath)
Add import statements to the current tab for all of packages inside the specified jar file.


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


preprocess

public java.lang.String preprocess(java.lang.String buildPath)
                            throws RunnerException
Build all the code for this sketch. In an advanced program, the returned class name could be different, which is why the className is set based on the return value. A compilation error will burp up a RunnerException. Setting purty to 'true' will cause exception line numbers to be incorrect. Unless you know the code compiles, you should first run the preprocessor with purty set to false to make sure there are no errors, then once successful, re-export with purty set to true.

Parameters:
buildPath - Location to copy all the .java files
Returns:
null if compilation failed, main class name if not
Throws:
RunnerException

build

public java.lang.String build(java.lang.String buildPath)
                       throws RunnerException
Preprocess and compile all the code for this sketch. In an advanced program, the returned class name could be different, which is why the className is set based on the return value. A compilation error will burp up a RunnerException.

Returns:
null if compilation failed, main class name if not
Throws:
RunnerException

exportApplet

public boolean exportApplet(java.lang.String appletPath)
                     throws RunnerException,
                            java.io.IOException
Handle export to applet.

Throws:
RunnerException
java.io.IOException

scrubComments

public static java.lang.String scrubComments(java.lang.String what)
Replace all commented portions of a given String as spaces. Utility function used here and in the preprocessor.


exportApplicationPrompt

public boolean exportApplicationPrompt()
                                throws java.io.IOException,
                                       RunnerException
Throws:
java.io.IOException
RunnerException

exportApplication

public boolean exportApplication(java.lang.String destPath,
                                 int exportPlatform)
                          throws java.io.IOException,
                                 RunnerException
Export to application without GUI.

Throws:
java.io.IOException
RunnerException

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.


hideExtension

public boolean hideExtension(java.lang.String what)
True if the specified extension should be hidden when shown on a tab. For Processing, this is true for .pde files. (Broken out for subclasses.)


hasDefaultExtension

public boolean hasDefaultExtension(SketchCode code)
True if the specified code has the default file extension.


isDefaultExtension

public boolean isDefaultExtension(java.lang.String what)
True if the specified extension is the default file extension.


validExtension

public boolean validExtension(java.lang.String what)
Check this extension (no dots, please) against the list of valid extensions.


getDefaultExtension

public java.lang.String getDefaultExtension()
Returns the default extension for this editor setup.


getExtensions

public java.lang.String[] getExtensions()
Returns a String[] array of proper extensions.


getName

public java.lang.String getName()
Returns the name of this sketch. (The pretty name of the main tab.)


getPrimaryFile

public java.io.File getPrimaryFile()
Returns a file object for the primary .pde of this sketch.


getMainFilePath

public java.lang.String getMainFilePath()
Returns path to the main .pde file for this sketch.


getFolder

public java.io.File getFolder()
Returns the sketch folder.


getDataFolder

public java.io.File getDataFolder()
Returns the location of the sketch's data folder. (It may not exist yet.)


prepareDataFolder

public java.io.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 java.io.File getCodeFolder()
Returns the location of the sketch's code folder. (It may not exist yet.)


prepareCodeFolder

public java.io.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.


getClassPath

public java.lang.String getClassPath()

getLibraryPath

public java.lang.String getLibraryPath()

getCode

public SketchCode[] getCode()

getCodeCount

public int getCodeCount()

getCode

public SketchCode getCode(int index)

getCodeIndex

public int getCodeIndex(SketchCode who)

getCurrentCode

public SketchCode getCurrentCode()

setUntitled

public void setUntitled(boolean u)

isUntitled

public boolean isUntitled()

getAppletClassName2

public java.lang.String getAppletClassName2()

checkName

public static java.lang.String checkName(java.lang.String origName)
Convert to sanitized name and alert the user if changes were made.


isSanitaryName

public static boolean isSanitaryName(java.lang.String name)
Return true if the name is valid for a Processing sketch.


sanitizeName

public static java.lang.String sanitizeName(java.lang.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.