This is an old revision of the document!


Polymorphic Form

Polymorhpic Form is a SOLoist sample application that demonstrates how a form can change depending on the type of the selected object that is provided on the form's input pin.

Selecting a Bank Adviser object (as a subkind of Person) from the combo box on the left will display a details form with an additional field Bank Name.

The implementation uses a GUIConformsToFilter component that selects one of the panels in a deck to show, according to the type of the object provided on its input pin.

Live example

UML Model

Business Logic Code

None.

GUI Code

package rs.sol.sampleapps;
 
import rs.sol.soloist.helpers.init.DefaultContextInit;
import rs.sol.soloist.helpers.init.Initializer;
import rs.sol.soloist.helpers.init.InitializerFailedException;
import rs.sol.soloist.server.builtindomains.builtindatatypes.Text;
import rs.sol.soloist.server.guiconfiguration.components.GUIApplicationComponent;
import rs.sol.soloist.server.guiconfiguration.components.GUIDeckComponent;
import rs.sol.soloist.server.guiconfiguration.components.GUILabelComponent;
import rs.sol.soloist.server.guiconfiguration.components.GUIPanelComponent;
import rs.sol.soloist.server.guiconfiguration.construction.GUIComponentBinding;
import rs.sol.soloist.server.guiconfiguration.elementcomponents.GUICollectionInput;
import rs.sol.soloist.server.guiconfiguration.elementcomponents.GUIComboWidget;
import rs.sol.soloist.server.guiconfiguration.elementcomponents.GUIElementComponent;
import rs.sol.soloist.server.guiconfiguration.elementcomponents.GUISlotEditorKind;
import rs.sol.soloist.server.guiconfiguration.nonvisualcompoments.GUIConformsToFilter;
import rs.sol.soloist.server.guiconfiguration.nonvisualcompoments.GUIFindAllInstancesSAPComponent;
import rs.sol.soloist.server.server.SoloistServiceServlet;
 
public enum Polymorphic implements Initializer
{
	INSTANCE;
 
	@Override
	public void init() throws InitializerFailedException
	{
		GUIApplicationComponent page = new GUIApplicationComponent();
		page.name.set(Text.fromString("Polymorphic")); 
		SoloistServiceServlet.registerApplication(page);
		page.context.set(DefaultContextInit.getRoot());
 
		GUIPanelComponent root = GUIPanelComponent.createFlow(page);
 
		GUILabelComponent title = GUILabelComponent.create(root, "Polymorphic");
		title.styleName.set(Text.fromString("titleStyle"));
 
		GUIPanelComponent topPanel = GUIPanelComponent.createFlow(root);
		topPanel.styleName.set(Text.fromString("topPanel"));
 
		GUIPanelComponent wrap = GUIPanelComponent.createFlow(topPanel);
 
		GUILabelComponent.create(wrap, "Choose person:");
 
		GUIFindAllInstancesSAPComponent allPersons = GUIFindAllInstancesSAPComponent.create(wrap, Person.FQ_TYPE_NAME);
		GUIElementComponent comboBox = GUIElementComponent.createInput(wrap, new GUIComboWidget(), new GUICollectionInput());
		GUIComponentBinding.create(allPersons.value, GUICollectionInput.get(comboBox).collection);
 
		GUIDeckComponent deck = GUIDeckComponent.create(wrap);
 
		GUIPanelComponent personDetailsTable = GUIPanelComponent.createTable(deck);
		createPersonsDetails(comboBox, personDetailsTable);
 
		GUIPanelComponent bankAdviserDetailsTable = GUIPanelComponent.createTable(deck);
		int row = createPersonsDetails(comboBox, bankAdviserDetailsTable);
		GUILabelComponent.create(bankAdviserDetailsTable, "Bank Name: ", row, 0);
		GUIElementComponent nameEditor = GUIElementComponent.createSlotEditor(bankAdviserDetailsTable, BankAdviser.PROPERTIES.bank, row++, 1);
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(nameEditor).element);
 
		// Is the selected object an instance of the BankAdviser class?
		GUIConformsToFilter isItBankAdviser = GUIConformsToFilter.create(deck, comboBox.value, BankAdviser.CLASSIFIER); 
		GUIComponentBinding.create(isItBankAdviser.yes, bankAdviserDetailsTable.show);
		GUIComponentBinding.create(isItBankAdviser.no, personDetailsTable.show);
	}
 
	private int createPersonsDetails(GUIElementComponent comboBox, GUIPanelComponent detailsTable) {
		int row = 0;
		GUILabelComponent.create(detailsTable, "Name: ", row, 0);
		GUIElementComponent nameEditor = GUIElementComponent.createSlotEditor(detailsTable, Person.PROPERTIES.name, row++, 1);
 
		GUILabelComponent.create(detailsTable, "Gender: ", row, 0);
		GUIElementComponent genderEditor = GUIElementComponent.createSlotEditor(detailsTable, Person.PROPERTIES.gender, row++, 1);
 
		GUILabelComponent.create(detailsTable, "Age: ", row, 0);
		GUIElementComponent ageEditor = GUIElementComponent.createSlotEditor(detailsTable, Person.PROPERTIES.age, row++, 1);
 
		GUILabelComponent.create(detailsTable, "Date of birth: ", row, 0);
		GUIElementComponent dateOfBirthEditor = GUIElementComponent.createSlotEditor(detailsTable, Person.PROPERTIES.dateOfBirth, row++, 1);
 
		GUILabelComponent.create(detailsTable, "Height [m]: ", row, 0);
		GUIElementComponent heightEditor = GUIElementComponent.createSlotEditor(detailsTable, Person.PROPERTIES.height, row++, 1);
 
		GUILabelComponent.create(detailsTable, "Is married: ", row, 0);
		GUIElementComponent isMarriedEditor = GUIElementComponent.createSlotEditor(detailsTable, Person.PROPERTIES.isMarried, row++, 1);
 
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(nameEditor).element);
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(genderEditor).element);
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(ageEditor).element);
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(dateOfBirthEditor).element);
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(heightEditor).element);
		GUIComponentBinding.create(comboBox.value, GUISlotEditorKind.get(isMarriedEditor).element);
		return row;
	}
}
Print/export