BlockTemplate

public class BlockTemplate extends Object

Template of a block, describing the initial state of a new block. Most methods return the template, for use in chaining.

Nested Class Summary

class BlockTemplate.FieldValue  

Field Summary

protected String mCommentText
protected Block mCopySource
protected BlockDefinition mDefinition
protected List<BlockTemplate.FieldValue> mFieldValues Ordered list of field names and string values, as loaded during XML deserialization.
protected String mId
protected Boolean mInlineInputs
protected Boolean mIsCollapsed
protected Boolean mIsDeletable
protected Boolean mIsDisabled
protected Boolean mIsEditable
protected Boolean mIsMovable
protected Boolean mIsShadow
protected String mMutation
protected Block mNextChild
protected Block mNextShadow
protected WorkspacePoint mPosition
protected String mTypeName

Public Constructor Summary

BlockTemplate()
Create a new block template.
BlockTemplate(String blockDefinitionName)
Create a new block descriptor for the named block type.
BlockTemplate(BlockTemplate src)
Copy constructor to create a new description based on a prior.

Public Method Summary

void
applyMutableState(Block block)
Applies the mutable state (block object values that can be updated after construction) described by a template.
BlockTemplate
atPosition(float x, float y)
Sets the position of the resulting block.
BlockTemplate
collapsed(boolean isCollapsed)
BlockTemplate
copyOf(Block source)
Sets a block as an example to copy.
BlockTemplate
deletable(boolean isDeletable)
BlockTemplate
disabled(boolean isDisabled)
BlockTemplate
editable(boolean isEditable)
BlockTemplate
fromDefinition(BlockDefinition definition)
Sets the block definition manually.
BlockTemplate
fromJson(JSONObject json)
Sets the block definition manually from a JSON object.
BlockTemplate
fromJson(String json)
Sets the block definition manually from a JSON string.
BlockTemplate
movable(boolean isMovable)
BlockTemplate
ofType(String definitionName)
Sets the block definition by name.
BlockTemplate
shadow()
Declares the new block will be a shadow block.
BlockTemplate
shadow(boolean isShadow)
Declares whether the new block will be a shadow block.
String
String
toString(String nounPrefix)
Constructs a string that describes this template, using the provided descriptive noun.
BlockTemplate
withComment(String commentText)
BlockTemplate
withId(String id)
Sets the id of the block to be created.
BlockTemplate
withInlineInputs(boolean isInline)
BlockTemplate
withMutation(String mutationString)

Inherited Method Summary

Fields

protected String mCommentText

protected Block mCopySource

protected BlockDefinition mDefinition

protected List<BlockTemplate.FieldValue> mFieldValues

Ordered list of field names and string values, as loaded during XML deserialization.

protected String mId

protected Boolean mInlineInputs

protected Boolean mIsCollapsed

protected Boolean mIsDeletable

protected Boolean mIsDisabled

protected Boolean mIsEditable

protected Boolean mIsMovable

protected Boolean mIsShadow

protected String mMutation

protected Block mNextChild

protected Block mNextShadow

protected WorkspacePoint mPosition

protected String mTypeName

Public Constructors

public BlockTemplate ()

Create a new block template.

public BlockTemplate (String blockDefinitionName)

Create a new block descriptor for the named block type.

Parameters
blockDefinitionName The name of the block definition to use.

public BlockTemplate (BlockTemplate src)

Copy constructor to create a new description based on a prior.

Parameters
src

Public Methods

public void applyMutableState (Block block)

Applies the mutable state (block object values that can be updated after construction) described by a template. Called after extensions are applied to the block (and thus event handles and mutators have been registered).

Parameters
block The block to update.

public BlockTemplate atPosition (float x, float y)

Sets the position of the resulting block.

Parameters
x The horizontal coordinate of the workspace position.
y The vertical coordinate of the workspace position.
Returns
  • This block template, for chaining.

public BlockTemplate collapsed (boolean isCollapsed)

Parameters
isCollapsed The collapsed state of the resulting block.
Returns
  • This block template, for chaining.

public BlockTemplate copyOf (Block source)

Sets a block as an example to copy. The source will be serialized and deserialized in the copy process. No additional data (e.g., block definition or mutable state) will be stored in this template.

 blockFactory.obtainBlockFrom(new BlockTemplate().copyOf(otherBlock));
 

