From be9972625ec4b0f7444f082e1904231422743aef Mon Sep 17 00:00:00 2001 From: Manish Pande Date: Tue, 2 Jun 2015 12:23:10 +0530 Subject: [PATCH] Refactored code --- formbuilder-android/AndroidManifest.xml | 2 +- .../src/com/vertis/formbuilder/Address.java | 68 +++++++--------- .../src/com/vertis/formbuilder/Capture.java | 24 ++---- .../formbuilder/CaptureContentAdapter.java | 4 +- .../src/com/vertis/formbuilder/Checkbox.java | 59 +++++++------- .../src/com/vertis/formbuilder/Contact.java | 35 +++------ .../formbuilder/CountriesArrayAdapter.java | 2 +- .../vertis/formbuilder/DateTimePicker.java | 35 +++++---- .../vertis/formbuilder/DisplayDateTime.java | 63 ++++++--------- .../src/com/vertis/formbuilder/Dropdown.java | 41 ++++------ .../src/com/vertis/formbuilder/Email.java | 40 +++------- .../com/vertis/formbuilder/FieldRegistry.java | 6 +- .../com/vertis/formbuilder/FormBuilder.java | 22 ++---- .../com/vertis/formbuilder/FullNameXml.java | 33 +++----- .../src/com/vertis/formbuilder/IField.java | 20 ++--- .../Listeners/CheckConditions.java | 2 +- .../CustomTextChangeListener.java | 6 +- .../Listeners/TextChangeListener.java | 28 ------- .../vertis/formbuilder/MultiLineEditText.java | 38 +++------ .../com/vertis/formbuilder/NumberField.java | 54 +++++++------ .../src/com/vertis/formbuilder/Price.java | 52 ++++++------- .../src/com/vertis/formbuilder/Radio.java | 31 +++----- .../com/vertis/formbuilder/ResultJson.java | 2 +- .../com/vertis/formbuilder/SelectElement.java | 7 +- .../vertis/formbuilder/SimpleEditText.java | 77 ++++--------------- .../com/vertis/formbuilder/ViewLookup.java | 4 +- .../formbuilder/parser/FieldConfig.java | 7 +- .../formbuilder/parser/FieldOptions.java | 3 +- .../vertis/formbuilder/parser/FormJson.java | 2 +- .../formbuilder/util/FormBuilderUtil.java | 17 ++-- .../src/com/vertis/test/MainActivity.java | 10 +-- local.properties | 4 +- 32 files changed, 304 insertions(+), 494 deletions(-) rename formbuilder-android/src/com/vertis/formbuilder/{ => Listeners}/CustomTextChangeListener.java (91%) delete mode 100644 formbuilder-android/src/com/vertis/formbuilder/Listeners/TextChangeListener.java diff --git a/formbuilder-android/AndroidManifest.xml b/formbuilder-android/AndroidManifest.xml index 54a451b..ac3d7ec 100644 --- a/formbuilder-android/AndroidManifest.xml +++ b/formbuilder-android/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionName="1.0" > getCountryList() { Locale[] locale = Locale.getAvailableLocales(); - ArrayList countries = new ArrayList(); + ArrayList countries = new ArrayList<>(); String country; int i = 0; for (Locale loc : locale) { @@ -188,7 +188,7 @@ private ArrayList getCountryList() { .getDisplayCountry(), 1)); } } - i = i++; + i++; } Collections.sort(countries); return countries; @@ -218,29 +218,19 @@ public boolean validate() { return valid; } - private boolean checkEmpty(TextView view, String street, String errorMessage) { - if(config.getRequired() && street.equals("")){ - errorMessage(view, errorMessage); - return false; - } else{ - noErrorMessage(view); - return true; - } - } - @SuppressLint("ResourceAsColor") private void errorMessage(TextView view ,String message) { if(view==null)return; view.setText((this.config.getRequired()?"*":"") ); view.setText(view.getText() + message); - view.setTextColor(R.color.ErrorMessage); + view.setTextColor(view.getContext().getResources().getColor(R.color.ErrorMessage)); } @SuppressLint("ResourceAsColor") private void noErrorMessage(TextView view ) { if(view==null)return; view.setText(this.config.getRequired()?"Street*":"" ); - view.setTextColor(R.color.TextViewNormal); + view.setTextColor(view.getContext().getResources().getColor(R.color.TextViewNormal)); } @Override @@ -295,20 +285,20 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(fullAddress.toLowerCase().contains(value.toLowerCase()) || fullAddress.trim().equals("")){ - return true; - } + return !condition.equals("equals") || fullAddress.toLowerCase().contains(value.toLowerCase()) || fullAddress.trim().equals(""); + } + + public boolean isHidden() { + return ad != null && !ad.isShown(); + } + + private boolean checkEmpty(TextView view, String actual_value, String errorMessage) { + if(config.getRequired() && actual_value.equals("")){ + errorMessage(view, errorMessage); return false; + } else{ + noErrorMessage(view); + return true; } - return true; } - - public boolean isHidden(){ - if(ad!=null) { - return !ad.isShown(); - } else { - return false; - } - } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Capture.java b/formbuilder-android/src/com/vertis/formbuilder/Capture.java index 3d8c207..4a9089e 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Capture.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Capture.java @@ -39,7 +39,7 @@ public class Capture implements IField{ TextView tvCapture; Button buttonCaptureImage, buttonCaptureVideo, buttonCaptureAudio; ArrayAdapter contentAdapter; - ArrayList captureList= new ArrayList(); + ArrayList captureList= new ArrayList<>(); ListView lvCapture; //Values @@ -57,7 +57,7 @@ public Capture(FieldConfig fcg){ @Override public void createForm(final Activity context) { this.context = context; - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + LayoutInflater inflater =context.getLayoutInflater(); llCapture=(LinearLayout) inflater.inflate(R.layout.capture,null); tvCapture = (TextView) llCapture.findViewById(R.id.textViewCapture); buttonCaptureImage=(Button) llCapture.findViewById(R.id.buttonImage); @@ -138,7 +138,7 @@ private void modifyListViewHeight(){ private void noErrorMessage() { if(tvCapture==null)return; tvCapture.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvCapture.setTextColor(R.color.TextViewNormal); + tvCapture.setTextColor(tvCapture.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -178,7 +178,7 @@ private void errorMessage(String message) { if(tvCapture==null)return; tvCapture.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvCapture.setText(tvCapture.getText() + message); - tvCapture.setTextColor(R.color.ErrorMessage); + tvCapture.setTextColor(tvCapture.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -218,21 +218,13 @@ public void showField() { public boolean validateDisplay(String value,String condition) { if(condition.equals("equals")) { - for (String capture : captureList) { - if (capture.equals(value)) { - return true; - } - } + for (String capture : captureList) if (capture.equals(value)) return true; return false; } return true; } - public boolean isHidden(){ - if(llCapture!=null) { - return !llCapture.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llCapture != null && !llCapture.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/CaptureContentAdapter.java b/formbuilder-android/src/com/vertis/formbuilder/CaptureContentAdapter.java index cd35510..5254e27 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/CaptureContentAdapter.java +++ b/formbuilder-android/src/com/vertis/formbuilder/CaptureContentAdapter.java @@ -9,7 +9,6 @@ import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; -import android.widget.ImageView; import android.widget.TextView; public class CaptureContentAdapter extends ArrayAdapter { @@ -21,7 +20,6 @@ public int getCount() { private ArrayList capturedObject; private Context context; - private Button cross; private Capture c; public CaptureContentAdapter(Context context, int resource, ArrayList objects, Capture c) { @@ -39,7 +37,7 @@ public View getView(final int position, View convertView, ViewGroup parent) { } TextView tv = (TextView)return_view.findViewById(R.id.tvCapturedFile); tv.setText(this.capturedObject.get(position)); - cross= (Button)return_view.findViewById(R.id.crossImage); + Button cross= (Button)return_view.findViewById(R.id.crossImage); cross.setTag(position); cross.setOnClickListener(new OnClickListener (){ @Override diff --git a/formbuilder-android/src/com/vertis/formbuilder/Checkbox.java b/formbuilder-android/src/com/vertis/formbuilder/Checkbox.java index dfb465a..3fee7d7 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Checkbox.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Checkbox.java @@ -15,26 +15,27 @@ import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.CheckBox; -import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + public class Checkbox implements IField { private FieldConfig config; // Views LinearLayout llCheckBox; TextView tvCheckBox; - EditText otherTextBox; - ArrayList checkedValues= new ArrayList(); - ArrayList cbValues = new ArrayList(); + ArrayList checkedValues= new ArrayList<>(); + ArrayList cbValues = new ArrayList<>(); //Values @Expose String cid; @Expose String optionSelected; - int checked = 0; @Expose String other; @@ -44,12 +45,12 @@ public Checkbox(FieldConfig fcg){ @Override public void createForm(Activity context) { - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + LayoutInflater inflater = context.getLayoutInflater(); llCheckBox=(LinearLayout) inflater.inflate(R.layout.checkbox,null); tvCheckBox = (TextView) llCheckBox.findViewById(R.id.tvCheckbox); tvCheckBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f); tvCheckBox.setTypeface(new FormBuilderUtil().getFontFromRes(context)); - int i = 0; + int i ; if(cbValues != null) cbValues.clear(); for (i = 0; i < this.config.getField_options().getOptions().size(); i++) { @@ -118,29 +119,37 @@ public boolean validate() { private void noErrorMessage() { if(tvCheckBox==null)return; tvCheckBox.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvCheckBox.setTextColor(R.color.TextViewNormal); + tvCheckBox.setTextColor(tvCheckBox.getContext().getResources().getColor(R.color.TextViewNormal)); } @SuppressLint("ResourceAsColor") private void errorMessage(String message) { if(tvCheckBox==null)return; tvCheckBox.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); -// tvCheckBox.setText(tvCheckBox.getText() + " " + message); - tvCheckBox.setTextColor(R.color.ErrorMessage); + tvCheckBox.setText(tvCheckBox.getText() + " " + message); + tvCheckBox.setTextColor(tvCheckBox.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override public void setValues() { - this.cid=config.getCid(); - if(llCheckBox!=null){ - checkedValues.clear(); - for (CheckBox tempCheck : cbValues) { - if(tempCheck.isChecked()){ - checkedValues.add(tempCheck.getText().toString()); + try { + this.cid=config.getCid(); + JSONArray jsonArray = new JSONArray(); + if(llCheckBox!=null){ + checkedValues.clear(); + for (CheckBox tempCheck : cbValues) { + if (tempCheck.isChecked()) + checkedValues.add(tempCheck.getText().toString()); + JSONObject jsonObject = new JSONObject(); + jsonObject.put(tempCheck.getText().toString(), tempCheck.isChecked()); + jsonArray.put(jsonObject); } } + validate(); + optionSelected = jsonArray.toString(); + } catch (JSONException e) { + e.printStackTrace(); } - validate(); } @Override @@ -172,20 +181,14 @@ public void showField() { @Override public boolean validateDisplay(String value,String condition) { if(condition.equals("equals")){ - for (String checkedValue : checkedValues) { - if(checkedValue.toLowerCase().equals(value.toLowerCase())){ - } - } + for (String checkedValue : checkedValues) + if (checkedValue.toLowerCase().equals(value.toLowerCase())) return true; return false; } return true; } - public boolean isHidden(){ - if(llCheckBox!=null) { - return !llCheckBox.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llCheckBox != null && !llCheckBox.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Contact.java b/formbuilder-android/src/com/vertis/formbuilder/Contact.java index 0c2bf4e..b1df131 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Contact.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Contact.java @@ -3,7 +3,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.vertis.formbuilder.Listeners.TextChangeListener; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Typeface; @@ -44,17 +44,14 @@ public Contact(FieldConfig fcg){ @Override public void createForm(Activity context) { font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + LayoutInflater inflater = context.getLayoutInflater(); llContact=(LinearLayout) inflater.inflate(R.layout.contact,null); tvContact = (TextView) llContact.findViewById(R.id.textViewContact); etContact = (EditText) llContact.findViewById(R.id.editTextContact); setTextTypefaseAndTextSize(etContact, 12.5f); setTextTypefaseAndTextSize(tvContact, 14); - tvContact.setTextColor(R.color.TextViewNormal); - - etContact.addTextChangedListener(new TextChangeListener(config)); - - defineViewSettings(context); + etContact.addTextChangedListener(new CustomTextChangeListener(config)); + defineViewSettings(); setViewValues(); mapView(); setValues(); @@ -70,7 +67,7 @@ public void setTextTypefaseAndTextSize(TextView view, float size) { private void noErrorMessage() { if(tvContact==null)return; tvContact.setText(this.config.getLabel() + (this.config.getRequired() ? "*" : "")); - tvContact.setTextColor(R.color.TextViewNormal); + tvContact.setTextColor(tvContact.getContext().getResources().getColor(R.color.TextViewNormal)); } @SuppressLint("ResourceAsColor") @@ -78,7 +75,7 @@ private void errorMessage(String message) { if(tvContact==null)return; tvContact.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvContact.setText(tvContact.getText() + message); - tvContact.setTextColor(R.color.ErrorMessage); + tvContact.setTextColor(tvContact.getContext().getResources().getColor(R.color.ErrorMessage)); } private void mapView() { @@ -92,7 +89,7 @@ private void setViewValues() { tvContact.setTextColor(-1); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { etContact.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { @@ -169,20 +166,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(contactNo.equals(value) || contactNo.equals("")){ - return true; - } - return false; - } - return false; + return condition.equals("equals") && (contactNo.equals(value) || contactNo.equals("")); } - public boolean isHidden(){ - if(llContact!=null) { - return !llContact.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llContact != null && !llContact.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/CountriesArrayAdapter.java b/formbuilder-android/src/com/vertis/formbuilder/CountriesArrayAdapter.java index 4df33ea..3b79da1 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/CountriesArrayAdapter.java +++ b/formbuilder-android/src/com/vertis/formbuilder/CountriesArrayAdapter.java @@ -24,7 +24,7 @@ public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInf = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = layoutInf.inflate(R.layout.spinner_textview, null); - } + } TextView tv = (TextView)v.findViewById(android.R.id.text1); tv.setText(this.CountryList.get(position).value); return v; diff --git a/formbuilder-android/src/com/vertis/formbuilder/DateTimePicker.java b/formbuilder-android/src/com/vertis/formbuilder/DateTimePicker.java index 4b01e6a..bcdaa39 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/DateTimePicker.java +++ b/formbuilder-android/src/com/vertis/formbuilder/DateTimePicker.java @@ -170,21 +170,26 @@ public String returnDateTime(String format) { } public void controlShow(String type) { - if (type.equals("birth_date")|| type.equals("date")) { - viewSwitcher.removeAllViews(); - viewSwitcher.addView(datePickerControl, 0); - setButtonsVisibility(GONE); - } else if (type.equals("time")) { - viewSwitcher.removeAllViews(); - viewSwitcher.addView(timePickerControl, 0); - setButtonsVisibility(GONE); - } else if (type.equals("endDateTimeDifference") - || type.equals("startDateTimeDifference") - || type.equals("date_time")) { - viewSwitcher.removeAllViews(); - viewSwitcher.addView(datePickerControl, 0); - viewSwitcher.addView(timePickerControl, 1); - setButtonsVisibility(VISIBLE); + switch (type) { + case "birth_date": + case "date": + viewSwitcher.removeAllViews(); + viewSwitcher.addView(datePickerControl, 0); + setButtonsVisibility(GONE); + break; + case "time": + viewSwitcher.removeAllViews(); + viewSwitcher.addView(timePickerControl, 0); + setButtonsVisibility(GONE); + break; + case "endDateTimeDifference": + case "startDateTimeDifference": + case "date_time": + viewSwitcher.removeAllViews(); + viewSwitcher.addView(datePickerControl, 0); + viewSwitcher.addView(timePickerControl, 1); + setButtonsVisibility(VISIBLE); + break; } } diff --git a/formbuilder-android/src/com/vertis/formbuilder/DisplayDateTime.java b/formbuilder-android/src/com/vertis/formbuilder/DisplayDateTime.java index 346f580..6af80f2 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/DisplayDateTime.java +++ b/formbuilder-android/src/com/vertis/formbuilder/DisplayDateTime.java @@ -1,13 +1,7 @@ package com.vertis.formbuilder; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -15,8 +9,6 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.app.Dialog; -import android.content.Context; -import android.content.res.Resources.NotFoundException; import android.graphics.Typeface; import android.util.TypedValue; import android.view.LayoutInflater; @@ -29,8 +21,6 @@ import android.widget.RelativeLayout; import android.widget.TextView; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; import com.vertis.formbuilder.parser.FieldConfig; import com.vertis.formbuilder.util.FormBuilderUtil; @@ -53,7 +43,6 @@ public class DisplayDateTime implements IField { @Expose String dateTimeToRetainValue=""; private Activity context; - private Typeface font; public DisplayDateTime(FieldConfig fcg){ this.config=fcg; @@ -63,13 +52,12 @@ public DisplayDateTime(FieldConfig fcg){ @Override public void createForm(Activity context) { this.context = context; - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); llDisplayDateTime=(LinearLayout) inflater.inflate(R.layout.display_date_time, null); showTextDateTime = (TextView) llDisplayDateTime.findViewById(R.id.showTextDateTime); tvDateTime=(TextView) llDisplayDateTime.findViewById(R.id.tvErrorMessage); datetime = config.getField_options().getExistingFieldValue(); - tvDateTime.setTextColor(R.color.TextViewNormal); showTextDateTime.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) 12.5); tvDateTime.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); tvDateTime.setTypeface(font); @@ -92,8 +80,7 @@ public void onClick(View v) { datetime); } }); - - defineViewSettings(context); + defineViewSettings(); setViewValues(); mapView(); setValues(); @@ -109,7 +96,7 @@ private void mapView() { private void noErrorMessage() { if(tvDateTime==null)return; tvDateTime.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvDateTime.setTextColor(R.color.TextViewNormal); + tvDateTime.setTextColor(tvDateTime.getContext().getResources().getColor(R.color.TextViewNormal)); } private void setViewValues() { @@ -118,7 +105,7 @@ private void setViewValues() { tvDateTime.setTextColor(-1); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { showTextDateTime.setOnFocusChangeListener( new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { @@ -152,7 +139,7 @@ private void errorMessage(String message) { if(tvDateTime==null)return; tvDateTime.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvDateTime.setText(tvDateTime.getText() + message); - tvDateTime.setTextColor(R.color.ErrorMessage); + tvDateTime.setTextColor(tvDateTime.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -273,27 +260,27 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(datetime.equals(value) || datetime.equals("")){ - return true; - } - } else if(condition.equals("is greater than")){ - if(Integer.parseInt(datetime)>Integer.parseInt(value) || datetime.equals("")){ - return true; - } - } else if(condition.equals("is less than")){ - if(Integer.parseInt(datetime) Integer.parseInt(value) || datetime.equals("")) { + return true; + } + break; + case "is less than": + if (Integer.parseInt(datetime) < Integer.parseInt(value) || datetime.equals("")) { + return true; + } + break; } return false; } - public boolean isHidden(){ - if(llDisplayDateTime!=null) { - return !llDisplayDateTime.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llDisplayDateTime != null && !llDisplayDateTime.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Dropdown.java b/formbuilder-android/src/com/vertis/formbuilder/Dropdown.java index f1d4216..6b3a23a 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Dropdown.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Dropdown.java @@ -33,7 +33,6 @@ public class Dropdown implements IField{ @Expose String drop=""; int ddPosition=0; - private Typeface font; public Dropdown(FieldConfig fcg){ this.config=fcg; @@ -41,8 +40,8 @@ public Dropdown(FieldConfig fcg){ @Override public void createForm(Activity context) { - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); llDropdown=(LinearLayout) inflater.inflate(R.layout.drop_down_xml, null); tvDropdown = (TextView) llDropdown.findViewById(R.id.textViewDropdown); sDropdown =(Spinner) llDropdown.findViewById(R.id.spinnerDropdown); @@ -60,7 +59,7 @@ public void createForm(Activity context) { private void noErrorMessage() { if(tvDropdown==null)return; tvDropdown.setText(this.config.getLabel() + (this.config.getRequired()?"*":"")); - tvDropdown.setTextColor(android.R.color.black); + tvDropdown.setTextColor(tvDropdown.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -80,13 +79,13 @@ private void defineViewSettings(Activity context) { } private ArrayAdapter getAdapter(Activity context) { - return new CountriesArrayAdapter(context, getContentList(context)); + return new CountriesArrayAdapter(context, getContentList()); } - private ArrayList getContentList(Activity context) { + private ArrayList getContentList() { int i=0; - ArrayList contents=new ArrayList(); - ArrayList new_contents=new ArrayList(); + ArrayList contents=new ArrayList<>(); + ArrayList new_contents=new ArrayList<>(); for (int j = 0; j < this.config.getField_options().getOptions().size(); j++) { new_contents.add(this.config.getField_options().getOptions().get(j).getLabel()); } @@ -103,12 +102,12 @@ public ViewGroup getView() { @Override public boolean validate() { - boolean valid=false; + boolean valid ; if(config.getRequired()&&drop.equals("")) { - valid=false; + valid = false; errorMessage(" Pick one!"); } else{ - valid=true; + valid = true; noErrorMessage(); } return valid; @@ -118,7 +117,7 @@ private void errorMessage(String message) { if(tvDropdown==null)return; tvDropdown.setText((this.config.getRequired()?"*":"")); tvDropdown.setText(tvDropdown.getText() + message); - tvDropdown.setTextColor(-65536); + tvDropdown.setTextColor(tvDropdown.getContext().getResources().getColor(R.color.Red)); } @Override @@ -158,20 +157,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(drop.toLowerCase().equals(value.toLowerCase()) || drop.equals("")){ - return true; - } - return false; - } - return true; + return !condition.equals("equals") || drop.toLowerCase().equals(value.toLowerCase()) || drop.equals(""); } - public boolean isHidden(){ - if(llDropdown!=null) { - return !llDropdown.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llDropdown != null && !llDropdown.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Email.java b/formbuilder-android/src/com/vertis/formbuilder/Email.java index 68a9ffb..e1c2701 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Email.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Email.java @@ -1,10 +1,9 @@ package com.vertis.formbuilder; -import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.vertis.formbuilder.Listeners.TextChangeListener; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Typeface; @@ -29,14 +28,12 @@ public class Email implements IField{ LinearLayout em; TextView emailTextBox; EditText emailEditBox; - ArrayList Conditions; //Values @Expose String cid; @Expose String emailid=""; - private Typeface font; public Email(FieldConfig fcg){ this.config=fcg; @@ -45,8 +42,8 @@ public Email(FieldConfig fcg){ @SuppressLint("ResourceAsColor") @Override public void createForm(Activity context) { - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); em=(LinearLayout) inflater.inflate(R.layout.email,null); emailTextBox = (TextView) em.findViewById(R.id.textViewEmail); emailEditBox = (EditText) em.findViewById(R.id.editTextEmail); @@ -54,11 +51,8 @@ public void createForm(Activity context) { emailTextBox.setTypeface(font); emailEditBox.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float) 12.5); emailTextBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); - emailTextBox.setTextColor(R.color.TextViewNormal); - - emailEditBox.addTextChangedListener(new TextChangeListener(config)); - - defineViewSettings(context); + emailEditBox.addTextChangedListener(new CustomTextChangeListener(config)); + defineViewSettings(); setViewValues(); mapView(); setValues(); @@ -69,7 +63,7 @@ public void createForm(Activity context) { private void noErrorMessage() { if(emailTextBox==null)return; emailTextBox.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - emailTextBox.setTextColor(R.color.TextViewNormal); + emailTextBox.setTextColor(emailTextBox.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -83,7 +77,7 @@ private void setViewValues() { emailTextBox.setTextColor(-1); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { emailEditBox.setOnFocusChangeListener( new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { @@ -128,7 +122,7 @@ private void errorMessage(String message) { if(emailTextBox==null)return; emailTextBox.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); emailTextBox.setText(emailTextBox.getText() + message); - emailTextBox.setTextColor(R.color.ErrorMessage); + emailTextBox.setTextColor(emailTextBox.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -168,20 +162,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(emailid.equals(value) || emailid.equals("")){ - return true; - } - return false; - } - return true; + return !condition.equals("equals") || emailid.equals(value) || emailid.equals(""); } - public boolean isHidden(){ - if(em!=null) { - return !em.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return em != null && !em.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/FieldRegistry.java b/formbuilder-android/src/com/vertis/formbuilder/FieldRegistry.java index 6ab9685..15ac122 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/FieldRegistry.java +++ b/formbuilder-android/src/com/vertis/formbuilder/FieldRegistry.java @@ -2,12 +2,10 @@ import java.util.HashMap; -import android.util.Config; - public class FieldRegistry { @SuppressWarnings("rawtypes") - static HashMap fields=new HashMap(); + static HashMap fields=new HashMap<>(); static{ fields.put("fullnamexml", FullNameXml.class); @@ -35,4 +33,4 @@ public class FieldRegistry { public static Class getField(String typeName){ return fields.get(typeName); } -} +} \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/FormBuilder.java b/formbuilder-android/src/com/vertis/formbuilder/FormBuilder.java index c7632dc..b13f2da 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/FormBuilder.java +++ b/formbuilder-android/src/com/vertis/formbuilder/FormBuilder.java @@ -29,18 +29,18 @@ public class FormBuilder { * (section id mapped to all views in that section ) * currentSection stores section id of the section currently displayed */ - public static TreeMap> fields = new TreeMap>(); + public static TreeMap> fields = new TreeMap<>(); /** * Form is the linearLayout containing various section breaks. * These section breaks will contain various fields. * the fields context will be taken from the constructor */ - public static TreeMap sectionsTreeMap= new TreeMap(); + public static TreeMap sectionsTreeMap= new TreeMap<>(); LinearLayout previousNextContainer; Button previousButton; Button nextButton; public FormBuilder() { - fields = new TreeMap>(); + fields = new TreeMap<>(); } public void setup(String json , LinearLayout form, Activity context) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ @@ -85,7 +85,7 @@ private void checkMoreThanOneSections(LinearLayout form) { form.addView(this.previousNextContainer); } else{ //No section break=>no previous/next buttons - View v = this.sectionsTreeMap.get(0).getView(); + View v = sectionsTreeMap.get(0).getView(); //render generates the required views(from json) and adds the required views to linearlayout passed to the functions this.render(0); form.addView(v); @@ -94,7 +94,7 @@ private void checkMoreThanOneSections(LinearLayout form) { private void loopOverSectionTreeMap(LinearLayout form) { for (int i = 0; i < sectionsTreeMap.size() ; i++) { - View v = this.sectionsTreeMap.get(i).getView(); + View v = sectionsTreeMap.get(i).getView(); v.setVisibility(View.GONE); this.render(i); form.addView(v); @@ -127,7 +127,7 @@ private void buttonSpecifications() { this.previousNextContainer.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams abc = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); abc.setMargins(0, 10, 0, 10); - this.previousNextContainer.setGravity(Gravity.RIGHT); + this.previousNextContainer.setGravity(Gravity.END); this.previousNextContainer.setLayoutParams(abc); this.nextButton=new Button(context); this.nextButton.setText("Next"); @@ -165,14 +165,6 @@ public void render(int sectionId){ } } - //delete all views currently displayed after saving their data(done by clearViews) - public void clear(int sectionId){ - for(IField field : fields.get(sectionId)){ - field.clearViews(); - } - //form_section.removeAllViews(); - } - //Submit works only if you are in the last section and all fields are valid //returns output json which is {values:[array of json objects from field.toJson ]} public String submit(){ @@ -185,7 +177,7 @@ public String submit(){ return "Not ready to submit!!!"; } ResultJson rj = new ResultJson(); - for (ArrayList fieldList: this.fields.values()) { + for (ArrayList fieldList: fields.values()) { for(IField field:fieldList){ field.setValues(); rj.addValue(field); diff --git a/formbuilder-android/src/com/vertis/formbuilder/FullNameXml.java b/formbuilder-android/src/com/vertis/formbuilder/FullNameXml.java index 50bd579..1d4530c 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/FullNameXml.java +++ b/formbuilder-android/src/com/vertis/formbuilder/FullNameXml.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import com.google.gson.annotations.Expose; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import com.vertis.formbuilder.parser.FieldConfig; import com.vertis.formbuilder.util.FormBuilderUtil; @@ -50,7 +51,6 @@ public class FullNameXml implements IField { String lastName=""; @Expose String fullName=""; - private Typeface font; //constructor to populate config public FullNameXml(FieldConfig fcg){ @@ -63,8 +63,8 @@ public FullNameXml(FieldConfig fcg){ //if focus lost, setValues is called //if focus gained, noErrorMessage is called public void createForm(Activity context) { - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); subForm=(LinearLayout) inflater.inflate(R.layout.fullname,null); headingText = (TextView) subForm.findViewById(R.id.textView1); prefixBox = (Spinner) subForm.findViewById(R.id.spinner1); @@ -76,6 +76,7 @@ public void createForm(Activity context) { firstNameTextBox.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float) 14); lastNameTextBox.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float) 14); defineViewSettings(context); + addTextChangedListeners(); setViewValues(); mapView(); setValues(); @@ -94,7 +95,7 @@ private ArrayAdapter getAdapter(Context context) { private ArrayList getPrefixList(Context context) { int i=0; String[] prefixes = context.getResources().getStringArray(R.array.prefixArray); - ArrayList prefixArr=new ArrayList(); + ArrayList prefixArr= new ArrayList<>(); for (String string : prefixes) prefixArr.add(new SelectElement(i, string, string)); return prefixArr; @@ -183,14 +184,14 @@ public void errorMessage(String message){ if(headingText==null)return; headingText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); headingText.setText(headingText.getText() + " " + message); - headingText.setTextColor(-65536); + headingText.setTextColor(headingText.getContext().getResources().getColor(R.color.Red)); } @SuppressLint("ResourceAsColor") public void noErrorMessage(){ if(headingText==null)return; headingText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - headingText.setTextColor(R.color.TextViewNormal); + headingText.setTextColor(headingText.getContext().getResources().getColor(R.color.TextViewNormal)); } public String getCIDValue() { @@ -213,20 +214,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(fullName.toLowerCase().contains(value.toLowerCase()) || fullName.trim().equals("")){ - return true; - } - return false; - } - return true; + return !condition.equals("equals") || fullName.toLowerCase().contains(value.toLowerCase()) || fullName.trim().equals(""); } - public boolean isHidden(){ - if(subForm!=null) { - return !subForm.isShown(); - } else { - return false; - } - } -}; \ No newline at end of file + public boolean isHidden() { + return subForm != null && !subForm.isShown(); + } +} \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/IField.java b/formbuilder-android/src/com/vertis/formbuilder/IField.java index 80ea2f2..d1c41c2 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/IField.java +++ b/formbuilder-android/src/com/vertis/formbuilder/IField.java @@ -31,13 +31,13 @@ public interface IField { *Convention: String id associated with layout is same as id in default values. Id of other views are id_of_parent + "_i", where i is a number * */ - public abstract void createForm(Activity context); + void createForm(Activity context); /** * getView * return layout which encloses all the views */ - public abstract ViewGroup getView(); + ViewGroup getView(); /** @@ -45,25 +45,25 @@ public interface IField { Check for validations. Return true if entries are valid. *You can also output a message like "This field required" in this function. */ - public abstract boolean validate(); + boolean validate(); /** * setValues * Use data from Views to populate Values to return. */ - public abstract void setValues(); + void setValues(); - public abstract void clearViews(); + void clearViews(); - public abstract String getCIDValue(); + String getCIDValue(); - public abstract void hideField(); + void hideField(); - public abstract void showField(); + void showField(); - public abstract boolean validateDisplay(String value,String condition); + boolean validateDisplay(String value,String condition); - public boolean isHidden(); + boolean isHidden(); } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Listeners/CheckConditions.java b/formbuilder-android/src/com/vertis/formbuilder/Listeners/CheckConditions.java index c663d64..7f18670 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Listeners/CheckConditions.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Listeners/CheckConditions.java @@ -25,7 +25,7 @@ public void loopOverCheckCondition(){ } public void checkCondition(String source, String condition, String value, String action, String target, Boolean isSource) { - String actualValue=""; + String actualValue =""; IField sourceField = getFieldFromCID(source); IField targetField = getFieldFromCID(target); diff --git a/formbuilder-android/src/com/vertis/formbuilder/CustomTextChangeListener.java b/formbuilder-android/src/com/vertis/formbuilder/Listeners/CustomTextChangeListener.java similarity index 91% rename from formbuilder-android/src/com/vertis/formbuilder/CustomTextChangeListener.java rename to formbuilder-android/src/com/vertis/formbuilder/Listeners/CustomTextChangeListener.java index 11239ef..41a552d 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/CustomTextChangeListener.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Listeners/CustomTextChangeListener.java @@ -1,9 +1,11 @@ -package com.vertis.formbuilder; +package com.vertis.formbuilder.Listeners; import java.util.ArrayList; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.vertis.formbuilder.FormBuilder; +import com.vertis.formbuilder.IField; import com.vertis.formbuilder.parser.FieldConfig; import android.text.Editable; @@ -12,7 +14,7 @@ public class CustomTextChangeListener implements TextWatcher { private FieldConfig config; - CustomTextChangeListener(FieldConfig config){ + public CustomTextChangeListener(FieldConfig config){ this.config = config; } /* Other methods protected by singleton-ness */ diff --git a/formbuilder-android/src/com/vertis/formbuilder/Listeners/TextChangeListener.java b/formbuilder-android/src/com/vertis/formbuilder/Listeners/TextChangeListener.java deleted file mode 100644 index f04a6e1..0000000 --- a/formbuilder-android/src/com/vertis/formbuilder/Listeners/TextChangeListener.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.vertis.formbuilder.Listeners; - -import com.vertis.formbuilder.parser.FieldConfig; - -import android.text.Editable; -import android.text.TextWatcher; - -public class TextChangeListener implements TextWatcher { - - private FieldConfig config; - - public TextChangeListener(FieldConfig config){ - this.config = config; - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, - int after) { } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - CheckConditions checkConditionsObject=new CheckConditions(config); - checkConditionsObject.loopOverCheckCondition(); - } - - @Override - public void afterTextChanged(Editable s) { } -} \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/MultiLineEditText.java b/formbuilder-android/src/com/vertis/formbuilder/MultiLineEditText.java index 93e5734..84c62b1 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/MultiLineEditText.java +++ b/formbuilder-android/src/com/vertis/formbuilder/MultiLineEditText.java @@ -1,6 +1,6 @@ package com.vertis.formbuilder; -import com.vertis.formbuilder.Listeners.TextChangeListener; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Typeface; @@ -30,7 +30,6 @@ public class MultiLineEditText implements IField{ String cid; @Expose String mtext=""; - private Typeface font; public MultiLineEditText(FieldConfig fcg){ this.config=fcg; @@ -39,8 +38,8 @@ public MultiLineEditText(FieldConfig fcg){ @SuppressLint("ResourceAsColor") @Override public void createForm(Activity context) { - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); llEditText=(LinearLayout) inflater.inflate(R.layout.multiline_edit_text,null); tvEditText = (TextView) llEditText.findViewById(R.id.multilineTextView); etEditText = (EditText) llEditText.findViewById(R.id.multilineEditText); @@ -48,11 +47,8 @@ public void createForm(Activity context) { tvEditText.setTypeface(font); etEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float) 12.5); tvEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); - tvEditText.setTextColor(R.color.TextViewNormal); - - etEditText.addTextChangedListener(new TextChangeListener(config)); - - defineViewSettings(context); + etEditText.addTextChangedListener(new CustomTextChangeListener(config)); + defineViewSettings(); setViewValues(); mapView(); setValues(); @@ -63,7 +59,7 @@ public void createForm(Activity context) { private void noErrorMessage() { if(tvEditText==null)return; tvEditText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvEditText.setTextColor(R.color.TextViewNormal); + tvEditText.setTextColor(tvEditText.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -77,7 +73,7 @@ private void setViewValues() { tvEditText.setTextColor(-1); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { etEditText.setOnFocusChangeListener( new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { @@ -113,7 +109,7 @@ private void errorMessage(String message) { if(tvEditText==null)return; tvEditText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvEditText.setText(tvEditText.getText() + message); - tvEditText.setTextColor(R.color.ErrorMessage); + tvEditText.setTextColor(tvEditText.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -153,20 +149,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(mtext.toLowerCase().equals(value.toLowerCase()) || mtext.equals("")){ - return true; - } - return false; - } - return true; + return !condition.equals("equals") || mtext.toLowerCase().equals(value.toLowerCase()) || mtext.equals(""); } - public boolean isHidden(){ - if(llEditText!=null) { - return !llEditText.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llEditText != null && !llEditText.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/NumberField.java b/formbuilder-android/src/com/vertis/formbuilder/NumberField.java index cce161b..32ba732 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/NumberField.java +++ b/formbuilder-android/src/com/vertis/formbuilder/NumberField.java @@ -13,6 +13,7 @@ import android.widget.TextView; import com.google.gson.annotations.Expose; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import com.vertis.formbuilder.parser.FieldConfig; import com.vertis.formbuilder.util.FormBuilderUtil; @@ -29,7 +30,6 @@ public class NumberField implements IField{ String cid; @Expose String number=""; - private Typeface font; public NumberField(FieldConfig fcg){ this.config=fcg; @@ -38,8 +38,8 @@ public NumberField(FieldConfig fcg){ @SuppressLint("ResourceAsColor") @Override public void createForm(Activity context) { - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); llNumber=(LinearLayout) inflater.inflate(R.layout.number_edit_text,null); tvNumber = (TextView) llNumber.findViewById(R.id.numberTextView); etNumber = (EditText) llNumber.findViewById(R.id.numberEditText); @@ -47,10 +47,8 @@ public void createForm(Activity context) { tvNumber.setTypeface(font); etNumber.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float) 12.5); tvNumber.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); - tvNumber.setTextColor(R.color.TextViewNormal); etNumber.addTextChangedListener(new CustomTextChangeListener(config)); - - defineViewSettings(context); + defineViewSettings(); setViewValues(); mapView(); setValues(); @@ -61,7 +59,7 @@ public void createForm(Activity context) { private void noErrorMessage() { if(tvNumber==null)return; tvNumber.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvNumber.setTextColor(R.color.TextViewNormal); + tvNumber.setTextColor(tvNumber.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -75,7 +73,7 @@ private void setViewValues() { tvNumber.setTextColor(-1); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { etNumber.setOnFocusChangeListener( new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { @@ -111,7 +109,7 @@ private void errorMessage(String message) { if(tvNumber==null)return; tvNumber.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvNumber.setText(tvNumber.getText() + message); - tvNumber.setTextColor(R.color.ErrorMessage); + tvNumber.setTextColor(tvNumber.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -154,27 +152,27 @@ public void showField() { @Override public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(number.equals(value) || number.equals("")){ - return true; - } - } else if(condition.equals("is greater than")){ - if(Integer.parseInt(number) > Integer.parseInt(value) || number.equals("")){ - return true; - } - } else if(condition.equals("is less than")){ - if(Integer.parseInt(number) < Integer.parseInt(value) || number.equals("")){ - return true; - } + switch (condition) { + case "equals": + if (number.equals(value) || number.equals("")) { + return true; + } + break; + case "is greater than": + if (Integer.parseInt(number) > Integer.parseInt(value) || number.equals("")) { + return true; + } + break; + case "is less than": + if (Integer.parseInt(number) < Integer.parseInt(value) || number.equals("")) { + return true; + } + break; } return false; } - public boolean isHidden(){ - if(llNumber!=null) { - return !llNumber.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llNumber != null && !llNumber.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Price.java b/formbuilder-android/src/com/vertis/formbuilder/Price.java index f8f17b9..b18e3e1 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Price.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Price.java @@ -1,6 +1,7 @@ package com.vertis.formbuilder; import com.google.gson.annotations.Expose; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import com.vertis.formbuilder.parser.FieldConfig; import com.vertis.formbuilder.util.FormBuilderUtil; @@ -20,7 +21,6 @@ public class Price implements IField { //config contains all data from json ie id,sid,label,field_options private FieldConfig config; - private Typeface font; public Price(FieldConfig fcg){ this.config=fcg; @@ -49,8 +49,8 @@ public Price(FieldConfig fcg){ @Override public void createForm(Activity context) { - font = new FormBuilderUtil().getFontFromRes(context); - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface font = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); llPrice=(LinearLayout) inflater.inflate(R.layout.price,null); tvPrice = (TextView) llPrice.findViewById(R.id.textViewPrice); etDollars = (EditText) llPrice.findViewById(R.id.dollars); @@ -64,7 +64,7 @@ public void createForm(Activity context) { addTextChangeListeners(); - defineViewSettings(context); + defineViewSettings(); setViewValues(); mapView(); setValues(); @@ -80,7 +80,7 @@ private void addTextChangeListeners() { private void noErrorMessage() { if(tvPrice==null)return; tvPrice.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvPrice.setTextColor(R.color.TextViewNormal); + tvPrice.setTextColor(tvPrice.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -96,7 +96,7 @@ private void setViewValues() { etCents.setText(cents); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { etDollars.setHint(dollarsHint); etDollars.setOnFocusChangeListener( new OnFocusChangeListener() { @Override @@ -146,7 +146,7 @@ private void errorMessage(String message) { if(tvPrice==null)return; tvPrice.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvPrice.setText(tvPrice.getText() + " " + message); - tvPrice.setTextColor(-65536); + tvPrice.setTextColor(tvPrice.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -189,27 +189,27 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(totalPrice.equals(value) || totalPrice.equals("")){ - return true; - } - } else if(condition.equals("is greater than")){ - if(Integer.parseInt(totalPrice)>Integer.parseInt(value) || totalPrice.equals("")){ - return true; - } - } else if(condition.equals("is less than")){ - if(Integer.parseInt(totalPrice) Integer.parseInt(value) || totalPrice.equals("")) { + return true; + } + break; + case "is less than": + if (Integer.parseInt(totalPrice) < Integer.parseInt(value) || totalPrice.equals("")) { + return true; + } + break; } return false; } - public boolean isHidden(){ - if(llPrice!=null) { - return !llPrice.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llPrice != null && !llPrice.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/Radio.java b/formbuilder-android/src/com/vertis/formbuilder/Radio.java index ea1c422..73cd036 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/Radio.java +++ b/formbuilder-android/src/com/vertis/formbuilder/Radio.java @@ -18,8 +18,7 @@ class Radio implements IField { private FieldConfig config; - // Default Values - boolean otherRequired = true; + String otherHeading = ""; // Views LinearLayout subForm; @@ -39,7 +38,7 @@ public Radio(FieldConfig fcg){ } public void createForm(Activity context) { - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + LayoutInflater inflater = context.getLayoutInflater(); subForm=(LinearLayout) inflater.inflate(R.layout.radio,null); headingText = (TextView) subForm.findViewById(R.id.tvRadio); otherHeading="other"; @@ -55,7 +54,7 @@ public void onClick(View v) { subForm.requestFocus(); } }); - int i = 0; + int i ; for (i = 0; i < this.config.getField_options().getOptions().size(); i++) { addButton(i, context); } @@ -121,14 +120,14 @@ public void errorMessage(String message){ if(headingText==null)return; headingText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); headingText.setText(headingText.getText() + " " + message); - headingText.setTextColor(R.color.ErrorMessage); + headingText.setTextColor(headingText.getContext().getResources().getColor(R.color.ErrorMessage)); } @SuppressLint("ResourceAsColor") public void noErrorMessage(){ if(headingText==null)return; headingText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - headingText.setTextColor(R.color.TextViewNormal); + headingText.setTextColor(headingText.getContext().getResources().getColor(R.color.TextViewNormal)); } @Override public void clearViews() { @@ -158,20 +157,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(optionSelected.toLowerCase().equals(value.toLowerCase()) || optionSelected.equals("")){ - return true; - } - return false; - } - return true; + return !condition.equals("equals") || optionSelected.toLowerCase().equals(value.toLowerCase()) || optionSelected.equals(""); } - public boolean isHidden(){ - if(subForm!=null) { - return !subForm.isShown(); - } else { - return false; - } - } -}; \ No newline at end of file + public boolean isHidden() { + return subForm != null && !subForm.isShown(); + } +} \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/ResultJson.java b/formbuilder-android/src/com/vertis/formbuilder/ResultJson.java index ebfd45f..75927ae 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/ResultJson.java +++ b/formbuilder-android/src/com/vertis/formbuilder/ResultJson.java @@ -6,7 +6,7 @@ public class ResultJson { @Expose - ArrayList values = new ArrayList(); + ArrayList values = new ArrayList<>(); public void addValue(Object value){ this.values.add(value); } diff --git a/formbuilder-android/src/com/vertis/formbuilder/SelectElement.java b/formbuilder-android/src/com/vertis/formbuilder/SelectElement.java index b82add3..9862f07 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/SelectElement.java +++ b/formbuilder-android/src/com/vertis/formbuilder/SelectElement.java @@ -1,5 +1,7 @@ package com.vertis.formbuilder; +import android.support.annotation.NonNull; + public class SelectElement implements Comparable { @Override public String toString() { @@ -34,9 +36,6 @@ public int compareTo(SelectElement another) { @Override public boolean equals(Object o) { SelectElement element = (SelectElement) o; - if (value.equals(element.value)) { - return true; - } - return false; + return value.equals(element.value); } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/SimpleEditText.java b/formbuilder-android/src/com/vertis/formbuilder/SimpleEditText.java index 9677a58..8d0b391 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/SimpleEditText.java +++ b/formbuilder-android/src/com/vertis/formbuilder/SimpleEditText.java @@ -1,16 +1,8 @@ package com.vertis.formbuilder; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; - -import com.vertis.formbuilder.Listeners.TextChangeListener; +import com.vertis.formbuilder.Listeners.CustomTextChangeListener; import android.annotation.SuppressLint; import android.app.Activity; -import android.content.Context; -import android.content.res.Resources.NotFoundException; import android.graphics.Typeface; import android.util.TypedValue; import android.view.LayoutInflater; @@ -23,6 +15,7 @@ import com.google.gson.annotations.Expose; import com.vertis.formbuilder.parser.FieldConfig; +import com.vertis.formbuilder.util.FormBuilderUtil; public class SimpleEditText implements IField{ @@ -45,58 +38,28 @@ public SimpleEditText(FieldConfig fcg){ @SuppressLint("ResourceAsColor") @Override public void createForm(Activity context) { - LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater(); + Typeface typeface = new FormBuilderUtil().getFontFromRes(context); + LayoutInflater inflater = context.getLayoutInflater(); llEditText=(LinearLayout) inflater.inflate(R.layout.simple_edit_text,null); tvEditText = (TextView) llEditText.findViewById(R.id.simpleTextView); etEditText = (EditText) llEditText.findViewById(R.id.simpleEditText); - etEditText.setTypeface(getFontFromRes(R.raw.roboto, context)); - tvEditText.setTypeface(getFontFromRes(R.raw.roboto, context)); + etEditText.setTypeface(typeface); + tvEditText.setTypeface(typeface); etEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float) 12.5); tvEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); - tvEditText.setTextColor(R.color.TextViewNormal); - - etEditText.addTextChangedListener(new TextChangeListener(config)); - - defineViewSettings(context); + etEditText.addTextChangedListener(new CustomTextChangeListener(config)); + defineViewSettings(); setViewValues(); mapView(); setValues(); noErrorMessage(); } - private Typeface getFontFromRes(int resource, Context context) - { - Typeface tf = null; - InputStream is = null; - try { - is = context.getResources().openRawResource(resource); - } - catch(NotFoundException e) { - } - String outPath = context.getCacheDir() + "/tmp" + System.currentTimeMillis()+".raw"; - try - { - byte[] buffer = new byte[is.available()]; - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath)); - int l = 0; - while((l = is.read(buffer)) > 0) - bos.write(buffer, 0, l); - bos.close(); - tf = Typeface.createFromFile(outPath); - new File(outPath).delete(); - } - catch (IOException e) - { - return null; - } - return tf; - } - @SuppressLint("ResourceAsColor") private void noErrorMessage() { if(tvEditText==null)return; tvEditText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); - tvEditText.setTextColor(R.color.TextViewNormal); + tvEditText.setTextColor(tvEditText.getContext().getResources().getColor(R.color.TextViewNormal)); } private void mapView() { @@ -110,7 +73,7 @@ private void setViewValues() { tvEditText.setTextColor(-1); } - private void defineViewSettings(Activity context) { + private void defineViewSettings() { etEditText.setOnFocusChangeListener( new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { @@ -146,7 +109,7 @@ private void errorMessage(String message) { if(tvEditText==null)return; tvEditText.setText(this.config.getLabel() + (this.config.getRequired()?"*":"") ); tvEditText.setText(tvEditText.getText() + message); - tvEditText.setTextColor(R.color.ErrorMessage); + tvEditText.setTextColor(tvEditText.getContext().getResources().getColor(R.color.ErrorMessage)); } @Override @@ -186,20 +149,10 @@ public void showField() { } public boolean validateDisplay(String value,String condition) { - if(condition.equals("equals")){ - if(text.toLowerCase().equals(value.toLowerCase()) || text.equals("")){ - return true; - } - return false; - } - return true; + return !condition.equals("equals") || text.toLowerCase().equals(value.toLowerCase()) || text.equals(""); } - public boolean isHidden(){ - if(llEditText!=null) { - return !llEditText.isShown(); - } else { - return false; - } - } + public boolean isHidden() { + return llEditText != null && !llEditText.isShown(); + } } \ No newline at end of file diff --git a/formbuilder-android/src/com/vertis/formbuilder/ViewLookup.java b/formbuilder-android/src/com/vertis/formbuilder/ViewLookup.java index 1ef6d27..ab139e6 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/ViewLookup.java +++ b/formbuilder-android/src/com/vertis/formbuilder/ViewLookup.java @@ -4,10 +4,10 @@ import android.view.View; public class ViewLookup { - static HashMap hashMap = new HashMap(); + static HashMap hashMap = new HashMap<>(); public static void mapField(String stringId,View field){ - int intId=View.generateViewId(); + int intId = View.generateViewId(); field.setId(intId); hashMap.put(stringId, intId); } diff --git a/formbuilder-android/src/com/vertis/formbuilder/parser/FieldConfig.java b/formbuilder-android/src/com/vertis/formbuilder/parser/FieldConfig.java index b9d3ad8..7906f3b 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/parser/FieldConfig.java +++ b/formbuilder-android/src/com/vertis/formbuilder/parser/FieldConfig.java @@ -1,7 +1,6 @@ package com.vertis.formbuilder.parser; import java.util.ArrayList; -import java.util.HashMap; public class FieldConfig { public String getLabel() { @@ -61,10 +60,10 @@ public FieldConfig(String cid, int section_id, String field_type, FieldOptions f String label; String field_type; Boolean required; - ArrayList options = new ArrayList(); + ArrayList options = new ArrayList<>(); FieldOptions field_options = new FieldOptions(); - ArrayList conditions= new ArrayList(); - //ArrayList> conditions = new ArrayList>(); + ArrayList conditions= new ArrayList<>(); + //ArrayList> conditions = new ArrayList<>(); String cid; int section_id=0; } diff --git a/formbuilder-android/src/com/vertis/formbuilder/parser/FieldOptions.java b/formbuilder-android/src/com/vertis/formbuilder/parser/FieldOptions.java index e331ca1..647e8dd 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/parser/FieldOptions.java +++ b/formbuilder-android/src/com/vertis/formbuilder/parser/FieldOptions.java @@ -1,7 +1,6 @@ package com.vertis.formbuilder.parser; import java.util.ArrayList; -import java.util.HashMap; public class FieldOptions { public int getSteps() { @@ -42,7 +41,7 @@ public FieldOptions() { this.maxDate = ""; this.existingFieldValue = ""; } - ArrayList options = new ArrayList(); + ArrayList options = new ArrayList<>(); int steps; String date_format, maxDate, existingFieldValue; diff --git a/formbuilder-android/src/com/vertis/formbuilder/parser/FormJson.java b/formbuilder-android/src/com/vertis/formbuilder/parser/FormJson.java index d0a71eb..88f6370 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/parser/FormJson.java +++ b/formbuilder-android/src/com/vertis/formbuilder/parser/FormJson.java @@ -4,7 +4,7 @@ public class FormJson { - ArrayList fields = new ArrayList(); + ArrayList fields = new ArrayList<>(); public ArrayList getFields() { return fields; diff --git a/formbuilder-android/src/com/vertis/formbuilder/util/FormBuilderUtil.java b/formbuilder-android/src/com/vertis/formbuilder/util/FormBuilderUtil.java index 51f7f3b..509a669 100644 --- a/formbuilder-android/src/com/vertis/formbuilder/util/FormBuilderUtil.java +++ b/formbuilder-android/src/com/vertis/formbuilder/util/FormBuilderUtil.java @@ -1,7 +1,6 @@ package com.vertis.formbuilder.util; import android.content.Context; -import android.content.res.Resources; import android.graphics.Typeface; import com.vertis.formbuilder.R; @@ -9,10 +8,10 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; /** + * Util Class helps user to set the Font for user * Created by manish on 20/05/15. */ public class FormBuilderUtil { @@ -20,21 +19,21 @@ public class FormBuilderUtil { public Typeface getFontFromRes( Context context) { Typeface tf = null; - InputStream is = null; + InputStream is ; try { is = context.getResources().openRawResource(R.raw.roboto); - } catch(Resources.NotFoundException e) { } - String outPath = context.getCacheDir() + "/tmp" + System.currentTimeMillis()+".raw"; - try { + String outPath = context.getCacheDir() + "/tmp" + System.currentTimeMillis() + ".raw"; byte[] buffer = new byte[is.available()]; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath)); - int l = 0; - while((l = is.read(buffer)) > 0) + int l ; + while ((l = is.read(buffer)) > 0) bos.write(buffer, 0, l); bos.close(); tf = Typeface.createFromFile(outPath); new File(outPath).delete(); - } catch (IOException e) { } + }catch (Exception e) { + e.printStackTrace(); + } return tf; } } diff --git a/formbuilderDemo/src/com/vertis/test/MainActivity.java b/formbuilderDemo/src/com/vertis/test/MainActivity.java index 07643d8..cd042de 100644 --- a/formbuilderDemo/src/com/vertis/test/MainActivity.java +++ b/formbuilderDemo/src/com/vertis/test/MainActivity.java @@ -11,15 +11,13 @@ import com.vertis.formbuilder.FormBuilder; public class MainActivity extends Activity { - private String result; FormBuilder formBuilder = new FormBuilder(); - private LinearLayout form; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - form=(LinearLayout)findViewById(R.id.Lay1); + LinearLayout form=(LinearLayout)findViewById(R.id.Lay1); //String jsonstr = "{\"fields\":[" + // "{\"label\":\"Full Name\", \"field_type\":\"fullnamexml\", \"required\":true, \"field_options\":{}, \"conditions\":[], \"cid\":\"c26\"}" + // ",{\"label\":\"Untitled\", \"field_type\":\"section_break\", \"required\":false, \"field_options\":{}, \"conditions\":[], \"cid\":\"c28\",\"section_id\":100}" + @@ -62,11 +60,11 @@ protected void onCreate(Bundle savedInstanceState) { } /** * link submit button to submit function of the formbuilder - * @param button + * @param button on which user has clicked */ public void submit(View button) { - this.result= formBuilder.submit(); - Toast.makeText(this, this.result, Toast.LENGTH_LONG).show(); + String result= formBuilder.submit(); + Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } public void save(View button) { diff --git a/local.properties b/local.properties index 2e4fc33..0f9c2bd 100644 --- a/local.properties +++ b/local.properties @@ -7,5 +7,5 @@ # Location of the SDK. This is only used by Gradle. # For customization when using a Version Control System, please read the # header note. -#Wed May 27 12:20:42 IST 2015 -sdk.dir=C\:\\New folder (3)\\android-sdk_r22.6.2-windows\\sdk\\android-sdk-windows +#Mon Jun 01 15:45:16 GMT+05:30 2015 +sdk.dir=/Users/manish/Manish/software/Android/android-sdks