Copy Existing Form

This script creates a copy of an existing form view, modifies that copy and saves it as a new form.

Defaults for this script are set for the Pubchem demo data tree in the sample project included in IJC.

import com.chemaxon.ijc.form.api.*;
import com.chemaxon.ijc.form.api.renderers.*;
import com.chemaxon.ijc.commons.datarequest.api.FieldReference;
import com.im.df.api.ddl.DFView;
 
ety = dataTree.rootVertex.entity
 
// load some form - e.g. template
DFView view = getViewByName("Sample form")
form = DFViews.loadForm(view)
 
// tweak the template
 
// change label
label = form.getFirstWidgetByName("My Label 123")
label.setText(label.getText() + " (Copy)")
label.setSize(label.getWidth() + 50, label.getHeight())
 
// add new textbox
textBox = new TextBoxWidget()
textBox.setPosition(0, 600)
textBox.setSize(400, 50)
 
textBox.setField(newFieldReference("Formula"))
textBox.setRenderer(new TextRenderer())
form.addWidget(textBox)
 
// save
DFViews.create(dataTree, form, view.name + " (Copy)")
 
// utility methods
def newFieldReference(name) {
field = ety.fields.items.find { it.name == name }
new FieldReference(ety.id, field.id)
}
 
def getViewByName(name) {
dataTree.views.items.find { it.name == name }
}