Android Open Source - interdroid-vdb-ui Commit Expandable List Adapter






From Project

Back to project page interdroid-vdb-ui.

License

The source code is released under:

Copyright (c) 2008-2011 Vrije Universiteit, The Netherlands All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the follo...

If you think the Android project interdroid-vdb-ui listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2008-2012 Vrije Universiteit, The Netherlands All rights
 * reserved.//w w  w. ja v a 2 s.co m
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Vrije Universiteit nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package interdroid.vdb.persistence.ui;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

import org.eclipse.jgit.revwalk.RevCommit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import interdroid.vdb.Authority;
import interdroid.vdb.R;
import interdroid.vdb.content.EntityUriBuilder;
import interdroid.vdb.persistence.api.VdbRepository;
import interdroid.vdb.persistence.ui.BranchExpandableListAdapter.OnRevisionClickListener;

import android.content.Context;
import android.net.Uri;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListView.OnChildClickListener;



public class CommitExpandableListAdapter extends BaseExpandableListAdapter implements OnChildClickListener, OnItemClickListener, OnItemLongClickListener {
  private static final Logger logger = LoggerFactory
  .getLogger(CommitExpandableListAdapter.class);

  private Context mContext;
  private VdbRepository mRepo;
  private String mBranchName;
  private OnRevisionClickListener mListener;

  private ArrayList<RevCommit> mCommits = new ArrayList<RevCommit>();

  private int mHeaderViewHeight;
  private int mChildViewHeight;

  private Uri getCommitUri(int childPosition) {
    return EntityUriBuilder.commitUri(Authority.VDB, mRepo.getName(), mCommits.get(childPosition).getName());
  }


  private Uri getBranchUri() {
    return EntityUriBuilder.branchUri(Authority.VDB,mRepo.getName(), mBranchName);
  }

  @Override
  public boolean onChildClick(ExpandableListView parent, View v,
      int groupPosition, int childPosition, long id) {
    logger.debug("onChildClick: "+ groupPosition + " : " + childPosition + " : " + id);
    if (mListener != null) {
      mListener.onRevisionClick(getCommitUri(childPosition));
      return true;
    }
    return false;
  }

