/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.micode.notes.ui;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.micode.notes.R;//引入资源文件
import net.micode.notes.data.Notes;//引入与Notes相关的数据处理类
import net.micode.notes.data.Notes.NoteColumns;//引入NOte数据表中的字段

//FoldersListAdapter 类是CursorAdapter的一个子类，用于提供文件夹数据的视图
public class FoldersListAdapter extends CursorAdapter {
    //定义查询结果的列，这里只用到ID和SNIPET列
    public static final String [] PROJECTION = {
        NoteColumns.ID,
        NoteColumns.SNIPPET
    };

    //定义列的索引，方便读取数据时使用
    public static final int ID_COLUMN   = 0;
    public static final int NAME_COLUMN = 1;

    //该类的构造函数
    public FoldersListAdapter(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    //必须重写的newView方法，用于创建新的列表项视图
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return new FolderListItem(context);//创建一个新的FoldersListItem实例
    }

    //用于将数据绑定到视图上
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        if (view instanceof FolderListItem) {//检查视图类型是否正确
            //根据ID列判断是否根文件夹，是则使用预定义字符串，否则使用数据库中的名字
            String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
                    .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
            ((FolderListItem) view).bind(folderName);//调用FolderListItem的bind方法绑定文件夹名称
        }
    }

    //辅助方法，根据位置获取文件夹名称
    public String getFolderName(Context context, int position) {
        Cursor cursor = (Cursor) getItem(position);//获取对应位置的游标
        //同样根据ID列判断文件夹名称
        return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
                .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
    }

    //FolderListItem是LinearLayout的一个内部子类，用于表示每一项文件夹
    private class FolderListItem extends LinearLayout {
        private TextView mName;//文件夹名称的Textview

        public FolderListItem(Context context) {
            super(context);//调用父类构造函数
            inflate(context, R.layout.folder_list_item, this);//填充布局
            mName = (TextView) findViewById(R.id.tv_folder_name);//获取布局中的TextView
        }

        public void bind(String name) {
            mName.setText(name);
        }//设置文件夹名称到Textview
    }

}