Parameters
source The JSON definition of the block.
Returns
  • This block template, for chaining.

public BlockTemplate deletable (boolean isDeletable)

Parameters
isDeletable Whether users will be able to delete the resulting block.
Returns
  • This block template, for chaining.

public BlockTemplate disabled (boolean isDisabled)

Parameters
isDisabled Whether the result block will be disabled.
Returns
  • This block template, for chaining.

public BlockTemplate editable (boolean isEditable)

Parameters
isEditable Whether fields on the resulting block will be editable.
Returns
  • This block template, for chaining.

public BlockTemplate fromDefinition (BlockDefinition definition)

Sets the block definition manually. Such definitions are might not be registered in the BlockFactory, and so such blocks will not load from XML. Some operations may fail, including duplication. That said, such definitions can be useful for one-off blocks, especially in test code.

blockFactory.obtainBlockFrom(new BlockTemplate().fromDefinition(booleanBlockDefinition));
 

Parameters
definition The definition of the block.
Returns
  • This block template, for chaining.

public BlockTemplate fromJson (JSONObject json)

Sets the block definition manually from a JSON object. Such definitions are not registered in the BlockFactory, and so such blocks will not load from XML. Some operations may fail, including duplication. That said, such definitions can be useful for one-off blocks, especially in test code.

 blockFactory.obtainBlockFrom(new BlockTemplate().fromJson(myJsonDefinition));
 

Parameters
json The JSON definition of the block.
Returns
  • This block template, for chaining.

public BlockTemplate fromJson (String json)

Sets the block definition manually from a JSON string. Such definitions are not registered in the BlockFactory, and so such blocks will not load from XML. Some operations may fail, including duplication. That said, such definitions can be useful for one-off blocks, especially in test code.

blockFactory.obtainBlockFrom(new BlockTemplate().fromJson("{\"output\": \"Number\"}"));
 

Parameters
json The JSON definition of the block.
Returns
  • This block template, for chaining.

public BlockTemplate movable (boolean isMovable)

Parameters
isMovable Whether users will be able to move the resulting block.
Returns
  • This block template, for chaining.

public BlockTemplate ofType (String definitionName)

Sets the block definition by name.

 blockFactory.obtainBlockFrom(new BlockTemplate().ofType("math_number"));
 

Parameters
definitionName The name of the definition, as registered with the block.
Returns
  • This block template, for chaining.

public BlockTemplate shadow ()

Declares the new block will be a shadow block. This is true, even if the template copies a non-shadow block via copyOf(Block), independent of call order. This is a fluent API shorthand for template.shadow(true);.

 blockFactory.obtainBlockFrom(new BlockTemplate().shadow().ofType("math_number"));
 

Returns
  • This block template, for chaining.

public BlockTemplate shadow (boolean isShadow)

Declares whether the new block will be a shadow block. Shadow blocks are usually used as default values for inputs, especially in ToolBox XMl.

blockFactory.obtainBlockFrom(new BlockTemplate().copyOf(originalShadow).shadow(false));
 

Parameters
isShadow Whether the block will be a shadow.
Returns
  • This block template, for chaining.

public String toString ()

Returns
  • a string describing this template.

public String toString (String nounPrefix)

Constructs a string that describes this template, using the provided descriptive noun.

Parameters
nounPrefix A generic noun, usually "BlockTemplate" or "Block".
Returns
  • a string describing this template, using the noun as a prefix.

public BlockTemplate withComment (String commentText)

Parameters
commentText The comment text of the resulting block.
Returns
  • This block template, for chaining.

public BlockTemplate withId (String id)

Sets the id of the block to be created. If the id is already in use, a new id will be generated for the new block.

 blockFactory.obtainBlockFrom(new BlockTemplate().withId("my-block"));
 

Parameters
id The id of the block to be created.
Returns
  • This block template, for chaining.

public BlockTemplate withInlineInputs (boolean isInline)

Parameters
isInline Whether inputs will be inlined on the resulting block.
Returns
  • This block template, for chaining.

public BlockTemplate withMutation (String mutationString)

Parameters
mutationString The mutator's serialized state. I.e, the <mutation> element.
Returns
  • This block template, for chaining.