Java tutorial
/* * ================================================================================= * Copyright (C) 2013 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only); * --------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy * of this License you may obtain at * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written in this * file but as it is described in the License, the software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF * ANY KIND. * * See the License for the specific language governing permissions and limitations * under the License. * ================================================================================= */ package com.wit.and.dialog; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Typeface; import android.os.Bundle; import android.os.Parcel; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import com.wit.and.dialog.manage.DialogOptions; import com.wit.and.style.styler.WidgetStyler; /** * <h4>Class Overview</h4> * <p> * </p> * * @author Martin Albedinsky */ public class EditDialog extends Dialog implements IEditDialog { /** * Constants ============================= */ /** * Log TAG. */ private static final String TAG = EditDialog.class.getSimpleName(); /** * Indicates if debug private output trough log-cat is enabled. */ // private static final boolean DEBUG = true; /** * Indicates if logging for user output trough log-cat is enabled. */ // private static final boolean USER_LOG = true; protected static final String BUNDLE_INPUT = "com.wit.and.dialog.EditDialog.Bundle.Input"; protected static final String BUNDLE_HINT = "com.wit.and.dialog.EditDialog.Bundle.Hint"; protected static final String BUNDLE_EDIT_STYLE = "com.wit.and.dialog.EditDialog.Bundle.EditStyle"; /** * Enums ================================= */ /** * Static members ======================== */ /** * Members =============================== */ /** * Widget stylers. */ private final WidgetStyler.EditTextStyler EDIT_STYLER = new WidgetStyler.EditTextStyler(0); /** * Main dialog edit text. */ private EditText mEdit = null; /** * Edit text saved input. */ private String mInput = ""; /** * Hint text for edit text. */ private String mHint = null; /** * Style for edit text widget. */ private int mEditStyle = 0; /** * Listeners ----------------------------- */ /** * Dialog edit listener. */ private OnEditListener iListener, iParentListener; /** * Arrays -------------------------------- */ /** * Booleans ------------------------------ */ /** * Constructors ========================== */ /** * Methods =============================== */ /** * Public -------------------------------- */ /** * <p> * Creates new instance of edit dialog fragment. * </p> * * @param editOptions * Specific dialog options for edit dialog. * @return New instance of edit dialog fragment. */ public static EditDialog newInstance(EditOptions editOptions) { final EditDialog dialog = new EditDialog(); dialog.setCancelable(editOptions.isCancelable()); // Set up dialog arguments. dialog.setArguments(createArguments(editOptions)); return dialog; } /** * Getters + Setters --------------------- */ /** * <p> * Registers callback to be invoked when edit value is being submitted. * </p> * * @param listener * Listener callback. */ public void setOnEditListener(OnEditListener listener) { this.iListener = listener; } /** */ @Override public EditText getCurrentEdit() { return mEdit; } /** */ @Override public void setEditError(String errorText) { if (mEdit != null) { mEdit.setError(errorText); } } /** */ @Override public EditOptions getOptions() { return (EditOptions) super.getOptions(); } /** * Protected ----------------------------- */ /** */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null && getArguments() != null) { this.initParams(getArguments()); } } /** */ @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); this.initParams(savedInstanceState); } /** */ @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(BUNDLE_HINT, mHint); outState.putInt(BUNDLE_EDIT_STYLE, mEditStyle); // Save current input value. if (mEdit != null && mEdit.getText() != null) { outState.putString(BUNDLE_INPUT, mEdit.getText().toString()); } } /** */ @Override public void onAttach(Activity activity) { super.onAttach(activity); // This resolves dialog listener even, when any listener wasn't set, but // if this dialog was showed in the context of activity (fragment) which is // currently implementing OnEditListener, we take that as that activity (fragment) // want to be informed about dialog callback. Fragment fragment = getCurrentFragment(); // Resolve parent listener. boolean resolved = false; if (fragment != null) { if (fragment instanceof OnEditListener) { this.iParentListener = (OnEditListener) fragment; resolved = true; } } if (!resolved && activity != null) { if (activity instanceof OnEditListener) { this.iParentListener = (OnEditListener) activity; } } } /** */ @Override protected View onCreateBodyView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final Context context = inflater.getContext(); EditText editText = new EditText(context); editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); editText.setId(R.id.And_Dialog_EditText); return editText; } /** */ @Override protected void onInitViews(View dialogView) { super.onInitViews(dialogView); this.mEdit = (EditText) findViewByID(R.id.And_Dialog_EditText); } /** */ @Override protected void onStyleDialog(View dialogView, TypedArray parsedArray) { final Context context = getActivity(); Log.d(TAG, "onStyleDialog() START"); final int editDialogStyle = parsedArray.getResourceId(R.styleable.And_Theme_Dialog_editDialogStyle, R.style.And_Dialog_EditDialog); final TypedArray dialogArray = context.obtainStyledAttributes(editDialogStyle, R.styleable.And_Theme_Dialog_EditDialog); if (dialogArray != null) { final int n = dialogArray.getIndexCount(); for (int i = 0; i < n; i++) { int attr = dialogArray.getIndex(i); /** * Obtain edit dialog sub-dialog style. */ if (attr == R.styleable.And_Theme_Dialog_EditDialog_dialogStyle) { /** * Let super to process edit dialog sub-dialog style. */ super.onStyleDialog(dialogView, dialogArray.getResourceId(attr, R.style.And_Dialog_Dialog)); } // Set up edit dialog edit. else if (attr == R.styleable.And_Theme_Dialog_EditDialog_android_editTextStyle) { final int editStyle = dialogArray.getResourceId(attr, R.style.And_Dialog_EditText); EDIT_STYLER.performStyling(editStyle, mEdit); } } dialogArray.recycle(); } Log.d(TAG, "onStyleDialog() set up edit style from options"); // Set up edit text. if (mEditStyle > 0) { Log.d(TAG, "onStyleDialog() edit style(" + mEditStyle + ")"); EDIT_STYLER.performStyling(mEditStyle, mEdit); } if (mHint != null) { mEdit.setHint(mHint); } /** * Set type face to text views. */ Typeface typeface = obtainTypeface(editDialogStyle); if (typeface != null) { applyTypeface(typeface, mEdit); } Log.d(TAG, "onStyleDialog() FINISH"); } /** */ @Override protected void onBindDialog(View dialogView, Bundle savedInstanceState) { super.onBindDialog(dialogView, savedInstanceState); // Get default input from options as message. this.mInput = getOptions().getMessage(); // Set up current input value. if (mInput != null) { mEdit.setText(mInput); } } /** */ @Override protected void onButtonClick(DialogButton button) { // Let base dialog listener callback be fired. super.onButtonClick(button); switch (button) { // Neutral or positive as submit. case NEUTRAL: case POSITIVE: if (iListener != null) { iListener.onEditSubmit(this); } if (iParentListener != null) { iParentListener.onEditSubmit(this); } break; } } /** * Private ------------------------------- */ /** * Initializes dialog base parameters from the given bundle. * * @param bundle * Bundle with parameters. */ private void initParams(Bundle bundle) { this.mHint = bundle.getString(BUNDLE_HINT); this.mEditStyle = bundle.getInt(BUNDLE_EDIT_STYLE, 0); if (bundle.containsKey(BUNDLE_INPUT)) { this.mInput = bundle.getString(BUNDLE_INPUT); } } /** * Abstract methods ---------------------- */ /** * Inner classes ========================= */ /** * <h4>Class Overview</h4> * <p> * Specific dialog options for {@link com.wit.and.dialog.EditDialog}. * </p> * * @author Martin Albedinsky * @see com.wit.and.dialog.EditDialog * @see DialogOptions */ public static class EditOptions extends DialogOptions<EditOptions> { /** * Members =============================== */ private String hint; private int editStyle = R.style.And_Dialog_EditText_EditDialog; /** * Constructors ========================== */ /** * <p> * Same as {@link com.wit.and.dialog.manage.DialogOptions#DialogOptions()}. * </p> */ public EditOptions() { super(); } /** * <p> * Same as {@link com.wit.and.dialog.manage.DialogOptions#DialogOptions(android.content.res.Resources)}. * </p> * * @param resources */ public EditOptions(Resources resources) { super(resources); } /** * <p> * Same as {@link com.wit.and.dialog.manage.DialogOptions#DialogOptions(android.os.Parcel)}. * </p> * * @param input */ protected EditOptions(Parcel input) { super(input); this.hint = input.readString(); this.editStyle = input.readInt(); } /** * Methods =============================== */ /** * Public -------------------------------- */ /** */ @Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(hint); dest.writeInt(editStyle); } /** * Getters + Setters --------------------- */ /** * <p> * </p> * * @param textRes * @return This options. */ public EditOptions hint(int textRes) { return hint(getString(textRes)); } /** * <p> * </p> * * @param text * @return This options. */ public EditOptions hint(String text) { this.hint = text; return this; } /** * <p> * </p> * * @return */ public String getHint() { return hint; } /** * <p> * </p> * * @param style * @return This options. */ public EditOptions editStyle(int style) { this.editStyle = style; return this; } /** * <p> * </p> * * @return */ public int getEditStyle() { return editStyle; } } /** * Interface ============================= */ }