Changing forms programtically

From Protege Wiki
Jump to navigationJump to search

Changing forms programmatically

The Protege API provides access also to the forms configuration. An application can change programmatically the display of a class or an instance the same way in which it is changed in the Forms tab in the Protege UI.


Replacing the slot widget for a class

A class display is made up of slot widgets. For each slot type, there is a default slot widget used to display the value of the slot. For example, slot of type String and cardinality single has as a default slot widget, the TextFieldWidget.

An application can replace the widget used to display a slot on a class form. The following code uses the Newspaper project and replaces the slot widget for slot other_information for class Editor from TextAreaWidget to TextFieldWidget>.

//the class for which the form needs to be customized
Cls editorCls = kb.getCls("Editor");

FormWidget formWidget = (FormWidget) kb.getProject().getDesignTimeClsWidget(editorCls);

//slot to customize
Slot testSlot = kb.getSlot("other_information");

SlotWidget sw = formWidget.getSlotWidget(testSlot);

formWidget.replaceWidget(testSlot, "edu.stanford.smi.protege.widget.TextFieldWidget");

//optional - set the label of the slot widget		
sw.setLabel("MyLabel");

//inform the UI that the class form has been customized
formWidget.getDescriptor().setDirectlyCustomizedByUser(true);


Hiding a slot widget from the class display

Hiding a slot widget is done by replacing the slot widget with null. So, in the code from above the line containing the replaceWidget call should be replaced with the following line:

formWidget.replaceWidget(testSlot, null);