Block

public class Block extends Observable<Block.Observer>

Base class for a Blockly Block.

Nested Class Summary

interface Block.Observer  
@interface Block.UpdateState  

Constant Summary

int UPDATE_COLOR
int UPDATE_COMMENT
int UPDATE_CONTEXT_MENU
int UPDATE_INPUTS_FIELDS_CONNECTIONS
int UPDATE_INPUTS_INLINE
int UPDATE_IS_COLLAPSED
int UPDATE_IS_DELETABLE
int UPDATE_IS_DISABLED
int UPDATE_IS_EDITABLE
int UPDATE_IS_MOVEABLE
int UPDATE_IS_SHADOW
int UPDATE_TOOLTIP
int UPDATE_WARNING

Inherited Field Summary

Public Method Summary

void
addAllBlockIds(List<String> outList)
Adds to outList all block ids for this block and all child blocks.
Block
deepCopy()
Creates a copy of this block and all inferior blocks connected to it.
void
getAllConnections(List<Connection> addTo)
Add all connections on the block to the given list.
List<Connection>
void
getAllConnectionsRecursive(List<Connection> addTo)
Add all connections on the block and its descendants to the given list.
int
String
BlocklyController
String
getEventWorkspaceId()
getEventWorkspaceId returns the id of the "workspace", as seen by the event framework.
Field
getFieldByName(String targetName)
Searches through all of the fields on all of the block's inputs.
String
getId()
Input
getInputByName(String targetName)
Searches through the block's list of inputs and returns the first one with the given name.
List<Input>
boolean
boolean
Block
Connection
getLastUnconnectedInputConnection()
Walks the chain of blocks in this block, at each stage checking if there are multiple value inputs.
final String
final Mutator
final String
Block
Connection
Input
Connection
Block
Connection
WorkspacePoint
getPosition()
Gets the position of this block.
Block
Connection
Block
getRootBlock()
Find the highest block in the hierarchy that this Block descends from.
String
Connection
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
boolean
void
reshape(List<Input> newInputList, Connection updatedOutput, Connection updatedPrev, Connection updatedNext)
reshape() updates the inputs and all connections with potentially new values, changing the shape of the block.
void
reshape(List<Input> newInputList)
Convenience form of reshape(List, Connection, Connection, Connection) that preserves output, previous, and next connections.
void
serialize(XmlSerializer serializer, boolean rootBlock, IOOptions options)
Writes information about the editable parts of the block as XML.
void
setCollapsed(boolean collapsed)
Users can collapse blocks, to render them in a condensed, text-only form.
void
setComment(String comment)
Set the comment on this block.
void
setDeletable(boolean deletable)
Users can delete (i.e., drag to trash) blocks that are deletable.
void
setDisabled(boolean disabled)
Disabling a block is effectively a way to comment out a block.
void
setEditable(boolean editable)
If a block is editable, the user can edit the values of the block's fields.
void
setEventCallback(BlocklyController.EventsCallback callback)
Sets an event callback that will receive BlocklyEvents for the lifetime of the block.
void
setEventWorkspaceId(String eventWorkspaceId)
Sets the block's "workspace" id, for the purposes of events.
void
setInputsInline(boolean inputsInline)
Set whether value inputs should be displayed inline (true), or on separate rows (false).
void
setMovable(boolean movable)
If a block is movable, users can drag it as a root block on the workspace, or disconnect it from its parent block.
final void
setMutation(String newValue)
Updates the mutation value of the block.
void
setPosition(float x, float y)
Set the position of this block in the workspace.
void
setShadow(boolean isShadow)
Configures whether this block should be a shadow block.
String
toString()
This method returns a string describing this Block in developer terms (type name and ID; English only).

Protected Method Summary

void

Inherited Method Summary

Constants

public static final int UPDATE_COLOR

Constant Value: 2

public static final int UPDATE_COMMENT

Constant Value: 4

public static final int UPDATE_CONTEXT_MENU

Constant Value: 512

public static final int UPDATE_INPUTS_FIELDS_CONNECTIONS

Constant Value: 1

public static final int UPDATE_INPUTS_INLINE

Constant Value: 1024

public static final int UPDATE_IS_COLLAPSED

Constant Value: 32

public static final int UPDATE_IS_DELETABLE

Constant Value: 128

public static final int UPDATE_IS_DISABLED

Constant Value: 16

public static final int UPDATE_IS_EDITABLE

Constant Value: 64

public static final int UPDATE_IS_MOVEABLE

Constant Value: 2048

public static final int UPDATE_IS_SHADOW

Constant Value: 8

public static final int UPDATE_TOOLTIP

