DocumentTools[Components]
State
generate XML for a State Component
Calling Sequence
Parameters
Description
Examples
Compatibility
State( stateName, field1, field2, ... )
stateName
-
{assignable,string}; name to which the state is assigned
fieldN
:{name,name=anything}; (optional); your choice of Record field names
The State command in the Component Constructors package can be used to create multiple instances of a collection of document components that are designed to work together. Each instantiation of such a collection will work independently of the others, with components internal to a given one only communicating among themselves. Component naming conflicts which would otherwise arise are avoided. Furthermore, any document containing multiple such instantiations can be saved and re-opened without loss of functionality, and without the need to reinstantiate the components.
State returns an XML function call which represents a "State Component". A "State Component" is a virtual construct that holds a reference to a name. When a State component is made active as part of a Worksheet instantiated with InsertContent, the name and its value are implicitly saved when the associated worksheet is saved. The value and name are also restored automatically after restart.
The returned value is generated XML, which can be used with the results of commands in the Layout Constructors package to create an entire Worksheet or Document in XML form. Such a representation of a Worksheet or Document can be inserted into any visible part of the current document, such as a table cell, textfield, or document block, using the InsertContent command. Including the state variable into the XML this way is necessary in order for the correct state to be saved and revived when the Worksheet is closed and re-opened.
The first argument is the global name of the state variable. This must be either a string or unevaluated name. A string tells the command what name to use, whereas an unevaluated name will cause a random unique name to be generated and assigned to that unevaluated name (as a pass-by-reference set value). This state variable can then be passed to action handlers so they can process the action with entirely self-contained state. This is important especially when the same component application can appear multiple times within the same worksheet.
The remaining arguments are field names or field-name = value pairs. If any field names are present, the stateName variable will be assigned to a Record containing those field names and values.
The State command currently uses a hidden DataTable component as the mechanism for implementing this functionality.
Counter := module() local ModuleApply := proc(val) local xml, stateVar, stateXML; uses DocumentTools:-Layout, DocumentTools:-Components; stateXML := State('stateVar','data'=val,'xmap'); xml := Worksheet( Table( Column(), Column(), Row( Cell(TextArea(convert(val,string),identity="TextArea0")), Cell(Button("Increment",action=sprintf("Counter:-Increment(%s)",stateVar)), stateXML ) ))); stateVar:-xmap := DocumentTools:-InsertContent(xml,output=table,'state'=convert(stateVar,string)); NULL; end proc; export Increment := proc( sv ) sv:-data := sv:-data + 1; DocumentTools:-SetProperty(sv:-xmap["TextArea0"],'value',sv:-data); end proc; end module:
Counter⁡1
Counter⁡10
The DocumentTools[Components][State] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
See Also
Component Code
Component Constructors
DocumentTools
Embedded Components
XMLTools
Download Help Document