/*
 * 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.view.Menu;
import android.view.MenuItem;//菜单条款
import android.view.View;
import android.view.View.OnClickListener;//监听点击
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

import net.micode.notes.R;//继续引用工具包

public class DropdownMenu {//声明一个下拉菜单
    private Button mButton;//定义按钮控件
    private PopupMenu mPopupMenu;//定义弹出式菜单
    private Menu mMenu;//定义菜单

    public DropdownMenu(Context context, Button button, int menuId) {//构造函数
        mButton = button;//设置button的样式
        mButton.setBackgroundResource(R.drawable.dropdown_icon);//设置其背景资源为下拉菜单的图标
        mPopupMenu = new PopupMenu(context, mButton);//初始化下拉菜单由mButton触发
        mMenu = mPopupMenu.getMenu(); //根据ID来确认menu的内容选项
        mPopupMenu.getMenuInflater().inflate(menuId, mMenu);//使用菜单项填充弹出式菜单的视图
        mButton.setOnClickListener(new OnClickListener() {//点击按钮控件对象mButton时,弹出显示菜单
            public void onClick(View v) {
                mPopupMenu.show();//展示下拉的菜单
            }
        });
    }

    public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {//设置下拉菜单的点击监听方法
        if (mPopupMenu != null) {//对非空的菜单的具体条目设置监听
            mPopupMenu.setOnMenuItemClickListener(listener);
        }
    }

    public MenuItem findItem(int id) {//根据id获取菜单所需的选项
        return mMenu.findItem(id);
    }

    public void setTitle(CharSequence title) {//根据索引搜索菜单需要的选项
        mButton.setText(title);
    }
}
