From d2c5a939ed2cf42374e0ddb528ee363b7fefee8c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 26 Jan 2016 17:23:24 +0100 Subject: [PATCH] show values in formfieldwrappers and allow form to be set to read only --- .../ui/forms/FormBooleanFieldWrapper.java | 14 ++++++++++++++ .../ui/forms/FormFieldWrapper.java | 11 +++++++---- .../ui/forms/FormJidSingleFieldWrapper.java | 11 +++++++++++ .../ui/forms/FormTextFieldWrapper.java | 17 +++++++++++++++++ .../conversations/ui/forms/FormWrapper.java | 6 ++++++ .../eu/siacs/conversations/xmpp/forms/Data.java | 6 +++--- 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java index eba0f12d9..6cb357a90 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormBooleanFieldWrapper.java @@ -39,6 +39,15 @@ public class FormBooleanFieldWrapper extends FormFieldWrapper { return values; } + @Override + protected void setValues(List values) { + if (values.size() == 0) { + checkBox.setChecked(false); + } else { + checkBox.setChecked(Boolean.parseBoolean(values.get(0))); + } + } + @Override public boolean validates() { if (checkBox.isChecked() || !field.isRequired()) { @@ -63,4 +72,9 @@ public class FormBooleanFieldWrapper extends FormFieldWrapper { protected int getLayoutResource() { return R.layout.form_boolean; } + + @Override + void setReadOnly(boolean readOnly) { + checkBox.setEnabled(!readOnly); + } } diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java index 8ba62bdd8..3a21ade3b 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormFieldWrapper.java @@ -45,10 +45,14 @@ public abstract class FormFieldWrapper { abstract List getValues(); + protected abstract void setValues(List values); + abstract boolean validates(); abstract protected int getLayoutResource(); + abstract void setReadOnly(boolean readOnly); + protected SpannableString createSpannableLabelString(String label, boolean required) { SpannableString spannableString = new SpannableString(label + (required ? " *" : "")); if (required) { @@ -61,11 +65,8 @@ public abstract class FormFieldWrapper { } protected void invokeOnFormFieldValuesEdited() { - Log.d(Config.LOGTAG, "invoke on form field values edited"); if (this.onFormFieldValuesEditedListener != null) { this.onFormFieldValuesEditedListener.onFormFieldValuesEdited(); - } else { - Log.d(Config.LOGTAG,"listener is null"); } } @@ -79,7 +80,9 @@ public abstract class FormFieldWrapper { protected static FormFieldWrapper createFromField(Class c, Context context, Field field) { try { - return c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field); + F fieldWrapper = c.getDeclaredConstructor(Context.class, Field.class).newInstance(context,field); + fieldWrapper.setValues(field.getValues()); + return fieldWrapper; } catch (Exception e) { e.printStackTrace(); return null; diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java index 3890a1a7a..553e8f21f 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormJidSingleFieldWrapper.java @@ -3,6 +3,8 @@ package eu.siacs.conversations.ui.forms; import android.content.Context; import android.text.InputType; +import java.util.List; + import eu.siacs.conversations.R; import eu.siacs.conversations.xmpp.forms.Field; import eu.siacs.conversations.xmpp.jid.InvalidJidException; @@ -30,4 +32,13 @@ public class FormJidSingleFieldWrapper extends FormTextFieldWrapper { } return super.validates(); } + + @Override + protected void setValues(List values) { + StringBuilder builder = new StringBuilder(""); + for(String value : values) { + builder.append(value); + } + editText.setText(builder.toString()); + } } diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java index 47b8d86cb..b7dac9510 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormTextFieldWrapper.java @@ -62,6 +62,18 @@ public class FormTextFieldWrapper extends FormFieldWrapper { return values; } + @Override + protected void setValues(List values) { + StringBuilder builder = new StringBuilder(""); + for(int i = 0; i < values.size(); ++i) { + builder.append(values.get(i)); + if (i < values.size() - 1 && "text-multi".equals(field.getType())) { + builder.append("\n"); + } + } + editText.setText(builder.toString()); + } + @Override public boolean validates() { if (getValue().trim().length() > 0 || !field.isRequired()) { @@ -77,4 +89,9 @@ public class FormTextFieldWrapper extends FormFieldWrapper { protected int getLayoutResource() { return R.layout.form_text; } + + @Override + void setReadOnly(boolean readOnly) { + editText.setEnabled(!readOnly); + } } diff --git a/src/main/java/eu/siacs/conversations/ui/forms/FormWrapper.java b/src/main/java/eu/siacs/conversations/ui/forms/FormWrapper.java index 51d15f0c2..eafe95cc8 100644 --- a/src/main/java/eu/siacs/conversations/ui/forms/FormWrapper.java +++ b/src/main/java/eu/siacs/conversations/ui/forms/FormWrapper.java @@ -52,6 +52,12 @@ public class FormWrapper { } } + public void setReadOnly(boolean b) { + for(FormFieldWrapper fieldWrapper : fieldWrappers) { + fieldWrapper.setReadOnly(b); + } + } + public boolean edited() { boolean edited = false; for(FormFieldWrapper fieldWrapper : fieldWrappers) { diff --git a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java index d05c9abb3..0053a399c 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java +++ b/src/main/java/eu/siacs/conversations/xmpp/forms/Data.java @@ -53,16 +53,16 @@ public class Data extends Element { public void submit() { this.setAttribute("type","submit"); - removeNonFieldChildren(); + removeUnnecessaryChildren(); for(Field field : getFields()) { field.removeNonValueChildren(); } } - private void removeNonFieldChildren() { + private void removeUnnecessaryChildren() { for(Iterator iterator = this.children.iterator(); iterator.hasNext();) { Element element = iterator.next(); - if (!element.getName().equals("field")) { + if (!element.getName().equals("field") && !element.getName().equals("title")) { iterator.remove(); } }