Dealing with children with different parameters generically for user input

Hi all,

This is not a per se jme question, sorry for that, but thought maybe it would be an interesting enough question it could be overlooked. Feel free to delete it if you think it’s inappropriate here.

I’m having problems dealing with children of a parent type with different parameters (and possibly different parameters types) when asking the user for parameters values via the 2D interface.

From what I found, only java 8 allows reflection to retrieve parameter names and therefore build user-friendly generic interfaces + creating the objects from the user added data.
I would really prefer not running into java version problems just for this.

Any ideas of a pattern or something? All my solutions are kind of lame for this. This problem keeps repeating so my lame solutions are becoming problematic.

Assuming I understand you correct, you’d want to use the Adapter pattern.

Edit: Here’s a definition: “The adapter pattern is a design pattern that is used to allow two incompatible types to communicate. Where one class relies upon a specific interface that is not implemented by another class, the adapter acts as a translator between the two types.”

If I have misunderstood, let me know.

1 Like

Thank you for your answer.

I’m currently checking it up… may take a lil before I come back saying if you understood my fumble explanation correctly… because for some reason I’m totally hermetic at understanding patterns… always takes a while with me, but I get there finally :).

Thank you @wezrule, that pattern totally helped me… not sure if I implemented it correctly, but it sure pointed me towards a way to do it good enough for me.

So, the aim was to easily display parameters of whatever object, and get back either a new object of that class filled with the new values or update an instance of that class with the new values on demand.

Basically:

  • create an adaptee class for that type, implementing the needed methods to get the params, a method to create a new instance and a method to update an instance.
  • a generic adapter that interrogates the adaptee to get the data and hand it over in an easy to use way; is also the link between the adaptee and the tonegodGUI adapter.
  • a tonegodGUI adapter that deals with the tonegodGUI specifics and returning a list of elements to display, with the needed data; also deals with recuperating the data from the UI.
  • the screen UI is responsible for positioning/dimensioning and such stuff

Currently:

  • creates a selectBox when passed an enum
  • creates a checkbox when passed a boolean
  • creates textField for other cases
  • deterministic naming in case of UI specifics
  • provides type / min / max values to allow validation on the UI side
  • sets default values or passes adapted values

It uses my “enriched” (yes, bad name lol) tonegodGUI controls.
Could be quickly modified to use the standard lib controls, but not sure how to add event handling then, while with my controls, the screenEventListener works the same way for generated and directly typed-in controls.

Thinking of adding support for tooltips, sliders and classes to validate content. Maybe dials too.

Interface the adaptees need to implement:
[java]
public interface ControlAdaptee {

public enum ParamType{
    STRING, FLOAT, DOUBLE, INTEGER, ENUM, BOOLEAN
}

public List<String> getParamNames();
public List<ParamType> getParamTypes();
public List<String> getParamDefaultValues();
public List<Class <? extends Enum>> getParamEnums();
public List<String> getParamMin();
public List<String> getParamMax();

public Object getNewAdapted(List<Object> values);
public Object getUpdatedAdapted(List<Object> values, Object adapted);

}
[/java]

Getting the controls to be displayed:
[java]
ControlAdapterTonegodGUI controlAdapter2D = new ControlAdapterTonegodGUI(new ControlAdapter());
List<? extends Element> list = controlAdapter2D.getParametersControls(UIDPrefix, screen, screenEventListener, new SpotLightAdaptee());
[/java]

Getting the controls to be displayed with default values filled by the adapted values:
[java]
List<? extends Element> list = controlAdapter2D.getParametersControls(UIDPrefix, screen, screenEventListener, new SpotLightAdaptee(), spotLight);
[/java]

Getting a new adapted back with it’s new values:
[java]
SpotLight adapted = (SpotLight)controlAdapter2D.getNewAdapted(UIDPrefix, screen, new SpotLightAdaptee());
[/java]

Updating an adapted with the new values:
[java]
spotLight = (SpotLight)getUpdatedAdapted(UIDPrefix, screen, new SpotLightAdaptee(), spotLight);
[/java]

If anybody is curious, I can post the code.

Anyway, thx a lot for your help!

1 Like

Nvm

nvm fixed (just found out I could edit my first post :D)