1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Improve data form API

Add

DataForm.getField(String)
DataForm.getHiddenFormTypeField()
This commit is contained in:
Florian Schmaus 2015-01-10 18:01:07 +01:00
parent 64242ace72
commit b71039660b
2 changed files with 60 additions and 8 deletions

View file

@ -35,6 +35,11 @@ public class FormField {
public static final String ELEMENT = "field"; public static final String ELEMENT = "field";
/**
* The constant String "FORM_TYPE"
*/
public static final String FORM_TYPE = "FORM_TYPE";
/** /**
* Form Field Types as defined in XEP-4 § 3.3. * Form Field Types as defined in XEP-4 § 3.3.
* *
@ -118,10 +123,11 @@ public class FormField {
} }
} }
private final String variable;
private String description; private String description;
private boolean required = false; private boolean required = false;
private String label; private String label;
private String variable;
private Type type; private Type type;
private final List<Option> options = new ArrayList<Option>(); private final List<Option> options = new ArrayList<Option>();
private final List<String> values = new ArrayList<String>(); private final List<String> values = new ArrayList<String>();
@ -142,6 +148,7 @@ public class FormField {
* name. * name.
*/ */
public FormField() { public FormField() {
this(null);
this.type = Type.fixed; this.type = Type.fixed;
} }
@ -214,7 +221,12 @@ public class FormField {
/** /**
* Returns the variable name that the question is filling out. * Returns the variable name that the question is filling out.
* * <p>
* According to XEP-4 § 3.2 the variable name (the 'var' attribute)
* "uniquely identifies the field in the context of the form" (if the field is not of type 'fixed', in which case
* the field "MAY possess a 'var' attribute")
* </p>
*
* @return the variable name of the question. * @return the variable name of the question.
*/ */
public String getVariable() { public String getVariable() {
@ -270,11 +282,19 @@ public class FormField {
/** /**
* Sets an indicative of the format for the data to answer. * Sets an indicative of the format for the data to answer.
* <p>
* This method will throw an IllegalArgumentException if type is 'fixed'. To create FormFields of type 'fixed' use
* {@link #FormField()} instead.
* </p>
* *
* @param type an indicative of the format for the data to answer. * @param type an indicative of the format for the data to answer.
* @see Type * @see Type
* @throws IllegalArgumentException if type is 'fixed'.
*/ */
public void setType(Type type) { public void setType(Type type) {
if (type == Type.fixed) {
throw new IllegalArgumentException("Can not set type to fixed, use FormField constructor without arguments instead.");
}
this.type = type; this.type = type;
} }

View file

@ -125,6 +125,24 @@ public class DataForm implements PacketExtension {
} }
} }
/**
* Return the form field with the given variable name or null.
*
* @param variableName
* @return the form field or null.
* @since 4.1
*/
public FormField getField(String variableName) {
synchronized (fields) {
for (FormField field : fields) {
if (variableName.equals(field.getVariable())) {
return field;
}
}
}
return null;
}
public String getElementName() { public String getElementName() {
return ELEMENT; return ELEMENT;
} }
@ -169,6 +187,11 @@ public class DataForm implements PacketExtension {
* @param field the field to add to the form. * @param field the field to add to the form.
*/ */
public void addField(FormField field) { public void addField(FormField field) {
String fieldVariableName = field.getVariable();
if (fieldVariableName != null && getField(fieldVariableName) != null) {
throw new IllegalArgumentException("This data form already contains a form field with the variable name '"
+ fieldVariableName + "'");
}
synchronized (fields) { synchronized (fields) {
fields.add(field); fields.add(field);
} }
@ -206,6 +229,20 @@ public class DataForm implements PacketExtension {
return Collections.unmodifiableList(extensionElements); return Collections.unmodifiableList(extensionElements);
} }
/**
* Returns the hidden FORM_TYPE field or null if this data form has none.
*
* @return the hidden FORM_TYPE field or null.
* @since 4.1
*/
public FormField getHiddenFormTypeField() {
FormField field = getField(FormField.FORM_TYPE);
if (field != null && field.getType() == FormField.Type.hidden) {
return field;
}
return null;
}
/** /**
* Returns true if this DataForm has at least one FORM_TYPE field which is * Returns true if this DataForm has at least one FORM_TYPE field which is
* hidden. This method is used for sanity checks. * hidden. This method is used for sanity checks.
@ -213,12 +250,7 @@ public class DataForm implements PacketExtension {
* @return true if there is at least one field which is hidden. * @return true if there is at least one field which is hidden.
*/ */
public boolean hasHiddenFormTypeField() { public boolean hasHiddenFormTypeField() {
for (FormField f : fields) { return getHiddenFormTypeField() != null;
if (f.getVariable().equals("FORM_TYPE") && f.getType() == FormField.Type.hidden) {
return true;
}
}
return false;
} }
@Override @Override