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		String label = field.getLabel();
23		if (label == null) {
24			label = field.getFieldName();
25		}
26		setLabel(label, field.isRequired());
27	}
28
29	public void submit() {
30		this.field.setValues(getValues());
31	}
32
33	public View getView() {
34		return view;
35	}
36
37	protected abstract void setLabel(String label, boolean required);
38
39	abstract List<String> getValues();
40
41	abstract boolean validates();
42
43	abstract protected int getLayoutResource();
44
45	protected static <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> c, Context context, Field field) {
46		try {
47			return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field);
48		} catch (Exception e) {
49			e.printStackTrace();
50			return null;
51		}
52	}
53}