FormFieldWrapper.java

 1package eu.siacs.conversations.ui.forms;
 2
 3import android.content.Context;
 4import android.text.SpannableString;
 5import android.text.style.ForegroundColorSpan;
 6import android.text.style.StyleSpan;
 7import android.util.Log;
 8import android.view.LayoutInflater;
 9import android.view.View;
10
11import java.util.List;
12
13import eu.siacs.conversations.Config;
14import eu.siacs.conversations.R;
15import eu.siacs.conversations.xmpp.forms.Field;
16
17public abstract class FormFieldWrapper {
18
19	protected final Context context;
20	protected final Field field;
21	protected final View view;
22	protected OnFormFieldValuesEdited onFormFieldValuesEditedListener;
23
24	protected FormFieldWrapper(Context context, Field field) {
25		this.context = context;
26		this.field = field;
27		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
28		this.view = inflater.inflate(getLayoutResource(), null);
29		String label = field.getLabel();
30		if (label == null) {
31			label = field.getFieldName();
32		}
33		setLabel(label, field.isRequired());
34	}
35
36	public final void submit() {
37		this.field.setValues(getValues());
38	}
39
40	public final View getView() {
41		return view;
42	}
43
44	protected abstract void setLabel(String label, boolean required);
45
46	abstract List<String> getValues();
47
48	protected abstract void setValues(List<String> values);
49
50	abstract boolean validates();
51
52	abstract protected int getLayoutResource();
53
54	abstract void setReadOnly(boolean readOnly);
55
56	protected SpannableString createSpannableLabelString(String label, boolean required) {
57		SpannableString spannableString = new SpannableString(label + (required ? " *" : ""));
58		if (required) {
59			int start = label.length();
60			int end = label.length() + 2;
61			spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), start, end, 0);
62			spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.accent)), start, end, 0);
63		}
64		return spannableString;
65	}
66
67	protected void invokeOnFormFieldValuesEdited() {
68		if (this.onFormFieldValuesEditedListener != null) {
69			this.onFormFieldValuesEditedListener.onFormFieldValuesEdited();
70		}
71	}
72
73	public boolean edited() {
74		return !field.getValues().equals(getValues());
75	}
76
77	public void setOnFormFieldValuesEditedListener(OnFormFieldValuesEdited listener) {
78		this.onFormFieldValuesEditedListener = listener;
79	}
80
81	protected static <F extends FormFieldWrapper> FormFieldWrapper createFromField(Class<F> c, Context context, Field field) {
82		try {
83			F fieldWrapper = c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field);
84			fieldWrapper.setValues(field.getValues());
85			return fieldWrapper;
86		} catch (Exception e) {
87			e.printStackTrace();
88			return null;
89		}
90	}
91
92	public interface OnFormFieldValuesEdited {
93		void onFormFieldValuesEdited();
94	}
95}