  @Override
  public boolean onItemLongClick(AdapterView<?> parent, View view,
      int position, long id) {
    logger.debug("onItemLongClick: {} {}", position, id);
    if (mListener != null) {
      // position zero is the branch, position 1 is the commit dropdown and position 2 is the first commit
      if (position == 0) {
        mListener.onRevisionLongClick(getBranchUri());
        return true;
      } else if (position > 1) {
        mListener.onRevisionLongClick(getCommitUri(position - 2));
        return true;
      }
    }
    return false;
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {
    logger.debug("onItemClick: {} {}", position, id);
    if (mListener != null) {
      mListener.onRevisionClick(getBranchUri());
    }
  }

  @Override
  public void notifyDataSetChanged() {
    refreshCommits();
    super.notifyDataSetChanged();
  }

  @Override
  public void notifyDataSetInvalidated() {
    refreshCommits();
    super.notifyDataSetInvalidated();
  }

  public CommitExpandableListAdapter(Context context, VdbRepository repo, String branchName) {
    mContext = context;
    mRepo = repo;
    mBranchName = branchName;
    refreshCommits();
  }

  public void setRevisionClickListener(OnRevisionClickListener listener) {
    mListener = listener;
  }

  public void setBranchName(String branchName) {
    mBranchName = branchName;
    notifyDataSetChanged();
  }

  private void refreshCommits() {
    mCommits.clear();
    try {
      for (RevCommit c : mRepo.enumerateCommits(mBranchName)) {
        mCommits.add(c);
      }
    } catch (IOException e) {
      logger.error("Error enumerating commits for branch: " + mBranchName);
      throw new RuntimeException("Error enumerating commits", e);
    }
  }

  @Override
  public int getGroupCount() {
    return 1;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return mCommits.size();
  }

  @Override
  public Object getGroup(int groupPosition) {
    return null;
  }

  @Override
  public Object getChild(int groupPosition, int childPosition) {
    return mCommits.get(childPosition);
  }

  @Override
  public long getGroupId(int groupPosition) {
    return 1;
  }

  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
  }

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

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded,
      View convertView, ViewGroup parent) {
    TextView view;
    if (convertView == null) {
      view = new TextView(mContext);
      view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 60));
      view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
      view.setPadding(60, 0, 0, 0);
      view.setTextSize(14);
    } else {
      view = (TextView)convertView;
    }

    view.setText(R.string.label_commits);

    return view;
  }

  @Override
  public View getChildView(int groupPosition, int childPosition,
      boolean isLastChild, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) {
      view = buildCommitItemView(parent);
    } else {
      view = convertView;
    }
    setupCommitItemView(view, childPosition);

    return view;
  }


  private void setupCommitItemView(View view, int childPosition) {
    final java.text.DateFormat dateFormat = DateFormat.getDateFormat(mContext);
    final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(mContext);

    RevCommit c = mCommits.get(childPosition);

    Date creationDate = new Date(1000 * c.getCommitTime());

    ((TextView)view.findViewById(R.id.title)).setText(c.getShortMessage());
    ((TextView)view.findViewById(R.id.commit_sha1)).setText(c.getName());
    ((TextView)view.findViewById(R.id.created_at)).setText(
        mContext.getString(R.string.label_creation_time) + " " + dateFormat.format(creationDate)
        + " " + timeFormat.format(creationDate));
    ((TextView)view.findViewById(R.id.created_by)).setText(
        mContext.getString(R.string.label_by) + " " + c.getAuthorIdent().getEmailAddress());
  }

  private View buildCommitItemView(ViewGroup parent) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.commitlist_item, parent, false);
    return view;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
  }

  public int getHeaderViewHeight(int widthMeasureSpec, int heightMeasureSpec) {
    if (mHeaderViewHeight == 0) {
      View view = getGroupView(0, false, null, null);
      view.measure(widthMeasureSpec, heightMeasureSpec);
      mHeaderViewHeight = view.getMeasuredHeight();
    }
    return mHeaderViewHeight;
  }

  public int getChildViewHeight(int widthMeasureSpec, int heightMeasureSpec) {
    if (mChildViewHeight == 0) {
      View view = buildCommitItemView(null);
      view.measure(widthMeasureSpec, heightMeasureSpec);
      mChildViewHeight = view.getMeasuredHeight();
    }
    return mChildViewHeight;
  }

  public void setRepoAndBranch(VdbRepository repo, String branchName) {
    mRepo = repo;
    mBranchName = branchName;
    notifyDataSetChanged();
  }

}




Java Source Code List

interdroid.vdb.persistence.content.PeerRegistry.java
interdroid.vdb.persistence.ui.AddBranchActivity.java
interdroid.vdb.persistence.ui.BaseEditRepositoryActivity.java
interdroid.vdb.persistence.ui.BranchExpandableListAdapter.java
interdroid.vdb.persistence.ui.CommitActivity.java
interdroid.vdb.persistence.ui.CommitExpandableListAdapter.java
interdroid.vdb.persistence.ui.CommitExpandableListView.java
interdroid.vdb.persistence.ui.EditLocalSharedRepositoriesActivity.java
interdroid.vdb.persistence.ui.EditPeerActivity.java
interdroid.vdb.persistence.ui.EditPeerDetailsActivity.java
interdroid.vdb.persistence.ui.EditRemoteActivity.java
interdroid.vdb.persistence.ui.EditRemoteSharedRepositoriesActivity.java
interdroid.vdb.persistence.ui.GitService.java
interdroid.vdb.persistence.ui.ManageLocalBranchesActivity.java
interdroid.vdb.persistence.ui.ManagePeersActivity.java
interdroid.vdb.persistence.ui.ManageRemotesActivity.java
interdroid.vdb.persistence.ui.ManageRepositoriesActivity.java
interdroid.vdb.persistence.ui.ManageRepositoryActivity.java
interdroid.vdb.persistence.ui.ManageRepositoryPropertiesActivity.java
interdroid.vdb.persistence.ui.Raven.java
interdroid.vdb.persistence.ui.RevisionPicker.java
interdroid.vdb.persistence.ui.VdbPreferences.java