FormFieldWrapper.java

 1package eu.siacs.conversations.ui.forms;
 2
 3import android.content.Context;
 4import android.view.LayoutInflater;
 5import android.view.View;
 6
 7import java.util.List;
 8
 9import eu.siacs.conversations.xmpp.forms.Field;
10
11public abstract class FormFieldWrapper {
12
13	protected final Context context;
14	protected final Field field;
15	protected final View view;
16
17	protected FormFieldWrapper(Context context, Field field) {
18		this.context = context;
19		this.field = field;
20		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
21		this.view = inflater.inflate(getLayoutResource(), null);
22		setLabel(field.getLabel(), field.isRequired());
23	}
24
25	public void submit() {
26		this.field.setValues(getValues());
27	}
28
29	public View getView() {
30		return view;
31	}
32
33	protected abstract void setLabel(String label, boolean required);
34
35	abstract List<String> getValues();
36
37	abstract protected int getLayoutResource();
38
39	protected static <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> c, Context context, Field field) {
40		try {
41			return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field);
42		} catch (Exception e) {
43			e.printStackTrace();
44			return null;
45		}
46	}
47}