com.wit.and.examples.dialogs.screen.ScreenDialogs.java Source code

Java tutorial

Introduction

Here is the source code for com.wit.and.examples.dialogs.screen.ScreenDialogs.java

Source

/*
 * =================================================================================
 * 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.examples.dialogs.screen;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.wit.and.dialog.IDialog;
import com.wit.and.dialog.internal.BaseDialog;
import com.wit.and.dialog.manage.DialogManager;
import com.wit.and.examples.dialogs.R;
import com.wit.and.examples.dialogs.screen.stringarray.ScreenStringArrayDarkRed;
import com.wit.and.examples.dialogs.screen.stringarray.ScreenStringArrayHoloDark;
import com.wit.and.examples.dialogs.screen.stringarray.ScreenStringArrayHoloLight;
import com.wit.and.examples.dialogs.screen.stringarray.ScreenStringArrayLime;
import com.wit.and.examples.dialogs.screen.xml.ScreenXmlDarkRed;
import com.wit.and.examples.dialogs.screen.xml.ScreenXmlHoloDark;
import com.wit.and.examples.dialogs.screen.xml.ScreenXmlHoloLight;
import com.wit.and.examples.dialogs.screen.xml.ScreenXmlLime;
import com.wit.and.examples.dialogs.screen.xmlset.ScreenXmlSetDarkRed;
import com.wit.and.examples.dialogs.screen.xmlset.ScreenXmlSetHoloDark;
import com.wit.and.examples.dialogs.screen.xmlset.ScreenXmlSetHoloLight;
import com.wit.and.examples.dialogs.screen.xmlset.ScreenXmlSetLime;
import com.wit.and.examples.dialogs.widget.DialogsListAdapter;

/**
 * @author Martin Albedinsky
 */
public abstract class ScreenDialogs extends ActionBarActivity implements DialogManager.OnDialogRestoreListener,
        AdapterView.OnItemClickListener, IDialog.OnDialogListener, IDialog.OnDialogCancelListener {

    public static final String INTENT_DIALOG_THEME = "com.wit.and.examples.dialogs.Intent.DialogTheme";

    /**
     * Log TAG.
     */
    private static final String TAG = ScreenDialogs.class.getSimpleName();

    public enum DialogTheme {
        /**
         *
         */
        HOLO_DARK(ScreenDialogsHoloDark.class, ScreenXmlHoloDark.class, ScreenXmlSetHoloDark.class,
                ScreenStringArrayHoloDark.class, Color.parseColor("#33b5e5"), R.color.HoloDark,
                R.drawable.btn_holo_dark, R.color.btn_txt_holo_dark),
        /**
         *
         */
        HOLO_LIGHT(ScreenDialogsHoloLight.class, ScreenXmlHoloLight.class, ScreenXmlSetHoloLight.class,
                ScreenStringArrayHoloLight.class, Color.parseColor("#33b5e5"), R.color.HoloDark,
                R.drawable.btn_holo_dark, R.color.btn_txt_holo_dark),
        /**
         *
         */
        DARK_RED(ScreenDialogsDarkRed.class, ScreenXmlDarkRed.class, ScreenXmlSetDarkRed.class,
                ScreenStringArrayDarkRed.class, Color.parseColor("#b00501"), R.color.DarkRed,
                R.drawable.btn_dark_red, R.color.btn_txt_dark_red),
        /**
         *
         */
        LIME(ScreenDialogsLime.class, ScreenXmlLime.class, ScreenXmlSetLime.class, ScreenStringArrayLime.class,
                Color.parseColor("#55aa44"), R.color.Lime, R.drawable.btn_lime, R.color.btn_txt_lime);

        public final Class<? extends ScreenDialogs> screenJava;

        public final Class<? extends ScreenDialogs> screenXml;

        public final Class<? extends ScreenDialogs> screenXmlDialogSet;

        public final Class<? extends ScreenDialogs> screenStringArray;

        public final int color;

        public final int colorRes;

        public final int buttonRes;

        public final int buttonTextColorRes;

        private DialogTheme(Class<? extends ScreenDialogs> java, Class<? extends ScreenDialogs> xml,
                Class<? extends ScreenDialogs> xmlDialogsSet, Class<? extends ScreenDialogs> stringArray, int color,
                int colorRes, int btnBgr, int btnTextColor) {
            // =================================================
            this.screenJava = java;
            this.screenXml = xml;
            this.screenXmlDialogSet = xmlDialogsSet;
            this.screenStringArray = stringArray;
            this.color = color;
            this.colorRes = colorRes;
            this.buttonRes = btnBgr;
            this.buttonTextColorRes = btnTextColor;
        }
    }

    private final DialogManager DIALOG_MANAGER = new DialogManager(getSupportFragmentManager());

    private ListView mListView = null;

    private DialogTheme mTheme = DialogTheme.HOLO_DARK;

    public DialogManager getDialogManager() {
        return DIALOG_MANAGER;
    }

    public ListView getListView() {
        return mListView;
    }

    public void setListAdapter(DialogsListAdapter adapter) {
        if (mListView != null) {
            adapter.setDialogTheme(mTheme);
            mListView.setAdapter(adapter);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Default just show dialog.
        DIALOG_MANAGER.showDialog((int) id);
    }

    @Override
    public void onDialogRestored(DialogFragment dialog, int dialogID) {
        Log.i(TAG, "Restored dialog with id(" + dialogID + ").");
    }

    @Override
    public void onDialogButtonClick(IDialog dialog, BaseDialog.DialogButton button) {
        Log.d(TAG, "Dialog(" + dialog.getDialogID() + ") button(" + button + ") clicked.");
    }

    @Override
    public void onDialogCancelled(IDialog dialog) {
        Log.d(TAG, "Dialog(" + dialog.getDialogID() + ") canceled.");
    }

    @Override
    public void finish() {
        super.finish();
        super.overridePendingTransition(R.anim.and_ex_show_screen_back, R.anim.and_ex_hide_screen_back);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ex_listview);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            this.mTheme = DialogTheme.values()[extras.getInt(INTENT_DIALOG_THEME, DialogTheme.HOLO_DARK.ordinal())];
        }

        // Set up dialog manager.
        DIALOG_MANAGER.setOnDialogRestoreListener(this);

        // Set up dialogs list.
        this.mListView = (ListView) findViewById(R.id.ListView);
        mListView.setOnItemClickListener(this);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        DIALOG_MANAGER.dispatchParentRestored();
    }
}