BlockViewFactory

public abstract class BlockViewFactory extends Object
Known Direct Subclasses

Base factory class responsible for creating all the BlockViews, InputViews, and FieldViews for a given block representation. Complete view trees are constructed via calls to buildBlockGroupTree(Block, ConnectionManager, BlockTouchHandler) or buildBlockViewTree(Block, BlockGroup, ConnectionManager, BlockTouchHandler).

Subclasses must override buildBlockView(Block, List, ConnectionManager, BlockTouchHandler), buildInputView(Input, List), and buildFieldView(Field). They may also override buildBlockGroup(), to provide customized implementations to the container view.

Nested Class Summary

interface BlockViewFactory.MutatorToggleListener Handles a user toggling the UI for a mutator on a block. 

Field Summary

protected final Map<String, WeakReference<BlockView extends BlockView>> mBlockIdToView
protected Context mContext Context for creating or loading views.
protected WorkspaceHelper mHelper Helper for doing conversions and style lookups.
protected BlockViewFactory.MutatorToggleListener mMutatorListener The callback to use when something triggers showing or hiding a block's mutator.
protected VariableRequestCallback mVariableCallback The callback to use for views that can request changes to the list of variables.
protected NameManager mVariableNameManager Name manager for the list of variables in this instance of Blockly.

Protected Constructor Summary

BlockViewFactory(Context context, WorkspaceHelper helper)

Public Method Summary

