de.aw.monma.snippets.SnippetTemplate.java Source code

Java tutorial

Introduction

Here is the source code for de.aw.monma.snippets.SnippetTemplate.java

Source

/*
 * MonMa: Eine freie Android-Application fuer die Verwaltung privater Finanzen
 *
 * Copyright [2015] [Alexander Winkler, 2373 Dahme/Germany]
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if
 * not, see <http://www.gnu.org/licenses/>.
 */
package de.aw.monma.snippets;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import de.aw.awlib.fragments.AWLoaderFragment;
import de.aw.monma.R;
import de.aw.monma.monmamain.MonMaInterface;

/**
 * Template fuer Snippets. Es werden folgende Argumente erwartet: <p> LAYOUT: Layout des Snippets,
 * muss eine ViewGroup (z.B. LinearLayout) mit id 'snippetDetails' enthalten.</p>
 * <p>VIEWHOLDERLAYOUT: Layout der Details im snippet</p><p> VIEWRESIDs: ViewResIDs des Snippets
 * </p>
 */
public abstract class SnippetTemplate extends AWLoaderFragment
        implements MonMaInterface, OnClickListener, OnSharedPreferenceChangeListener {
    protected static final int layout = R.layout.snippet_default;
    private ViewGroup mContainer;

    /**
     * Diese Methode wird nach dem Laden zuerst gerufen. Gibt die Methode true zurueck, wird
     * anschliessend davon ausgegangen, dass der Cursor vollstaendig bearbeitet wurde. Ansonsten
     * wird zeilenweise die View ueber onBindView gefuellt.
     *
     * @return
     */
    protected boolean createViewFromCursor() {
        return false;
    }

    /**
     * Wird gerufen, wenn {@link SnippetTemplate#createViewFromCursor()} 'true' zuruckgeliefert
     * hat.
     *
     * @param view
     *         DetailView, wie in VIEWHOLDERLAYOUT angegeben
     * @param cursor
     *         Cursor
     * @return View, die hinzugefuegt werden soll. Wird hier null zuruckgegeben, wird der gesamte
     * Container ausgeblendet.
     *
     * @throws IllegalStateException,
     *         wenn {@link SnippetTemplate#createViewFromCursor()} 'true' ergab, aber diese Methode
     *         nicht ueberschrieben wurde.
     */
    protected View createViewFromCursor(View view, Cursor cursor) {
        throw new IllegalStateException(
                "Wenn createViewFromCursor true zuruckgibt, muss hier eine View zuruckgegeben " + "werden.");
    }

    /**
     * @return ID der View, dia als Header vewendet werden soll.
     */
    protected int headerView() {
        return 0;
    }

    /**
     * @return ResID des Textes, der eingeblendet wird, wenn keine Daten vorhanden sind. Ist kein
     * solcher vorhanden, wird die gesamte View ausgeblendet.
     */
    protected int noDataText() {
        return 0;
    }

    /**
     * Methode, um die entsprechenden Views zu fuellen. Muss implementiert werden.
     *
     * @param view
     * @param resID
     * @param cursor
     * @param cursorPosition
     */
    protected abstract void onBindView(View view, int resID, Cursor cursor, int cursorPosition);

    @Override
    public void onClick(View v) {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (headerView() != 0) {
            View header = inflater.inflate(headerView(), container, false);
            ViewGroup vg = view.findViewById(R.id.snippetHeader);
            vg.addView(header);
        }
        mContainer = view.findViewById(R.id.snippetDetails);
        mContainer.setOnClickListener(this);
        return view;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        super.onLoadFinished(loader, cursor);
        ((View) getView().getParent()).setVisibility(View.VISIBLE);
        mContainer.removeAllViews();
        TextView emptyData = getView().findViewById(R.id.tvEmptyData);
        if (cursor.getCount() == 0) {
            if (noDataText() == 0) {
                ((View) getView().getParent()).setVisibility(View.GONE);
                return;
            }
            emptyData.setText(noDataText());
            emptyData.setVisibility(View.VISIBLE);
            return;
        } else {
            emptyData.setVisibility(View.GONE);
        }
        int[] viewResIDs = args.getIntArray(VIEWRESIDS);
        int detailViewLayout = args.getInt(VIEWHOLDERLAYOUT);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        if (createViewFromCursor()) {
            View view = inflater.inflate(detailViewLayout, null);
            View detailView = createViewFromCursor(view, cursor);
            if (detailView != null) {
                mContainer.addView(detailView);
            } else {
                ((View) getView().getParent()).setVisibility(View.GONE);
            }
        } else {
            if (cursor.moveToFirst()) {
                do {
                    View view = inflater.inflate(detailViewLayout, null);
                    for (int position = 0; position < viewResIDs.length; position++) {
                        int resID = viewResIDs[position];
                        View detailView = view.findViewById(resID);
                        onBindView(detailView, resID, cursor, position);
                    }
                    mContainer.addView(view);
                } while (cursor.moveToNext());
            }
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    }

    @Override
    protected void setInternalArguments(Bundle args) {
        super.setInternalArguments(args);
        args.putInt(LAYOUT, layout);
    }
}