Constant Value: 256

public static final int UPDATE_WARNING

Constant Value: 4096

Public Methods

public void addAllBlockIds (List<String> outList)

Adds to outList all block ids for this block and all child blocks. This will not include occluded shadow block ids.

Parameters
outList List of ids to add to.

public Block deepCopy ()

Creates a copy of this block and all inferior blocks connected to it.

Returns
  • A new block tree with a copy of this block as the root.

public void getAllConnections (List<Connection> addTo)

Add all connections on the block to the given list.

Parameters
addTo The list to update.

public List<Connection> getAllConnections ()

Returns
  • A list of connections (including inputs, next, previous and outputs) on this block.

public void getAllConnectionsRecursive (List<Connection> addTo)

Add all connections on the block and its descendants to the given list.

Parameters
addTo The list to update.

public int getColor ()

Returns
  • The color this block should be drawn in.

public String getComment ()

Returns
  • The comment on this block.

public BlocklyController getController ()

Returns
  • The controller for this Blockly instance.

public String getEventWorkspaceId ()

getEventWorkspaceId returns the id of the "workspace", as seen by the event framework. This might be the workspace id, if attached to a Workspace. It may also be WORKSPACE_ID_TOOLBOX or WORKSPACE_ID_TRASH. If the block is not attached to to any of these (directly or indirectly), the event workspace should be null.

Returns
  • The id of the "workspace", as seen by the event framework. Null if not attached.

public Field getFieldByName (String targetName)

Searches through all of the fields on all of the block's inputs. Returns the first field with the given name.

Parameters
targetName The name of the field to search for.
Returns
  • The first Field with that name.

public String getId ()

Returns
  • The unique identifier of the block. Not for display.

public Input getInputByName (String targetName)

Searches through the block's list of inputs and returns the first one with the given name.

Parameters
targetName The name of the input to search for.
Returns
  • The first Input with that name.

public List<Input> getInputs ()

Returns
  • The set of inputs on this block.

public boolean getInputsInline ()

Returns
  • The current state of the flag for displaying inputs in-line.

public boolean getInputsInlineModified ()

Returns
  • Whether the flag for displaying inputs in-line has been explicitly set.

public Block getLastBlockInSequence ()

Returns
  • The Block for the last non-shadow child in this sequence, possibly itself.

public Connection getLastUnconnectedInputConnection ()

Walks the chain of blocks in this block, at each stage checking if there are multiple value inputs. If there is only one value input at each block, follows that input to the next block. If at any point there is more than one value input on a block or there are no value inputs on a block, returns null. If the next block in the sequence is a shadow block this returns the connection before the shadow.

Returns
  • the Connection on the only input on the last block in the chain.

public final String getMutation ()

Returns
  • The string form of the mutation.

public final Mutator getMutator ()

Returns
  • The block's Mutator, if any. Otherwise null.

public final String getMutatorId ()

Returns
  • The block's Mutator id, if any. Otherwise null.

public Block getNextBlock ()

Returns
  • The block connected to this block's next connection or null.

public Connection getNextConnection ()

Returns
  • The block's next Connection.

public Input getOnlyValueInput ()

Returns
  • The only value input on the block, or null if there are zero or more than one.

public Connection getOutputConnection ()

Returns
  • The block's output Connection.

public Block getParentBlock ()

Returns
  • The block connected to the output or previous Connection, if present. Otherwise null.

public Connection getParentConnection ()

Returns
  • The next or input Connection this block is connected to, or null.

public WorkspacePoint getPosition ()

Gets the position of this block. The position is the top left for LTR configurations and the top right for RTL configurations. This position is only meaningful for top level blocks. All other blocks will have a position that is dependent on the block rendering relative to the top level block.

Returns
  • The coordinates of the start corner of this block in Workspace coordinates.

public Block getPreviousBlock ()

Returns
  • The block connected to this block's previous connection or null.

public Connection getPreviousConnection ()

Returns
  • The block's previous Connection.

public Block getRootBlock ()

Find the highest block in the hierarchy that this Block descends from.

Returns
  • The highest block found.

public String getType ()

Returns
  • The name of the block. Not for display.

public Connection getUpwardsConnection ()

Returns
  • The previous or output Connection on this block, or null.

public boolean hasInputs ()

