Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Friday, March 18, 2011

GXT Nuggets: Hipocrite's combo box

Bananas for the code monkey


Ever faced a situation where you want to show the user some information 'X' but you actually need 'Y' for the backend logic? Say you have an employee entity with a employee details and a auto-generated PK. You have designed a form to show the employee details, based on the user selection (in a drop-down combo). A hiprocrite's combo is up for this task. It shows employee name/number in the UI but secretly holds the PK deep down.

You may wonder why all the fuss, why not just set the state in the drop down. Works wonderfully provided you 'remember' the state's 'key'. If I have 10 such forms, this involves 10 if() stmts atleast. To overcome this and let the contract-driven design make life easier for us, I re-invented the combo box. I added an adaptive behaviour in form of DynamicProperyEditor. This enables us to query for various model properties without excessive intrusion.

public class DynamicPropertyEditor<Data extends BaseModelData> extends ListModelPropertyEditor<BaseModelData>{

private String propertyName;

public DynamicPropertyEditor(String property) {
this.propertyName = property;
}

public String getStringValue(Data value){
return value.get(propertyName);
}

public String getPropertyName() {
return propertyName;
}
}

public class X10ComboBox<D extends BaseModelData> extends ComboBox<D> {
private DynamicPropertyEditor<D> dynamicPropertyEditor = null;

public DynamicPropertyEditor<D> getDynamicPropertyEditor(){
return dynamicPropertyEditor;
}

public void setDynamicPropertyEditor(DynamicPropertyEditor<D> dynamicPropertyEditor) {
this.dynamicPropertyEditor = dynamicPropertyEditor;
}
@Override
public ListModelPropertyEditor<D> getPropertyEditor() {
....
}
}

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.