Back to project page Resonos-Android-Framework.
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.
package com.resonos.apps.library.widget; /*ww w . j av a2 s. c o m*/ import android.content.Context; import android.graphics.Color; import android.graphics.LightingColorFilter; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.InputType; import android.text.method.PasswordTransformationMethod; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.WazaBe.HoloEverywhere.CheckBoxHolo; import com.WazaBe.HoloEverywhere.EditTextHolo; import com.resonos.apps.library.util.AppUtils; /** * This class is a streamlined method of building a single form element. * Most of the functions have short descriptive names and chain together * for ease of use. * * @author Chris */ public class FormElement { // context private final FormBuilder mParent; // vars protected boolean mBuilt = false; private FormElementData mData; /** * A simple class that contains all of the data for a form element. */ public static class FormElementData { public boolean mClickable = false; public CharSequence mTitle, mSubtitle; public int mIcon = -1; public Drawable mDrawable = null; public int mTextColor = Color.TRANSPARENT, mIconColor = Color.TRANSPARENT; public int mGravity = Gravity.CENTER_VERTICAL; public Rect mDrawableBounds; public Enum<?> mTag; public View mView, mIconView; public boolean mHeader; public boolean mDivider; public boolean mSuppressDivider; public int mIndex; } /** * Create a new form item using its builder * @param fb : the parent {@link FormBuilder} */ public FormElement(FormBuilder fb) { mParent = fb; mData = new FormElementData(); mData.mIndex = mParent.count++; } /** * Make this element a divider. Usually only used internally, * but may make sense for some FormBuilder implementations. * @return this object, for chaining */ public FormElement divider() { mData.mDivider = true; return this; } /** * Suppress the addition of an automatic divider after this item * (assuming your FormBuilder implementation has automatic dividers). * @return this object, for chaining */ public FormElement suppressDivider() { mData.mSuppressDivider = true; return this; } /** * Finish up with this form element. * @return the parent form builder, to continue overall chaining */ public FormBuilder done() { mParent.buildItem(this); return mParent; } /** * Center this form item. * @return this object, for chaining */ public FormElement center() { mData.mGravity = Gravity.CENTER; return this; } /** * Set a custom gravity for this form item. * @param gravity : the custom gravity * @return this object, for chaining */ public FormElement gravity(int gravity) { mData.mGravity = gravity; return this; } /** * Set an icon for this form item. * @param icon : a drawable resource * @return this object, for chaining */ public FormElement icon(int drawable) { mData.mIcon = drawable; return this; } /** * Set a custom view for this form item. * @param v : the custom view to display * @return this object, for chaining */ public FormElement view(View v) { mData.mView = v; return this; } /** * Set this form item to be an edittext * @param hint : a string resource for the edittext's hint * @param tag : an enum tag to use for retrieving this edittext's value * @return the edittext created */ public EditTextHolo editable(int hint, Enum<?> tag) { EditTextHolo edit = new EditTextHolo(mParent.getContext(), null); edit.setSingleLine(true); edit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); edit.setHint(hint); mData.mView = edit; mData.mTag = tag; mParent.mFormLinks.put(tag, edit); if (mParent.inState != null) { String str = mParent.inState.getString("edit_"+tag.getClass().getSimpleName()+"_"+tag.name()); if (str != null) edit.setText(str); } mParent.mLastEdit = edit; mParent.mLastClickableAfterEdit = null; return edit; } /** * Set this form item to be an edittext * @param hint : a string for the edittext's hint * @param tag : an enum tag to use for retrieving this edittext's value * @return the edittext created */ public EditTextHolo editable(String hint, Enum<?> tag) { EditTextHolo edit = new EditTextHolo(mParent.getContext(), null); edit.setSingleLine(true); edit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); edit.setHint(hint); mData.mView = edit; mData.mTag = tag; mParent.mFormLinks.put(tag, edit); if (mParent.inState != null) { String str = mParent.inState.getString("edit_"+tag.getClass().getSimpleName()+"_"+tag.name()); if (str != null) edit.setText(str); } mParent.mLastEdit = edit; mParent.mLastClickableAfterEdit = null; return edit; } /** * Set this form item to be a password edittext * @param hint : a string resource for the edittext's hint * @param tag : an enum tag to use for retrieving this edittext's value * @return the edittext created */ public EditTextHolo editablePass(int hint, Enum<?> tag) { EditTextHolo edit = new EditTextHolo(mParent.getContext(), null); edit.setSingleLine(true); edit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); edit.setHint(hint); edit.setTransformationMethod(PasswordTransformationMethod.getInstance()); mData.mView = edit; mData.mTag = tag; mParent.mFormLinks.put(tag, edit); if (mParent.inState != null) { String str = mParent.inState.getString("edit_"+tag.getClass().getSimpleName()+"_"+tag.name()); if (str != null) edit.setText(str); } mParent.mLastEdit = edit; mParent.mLastClickableAfterEdit = null; return edit; } /** * Set this form item to be a password edittext * @param hint : a string for the edittext's hint * @param tag : an enum tag to use for retrieving this edittext's value * @return the edittext created */ public EditTextHolo editablePass(String hint, Enum<?> tag) { EditTextHolo edit = new EditTextHolo(mParent.getContext(), null); edit.setSingleLine(true); edit.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); edit.setHint(hint); edit.setTransformationMethod(PasswordTransformationMethod.getInstance()); mData.mView = edit; mData.mTag = tag; mParent.mFormLinks.put(tag, edit); if (mParent.inState != null) { String str = mParent.inState.getString("edit_"+tag.getClass().getSimpleName()+"_"+tag.name()); if (str != null) edit.setText(str); } mParent.mLastEdit = edit; mParent.mLastClickableAfterEdit = null; return edit; } /** * Set this form item to be a checkbox * @param txt : a string resource for the checkbox's text * @param tag : an enum tag to use for retrieving this checkbox's state * @return the checkbox created */ public CheckBoxHolo checkable(int txt, Enum<?> tag) { CheckBoxHolo chk = new CheckBoxHolo(mParent.getContext(), null); chk.setText(txt); mData.mView = chk; mData.mTag = tag; mParent.mFormLinks.put(tag, chk); if (mParent.inState != null) { boolean b = mParent.inState.getBoolean( "chk_"+tag.getClass().getSimpleName()+"_"+tag.name()); chk.setChecked(b); } return chk; } /** * Set this form item to be a checkbox * @param txt : a string for the checkbox's text * @param tag : an enum tag to use for retrieving this checkbox's state * @return the checkbox created */ public CheckBoxHolo checkable(String txt, Enum<?> tag) { CheckBoxHolo chk = new CheckBoxHolo(mParent.getContext(), null); chk.setText(txt); mData.mView = chk; mData.mTag = tag; mParent.mFormLinks.put(tag, chk); if (mParent.inState != null) { boolean b = mParent.inState.getBoolean( "chk_"+tag.getClass().getSimpleName()+"_"+tag.name()); chk.setChecked(b); } return chk; } /** * Set this form item to be a spinner * @param txt : a string for the spinner's prompt * @param choices : a string array of spinner choices * @param tag : an enum tag to use for retrieving this spinner's selection * @return the spinner created */ public Spinner spinner(String txt, String[] choices, Enum<?> tag) { Spinner spinner = new Spinner(mParent.getContext()); spinner.setPrompt(txt); ArrayAdapter<String> adapter = new ArrayAdapter<String>(mParent.getContext(), android.R.layout.simple_spinner_item, choices); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); mData.mView = spinner; mData.mTag = tag; mParent.mFormLinks.put(tag, spinner); if (mParent.inState != null) { int choice = mParent.inState.getInt( "spinner_"+tag.getClass().getSimpleName()+"_"+tag.name()); spinner.setSelection(choice == AdapterView.INVALID_POSITION ? 0 : choice); } return spinner; } /** * Set a custom view to be displayed where an icon normally would be * @param v : the custom icon view * @return this object, for chaining */ public FormElement iconView(View v) { mData.mIconView = v; return this; } /** * Set the form item's title * @param title : the title text * @return this object, for chaining */ public FormElement title(CharSequence title) { mData.mTitle = title; return this; } /** * Set the form item's title and indicate it should appear as a header * @param title : the title text * @return this object, for chaining */ public FormElement setAsHeader(CharSequence s) { mData.mTitle = s; mData.mHeader = true; return this; } /** * Set the form item's title * @param title : the title string resource * @return this object, for chaining */ public FormElement title(int title) { mData.mTitle = mParent.getContext().getString(title); return this; } /** * Set the form item's title and subtitle * @param title : the title text * @param subtitle : the subtitle text * @return this object, for chaining */ public FormElement text(CharSequence title, CharSequence subtitle) { mData.mTitle = title; mData.mSubtitle = subtitle; return this; } /** * Set the form item's title and subtitle * @param title : the title string resource * @param subtitle : the subtitle string resource * @return this object, for chaining */ public FormElement text(int title, int subtitle) { mData.mTitle = mParent.getContext().getString(title); mData.mSubtitle = mParent.getContext().getString(subtitle); return this; } /** * Set the form item's subtitle * @param summary : the subtitle string resource * @return this object, for chaining */ public FormElement subtitle(CharSequence summary) { mData.mSubtitle = summary; return this; } /** * Set the form item's subtitle * @param summary : the subtitle string resource * @return this object, for chaining */ public FormElement subtitle(int summary) { mData.mSubtitle = mParent.getContext().getString(summary); return this; } /** * Set the form item's text color * @param color : the text color * @return this object, for chaining */ public FormElement textColor(int color) { mData.mTextColor = color; return this; } /** * Make this form item clickable and associate a tag to listen for * @param tag : the enum tag to associate with this item when listening for clicks * @return this object, for chaining */ public FormElement onClick(Enum<?> tag) { mData.mClickable = true; mData.mTag = tag; if (mParent.mLastEdit != null) mParent.mLastClickableAfterEdit = tag; return this; } /** * Set the form item icon's draw size * @param w : the draw width * @param h : the draw height * @return this object, for chaining */ public FormElement drawSize(int w, int h) { mData.mDrawableBounds = new Rect(0, 0, w, h); return this; } /** * Set the form item icon's draw size, assuming it is square * @param size : the draw width and height * @return this object, for chaining */ public FormElement drawSize(int size) { mData.mDrawableBounds = new Rect(0, 0, size, size); return this; } /** * Set the form item icon to be drawn with a certain hue tint. * This works best with a white or grayscale icon. * @param iconColor : the tinting color * @return this object, for chaining */ public FormElement drawColor(int iconColor) { mData.mIconColor = iconColor; return this; } /** * Set the form item icon by drawable * @param draw : the icon drawable * @return this object, for chaining */ public FormElement drawable(Drawable draw) { mData.mDrawable = draw; return this; } /** * Get the data class object for this form item * @return the {@link FormElementData} object */ public FormElementData getData() { return mData; } /** * Create the drawable to use for this form item's icon, * taking into account the drawable, drawable resource, and * tinting color, depending on which have been set. * @param cx : context to use for resource access * @return the drawable to use for this form item */ public Drawable calculateDrawableToUse(Context cx) { Drawable dr; if (mData.mDrawable != null) dr = mData.mDrawable; else if (mData.mIconColor != Color.TRANSPARENT) { dr = AppUtils.getMutableDrawable(cx, mData.mIcon); dr.setColorFilter(new LightingColorFilter(mData.mIconColor, 0)); } else { dr = cx.getResources().getDrawable(mData.mIcon); } return dr; } }