- java.lang.Object
-
- org.praxislive.code.CodeContext<D>
-
- Type Parameters:
D- supported delegate type
- Direct Known Subclasses:
AudioCodeContext,CodeContainer.Context,CoreCodeContext,P2DCodeContext,P3DCodeContext,VideoCodeContext
public abstract class CodeContext<D extends CodeDelegate> extends Object
A CodeContext wraps eachCodeDelegate, managing state and the transition from one iteration of delegate to the next on behalf of aCodeComponent.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCodeContext.ClockListenerListener for responding to time changes inside the context.
-
Constructor Summary
Constructors Modifier Constructor Description protectedCodeContext(CodeConnector<D> connector)Create a CodeContext by processing the providedCodeConnector(containing CodeDelegate).protectedCodeContext(CodeConnector<D> connector, boolean requireClock)Create a CodeContext by processing the providedCodeConnector(containing CodeDelegate).
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddClockListener(CodeContext.ClockListener listener)Add a clock listener.protected booleancheckActive()Check whether the CodeContext is running inside an ExecutionContext with active state.protected voidconfigure(CodeComponent<D> cmp, CodeContext<D> oldCtxt)A hook method that will be called when the CodeContext is configured on a component.protected voiddispose()Hook called during disposal of code context.protected voidflush()Flush the code context.protected ControlAddressgetAddress(Control control)Find the address of the passed in control, or null if it does not have one.CodeComponent<D>getComponent()Get the code component this code context is attached to, if there is one.protected ControlgetControl(String id)Get the control to handle the specified ID, or null if there isn't one.protected ControlDescriptorgetControlDescriptor(String id)Get the control descriptor for the specified ID, or null if there isn't one.protected String[]getControlIDs()Deprecated.DgetDelegate()Get the delegate this context wraps.protected ExecutionContextgetExecutionContext()Get the execution context, or null if not attached.protected ComponentInfogetInfo()Get component info.LogBuildergetLog()Get the log builder for writing log messages.protected LogLevelgetLogLevel()Get the active log level.LookupgetLookup()Get lookup.protected PortgetPort(String id)Get the port with the specified ID, or null if there isn't one.protected PortDescriptorgetPortDescriptor(String id)Get the port descriptor for the specified ID, or null if there isn't one.protected String[]getPortIDs()Deprecated.longgetTime()Get current time in nanoseconds.protected voidhierarchyChanged()Called when the hierarchy changes, which might be because the component hierarchy has changed (seeComponent.hierarchyChanged()), the context has been added or is being removed from the component, or for any other reason that cached information should be invalidated (eg.voidinvoke(long time, Runnable task)Invoke the provided task if the context is active, and after updated the time to the specified time.Optional<ComponentAddress>locateService(Class<? extends Service> type)Locate the provided service type, if available.protected voidlog(LogBuilder log)Process and send messages from an external log builder.voidremoveClockListener(CodeContext.ClockListener listener)Remove a clock listener.protected voidreset(boolean full)Reset all control, port and reference descriptors.protected voidstarting(ExecutionContext source)Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE) or the context is added to a component within an active execution context.protected voidstarting(ExecutionContext source, boolean fullStart)Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE) or the context is added to a component within an active execution context.protected voidstopping(ExecutionContext source)Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE) or the context is removed from a component within an active execution context.protected voidstopping(ExecutionContext source, boolean fullStop)Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE) or the context is removed from a component within an active execution context.protected voidtick(ExecutionContext source)Hook called by the clock listener on the execution context.protected voidupdate(long time)Update the time in this context to the specified time.
-
-
-
Constructor Detail
-
CodeContext
protected CodeContext(CodeConnector<D> connector)
Create a CodeContext by processing the providedCodeConnector(containing CodeDelegate).- Parameters:
connector- code connector with delegate
-
CodeContext
protected CodeContext(CodeConnector<D> connector, boolean requireClock)
Create a CodeContext by processing the providedCodeConnector(containing CodeDelegate). This constructor takes a boolean to force connecting the context to the execution clock, should the subtype always require clock signals.- Parameters:
connector- code connector with delegaterequireClock- true to force clock connection
-
-
Method Detail
-
configure
protected void configure(CodeComponent<D> cmp, CodeContext<D> oldCtxt)
A hook method that will be called when the CodeContext is configured on a component. It is called after controls, ports and refs have been configured. Subclasses may override this to do additional configuration. The default implementation does nothing.- Parameters:
cmp- component being attached tooldCtxt- previous context, or null if there was none
-
hierarchyChanged
protected void hierarchyChanged()
Called when the hierarchy changes, which might be because the component hierarchy has changed (seeComponent.hierarchyChanged()), the context has been added or is being removed from the component, or for any other reason that cached information should be invalidated (eg. anything retrieved from the lookup). Subclasses may override this to handle such events / invalidate lookup results. The default implementation does nothing.
-
starting
protected void starting(ExecutionContext source, boolean fullStart)
Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE) or the context is added to a component within an active execution context. Full start will be true in the former case when the execution context itself is changing state.This method may be overridden in subclasses. The default implementation delegates to
starting(org.praxislive.core.ExecutionContext).- Parameters:
source- execution contextfullStart- whether the context itself is transitioning state
-
starting
protected void starting(ExecutionContext source)
Hook called when the execution context is started (moves to stateExecutionContext.State.ACTIVE) or the context is added to a component within an active execution context.- Parameters:
source- execution context
-
stopping
protected void stopping(ExecutionContext source, boolean fullStop)
Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE) or the context is removed from a component within an active execution context. Full stop will be true in the former case when the execution context itself is changing state.This method may be overridden in subclasses. The default implementation delegates to
stopping(org.praxislive.core.ExecutionContext).- Parameters:
source- execution contextfullStop- whether the context itself is transitioning state
-
stopping
protected void stopping(ExecutionContext source)
Hook called when the execution context is stopped (moves away from stateExecutionContext.State.ACTIVE) or the context is removed from a component within an active execution context.- Parameters:
source- execution context
-
tick
protected void tick(ExecutionContext source)
Hook called by the clock listener on the execution context. The default implementation does nothing.- Parameters:
source- execution context
-
reset
protected final void reset(boolean full)
Reset all control, port and reference descriptors. A full reset generally happens on execution context state changes as opposed to code change transitions. Descriptors may handle this differently - eg. clear injected values or dispose references on full.- Parameters:
full- whether reset is full (eg. execution state change)
-
dispose
protected void dispose()
Hook called during disposal of code context. The default implementation does nothing.
-
getComponent
public CodeComponent<D> getComponent()
Get the code component this code context is attached to, if there is one.- Returns:
- code component, or null
-
getDelegate
public D getDelegate()
Get the delegate this context wraps.- Returns:
- delegate
-
getControl
protected Control getControl(String id)
Get the control to handle the specified ID, or null if there isn't one.- Parameters:
id- control ID- Returns:
- control or null
-
getControlDescriptor
protected ControlDescriptor getControlDescriptor(String id)
Get the control descriptor for the specified ID, or null if there isn't one.- Parameters:
id- control ID- Returns:
- control descriptor or null
-
getControlIDs
@Deprecated protected String[] getControlIDs()
Deprecated.Get all the available control IDs.- Returns:
- control IDs
-
getPort
protected Port getPort(String id)
Get the port with the specified ID, or null if there isn't one.- Parameters:
id- port ID- Returns:
- port or null
-
getPortDescriptor
protected PortDescriptor getPortDescriptor(String id)
Get the port descriptor for the specified ID, or null if there isn't one.- Parameters:
id- port ID- Returns:
- port descriptor or null
-
getPortIDs
@Deprecated protected String[] getPortIDs()
Deprecated.Get the available port IDs.- Returns:
- port IDs
-
getInfo
protected ComponentInfo getInfo()
Get component info.- Returns:
- component info
-
getAddress
protected ControlAddress getAddress(Control control)
Find the address of the passed in control, or null if it does not have one.- Parameters:
control- control to find address for- Returns:
- control address or null
-
getLookup
public Lookup getLookup()
Get lookup.- Returns:
- lookup
-
locateService
public Optional<ComponentAddress> locateService(Class<? extends Service> type)
Locate the provided service type, if available.- Parameters:
type- service to lookup- Returns:
- optional service address
-
getTime
public long getTime()
Get current time in nanoseconds.- Returns:
- time in nanoseconds
-
addClockListener
public void addClockListener(CodeContext.ClockListener listener)
Add a clock listener. Resources used inside code delegates should add a clock listener rather than listen directly on the execution context.- Parameters:
listener- clock listener
-
removeClockListener
public void removeClockListener(CodeContext.ClockListener listener)
Remove a clock listener.- Parameters:
listener- to remove
-
getExecutionContext
protected ExecutionContext getExecutionContext()
Get the execution context, or null if not attached.- Returns:
- execution context, or null
-
checkActive
protected boolean checkActive()
Check whether the CodeContext is running inside an ExecutionContext with active state. If the execution context is active, but a transition to active has not yet been handled in this code context, the state transition will be triggered.- Returns:
- true if active
-
update
protected void update(long time)
Update the time in this context to the specified time. A value the same or behind the current value will be ignored. This method will call all clock listeners.- Parameters:
time- updated time
-
invoke
public void invoke(long time, Runnable task)Invoke the provided task if the context is active, and after updated the time to the specified time. After task execution, flush will be called.- Parameters:
time- new clock timetask- runnable task to execute
-
flush
protected void flush()
Flush the code context. By default this message checks for pending log messages and delivers to the log.
-
getLog
public LogBuilder getLog()
Get the log builder for writing log messages.- Returns:
- log builder
-
getLogLevel
protected LogLevel getLogLevel()
Get the active log level.- Returns:
- active log level
-
log
protected void log(LogBuilder log)
Process and send messages from an external log builder.- Parameters:
log- externl log builder
-
-