GUIKit Example: Making Progress (Extended)
CodeExampleThis example demonstrates various techniques of designing a progress bar dialog and a number of options one has to wrap the reusable widget involved. Basic implementationHere is one technique of defining a progress bar using the Mathematica expression syntax. A progress bar would be most useful in a modeless session where it could be used to update the state of another ongoing kernel evaluation.
| Out[3]= |  |
There are three important widgets in the dialog that have been registered with reference names for easy lookup. Here are the most common properties worth setting during the lifetime of the dialog. Once the dialog is not needed, you can call ReleaseGUIObject to dispose of the instance. The following variants also show different techniques for defining a reusable progress dialog within the layout of an AddOn, and discuss the many options available for exposing only a subset of the properties and widgets that make up the user interface. Example 1 of a progress bar implementationHere is an example that provides a progress bar implementation similar to the J/Link example. In this version, we postmanipulate the Java objects in Mathematica by looking up the instances from their names after loading the definition file. After modifying the frame's title and the label's text, we will then call GUIRun, which displays the dialog. Here is a function that simulates a typical interaction with the GUIObject representing a progress bar dialog. To use the progress bar in a computation, we call CreateProgressBar1, which loads the user interface definition and returns a GUIObject. Then, while the computation is running, we periodically change the value property to update the bar's appearance. When the computation is done, we call ReleaseGUIObject.
| Out[10]= |  |
We can also expose certain properties as optional arguments to the CreateProgressBar1 function to reuse the dialog in multiple situations.
| Out[12]= |  |
Example 2A of a progress bar implementationThis example illustrates how one can alternatively create reusable versions of a progress bar or any widget by placeholding dynamic properties that may need to change for each instance. This is done by using WidgetReference["#n"] within a base .m and creating various wrapper .m files that define these needed arguments to the base script externally. In this example, we use a base user interface called ProgressBar2Base.m that contains uses of WidgetReference["#n"] and expects these values to be passed as initial arguments when called. The source of ProgressBar2Base.m clarifies what dynamic elements of the progress bar are unique to this instance by only exposing certain properties within ProgressBar2Base.m, but setting all these modifiable properties within the wrapping .m file. Here is the revised CreateProgressBar2A implementation reusing the same RunProgressBar.
| Out[15]= |  |
Example 2B of a progress bar implementationTaking this wrapper and base .m a step further, the Mathematica functions that load and run GUIObject instances can also pass script arguments when they are called using an optional second argument. Here we show how the Mathematica code can dynamically pass arguments to the base .m file that we used previously, duplicating the functionality of example 1, but greatly simplifying the interaction with the reusable user interface by not seeing all the individual property processing calls within the CreateProgressBar1 Mathematica code. Notice how the second argument to GUILoad is simply: {title, caption, percent}, which will be registered in the widget name registry as "#1", "#2", and "#3", respectively. The two-argument call to GUILoad using ProgressBar2Base.m is the equivalent of example 2A of wrapping ProgressBar2Base.m with ProgressBar2.m containing the values desired.
General::"spell1": "Possible spelling error: new symbol name \"\!\(CreateProgressBar2B\)\" is similar to existing symbol \"\!\(CreateProgressBar2A\)\". \!\(\*ButtonBox[\"More...\", ButtonStyle->\"RefGuideLinkText \", ButtonFrame->None, ButtonData:>\"General::spell1\"]\)"
Here is the revised CreateProgressBar2B implementation reusing the same RunProgressBar.
| Out[18]= |  |
|