Workspace

public class Workspace extends Object

The root class for the Blockly model. Keeps track of all the global state used in the workspace.

Public Constructor Summary

Workspace(Context context, BlocklyController controller, BlockFactory factory)
Create a workspace.

Public Method Summary

void
addBlockFromTrash(Block trashedBlock)
Moves trashedBlock out of ERROR(/#mTrashCategory) and into ERROR(/#mRootBlocks).
void
addBlockToTrash(Block block)
Add a root block to the trash.
void
addRootBlock(Block block, boolean isNewBlock)
Adds a new block to the workspace as a root block.
String
addVariable(String requestedName, boolean allowRename)
Attempts to add a variable to the workspace.
BlockFactory
getBlockFactory()
Gets the BlockFactory being used by this workspace.
ConnectionManager
String
getId()
ProcedureManager
ArrayList<Block>
BlocklyCategory
BlocklyCategory
VariableInfo
getVariableInfo(String variable)
NameManager
getVariableNameManager()
Gets the NameManager.VariableNameManager being used by this workspace.
boolean
boolean
boolean
void
loadToolboxContents(int toolboxResId)
Loads the toolbox category, blocks, and buttons from the /raw/ resources directory.
void
loadToolboxContents(InputStream source)
Loads the toolbox category, blocks, and buttons.
void
loadToolboxContents(String toolboxXml)
Set up toolbox's contents.
void
loadTrashContents(InputStream source)
Loads a list of blocks into the trash from an input stream.
void
loadTrashContents(String trashXml)
Loads a list of blocks into the trash from an input stream.
void
loadWorkspaceContents(InputStream is)
Reads the workspace in from a XML stream.
void
loadWorkspaceContents(String xml)
Reads the workspace in from a XML stream.
boolean
removeRootBlock(Block block, boolean cleanupStats)
Remove a block from the workspace.
void
resetWorkspace()
Reset the workspace view when changing workspaces.
void
serializeToXml(OutputStream os)
Outputs the workspace as an XML string.

Inherited Method Summary

Public Constructors

public Workspace (Context context, BlocklyController controller, BlockFactory factory)

Create a workspace.

Parameters
context The context this workspace is associated with.
controller The controller for this Workspace.
factory The factory used to build blocks in this workspace.

Public Methods

public void addBlockFromTrash (Block trashedBlock)

Moves trashedBlock out of ERROR(/#mTrashCategory) and into ERROR(/#mRootBlocks).

Parameters
trashedBlock The Block to move.
Throws
IllegalArgumentException When trashedBlock is not found in ERROR(/#mTrashCategory).

public void addBlockToTrash (Block block)

Add a root block to the trash.

Parameters
block The block to put in the trash, possibly with descendants attached.

public void addRootBlock (Block block, boolean isNewBlock)

Adds a new block to the workspace as a root block.

Parameters
block The block to add to the root of the workspace.
isNewBlock Set when the block is new to the workspace (compared to moving it from some previous connection).
Throws
IllegalArgumentException If the block or its children are references to undefined procedures.

public String addVariable (String requestedName, boolean allowRename)

Attempts to add a variable to the workspace.

Parameters
requestedName The preferred variable name. Usually the user name.
allowRename Whether the variable name should be rename
Returns
  • The name that was added, if any. May be null if renaming is not allowed.

public BlockFactory getBlockFactory ()

Gets the BlockFactory being used by this workspace. This can be used to update or replace the set of known blocks.

Returns
  • The block factory used by this workspace.

public ConnectionManager getConnectionManager ()

Returns

public String getId ()

Returns
  • The string identifier of this workspace. Used by events.

public ProcedureManager getProcedureManager ()

Returns

public ArrayList<Block> getRootBlocks ()

public BlocklyCategory getToolboxContents ()

public BlocklyCategory getTrashCategory ()

public VariableInfo getVariableInfo (String variable)

Parameters
variable The variable name in question.
Returns
  • The usages of the variable, if any. Otherwise, null.

public NameManager getVariableNameManager ()

Gets the NameManager.VariableNameManager being used by this workspace. This can be used to get a list of variables in the workspace.

Returns
  • The name manager for variables in this workspace.

public boolean hasBlocks ()

Returns
  • if the workspace currently has any blocks.

public boolean hasDeletedBlocks ()

public boolean isRootBlock (Block block)

Parameters
block

public void loadToolboxContents (int toolboxResId)

Loads the toolbox category, blocks, and buttons from the /raw/ resources directory.

Parameters
toolboxResId The resource id of the set of blocks or block groups to show in the
Throws
BlockLoadingException If toolbox was not loaded. May wrap an IOException or another BlockLoadingException.

public void loadToolboxContents (InputStream source)

Loads the toolbox category, blocks, and buttons.

Parameters
source The source of the set of blocks or block groups to show in the toolbox.
Throws
BlockLoadingException If toolbox was not loaded. May wrap an IOException or another BlockLoadingException.

public void loadToolboxContents (String toolboxXml)

Set up toolbox's contents.

Parameters
toolboxXml The xml of the set of blocks or block groups to show in the toolbox.
Throws
BlockLoadingException If toolbox was not loaded. May wrap an IOException or another BlockLoadingException.

public void loadTrashContents (InputStream source)

Loads a list of blocks into the trash from an input stream. The trash is loaded like a toolbox and can have a name, color, and set of blocks to start with. Unlike a toolbox it may not have subcategories.

Parameters
source The source to initialize the trash.
Throws
BlockLoadingException If trash was not loaded. May wrap an IOException or another BlockLoadingException.

public void loadTrashContents (String trashXml)

Loads a list of blocks into the trash from an input stream. The trash is loaded like a toolbox and can have a name, color, and set of blocks to start with. Unlike a toolbox it may not have subcategories.

Parameters
trashXml The xml of the flyout to configure the trash.
Throws
BlockLoadingException If trash was not loaded. May wrap an IOException or another BlockLoadingException.

public void loadWorkspaceContents (InputStream is)

Reads the workspace in from a XML stream. This will clear the workspace and replace it with the contents of the xml.

Parameters
is The input stream to read from.
Throws
BlockLoadingException If workspace was not loaded. May wrap an IOException or another BlockLoadingException.

public void loadWorkspaceContents (String xml)

Reads the workspace in from a XML stream. This will clear the workspace and replace it with the contents of the xml.

Parameters
xml The XML source string to read from.
Throws
BlockLoadingException If toolbox was not loaded. May wrap an IOException or another BlockLoadingException.

public boolean removeRootBlock (Block block, boolean cleanupStats)

Remove a block from the workspace.

Parameters
block The block block to remove, possibly with descendants attached.
cleanupStats True if this block is being deleted and its connections and references should be removed.
Returns
  • True if the block was removed, false otherwise.

public void resetWorkspace ()

Reset the workspace view when changing workspaces. Removes old views and creates all necessary new views.

public void serializeToXml (OutputStream os)

Outputs the workspace as an XML string.

Parameters
os The output stream to write to.
Throws
BlocklySerializerException if there was a failure while serializing.