de.egore911.drilog.adapter.ShowDataAdapter.java Source code

Java tutorial

Introduction

Here is the source code for de.egore911.drilog.adapter.ShowDataAdapter.java

Source

/* ShowDataAdapter.java
 *
 * Copyright (C) 2011-2012 Christoph Brill
 * 
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package de.egore911.drilog.adapter;

import android.content.res.Resources;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import de.egore911.drilog.R;
import de.egore911.drilog.ShowActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

import de.egore911.drilog.model.Comment;
import de.egore911.drilog.model.ListElement;
import de.egore911.drilog.model.User;
import de.egore911.drilog.util.DateUtil;

/**
 * This class is capable of rendering the elements of a ListView. The Lines to
 * be show must implement {@link ListElement}.
 *
 * @author Christoph Brill <egore911@egore911.de>
 * @since 0.1
 */
public class ShowDataAdapter extends BaseAdapter {

    public static class UserViewHolder {
        TextView nameField;
        ImageView avatarField;
        public User user;
    }

    public static class CommentViewHolder {
        TextView commentField;
        TextView dateField;
        public Comment comment;
    }

    private static final int TYPE_NAME = 0x0;
    private static final int TYPE_COMMENT = 0x1;

    private final ShowActivity activity;
    private final LayoutInflater layoutInflater;
    protected Set<String> watchList;

    protected List<ListElement> listElements;

    public Date maxDate;
    public String channel;
    public Date date;
    public Comment anchor;

    public ShowDataAdapter(ShowActivity activity, JSONObject o, Set<String> watchList) {
        this.activity = activity;
        // Store references to some of the Activities members (for using them
        // later)
        this.layoutInflater = activity.getLayoutInflater();

        updateElements(o, watchList);
    }

    public void updateElements(JSONObject o, Set<String> watchList) {
        this.watchList = watchList;
        // Prepare the data
        this.listElements = new ArrayList<ListElement>();
        if (o != null) {
            try {
                // Read some static data from the JSON-Structure
                channel = o.getString("channel");
                date = DateUtil.getDate(o.getString("date"));
                maxDate = DateUtil.getDate(o.getString("maxdate"));

                Map<String, User> userMap = new HashMap<String, User>();
                // Now let's read the users
                JSONArray users = o.getJSONArray("users");
                for (int i = 0; i < users.length(); i++) {
                    JSONObject user = users.getJSONObject(i);
                    User u = User.create(user);
                    userMap.put(user.getString("name"), u);
                }

                // Now read the log lines
                User lastUser = null;
                JSONArray data = o.getJSONArray("data");
                for (int i = 0; i < data.length(); i++) {
                    // Now let's add the log lines from this user
                    JSONObject comment = data.getJSONObject(i);
                    Comment c = Comment.create(comment);
                    User user = userMap.get(comment.getString("user"));
                    c.user = user;
                    if (lastUser != user) {
                        listElements.add(user);
                        lastUser = user;
                    }
                    listElements.add(c);
                }
            } catch (JSONException e) {
                Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listElements.size();
    }

    @Override
    public Object getItem(int position) {
        return listElements.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object o = getItem(position);
        boolean isUser = o instanceof User;

        ViewGroup row;
        if (null == convertView) {
            // Create a new view for the user or comment
            if (isUser) {
                row = (RelativeLayout) layoutInflater.inflate(R.layout.line_name, null);
                UserViewHolder holder = new UserViewHolder();
                holder.nameField = (TextView) row.findViewById(R.id.name);
                holder.avatarField = (ImageView) row.findViewById(R.id.avatar);
                holder.user = (User) o;
                row.setTag(holder);
            } else {
                row = (RelativeLayout) layoutInflater.inflate(R.layout.line_comment, null);
                CommentViewHolder holder = new CommentViewHolder();
                holder.commentField = (TextView) row.findViewById(R.id.comment);
                holder.dateField = (TextView) row.findViewById(R.id.date);
                holder.comment = (Comment) o;
                row.setTag(holder);
            }
        } else {
            // Recycle a previous view if possible
            row = (ViewGroup) convertView;
            if (isUser) {
                UserViewHolder holder = (UserViewHolder) row.getTag();
                holder.user = (User) o;
            } else {
                CommentViewHolder holder = (CommentViewHolder) row.getTag();
                holder.comment = (Comment) o;
            }
        }

        if (isUser) {
            User user = (User) o;

            setDefaultCellBackground(position, row, false);

            // Render a user
            UserViewHolder holder = (UserViewHolder) row.getTag();
            if (watchList != null && watchList.contains(user.nick)) {
                holder.nameField.setTypeface(null, Typeface.BOLD);
            } else {
                holder.nameField.setTypeface(null, Typeface.NORMAL);
            }
            holder.nameField.setText(user.name);
            Picasso.with(activity).load(user.image).placeholder(R.drawable.ic_launcher).into(holder.avatarField);
        } else {
            Comment comment = (Comment) o;

            setDefaultCellBackground(position, row, anchor == comment);

            // Render a comment
            CommentViewHolder holder = (CommentViewHolder) row.getTag();
            holder.commentField.setText(comment.content);
            holder.dateField.setText(comment.time);
        }

        return row;
    }

    @Override
    public int getViewTypeCount() {
        // We got two types of lines: users and comments
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof User) {
            return TYPE_NAME;
        }
        return TYPE_COMMENT;
    }

    public List<ListElement> getListElements() {
        return listElements;
    }

    private void setDefaultCellBackground(int position, ViewGroup row, boolean selected) {
        if (getCount() == 0) {
            // Only one element -> border at top and bottom
            int drawable = R.drawable.table_shape_both;
            row.setBackgroundResource(drawable);
        } else if (getItemViewType(position) == TYPE_NAME) {
            // User -> border at top
            int drawable = R.drawable.table_shape_top;
            row.setBackgroundResource(drawable);
        } else if (position == (getCount() - 1) || (getItem(position + 1) instanceof User)) {
            // Last element or next element is a user -> border at bottom
            int drawable = R.drawable.table_shape_bottom;
            row.setBackgroundResource(drawable);
        } else {
            // Nothing special, border at the left and right
            int drawable = R.drawable.table_shape_middle;
            row.setBackgroundResource(drawable);
        }
    }

    public void setWatchList(Set<String> watchList) {
        this.watchList = watchList;
    }

}