Android Open Source - Resonos-Android-Framework Form Builder






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.resonos.apps.library.widget;
/*from w ww .  j a v  a 2s .c o m*/
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.WazaBe.HoloEverywhere.CheckBoxHolo;
import com.WazaBe.HoloEverywhere.EditTextHolo;

/**
 * Generic class represnting a form builder. This is meant to be simpler to
 *  use and more out of the box than building simple layouts and forms with
 *  xml, as so many more features are just plug and play. This class requires
 *  an implementation, and in this library, {@link ListFormBuilder} is provided.
 *  
 * @author Chris
 */
public abstract class FormBuilder {

  // context
  private final Context cx;
  
  // objects
  private View mMasterView = null;
  private View mCurSection = null;
  private FormElement mCurElement = null;
  protected Map<Enum<?>,View> mFormLinks = new HashMap<Enum<?>,View>(); 
  protected EditText mLastEdit;
  
  // vars
  private boolean inSection = false;
  protected Enum<?> mLastClickableAfterEdit;
  protected final Bundle inState;
  protected int count;
  
  /**
   * Create the form builder
   * @param cx : activity context
   * @param inState : the saved state, or null if none
   */
  public FormBuilder(Context cx, Bundle inState) {
    this.cx = cx;
    this.inState = inState;
  }
  
  /**
   * Basic form creation start process.
   */
  protected void create() {
    if (cx == null)
      throw new NullPointerException("context cannot be null!");
    count = 0;
    mLastEdit = null;
    mLastClickableAfterEdit = null;
    mMasterView = start();
  }

  /**
   * Override this to provide logic for starting the form
   *  creation process in your implementation
   * @return the View that holds your entire form
   */
  protected abstract View start();
  
  /**
   * Override this to provide logic for starting a new nameless section in your form
   * @return the View that holds this section, or null if you
   *  don't have individual views for each section
   */
  protected abstract View startSection();
  
  /**
   * Override this to provide logic for starting a new section in your form
   * @param s : the name of this section
   * @return the View that holds this section, or null if you
   *  don't have individual views for each section
   */
  protected abstract View startSection(CharSequence s);
  
  /**
   * Override this to provide logic for adding a new item in your form
   * @param fe : a {@link FormElement} object representing the item
   */
  protected abstract void addItem(FormElement fe);
  
  /**
   * Override this to provide logic for finishing a section in your form
   */
  protected abstract void endSection();
  
  /**
   * Override this to provide logic for finishing your entire form.
   *  This should probably call some kind of state-committing function.
   */
  protected abstract void end();
  
  /**
   * Override this to pass item clicks to your own implementations listener
   * @param tag : the enum represting the item
   */
  protected abstract void onClick(Enum<?> tag);
  
  /**
   * Override this to provide logic for clearing your form of all items
   */
  public abstract void clear();
  
  /**
   * Save the state of all form elements
   * @return a Bundle filled with state values
   */
  public Bundle onSaveInstanceState() {
    Bundle b = new Bundle();
    for (Entry<Enum<?>,View> e : mFormLinks.entrySet()) {
      View v = e.getValue();
      Enum<?> tag = e.getKey();
      if (v instanceof EditText)
        b.putString("edit_"+tag.getClass().getSimpleName()+"_"+tag.name(), ((EditText)v).getText().toString());
      else if (v instanceof CheckBox)
        b.putBoolean("chk_"+tag.getClass().getSimpleName()+"_"+tag.name(), ((CheckBox)v).isChecked());
      else if (v instanceof Spinner)
        b.putInt("spinner_"+tag.getClass().getSimpleName()+"_"+tag.name(), ((Spinner)v).getSelectedItemPosition());
    }
    return b;
  }
  
