OSDN Git Service

Merge "Import translations. DO NOT MERGE" into gb-ub-photos-arches
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ui / CustomMenu.java
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.gallery3d.ui;
18
19 import android.content.Context;
20 import android.view.Menu;
21 import android.view.MenuItem;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.Button;
25 import android.widget.PopupMenu;
26 import android.widget.PopupMenu.OnMenuItemClickListener;
27
28 import java.util.ArrayList;
29
30 public class CustomMenu implements OnMenuItemClickListener {
31     @SuppressWarnings("unused")
32     private static final String TAG = "FilterMenu";
33
34     public static class DropDownMenu {
35         private Button mButton;
36         private PopupMenu mPopupMenu;
37         private Menu mMenu;
38
39         public DropDownMenu(Context context, Button button, int menuId,
40                 OnMenuItemClickListener listener) {
41             mButton = button;
42             mPopupMenu = new PopupMenu(context, mButton);
43             mMenu = mPopupMenu.getMenu();
44             mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
45             mPopupMenu.setOnMenuItemClickListener(listener);
46             mButton.setOnClickListener(new OnClickListener() {
47                 @Override
48                 public void onClick(View v) {
49                     mPopupMenu.show();
50                 }
51             });
52         }
53
54         public MenuItem findItem(int id) {
55             return mMenu.findItem(id);
56         }
57
58         public void setTitle(CharSequence title) {
59             mButton.setText(title);
60         }
61     }
62
63     private Context mContext;
64     private ArrayList<DropDownMenu> mMenus;
65     private OnMenuItemClickListener mListener;
66
67     public CustomMenu(Context context) {
68         mContext = context;
69         mMenus = new ArrayList<DropDownMenu>();
70     }
71
72     public DropDownMenu addDropDownMenu(Button button, int menuId) {
73         DropDownMenu menu = new DropDownMenu(mContext, button, menuId, this);
74         mMenus.add(menu);
75         return menu;
76     }
77
78     public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
79         mListener = listener;
80     }
81
82     @Override
83     public boolean onMenuItemClick(MenuItem item) {
84         if (mListener != null) {
85             return mListener.onMenuItemClick(item);
86         }
87         return false;
88     }
89 }