This is the second step in building the oneway advisor. In step 1 the code for the main window was developed. But the window didn’t contain any icons – that’s the goal of this step.
Referencing External Files
A framework needs to be established for referencing external files. To achieve this open a new script window and write the following code:
1 2 3 4 5 6 7 8 9 10 11 |
Get Icons Namespace = Function({},{Default Local}, nsICONS = New Namespace("icons.oneway-advisor"); thisDir = Get Default Directory(); resourcesDir = Substitute(thisDir,"Scripts","Resources"); nsICONS:PASS_ICON = resourcesDir || "tick14.gif"; nsICONS:FAIL_ICON = resourcesDir || "cross14.gif"; nsICONS:READY_ICON = resourcesDir || "ready14.png"; Return(nsICONS); ); |
The code creates a user-defined function called Get Icons Namespace. As the name suggests the function “gets” a namespace. That namespace is created on line 3 and returned in line 9. A namespace is simply a container that is used to store variables. Those variables are created on lines 6 to 8; they identify the files used to display the icons in the application. This script needs to be saved with the name Filepath Definitions.jsl.
Abstraction
Creating functions and namespaces just to reference a file path might seem somewhat long-winded and unnecessary. But file names are one of the few elements of a script that have the potential to lock the code to our own particular computer configuration.
I want to be confident that if the script is run on another computer that it will still work. I also want to be confident that if the script is packaged as an JMP Add-In then it will still work. Or if it doesn’t work then I want just a single location where I can fix problems related to file locations.
Once the Filepath Definitions file is created this is the single place for me to look for filepath-related problems. Outside of this file, I don’t need to worry, I can use abstract names such as nsICONS:PASS_ICON without worrying about the file-specific implementation.
Also, I can easily change an icon if I decide I don’t like it!
Placing Icons On A Button Box
Now back to the original script that was saved as step1.jsl. The code needs to be revised to include references to the icons namespace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Names Default To Here(1); // Include files Include("Filepath Definitions.jsl"); // Namespaces nsICONS = Get Icons Namespace(); // Main window wrapSize = 220; New Window("Oneway Advisor", Show Menu( 0 ), Show Toolbars( 0 ), Border Box(Top(20),Bottom(20),Left(10),Right(10), V List Box( H List Box( Spacer Box(Size(30,0)), Text Box("Assumption Checker",<<Set Font Style("Bold")) ), LineUpBox(NCol(2), Spacing(30,20), Text Box("Data within each level of the grouping variable are normally distributed",<<Set Wrap(wrapSize)), Button Box( "",,<<Set Icon(nsICONS:READY_ICON)), Text Box("Residuals are normally distributed",<<Set Wrap(wrapSize)), Button Box( "",,<<Set Icon(nsICONS:READY_ICON)), Text Box("Data within each level of the grouping variable have equal variance",<<Set Wrap(wrapSize)), Button Box( "",,<<Set Icon(nsICONS:READY_ICON)), Text Box("The data are independent, exhibiting no serial correlation",<<Set Wrap(wrapSize)), Button Box( "",,<<Set Icon(nsICONS:READY_ICON)), ) ) ) ); |
Let’s take a look at what has changed. The original code now appears on lines 10 to 32. Note the change to the Button Boxes:
Button Box("",, <<Set Icon(nsICONS:READY_ICON) )
Now, instead of displaying an ‘X’ the button will display an icon:
Including JSL Scripts
In order to reference the icon namespace the script “includes” the FIlepath Definitions file (line 4) and invokes the function Get Icons Namespace (line 7).
And Finally
Line 1 has been added to be consistent with best practice according to JMP guidelines.
The revised code should be saved as step2.jsl.
Thanks, this site is extremely valuable.