The Plate Example

You will need to provide an editor that allows the user to specify the properties defined in the previous section—the “Shape” and the “Material” —and the initial value for the “Thickness” parameter (although that can be provided in the general Parameters table.

This example describes how to determine the surface area, volume, and weight of a thick plate of a specified shape and size.

It is good practice to obtain and store a reference to the properties and parameters in the initEditor method, which is the first method called in the editor; as a result, you can easily get and set their values in any of the other methods.

shapeProp = ((DtScalarVariable)component.getProperty("Shape"));
materialProp = ((DtScalarVariable)component.getProperty("Material"));
thicknessParam = 
((DtScalarVariable)component.getParameter("Thickness"));

When the editor is opened (open method is called), you need to create the GUI. It is recommended that you then store a handle to the GUI you create so that it can be reused on subsequent openings. The AbstractDesktopEditor extends JPanel, so you can add widgets to this as needed. For the example problem, you will need a menu to select the shape, an entry to specify the thickness, and a menu to select the material.

public void initEditor (DtComponent comp, ProgressRange ignore){
myPanel = new JPanel(new GridBagLayout());
this.add(myPanel, BorderLayout.NORTH);
JLabel instructionsText = new JLabel("This component will
calculate the area, volume, and weight of a plate.");
JLabel instructionsText2 = new JLabel("(All dimensions should
be specified in meters)");
myPanel.add(instructionsText, new GridBagConstraints(0, 0, 2, 1, 0.0,
0.0,GridBagConstraints.WEST, GridBagConstraints.NONE, new
Insets(0, 0, 5, 0), 0, 0));
myPanel.add(instructionsText2, new GridBagConstraints(0, 1, 2, 1, 0.0,
0.0,GridBagConstraints.WEST, GridBagConstraints.NONE, new
Insets(0, 0, 10, 0), 0, 0));
// Widgets to specify shape
JLabel shapeLabel = new JLabel("Shape");
shapeMenu = new JComboBox(new String[] {"circle", "rectangle", 
"triangle"});
// Widgets to specify material
JLabel materialLabel = new JLabel("Material"); 
materialMenu = new JComboBox(new String[] {"aluminum", "steel", 
"wood"});
// Widgets to specify thickness
JLabel thicknessLabel = new JLabel("Thickness");
thicknessEntry = new JTextField(8);
thicknessEntry.setText(thicknessParam.getValueObj().getAsString());
// Insert additional code to add these widgets to myPanel

After you have specified the appropriate information, click OK or Apply to invoke the apply method. You use the apply method to get the users selections and set the values of the corresponding properties.

public void apply (boolean willclose){
theShape = (String)shapeMenu.getSelectedItem();
shapeProp.getValueObj().setValue(theShape);
materialProp.getValueObj().setValue((String)materialMenu.getSelectedItem
());
thicknessParam.getValueObj().setValue(thicknessEntry.getText());

In addition, for this component you have specified that the Dimensions input aggregate parameter should have members added to it depending on the selected shape. Make sure the shape has actually changed because removing the existing members will remove any mappings that might be defined to pass new values in for the dimensions.

DtAggregateVariable dimensions = 
((DtAggregateVariable)component.getParameter("Dimensions"));
if (!theShape.equals(oldShape) || dimensions.getMemberList().size() == 
0) {
dimensions.removeAll();
Vector newDimensions = new Vector();
if (theShape.equals("circle")) {
newDimensions.add("radius");
}
else if (theShape.equals("rectangle")) {
newDimensions.add("width");
newDimensions.add("height");
}
else if (theShape.equals("triangle")) {0
newDimensions.add("base");
newDimensions.add("height");
}
DtScalarVariable dimensionItem;
for (int i = 0; i < newDimensions.size(); i++) {
dimensionItem = DtModelManager.createScalarVariable
((String)newDimensions.get(i), EsiTypes.REAL,
Variable.ROLE_PARAMETER, Variable.MODE_INPUT, null, 
null);
dimensions.addMember(dimensionItem);
}
}

The complete code for the editor for this Plate component can be found in Component Development Reference.