当前,我们有一个方法addLabelToNode
可以在控件的值更改(例如下拉选择等)上运行。目前,该方法是从文件DataModel.scala中的notifyValueChange
调用的。
这在Form Runner中工作正常,但是如果我们为Form Builder中的控件设置默认值,它将不会运行addLabelToNode
。
notifyValueChange:
private def notifyValueChange(
containingDocument: XFormsContainingDocument,
nodeInfo: NodeInfo,
oldValue: String,
newValue: String,
isCalculate: Boolean,
collector: XFormsEvent ⇒ Unit
) =
Option(containingDocument.getInstanceForNode(nodeInfo)) match {
case Some(modifiedInstance) ⇒
// Tell the model about the value change
try {
val nodeName = nodeInfo.name
var objectControl: XFormsObject = null
try {
objectControl = containingDocument.resolveObjectById(containingDocument.getEffectiveId, nodeName + "-control", null)
} catch {
case _: NullPointerException =>
case e: Exception => e.printStackTrace(); XFormsServer.logger.warn(e.getMessage);
}
objectControl match {
case selectControl: XFormsSelect1Control =>
addLabelToNode(selectControl, newValue, nodeInfo)
case valueComponentControl: XFormsValueComponentControl =>
valueComponentControl.children.head match {
case componentRootControl: XXFormsComponentRootControl =>
componentRootControl.children.foreach {
case wrappedSelectControl: XFormsSelect1Control =>
addLabelToNode(wrappedSelectControl, newValue, nodeInfo)
case _ =>
}
case _ =>
}
case _ =>
}
} catch {
case e: Exception => e.printStackTrace(); XFormsServer.logger.warn(e.getMessage);
}
// rest of the method
addLabelToNode:
private def addLabelToNode(control: XFormsSelect1Control, newValue: String, nodeInfo: NodeInfo): Unit = {
try {
val name = control.element.getQName.getName
if ("select1".equals(name)) {
val label = control.getItemset.selectedItems(newValue).head.label.label
XFormsAPI.ensureAttribute(nodeInfo, new QName("label", null, null), label)
} else if ("select".equals(name)) {
val stringList = new util.ArrayList[String]()
val newValueArray = newValue.split(" ")
newValueArray.foreach(
v => {
val item = control.getItemset.selectedItems(newValue).find(i => i.value == v).get
stringList.add(item.label.label)
}
)
val label = stringList.toArray.mkString(" ")
XFormsAPI.ensureAttribute(nodeInfo, new QName("label", null, null), label)
}
} catch {
case _: NoSuchElementException => XFormsAPI.ensureAttribute(nodeInfo, new QName("label", null, null), "")
}
}