Difference between revisions of "Inserting Choice Controls in Edit Table Cells"
|Line 177:||Line 177:|
Capacity: 90.2 MW
Capacity: 90.2 MW
Revision as of 23:14, 16 July 2007
Mini-tutorial: Choice controls in edit table cells
A new feature in Analytica 4.0 is the ability to present pulldown controls in the cells of an edit table. Using pulldowns in this fashion can provide a convenience to the users of your model, and ensure that they select only valid values for cell values. The following screenshot demonstrates the use of pulldowns in an edit table:
This short mini-tutorial takes you step-by-step through a simple example to produce the table shown.
How it is done
The edit table displayed above actually contains the following cell contents:
When the edit table is viewed from edit mode (i.e., by a model builder), we see this. When the same table is viewed from browse mode (i.e., by the user of the model), the controls display. For the table to be changeable when viewed in browse mode, the table must have an input node. So, the basic steps are to create the indexes containing the list of valid choices for each pulldown, create the edit table, make an input node for the table, and finally populate the edit table with calls to the choice function, where appropriate. Later in the tutorial, we'll also learn how to configure the table so that if new rows are added, the default cell contents for the new row (with choice controls) are automatically inserted.
Identifying Possible Selections
1. Start Analytica 4.0 and fill in the object window:
2. Close the object window
3. We'll begin by defining the possible Plant types. Drag an index to the diagram and title it: Plant Type
5. Fill in the possible plant types as shown (pressing down-arrow from the last cell to append a new cell each time):
6. Repeat steps 3-5 to define an index County:
7. Next, create an index named Boolean having the values [False, True]. In this case, instead of using a list-of-labels, we are going to use a "List". This means that cells in the list contains the identifiers False and True, rather than the string "False" and "True". These evaluate to 0 and 1 respectively. This will allow the text "False" and "True" to display in the pulldown, but when the Choice expression is evaluated, it will evaluate to the numeric values of 0 or 1. If we were to define as a list of labels, they would evaluate to the text "False" or "True". Drag an index node to the diagram, title it "Boolean", and select "List" for the definition type. Enter False, press downarrow, and enter True.
At this point, we have specified the possible values for each choice control.
Understanding the Choice function
You may have previously used the Choice function to create pulldowns. Here, we'll review this briefly before we actually insert these into the edit table cells.
8. Drag a variable node to the diagram, name it "Select Plant Type". In the definition type pulldown, select "Choice".
9. Fill in the parameters as shown:
For the I parameter, we've specifies the Plant_type index that we defined in Steps 3-5. These are the possible choices. We've tentatively set n=1 to select the first item in the list (which is valid until the user actually selects something different in the control). And finally we've set inclAll to false. This third parameter turns off the "All" option so the user does not have the option of selecting every plant type. When placing pull-down controls inside edit tables, you will probably not want "All" to be an option.
10. Press OK. In the attribute dialog, a choice pulldown appears.
11. Click on the pop-up (which currently displays "Natural Gas") and select "Nuclear". Then, change the definition-type pulldown to expr (Expression). Notice that the following definition appears:
Notice that the second parameter has changed from 1 to 4 as a result of selecting "Nuclear". Analytica automatically re-writes the definition of a Choice function when the user selects a new option.
12. Click on the "Select Plant Type" and press Delete. Answer yes, that this is what you intend. We won't be needing this variable any more.
Creating the Edit Table
13. Create another index and title it "Plant ID". Make it a list-of-labels, and fill in the IDs for our power plants as follows:
14. Create an index titled Plant Fields, which will serve as the columns of our edit table, another list-of-labels, and fill in as follows:
15. Create a variable node, name it Power Plant Data. Use the definition type pulldown to select "Table":
16. The index chooser appears. In the left-hand column, find Plant ID and Plant Fields and move these to the right to select.
17. Press OK, and then close the edit table window.
18. With Power Plant Data selected, select "Make Input Node" from the Object menu.
What we have done with this step is to create an input node for our edit table. The input node means that our table can be edited from browse mode. Whenever you are using choice controls in an edit table, you will want this to be possible, since the controls actually appear when the table is edited from browse mode, so this is a critical step.
Define Choice Cells
19. Bring up the edit table for Power Plant Data.
20. In the top cell in the Plant Type column, type:
and press enter to accept typing.
21. With the cell just typed selected, select Edit → Copy
22. Click on the Plant Type column header to select the entire row and select Edit → Paste. Now every cell in that column is defined as a choice.
23. Repeat steps 20-22 to set the definition of the cells in the County row to:
and the cells of Cogen to
The table now should be as follows:
25. Enter the table data for the power plants, taking advantage of the pulldown controls you've defined, using the following data. Press the green check when done.
At this point, the user of your model does not have the ability to add additional power plants to the table (i.e., from browse mode). Try it yourself to see:
26. Ensure that you are still in browse mode (BrowseTool.jpg is depressed on the toolbar). Click on the last row header (T0008). Press the down arrow to append a row. (Nothing happens) Try to select Insert Rows from the Edit menu. (You can't -- it is disabled).
Users cannot add a power plant to the list because the index Plant_ID cannot currently be edited from browse mode. To allow Plant_ID to be edited, you must create an input node for it.
27. Close the edit table window. Enter edit mode (click ), and select the Plant ID node. From the menu, select Edit → Make Input Node. Position the newly created input node appropriately. Press to re-enter browse mode.
28. Press the Edit Table button for Power Plant Data.
At this point, we can insert rows (you can verify that the menu item is now enabled) or append to the end. However, we have not specified the default values for those new cells. Analytica's internal default is to define any new cells as 0 (zero). We'll try inserting a row to see the problem.
29. Click on the last row header, T0008, and press the down arrow button.
Notice that zeroes have been inserted, even for the columns that should contain choice controls. The way to get choice pulldowns to show up in newly inserted rows is to specify what the default cell value is for the table. Since the default changes for each column, the default value provided must be indexed by Plant_fields. In other words, the default cell value will be specifies using a 1-D table indexed by Plant_fields. Let's set this up now.
30. First, let's get rid of that zero row, so we can insert it again later. Click on the row header for the newly inserted row, then select Edit → Delete Rows. The table should now be back into the state it was in before we added the row of zeros. Close the edit table window.
31. Enter edit mode () and create a new constant node entitled "Default Plant Data". (We could use a variable node, etc., but I like to use a constant here, since I think of this as being an unchangeable part of the model). Define the constant as a Table, using the index Plant fields.
32. Enter the cell defaults for each column. In the screenshot here, I have pivoted the edit table so the index appears along the column headers, and I have also entered (two single quotes with nothing between them), signifying an empty string, as the default plant name.
33. Close the edit table window for Default Plant Data.
34. Now, we need to tell Analytica that Default_plant_data contains the cell defaults for Power_Plant_Data. There is currently not a graphical UI option for this, so we must do this from the typescript window. Open the typescript window by pressing CTRL-' (or CTRL-F12 on a non-English keyboard).
35. At the typescript prompt, type:
TableCellDefault Power_plant_data : Default_plant_Data
Notice that identifiers (with underscores) are used, not titles. This typescript command sets the TableCellDefault attribute of Power_plant_data to be Default_plant_data.
37. Click on the last row header, T0008, and press the down arrow button.
Now the row inserts with the default values, including three pulldowns. Try entering data for this power plant:
Plant ID: H0027 Name: Big Creek 1 Type: Hydroelectric County: Fresno Cogen: No Capacity: 90.2 MW
Download the model
The model created during this example can be downloaded: media:California_Power_Plants.ANA