/*
 * 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;//这个类包含在 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;
import net.micode.notes.data.Notes.NoteColumns;//便签栏


public class FoldersListAdapter extends CursorAdapter {//文件夹列表适配器，用于显示文件夹列表的视图
    public static final String [] PROJECTION = {//调用数据库中便签的ID和片段
        NoteColumns.ID,
        NoteColumns.SNIPPET
    };

    public static final int ID_COLUMN   = 0;//定义常量，文件夹名称栏编号
    public static final int NAME_COLUMN = 1;//定义常量，文件夹名称

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

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {//创建一个文件夹，对于各文件夹中子标签的初始化
        return new FolderListItem(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {//将各个布局文件绑定起来，形成一个整体
        if (view instanceof FolderListItem) {//判断视图是否是文件夹列表的一项
            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);
        }
    }

    public String getFolderName(Context context, int position) {//根据数据库中标签的ID得到标签的各项内容
        Cursor cursor = (Cursor) getItem(position);//获取游标
        return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
                .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);//选定当前文件夹
    }

    private class FolderListItem extends LinearLayout {//设置文件夹项目列表的名称
        private TextView mName;

        public FolderListItem(Context context) {//文件夹项的构造函数
            super(context);
            inflate(context, R.layout.folder_list_item, this);//将一个xml中定义的布局找出来
            mName = (TextView) findViewById(R.id.tv_folder_name);
        }

        public void bind(String name) {//绑定，设置文件标题
            mName.setText(name);
        }
    }

}
