fr.eoit.activity.util.ItemListViewBinder.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.util.ItemListViewBinder.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.activity.util;

import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import fr.eoit.R;
import fr.eoit.db.bean.Blueprint;
import fr.eoit.db.bean.Item;
import fr.eoit.db.dto.ColumnsNames.Prices;
import fr.eoit.formula.FormulaCalculator;
import fr.eoit.util.IconUtil;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author picon.software
 *
 */
public class ItemListViewBinder implements SimpleCursorAdapter.ViewBinder, ViewBinder {

    public static enum RedQuantityBehavior {
        NONE, WITH_WASTE_QUANTITY, WITH_REFINE_QUANTITY, PRICE
    }

    private static final NumberFormat nf = new DecimalFormat("#,##0.##");
    private static final NumberFormat nfPercent = new DecimalFormat("##0.##%");

    private RedQuantityBehavior behavior = RedQuantityBehavior.NONE;

    private long id = 0;
    private long quantity = 0;
    private double price = 0;

    private double totalPrice = 0, totalVolume = 0;

    private int numberOfElements = -1;
    private int currentPosition = 0;

    public ItemListViewBinder() {
        super();
    }

    public ItemListViewBinder(RedQuantityBehavior behavior) {
        this();
        this.behavior = behavior;
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();

        numberOfElements = cursor.getCount();
        currentPosition = cursor.getPosition();

        switch (viewId) {
        case R.id.item_name:
        case R.id.ITEM_NAME:
            String value = cursor.getString(columnIndex);
            initName(view, value);
            break;

        case R.id.item_icon:
        case R.id.ITEM_ICON:
            id = cursor.getLong(columnIndex);
            initIcon(view, id);
            break;

        case R.id.item_quantity:
        case R.id.ITEM_QUANTITY:
            quantity = cursor.getLong(columnIndex);
            int ml = 0;
            int groupId = 0;
            if (cursor.getColumnIndex(Blueprint.COLUMN_NAME_ML) != -1) {
                ml = cursor.getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_ML));
            }
            if (cursor.getColumnIndex(Item.COLUMN_NAME_GROUP_ID) != -1) {
                groupId = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_GROUP_ID));
            }
            if (cursor.getColumnIndex(Prices.COLUMN_NAME_PRODUCE_PRICE) != -1) {
                price = cursor.getDouble(cursor.getColumnIndexOrThrow(Prices.COLUMN_NAME_PRODUCE_PRICE));
            }
            initQuantity(id, view, quantity, ml, groupId, price);
            break;

        case R.id.ITEM_DMG_PER_JOB:
            float damagePerJob = cursor.getFloat(columnIndex);
            initDamagePerJob(view, damagePerJob);
            break;

        case R.id.warn_icon:
        case R.id.WARN_ICON:
            price = PricesUtils.getPriceOrNaN(cursor);
            initWarnIcon(view, price);
            break;

        case R.id.item_price:
        case R.id.ITEM_PRICE:
            addToTotalPrice(id, price, quantity);
            break;

        case R.id.item_volume:
        case R.id.ITEM_VOLUME:
            double volume = cursor.getDouble(columnIndex);
            addToTotalVolume(id, volume, quantity);
            break;

        default:
            throw new IllegalArgumentException("viewId : " + viewId);
        }

        return true;
    }

    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        int viewId = view.getId();

        switch (viewId) {
        case R.id.ITEM_NAME:
            String value = String.valueOf(data);
            initName(view, value);
            break;

        case R.id.ITEM_ICON:
            id = (Long) data;
            initIcon(view, id);
            break;

        case R.id.ITEM_QUANTITY:
            quantity = (Integer) data;
            initQuantity(id, view, quantity, 0, 0, 0);
            break;

        case R.id.ITEM_DMG_PER_JOB:
            float damagePerJob = (Float) data;
            initDamagePerJob(view, damagePerJob);
            break;

        case R.id.WARN_ICON:
            price = (Double) data;
            initWarnIcon(view, price);
            break;

        case R.id.ITEM_PRICE:
            price = (Double) data;
            addToTotalPrice(id, price, quantity);
            break;

        case R.id.ITEM_VOLUME:
            addToTotalVolume(id, (Double) data, quantity);
            break;
        default:
            throw new IllegalArgumentException("viewId : " + viewId);
        }

        return true;
    }

    private void initIcon(View view, long id) {
        ImageView imageView = (ImageView) view;
        IconUtil.initIcon(id, imageView);
    }

    private void initName(View view, String value) {
        TextView textView = (TextView) view;
        textView.setText(value);
    }

    protected void initQuantity(long id, View view, long quantity, int ml, int groupId, double price) {
        TextView textView = (TextView) view;
        textView.setText("x" + nf.format(quantity));
        textView.setVisibility(View.VISIBLE);

        TextView itemRedQantityTextView = (TextView) ((ViewGroup) view.getParent())
                .findViewById(R.id.ITEM_RED_QUANTITY);
        long redQuantity = 0;
        switch (behavior) {
        case NONE:
            break;
        case WITH_WASTE_QUANTITY:
            redQuantity = FormulaCalculator.calculateWastedMaterial(ml, quantity);
            itemRedQantityTextView.setText((redQuantity > 0 ? "+" : "") + nf.format(redQuantity));
            break;
        case WITH_REFINE_QUANTITY:
            redQuantity = -FormulaCalculator.calculateRefiningWaste(quantity, groupId);
            long taxe = -FormulaCalculator.calculateReprocessStationTake(quantity);
            String text = (redQuantity != 0 ? nf.format(redQuantity) + " " : "")
                    + (taxe != 0 ? nf.format(taxe) : "");
            redQuantity += taxe;
            itemRedQantityTextView.setText(text);
            break;
        case PRICE:
            redQuantity = 1;
            itemRedQantityTextView.setText(PricesUtils.formatPrice(price, view.getContext(), true));
            break;
        default:
            break;
        }

        if (itemRedQantityTextView != null) {
            if (redQuantity == 0) {
                itemRedQantityTextView.setVisibility(View.GONE);
            } else {
                itemRedQantityTextView.setVisibility(View.VISIBLE);
            }
        }
    }

    private void initDamagePerJob(View view, float damagePerJob) {
        TextView textView = (TextView) view;
        if (damagePerJob < 1 && damagePerJob > 0) {
            textView.setVisibility(View.VISIBLE);
            textView.setText("(" + nfPercent.format(damagePerJob) + " "
                    + view.getResources().getString(R.string.damageperjob) + ")");
        } else {
            textView.setVisibility(View.GONE);
        }
    }

    private void initWarnIcon(View view, double price) {
        if (Double.isNaN(price)) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    protected void addToTotalPrice(long id, double price, long quantity) {
        totalPrice += (price * quantity);
    }

    protected void addToTotalVolume(long id, double volume, long quantity) {
        totalVolume += (volume * quantity);
    }

    /**
     * @return the totalPrice
     */
    public double getTotalPrice() {
        return totalPrice;
    }

    /**
     * @return the totalVolume
     */
    public double getTotalVolume() {
        return totalVolume;
    }

    /**
     * @return the numberOfElements
     */
    public int getNumberOfElements() {
        return numberOfElements;
    }

    /**
     * @return the currentPosition
     */
    public int getCurrentPosition() {
        return currentPosition;
    }

}