  /**
   * Supply an edit with the ability to submit the form
   * @param e : the edittext
   * @param a : the tag of the submit item
   */
  private void linkEditAndClickable(final EditText e, final Enum<?> a) {
      e.setImeOptions(EditorInfo.IME_ACTION_DONE);
      e.setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
              if (actionId == EditorInfo.IME_ACTION_DONE) {
                try {
                      InputMethodManager imm = (InputMethodManager)cx.getSystemService(Context.INPUT_METHOD_SERVICE);
                      imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
                      onClick(a);
                } catch (Exception ex) {}
                  return true;
              }
              return false;
          }
      });
  }
  
  /**
   * Call this to finish building your form
   * @return the View that contains your built form
   */
  public View finish() {
    if (mCurElement != null)
      buildItem(mCurElement);
    if (inSection)
      endSection();
    inSection = false;
    end();
    if (mLastEdit != null && mLastClickableAfterEdit != null)
      linkEditAndClickable(mLastEdit, mLastClickableAfterEdit);
    mLastEdit = null;
    mLastClickableAfterEdit = null;
    count = 0;
    return mMasterView;
  }
  
  /**
   * Get the context used in this form builder
   */
  protected Context getContext() {
    return cx;
  }
  
  /**
   * Get the master View this form is contained in.
   *  Only valid after form creation has started.
   */
  protected View getMaster() {
    return mMasterView;
  }
  
  /**
   * Get the current section's container view.
   * This may be null if your implementation doesn't use section views,
   *  or if it is not during the form building process.
   */
  protected View getCurSection() {
    return mCurSection;
  }
  
  /**
   * Start a new named section.
   * @param s : a string resource
   * @return the same FormBuilder, for chaining
   */
  public FormBuilder newSection(int s) {
    return newSection(cx.getString(s));
  }

  /**
   * Start a new named section.
   * @param s : a charsequence
   * @return the same FormBuilder, for chaining
   */
  public FormBuilder newSection(CharSequence s) {
    if (mCurElement != null)
      buildItem(mCurElement);
    if (inSection)
      endSection();
    inSection = false;
    mCurSection = startSection(s);
    mCurElement = null;
    return this;
  }

  /**
   * Start a new form item.
   * @return the new item's {@link FormElement} object
   */
  public FormElement newItem() {
    if (!inSection) {
      mCurSection = startSection();
      inSection = true;
      mCurElement = null;
    }
    if (mCurElement != null) {
      FormElement fe = mCurElement;
      buildItem(fe);
      if (!fe.getData().mSuppressDivider) {
        mCurElement = new FormElement(this);
        mCurElement.divider();
        buildItem(mCurElement);
      }
    }
    mCurElement = new FormElement(this);
    return mCurElement;
  }
  
  /**
   * Build a form item once it is completed being initialized.
   * @param fe : the item's data
   */
  protected void buildItem(FormElement fe) {
    if (!fe.mBuilt) {
      addItem(fe);
      fe.mBuilt = true;
    }
    mCurElement = null;
  }
  
  /**
   * Get the form element view (edit, checkbox, etc) associated
   *  with a certain tag
   * @param key : the enum tag
   * @return : the form element's View, if found, or else null
   */
  public View getFormElement(Enum<?> key) {
    return mFormLinks.get(key);
  }
  
  /**
   * Get the container view associated with a certain form item
   * @param key : the item tag
   * @return the container view, if found, or else null
   */
  public abstract View getListItem(Enum<?> key);
  
  /**
   * Get the currently selected choice of a form spinner
   * @param key : the spinner's tag
   * @param defVal : the default value
   * @return the currently selected spinner value, or defVal if not found
   */
  public int getFormSpinnerChoice(Enum<?> key, int defVal) {
    View v = mFormLinks.get(key);
    if (v != null) {
      if (v instanceof Spinner) {
        int pos = ((Spinner)v).getSelectedItemPosition();
        return pos == AdapterView.INVALID_POSITION ? defVal : pos;
      }
    }
    return defVal;
  }

  /**
   * Get the current checked state of a form checkbox
   * @param key : the checkbox's tag
   * @param defVal : the default value
   * @return the current checked state, or defVal if not found
   */
  public boolean getFormChecked(Enum<?> key, boolean defVal) {
    View v = mFormLinks.get(key);
    if (v != null) {
      if (v instanceof CheckBoxHolo)
        return ((CheckBoxHolo)v).isChecked();
    }
    return defVal;
  }

  /**
   * Get the current text of a form edit
   * @param key : the edittext's tag
   * @return the current edit text, or null if not found
   */
  public String getFormEditText(Enum<?> key) {
    View v = mFormLinks.get(key);
    if (v != null) {
      if (v instanceof EditTextHolo)
        return ((EditTextHolo)v).getText().toString();
    }
    return null;
  }
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java