Returns
  • True if and only if this block has inputs, i.e., {#link mInputList} is non-empty.

public boolean isCollapsed ()

Returns
  • Whether the block is collapsed.

public boolean isDeletable ()

Returns
  • Whether the user can delete / trash this block.

public boolean isDisabled ()

Returns
  • Whether the block is disabled, possibly via a disabled parent or ancestor block.

public boolean isDisabledBlock ()

Returns
  • Whether the block is disabled. Does not check if parent or ancestor is disabled.

public boolean isDraggable ()

Returns
  • True if this block can be dragged by the user, false otherwise.

public boolean isEditable ()

Returns
  • Whether the user can edit field values.

public boolean isMovable ()

Returns
  • Whether the block can be moved.

public boolean isShadow ()

Returns
  • True if this is a shadow block, false otherwise.

public void reshape (List<Input> newInputList, Connection updatedOutput, Connection updatedPrev, Connection updatedNext)

reshape() updates the inputs and all connections with potentially new values, changing the shape of the block. This method should only be called by the constructor, or Mutators.

Changes to Input and their Field change by updating the whole input list. Inputs can be reused, and must be reused if blocks are to remain connected to a block. Removed inputs must be previously disconnected (moved or deleted), and added inputs must also be empty (i.e., not connect to child blocks).

Similarly, Connections that should remain connected should be reused, and added/removed connections must not be connected to another block. All connections must be constructed with the correct connection type for their position.

Parameters
newInputList The new list of inputs.
updatedOutput The updated output connection, if any.
updatedPrev The updated previous connection, if any.
updatedNext The updated next connection, if any.

public void reshape (List<Input> newInputList)

Convenience form of reshape(List, Connection, Connection, Connection) that preserves output, previous, and next connections. This method should only be called by Mutators.

Parameters
newInputList The new list of inputs.

public void serialize (XmlSerializer serializer, boolean rootBlock, IOOptions options)

Writes information about the editable parts of the block as XML.

Parameters
serializer The XmlSerializer to write to.
rootBlock
options I/O options.
Throws
IOException

public void setCollapsed (boolean collapsed)

Users can collapse blocks, to render them in a condensed, text-only form. Warning: Not yet supported by Android BlockViews (#307).

Parameters
collapsed Whether the block should be collapsed.

public void setComment (String comment)

Set the comment on this block.

Parameters
comment The text of the comment.

public void setDeletable (boolean deletable)

Users can delete (i.e., drag to trash) blocks that are deletable. Blocks are deletable by default. Warning: The Android Dragger implementation does not respect deletable false state (#305).

public void setDisabled (boolean disabled)

Disabling a block is effectively a way to comment out a block. The block is skipped during code generation, including all connection value input and statement input children. Next statement children are not effected. By default, blocks are not disabled. Warning: BlockViews do not yet render disabled state. (#306).

public void setEditable (boolean editable)

If a block is editable, the user can edit the values of the block's fields. The editable state does not affect whether child blocks can be connected or disconnected. Blocks are editable by default. Warning: FieldViews do not yet respect block disabled state (#303).

public void setEventCallback (BlocklyController.EventsCallback callback)

Sets an event callback that will receive BlocklyEvents for the lifetime of the block.

Parameters
callback The block's callback, or null to unset.

public void setEventWorkspaceId (String eventWorkspaceId)

Sets the block's "workspace" id, for the purposes of events. If the block is on a proper Workspace, it should be the getId(). If the block is in the toolbox or trash, the value should be WORKSPACE_ID_TOOLBOX or WORKSPACE_ID_TRASH, respectively. When the block is detached, the event workspace should be set to null. Setting this values recursively sets the value on all child blocks.

Parameters
eventWorkspaceId The workspace id, as defined by event framework.

public void setInputsInline (boolean inputsInline)

Set whether value inputs should be displayed inline (true), or on separate rows (false).

Parameters
inputsInline True if value inputs should be show in a single line. Otherwise, false.

public void setMovable (boolean movable)

If a block is movable, users can drag it as a root block on the workspace, or disconnect it from its parent block. Blocks are movable by default.

public final void setMutation (String newValue)

Updates the mutation value of the block. It requires the block to have an assigned Mutator, and the mutator must be able to parse the assigned string, or a BlockLoadingException will be thrown. The mutation string is a XML <mutation> tag in its serialized string form.

Parameters
newValue The new mutation value, a <mutation> tag in string form.
Throws
BlockLoadingException If the mutator is not able to parse the mutation.

public void setPosition (float x, float y)

Set the position of this block in the workspace.

Parameters
x The workspace x position.
y The workspace y position.

public void setShadow (boolean isShadow)

Configures whether this block should be a shadow block. This should only be called during block initialization.

Parameters
isShadow If true the block will act as a shadow.

public String toString ()

This method returns a string describing this Block in developer terms (type name and ID; English only). Intended to on be used in console logs and errors.

Returns
  • The description.

Protected Methods

protected void finalize ()

Throws
Throwable