BlockGroup
final BlockGroup
buildBlockGroupTree(Block rootBlock, ConnectionManager connectionManager, BlockTouchHandler touchHandler)
Creates a BlockGroup for the given block and its children using the workspace's default style.
final BlockView
buildBlockViewTree(Block block, BlockGroup parentGroup, ConnectionManager connectionManager, BlockTouchHandler touchHandler)
Called to construct the complete hierarchy of views representing a Block and its subcomponents, added to parentGroup.
int
getDragAndDropFlags()
Constructs the drag and drop flags used by ERROR(/ViewCompat#startDragAndDrop).
MutatorFragment
getMutatorFragment(Mutator mutator)
Retrieves a MutatorFragment for the given mutator.
final BlockView
getView(Block block)
This returns the view constructed to represent a Block.
WorkspaceHelper
boolean
hasUiForMutator(String mutatorId)
BlockView
rebuildBlockView(BlockView original)
Rebuilds a BlockView based on the latest model state, replacing it in-place in the view tree.
void
registerMutatorUi(String mutatorId, Factory mutatorUiFactory)
Registers a MutatorFragment.Factory for the named mutator type.
void
setMutatorToggleListener(BlockViewFactory.MutatorToggleListener listener)
Sets the listener to call when the user toggles a mutator.
void
setVariableNameManager(NameManager variableNameManager)
Set the NameManager being used to track variables in the workspace.
void
setVariableRequestCallback(VariableRequestCallback callback)
Sets the callback to use for variable view events, such as the user selected delete/rename.

Protected Method Summary

abstract BlockView
buildBlockView(Block block, List<InputView> inputViews, ConnectionManager connectionManager, BlockTouchHandler touchHandler)
Build and populate the BlockView for block, using the provided InputViews.
FieldView
buildFieldView(Field field)
Build and populate the FieldView for field.
abstract InputView
buildInputView(Input input, List<FieldView> fieldViews)
Build and populate the InputView for input.
List<InputView>
buildInputViews(Block block, ConnectionManager connectionManager, BlockTouchHandler touchHandler)
SpinnerAdapter
final void
unregisterView(BlockView blockView)
Removes the mapping to this view from its block.

Inherited Method Summary

Fields

protected final Map<String, WeakReference<BlockView extends BlockView>> mBlockIdToView

protected Context mContext

Context for creating or loading views.

protected WorkspaceHelper mHelper

Helper for doing conversions and style lookups.

protected BlockViewFactory.MutatorToggleListener mMutatorListener

The callback to use when something triggers showing or hiding a block's mutator.

protected VariableRequestCallback mVariableCallback

The callback to use for views that can request changes to the list of variables.

protected NameManager mVariableNameManager

Name manager for the list of variables in this instance of Blockly.

Protected Constructors

protected BlockViewFactory (Context context, WorkspaceHelper helper)

Parameters
context
helper

Public Methods

public BlockGroup buildBlockGroup ()

Returns
  • A new, empty BlockGroup container view for a sequence of blocks.

public final BlockGroup buildBlockGroupTree (Block rootBlock, ConnectionManager connectionManager, BlockTouchHandler touchHandler)

Creates a BlockGroup for the given block and its children using the workspace's default style.

Parameters
rootBlock The root block to generate a view for.
connectionManager The ConnectionManager to update when moving connections.
touchHandler The BlockTouchHandler to manage all touches.
Returns
  • A view for the block.

public final BlockView buildBlockViewTree (Block block, BlockGroup parentGroup, ConnectionManager connectionManager, BlockTouchHandler touchHandler)

Called to construct the complete hierarchy of views representing a Block and its subcomponents, added to parentGroup.

Parameters
block The root block to generate a view for.
parentGroup T
connectionManager The ConnectionManager to update when moving connections.
touchHandler The BlockTouchHandler to manage all touches.
Returns
  • A view for the block and all its descendants.

public int getDragAndDropFlags ()

Constructs the drag and drop flags used by ERROR(/ViewCompat#startDragAndDrop).

public MutatorFragment getMutatorFragment (Mutator mutator)

Retrieves a MutatorFragment for the given mutator. They fragment may be displayed to the user. If a fragment could not be found null is returned instead.

Parameters
mutator The mutator to return a fragment for.
Returns

public final BlockView getView (Block block)

This returns the view constructed to represent a Block. Each block is only allowed one view instance among the views managed by this factory (including WorkspaceFragment and any FlyoutFragments. Views are constructed in buildBlockViewTree(Block, BlockGroup, ConnectionManager, BlockTouchHandler), either directly or via recursion. If the block view has not been constructed, this method will return null.

Calling unlinkModel() (possibly via unlinkModel()) will disconnect this view from its model, and will it no longer be returned from this method.

Parameters
block The Block to get the view for.
Returns
  • The previously constructed and active view of block. Otherwise null.

public WorkspaceHelper getWorkspaceHelper ()

public boolean hasUiForMutator (String mutatorId)

Parameters
mutatorId
Returns
  • True if there is a UI registered for the given mutator id.

public BlockView rebuildBlockView (BlockView original)

Rebuilds a BlockView based on the latest model state, replacing it in-place in the view tree. Some minor view state is not propagated (pressed, highlight, text selection, etc.) during the replacement.

Parameters
original The original BlockView to be rebuilt and replaced.
Returns
  • The newly reconstructed BlockView.
Throws
ClassCastException If original is connected to a parent that is not a ViewGroup.

public void registerMutatorUi (String mutatorId, Factory mutatorUiFactory)

Registers a MutatorFragment.Factory for the named mutator type. Mutators that have a UI registered will display a user affordance for opening and closing the UI.

Parameters
mutatorId The name / id of this mutator type.
mutatorUiFactory The factory that builds fragments for this mutator type.

public void setMutatorToggleListener (BlockViewFactory.MutatorToggleListener listener)

Sets the listener to call when the user toggles a mutator. This is typically in response to a mutator button being tapped on a block.

Parameters
listener The listener to call when a user toggles a mutator.

public void setVariableNameManager (NameManager variableNameManager)

Set the NameManager being used to track variables in the workspace.

Parameters
variableNameManager The name manager for the variables in the associated workspace.

public void setVariableRequestCallback (VariableRequestCallback callback)

Sets the callback to use for variable view events, such as the user selected delete/rename.

Parameters
callback The callback to set on variable field views.

Protected Methods

protected abstract BlockView buildBlockView (Block block, List<InputView> inputViews, ConnectionManager connectionManager, BlockTouchHandler touchHandler)

Build and populate the BlockView for block, using the provided InputViews.

This method should not recurse the model to generate more than one view. buildBlockViewTree(Block, BlockGroup, ConnectionManager, BlockTouchHandler) will traverse the model and call this method for each Block.

Parameters
block The Block to build a view for.
inputViews The list of InputViews in this block.
connectionManager The ConnectionManager for the Workspace.
touchHandler The BlockTouchHandler this view should start with.
Returns

protected FieldView buildFieldView (Field field)

Build and populate the FieldView for field.

Note: Variables need some extra setup when they are created by a custom ViewFactory.

  • If they use an adapter to display the list of variables it must be set.
  • If they have delete/rename/create options they must have a VariableRequestCallback set on them. mVariableCallback may be used for this purpose.

Parameters
field The Field to build a view for.
Returns

protected abstract InputView buildInputView (Input input, List<FieldView> fieldViews)

Build and populate the InputView for input.

This method should not recurse the model to generate more than one view. buildBlockViewTree(Block, BlockGroup, ConnectionManager, BlockTouchHandler) will traverse the model and call this method for each Input.

Parameters
input The Input to build a view for.
fieldViews The list of FieldViews in the constructed view.
Returns

protected List<InputView> buildInputViews (Block block, ConnectionManager connectionManager, BlockTouchHandler touchHandler)

Parameters
block
connectionManager
touchHandler

protected SpinnerAdapter getVariableAdapter ()

protected final void unregisterView (BlockView blockView)

Removes the mapping to this view from its block. This should only be called from unlinkModel(), which is already handled in AbstractBlockView.

Parameters
blockView The BlockView to disassociate from its Block model.