Plate Handler

This section contains the full code for the Plate Handler.

public class PlateHandler
extends DefaultComponentHandler {

transient private static final Class CLASS = PlateHandler.class;

private ScalarVariable shapeProp;
private ScalarVariable materialProp;
private ScalarVariable thicknessParam;


/**
* This implementation saves a reference to the properties
* @param component
* @throws SDKException
*/
public void initHandler(DtComponent component)
throws SDKException {
super.initHandler(component);
}

public void addedToModel() throws SDKException {

try {
shapeProp = 
((DtScalarVariable)component.getProperty("Shape"));
materialProp = ((DtScalarVariable)component.getProperty
("Material"));
thicknessParam = ((DtScalarVariable)component.getParameter
("Thickness"));
}
catch (Throwable e) {
throw new SDKException(e, "Error initializing component 
Handler.");
}
}

/**
* Reacts to changes to the pre-defined parameters
* @param event
* @throws DtModelException
* @throws VariableException
*/
public void modelChanged(DtModelEvent event)
throws DtModelException, VariableException {

// A MODEL_UPDATED type of event is a way to bundle numerous 
events
// under a single one so they can all be processed together. We 
must 
// account for these here by looping over all of the bundled 
events  
// and processing each
if (event.getEventType() == DtModelEvent.MODEL_UPDATED) {
Iterator eventIter = 
event.getAccumulatedEvents().iterator();
while (eventIter.hasNext()) {
this.modelChanged((DtModelEvent)eventIter.next());
}
return;
}

DtModelElement dtme = event.getModelElement();

// Only interested in Variable events for my component
DtComponent eventComp = event.getParentCompForEvent();
if (eventComp == myComponent && dtme instanceof Variable) {

Variable eVar = (Variable)dtme;

Variable rootVar = VariableUtil.getRootVariable(eVar);
String rootVarName = rootVar.getName();

// if one of our predefined parameter names changes, change 
it back
if (event.getChangeType() == DtModelEvent.CHANGED_VARNAME) {
String oldName = (String)event.getOldValue();
String newName = 
VariableUtil.getVariablePathAsString(eVar);

if (oldName.equals("Dimensions")) {
((DtVariable)eVar).setName("Dimensions");
}
else if (oldName.equals("Area")) {
((DtVariable)eVar).setName("Area");
}
else if (oldName.equals("Volume")) {
((DtVariable)eVar).setName("Volume");
}
else if (oldName.equals("Weight")) {
((DtVariable)eVar).setName("Weight");
}
}


// if a pre-defined parameter is deleted, add it back
if (event.getEventType() == DtModelEvent.ELEMENT_REMOVED) {

String varName = eVar.getName();
DtVariable dtVar = (DtVariable)eVar;

if (varName.equals("Dimensions") || 
varName.equals("Area")
|| varName.equals("Weight") || varName.equals("Volume")) 
{
myComponent.addParameter(dtVar);
}
}

// if a pre-defined parameter is changed to an inappropriate 
type
if (event.getChangeType() == DtModelEvent.CHANGED_TYPE) {

String varName = eVar.getName();
DtVariable dtVar = (DtVariable)eVar;

if (varName.equals("Dimensions")) {
dtVar.setMode(Variable.MODE_INPUT);
}
else if (varName.equals("Area") || 
varName.equals("Weight") ||
varName.equals("Volume")) {
dtVar.setMode(Variable.MODE_OUTPUT);
}
}

}


}

/**
* Validates the values of the properties
* @param validationList
* @throws SDKException
*/
public void validate(java.util.List validationList)
throws SDKException {

try {
String name = myComponent.getName();
MetaModel myMM = myComponent.getMetaModel();

// warn if shape, material, or thickness are not 
specified
if (shapeProp.getValueObj().getAsString().length() == 0) 
{
validationList.add(new
ValidationResult(ValidationEvent.SEVERITY_WARNING, 
myComponent,
new IString(CLASS, 0, "{0}: No shape 
specified for the plate", name), "shape"));
}
if (materialProp.getValueObj().getAsString().length() == 
0) {
validationList.add(new 
ValidationResult(ValidationEvent.SEVERITY_WARNING, 
myComponent,
new IString(CLASS, 0, "{0}: No 
material specified for the plate", name), "material"));
}
if (thicknessParam.getValueObj().getAsString().length() 
== 0) {
validationList.add(new 
ValidationResult(ValidationEvent.SEVERITY_WARNING, 
myComponent,new IString(CLASS, 0, "{0}: No thickness 
specified for the plate", name), "thickness"));
}


}
catch (Exception e) {
throw new SDKException(e, e.getMessage());
}
}

}