com.sentaroh.android.TextFileBrowser.ViewedFileListAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.sentaroh.android.TextFileBrowser.ViewedFileListAdapter.java

Source

package com.sentaroh.android.TextFileBrowser;

/*
The MIT License (MIT)
Copyright (c) 2011-2013 Sentaroh
    
Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to 
the following conditions:
    
The above copyright notice and this permission notice shall be included in all copies or 
substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
    
*/

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;

import com.sentaroh.android.Utilities.ThreadCtrl;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ViewedFileListAdapter extends ArrayAdapter<ViewedFileListItem> {

    private ArrayList<ViewedFileListItem> mFileViewList = null;
    private Context mContext = null;
    @SuppressWarnings("unused")
    private Activity mActivity = null;
    private int mResourceId = -1;
    //   private int mTextViewResourceId=-1;

    public ViewedFileListAdapter(Activity a, int resourceId, ArrayList<ViewedFileListItem> objects) {
        super(a, resourceId, objects);
        mActivity = a;
        mContext = a.getApplicationContext();
        mResourceId = resourceId;
        mFileViewList = objects;
    }

    @Override
    public ViewedFileListItem getItem(int pos) {
        return mFileViewList.get(pos);
    }

    @SuppressWarnings("deprecation")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView view;
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = (TextView) vi.inflate(mResourceId, null);

        } else {
            view = (TextView) convertView;
        }
        //        view=(TextView)super.getView(position,convertView,parent);
        view.setText(getItem(position).file_name);
        view.setCompoundDrawablePadding(10);
        view.setCompoundDrawablesWithIntrinsicBounds(
                mContext.getResources().getDrawable(android.R.drawable.arrow_down_float), null, null, null);

        //        view.setTextColor(Color.BLACK);
        //        if (text_size!=0) view.setTextSize(text_size);

        return view;
    }

    @SuppressWarnings("deprecation")
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        final TextView text = (TextView) super.getDropDownView(position, convertView, parent);
        //        text.setBackgroundColor(Color.LTGRAY);
        if (Build.VERSION.SDK_INT >= 11) {
            //          text.setCompoundDrawablePadding(10);
            //          text.setCompoundDrawablesWithIntrinsicBounds(
            //                mContext.getResources().getDrawable(android.R.drawable.btn_radio), 
            //                null, null, null);
            text.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    mContext.getResources().getDrawable(android.R.drawable.btn_radio), null);

        }
        text.setText(getItem(position).file_path);
        //       text.setEllipsize(TruncateAt.START);
        text.post(new Runnable() {
            @Override
            public void run() {
                text.setSingleLine(false);
            }
        });

        //        text.setTextColor(Color.BLACK);
        //        if (text_color!=0) text.setTextColor(text_color);
        //        if (text_size!=0) text.setTextSize(text_size);

        return text;
    }

}

class ViewedFileListItem implements Externalizable {
    public String file_path = "";
    public String file_name = "";
    public Fragment file_view_fragment = null;
    public ThreadCtrl tc_view = null;
    public IndexedFileReader ix_reader_view = null;

    public String encodeName = "";

    public boolean viewerParmsInitRequired = true;
    public boolean viewerParmsRestoreRequired = false;

    public int[] listViewPos = new int[] { -1, -1 };
    public int copyFrom = -1, copyTo = 0;
    public int horizontalPos = 0;
    public int findResultPos = -1;
    public boolean findPosIsValid = false;
    public boolean searchEnabled = false;
    public String searchString = "";
    public boolean searchCaseSensitive = false;

    public int lineBreak = -1;
    public int browseMode = -1;
    public boolean showLineNo = true;

    ViewedFileListItem() {
    };

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        file_path = in.readUTF();
        file_name = in.readUTF();
        listViewPos[0] = in.readInt();
        listViewPos[1] = in.readInt();
        copyFrom = in.readInt();
        copyTo = in.readInt();
        horizontalPos = in.readInt();
        findResultPos = in.readInt();
        findPosIsValid = in.readBoolean();
        searchEnabled = in.readBoolean();
        searchString = in.readUTF();
        searchCaseSensitive = in.readBoolean();

        lineBreak = in.readInt();
        browseMode = in.readInt();
        showLineNo = in.readBoolean();

        encodeName = in.readUTF();
        viewerParmsInitRequired = in.readBoolean();
        viewerParmsRestoreRequired = in.readBoolean();

    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(file_path);
        out.writeUTF(file_name);
        out.writeInt(listViewPos[0]);
        out.writeInt(listViewPos[1]);
        out.writeInt(copyFrom);
        out.writeInt(copyTo);
        out.writeInt(horizontalPos);
        out.writeInt(findResultPos);
        out.writeBoolean(findPosIsValid);
        out.writeBoolean(searchEnabled);
        out.writeUTF(searchString);
        out.writeBoolean(searchCaseSensitive);

        out.writeInt(lineBreak);
        out.writeInt(browseMode);
        out.writeBoolean(showLineNo);

        out.writeUTF(encodeName);
        out.writeBoolean(viewerParmsInitRequired);
        out.writeBoolean(viewerParmsRestoreRequired);
    }

}

//class FragmentViewerHolder implements Externalizable{
//   private static final long serialVersionUID = 1L;
//   public String file_path="";
//   public String file_name="";
//   
//   public int[] listViewPos=new int[]{-1,-1};
//   public int copyFrom=-1, copyTo=0;
//   public int horizontalPos=0;
//   public int findResultPos=-1;
//   public boolean findPosIsValid=false;
//   public boolean searchEnabled=false;
//   public String searchString="";
//   public boolean searchCaseSensitive=false;
//
//   public int lineBreak=-1;
//   public int browseMode=-1;
//   public boolean showLineNo=true;
//   
//   FragmentViewerHolder() {};
//   
//   @Override
//   public void readExternal(ObjectInput in) throws IOException,
//         ClassNotFoundException {
//      file_path=in.readUTF();
//      file_name=in.readUTF();
//      listViewPos[0]=in.readInt();
//      listViewPos[1]=in.readInt();
//      copyFrom=in.readInt();
//      copyTo=in.readInt();
//      horizontalPos=in.readInt();
//      findResultPos=in.readInt();
//      findPosIsValid=in.readBoolean();
//      searchEnabled=in.readBoolean();
//      searchString=in.readUTF();
//      searchCaseSensitive=in.readBoolean();
//
//      lineBreak=in.readInt();
//      browseMode=in.readInt();
//      showLineNo=in.readBoolean();
//   }
//   @Override
//   public void writeExternal(ObjectOutput out) throws IOException {
//      out.writeUTF(file_path);
//      out.writeUTF(file_name);
//      out.writeInt(listViewPos[0]);
//      out.writeInt(listViewPos[1]);
//      out.writeInt(copyFrom);
//      out.writeInt(copyTo);
//      out.writeInt(horizontalPos);
//      out.writeInt(findResultPos);
//      out.writeBoolean(findPosIsValid);
//      out.writeBoolean(searchEnabled);
//      out.writeUTF(searchString);
//      out.writeBoolean(searchCaseSensitive);
//
//      out.writeInt(lineBreak);
//      out.writeInt(browseMode);
//      out.writeBoolean(showLineNo);
//   }
//};