com.flowzr.budget.holo.activity.EntityListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.flowzr.budget.holo.activity.EntityListFragment.java

Source

/*******************************************************************************
 * Copyright (c) 2010 Denis Solonenko.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Denis Solonenko - initial API and implementation
 ******************************************************************************/
package com.flowzr.budget.holo.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.Toast;
import com.flowzr.budget.holo.R;
import com.flowzr.budget.holo.adapter.EntityTypeListAdapter;
import com.flowzr.budget.holo.filter.WhereFilter;
import com.flowzr.budget.holo.model.EntityType;
import com.flowzr.budget.holo.model.Transaction;

public class EntityListFragment extends AbstractListFragment {

    public final EntityType[] entities = new EntityType[] { EntityType.CURRENCIES, EntityType.EXCHANGE_RATES,
            EntityType.CATEGORIES, EntityType.PAYEES, EntityType.PROJECTS, EntityType.LOCATIONS };

    public EntityListFragment() {
        super(R.layout.entity_list);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(createAdapter(null));
    }

    @Override
    protected void prepareActionGrid() {
        // TODO Auto-generated method stub

    }

    @Override
    protected Cursor createCursor() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected ListAdapter createAdapter(Cursor cursor) {
        return new EntityTypeListAdapter(this.getActivity(), entities);
    }

    @Override
    protected void deleteItem(View v, int position, long id) {
        // nothing to do
    }

    @Override
    protected void editItem(View v, int position, long id) {
        // nothing to do

    }

    @Override
    protected void viewItem(View v, int position, long id) {
        EntityListActivity main = ((EntityListActivity) getActivity());
        main.getSupportActionBar().removeAllTabs();
        main.loadFragment(entities[position].getActivityClass());
    }

    @Override
    protected String getMyTitle() {
        return getString(R.string.entities);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("flowzr", "on activity result");

        if (resultCode == MainActivity.RESULT_OK
                && requestCode == BlotterFragment.NEW_TRANSACTION_FROM_TEMPLATE_REQUEST) {
            createTransactionFromTemplate(data);
        }
    }

    public void createTransactionFromTemplate(Intent data) {
        long templateId = data.getLongExtra(SelectTemplateActivity.TEMPATE_ID, -1);
        int multiplier = data.getIntExtra(SelectTemplateActivity.MULTIPLIER, 1);
        boolean edit = data.getBooleanExtra(SelectTemplateActivity.EDIT_AFTER_CREATION, false);
        if (templateId > 0) {
            long id = duplicateTransaction(templateId, multiplier);
            Transaction t = db.getTransaction(id);
            if (t.fromAmount == 0 || edit) {
                new BlotterOperations(new BlotterFragment(), db, id).asNewFromTemplate().editTransaction();
            }
        }
    }

    private long duplicateTransaction(long id, int multiplier) {
        long newId = new BlotterOperations(new BlotterFragment(), db, id).duplicateTransaction(multiplier);
        String toastText;
        if (multiplier > 1) {
            toastText = getString(R.string.duplicate_success_with_multiplier, multiplier);
        } else {
            toastText = getString(R.string.duplicate_success);
        }
        Toast.makeText(this.getActivity(), toastText, Toast.LENGTH_LONG).show();
        recreateCursor();
        AccountWidget.updateWidgets(new BlotterFragment().getActivity());
        return newId;
    }

}