/*
 * 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;
import net.micode.notes.data.Notes.NoteColumns;

                                        //将cursor对象数据加载到界面的适配器
public class FoldersListAdapter extends CursorAdapter {//将文件夹数据转换成视图
    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
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {//创建新的视图
        return new FolderListItem(context);//创建一个新的FolderListItem视图
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {//绑定视图与数据
        if (view instanceof FolderListItem) {//判断视图是否为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);//将文件夹名称绑定到FolderListItem视图
        }
    }

    public String getFolderName(Context context, int position) {//根据位置找到对应文件夹的名称
        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);
    }
                                        //布局管理器 之一，属于 ViewGroup 的子类，用于 将子View按照线性方向排列
    private class FolderListItem extends LinearLayout {//文件夹列表项视图
        private TextView mName;
        public FolderListItem(Context context) {
            super(context);
            inflate(context, R.layout.folder_list_item, this);//对 LayoutInflater.inflate()的简化
            //将R.layout.folder_list_item布局文件实例化并添加到当前视图（FolderListItem）中
            mName = (TextView) findViewById(R.id.tv_folder_name);//获取布局文件中的TextView控件
        }

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

}
