Sample Maplet Application: Get Input
This worksheet demonstrates how to write Maplet applications that function similarly to the Get Input Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
Simple Example
restart:
# Invoke the Maplets Elements subpackage. with(Maplets[Elements]): # Define the get input Maplet application. Define an abnormal # shutdown as one that returns FAIL. maplet1 := Maplet( 'abnormalshutdown' = "FAIL", InputDialog['ID1']( # The input request message. "Enter an integer:", # The Maplet application title. 'title' = "Request", # The value returned to the Maple session. # Click "OK", the value referenced by ID1. # Click "Cancel", FAIL. 'onapprove' = Shutdown( ['ID1'] ), 'oncancel' = Shutdown( "FAIL" ) ) ):
# Assign result to the value returned by the Maplet application. result := Maplets[Display](maplet1);
Complex Example
The following example does not use any of the simplifications that have been included to make writing Maplet applications easier which leads to longer code but faster execution and for more complex Maplet applications, greater control of the Maplet application appearance and behavior. The required changes are:
1. An onstartup action must be specified. This action must be referenced and contain a RunDialog element which starts the InputDialog element.
2. Actions associated with onapprove and oncancel must be given by a reference and located outside the InputDialog element. The command elements must be wrapped in Action elements.
3. All option names must be specified, including caption, reference, and value.
with(Maplets[Elements]): # Define the "onstartup" option referenced by A0. maplet2 := Maplet( 'abnormalshutdown' = "FAIL", 'onstartup' = 'A0', InputDialog( 'reference' = 'ID1', 'caption' = "Enter an integer:", 'title' = "Request", 'onapprove' = 'A1', 'oncancel' = 'A2' ), # Start the InputDialog. Action( 'reference' = 'A0', RunDialog( 'dialog' = 'ID1' ) ), # Define the resulting action if "OK" is # clicked. Action( 'reference' = 'A1', Shutdown( '`return`' = 'R1' ) ), # Define the resulting action if "Cancel" # is clicked. Action( 'reference' = 'A2', Shutdown( 'value' = "FAIL" ) ), # Return the value of "R1" to Maple session. Return( 'reference' = 'R1', ReturnItem( 'item' = 'ID1' ) ) ):
result := Maplets[Display](maplet2);
Comparison
Maplet applications can be viewed by using the Maplets[Tools][Print] function. This function prints the XML data structure of the Maplet application.
Compare the resulting output of the previous two Maplet applications. Note that the only major difference is that the first example replaces the name references with _Maplets_reference_# where # is a unique number and the second example leaves the string references unchanged.
Maplets[Tools][Print](maplet1);
Maplets[Tools][Print](maplet2);
Parse/Type Check Example
The following procedure example displays a Maplet application that prompts the user for an integer. If the user selects OK without entering any input, an error is raised. If the user input does not parse or is not of type integer, an appropriate dialog displays prompting the user to make a correction. This continues until the user enters an integer or selects Cancel.
# Invoke the Maplets Elements subpackage. with(Maplets[Elements]): # Define the procedure MyProc. MyProc := proc() # Declare maplet and result as local # variables. local maplet, result; # Define the Maplet application. maplet := Maplet( 'abnormalshutdown' = "FAIL", InputDialog['ID1']( "Enter an integer:", 'title' = "Request", 'onapprove' = Shutdown( ['ID1'] ), 'oncancel' = Shutdown( "FAIL" ) ) ); # Run the Maplet application. Assign result to the value # returned by the Maplet application. result := Maplets[Display]( maplet ); if result = "FAIL" then error "request failure, an integer was not entered"; else do try # Try to parse the result. result := parse( result[1] ); if type( [result], ['integer'] ) then return result; else # Define the Maplet application that returns to # the user if result is not an integer. maplet := Maplet( 'abnormalshutdown' = "FAIL", InputDialog[ID1]( sprintf( "Expecting an integer, but this is of type `%a`:", whattype( result ) ), 'title' = "Request", 'value' = sprintf( "%q", result ), 'onapprove' = Shutdown( ['ID1'] ), 'oncancel' = Shutdown( "FAIL" ) ) ); result := Maplets:-Display( maplet ); if result = "FAIL" then error "request failure, an integer was not entered"; end if; end if; catch: # Define the Maplet application that returns to the user # if the result does not parse. maplet := Maplet( 'abnormalshutdown' = "FAIL", InputDialog['ID1']( "Expecting an integer, but this does not parse:", 'title' = "Request", 'value' = result[1], 'onapprove' = Shutdown( ['ID1'] ), 'oncancel' = Shutdown( "FAIL" ) ) ); result := Maplets:-Display( maplet ); if result = "FAIL" then error "request failure, an integer was not entered"; end if; end try; end do; end if; end proc:
MyProc();
Maplets[Examples][GetInput]
The Maplets[Examples][GetInput] function displays a Maplet application similar to that of the previous examples. The function, however, allows the user to modify the caption and title options.
For help on this Maplet application, see:
?Maplets[Examples][GetInput]
To view the source code, enter:
print( Maplets[Examples][GetInput] );
See Also
Maplets[Examples], Maplets[Examples][GetInput], Maplets[Elements][InputDialog]
Return to Index for Example Worksheets
Download Help Document