java.lang.Object
org.praxislive.code.CodeContext<D>
- Type Parameters:
D- supported delegate type
- Direct Known Subclasses:
AudioCodeContext,CodeContainer.Context,CodeRoot.Context,P2DCodeContext,P3DCodeContext,VideoCodeContext
A CodeContext wraps each
CodeDelegate, managing state and the
transition from one iteration of delegate to the next on behalf of a
CodeComponent.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceListener for responding to time changes inside the context. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCodeContext(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
Modifier and TypeMethodDescriptionvoidaddClockListener(CodeContext.ClockListener listener) Add a clock listener.protected booleanCheck 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.Get all the available control IDs.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.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 ControlDescriptorGet the control descriptor for the specified ID, or null if there isn't one.Get the delegate this context wraps.protected ExecutionContextGet the execution context, or null if not attached.protected ComponentInfogetInfo()Get component info.getLog()Get the log builder for writing log messages.protected LogLevelGet the active log level.Get lookup.protected PortGet the port with the specified ID, or null if there isn't one.protected PortDescriptorGet the port descriptor for the specified ID, or null if there isn't one.longgetTime()Get current time in nanoseconds.protected voidCalled 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).voidInvoke the provided task, if the context is active, and after updating the clock to the specified time (if later).<V> VinvokeCallable(long time, Callable<V> task) Invoke the provided task and return the result, if the context is active, and after updating the clock to the specified time (if later).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.protected voidonInit()Hook called when the code context becomes active, either as a result of the execution context becoming active or the code context being attached to a component within an active execution context.protected voidonReset()Hook called when the context is being reset.protected voidonStart()Hook called when the execution context becomes active.protected voidonStop()Hook called when the execution context is stopping.portIDs()Get the available port IDs.voidremoveClockListener(CodeContext.ClockListener listener) Remove a clock listener.protected final voidReset and (if active) reinitialize all control, port and reference descriptors.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 Details
-
CodeContext
Create a CodeContext by processing the providedCodeConnector(containing CodeDelegate).- Parameters:
connector- code connector with delegate
-
CodeContext
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 Details
-
configure
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. -
onInit
protected void onInit()Hook called when the code context becomes active, either as a result of the execution context becoming active or the code context being attached to a component within an active execution context. -
onStart
protected void onStart()Hook called when the execution context becomes active. TheonInit()hook will always have been called before this hook. -
onStop
protected void onStop()Hook called when the execution context is stopping. -
onReset
protected void onReset()Hook called when the context is being reset. -
tick
Hook called by the clock listener on the execution context. The default implementation does nothing.- Parameters:
source- execution context
-
resetAndInitialize
protected final void resetAndInitialize() -
dispose
protected void dispose()Hook called during disposal of code context. The default implementation does nothing. -
getComponent
Get the code component this code context is attached to, if there is one.- Returns:
- code component, or null
-
getDelegate
Get the delegate this context wraps.- Returns:
- delegate
-
getControl
Get the control to handle the specified ID, or null if there isn't one.- Parameters:
id- control ID- Returns:
- control or null
-
getControlDescriptor
Get the control descriptor for the specified ID, or null if there isn't one.- Parameters:
id- control ID- Returns:
- control descriptor or null
-
controlIDs
Get all the available control IDs.- Returns:
- control IDs
-
getPort
Get the port with the specified ID, or null if there isn't one.- Parameters:
id- port ID- Returns:
- port or null
-
getPortDescriptor
Get the port descriptor for the specified ID, or null if there isn't one.- Parameters:
id- port ID- Returns:
- port descriptor or null
-
portIDs
Get the available port IDs.- Returns:
- port IDs
-
getInfo
Get component info.- Returns:
- component info
-
getAddress
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
Get lookup.- Returns:
- lookup
-
locateService
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
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
Remove a clock listener.- Parameters:
listener- to remove
-
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
Invoke the provided task, if the context is active, and after updating the clock to the specified time (if later). Any exception will be caught and logged, and the context will be flushed.- Parameters:
time- new clock timetask- runnable task to execute
-
invokeCallable
Invoke the provided task and return the result, if the context is active, and after updating the clock to the specified time (if later). Any exception will be logged and rethrown, and the context flushed. Throws anIllegalStateExceptionifcheckActive()returnsfalse.- Type Parameters:
V- the result type of method call- Parameters:
time- new clock timetask- runnable task to execute- Returns:
- result
- Throws:
Exception- if unable to compute a result
-
flush
protected void flush()Flush the code context. By default this message checks for pending log messages and delivers to the log. -
getLog
Get the log builder for writing log messages.- Returns:
- log builder
-
getLogLevel
Get the active log level.- Returns:
- active log level
-
log
Process and send messages from an external log builder.- Parameters:
log- external log builder
-