|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.widget; |
|
7 |
|
8 import org.mozilla.gecko.menu.GeckoMenu; |
|
9 import org.mozilla.gecko.menu.GeckoMenuInflater; |
|
10 import org.mozilla.gecko.menu.MenuPanel; |
|
11 import org.mozilla.gecko.menu.MenuPopup; |
|
12 |
|
13 import android.content.Context; |
|
14 import android.view.Menu; |
|
15 import android.view.MenuInflater; |
|
16 import android.view.MenuItem; |
|
17 import android.view.View; |
|
18 |
|
19 /** |
|
20 * A PopupMenu that uses the custom GeckoMenu. This menu is |
|
21 * usually tied to an anchor, and show as a dropdrown from the anchor. |
|
22 */ |
|
23 public class GeckoPopupMenu implements GeckoMenu.Callback, |
|
24 GeckoMenu.MenuPresenter { |
|
25 |
|
26 // An interface for listeners for dismissal. |
|
27 public static interface OnDismissListener { |
|
28 public boolean onDismiss(GeckoMenu menu); |
|
29 } |
|
30 |
|
31 // An interface for listeners for menu item click events. |
|
32 public static interface OnMenuItemClickListener { |
|
33 public boolean onMenuItemClick(MenuItem item); |
|
34 } |
|
35 |
|
36 private View mAnchor; |
|
37 |
|
38 private MenuPopup mMenuPopup; |
|
39 private MenuPanel mMenuPanel; |
|
40 |
|
41 private GeckoMenu mMenu; |
|
42 private GeckoMenuInflater mMenuInflater; |
|
43 |
|
44 private OnDismissListener mDismissListener; |
|
45 private OnMenuItemClickListener mClickListener; |
|
46 |
|
47 public GeckoPopupMenu(Context context) { |
|
48 initialize(context, null); |
|
49 } |
|
50 |
|
51 public GeckoPopupMenu(Context context, View anchor) { |
|
52 initialize(context, anchor); |
|
53 } |
|
54 |
|
55 /** |
|
56 * This method creates an empty menu and attaches the necessary listeners. |
|
57 * If an anchor is supplied, it is stored as well. |
|
58 */ |
|
59 private void initialize(Context context, View anchor) { |
|
60 mMenu = new GeckoMenu(context, null); |
|
61 mMenu.setCallback(this); |
|
62 mMenu.setMenuPresenter(this); |
|
63 mMenuInflater = new GeckoMenuInflater(context); |
|
64 |
|
65 mMenuPopup = new MenuPopup(context); |
|
66 mMenuPanel = new MenuPanel(context, null); |
|
67 |
|
68 mMenuPanel.addView(mMenu); |
|
69 mMenuPopup.setPanelView(mMenuPanel); |
|
70 |
|
71 setAnchor(anchor); |
|
72 } |
|
73 |
|
74 /** |
|
75 * Returns the menu that is current being shown. |
|
76 * |
|
77 * @return The menu being shown. |
|
78 */ |
|
79 public Menu getMenu() { |
|
80 return mMenu; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Returns the menu inflater that was used to create the menu. |
|
85 * |
|
86 * @return The menu inflater used. |
|
87 */ |
|
88 public MenuInflater getMenuInflater() { |
|
89 return mMenuInflater; |
|
90 } |
|
91 |
|
92 /** |
|
93 * Inflates a menu resource to the menu using the menu inflater. |
|
94 * |
|
95 * @param menuRes The menu resource to be inflated. |
|
96 */ |
|
97 public void inflate(int menuRes) { |
|
98 if (menuRes > 0) { |
|
99 mMenuInflater.inflate(menuRes, mMenu); |
|
100 } |
|
101 } |
|
102 |
|
103 /** |
|
104 * Set a different anchor after the menu is inflated. |
|
105 * |
|
106 * @param anchor The new anchor for the popup. |
|
107 */ |
|
108 public void setAnchor(View anchor) { |
|
109 mAnchor = anchor; |
|
110 } |
|
111 |
|
112 public void setOnDismissListener(OnDismissListener listener) { |
|
113 mDismissListener = listener; |
|
114 } |
|
115 |
|
116 public void setOnMenuItemClickListener(OnMenuItemClickListener listener) { |
|
117 mClickListener = listener; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Show the inflated menu. |
|
122 */ |
|
123 public void show() { |
|
124 if (!mMenuPopup.isShowing()) |
|
125 mMenuPopup.showAsDropDown(mAnchor); |
|
126 } |
|
127 |
|
128 /** |
|
129 * Hide the inflated menu. |
|
130 */ |
|
131 public void dismiss() { |
|
132 if (mMenuPopup.isShowing()) { |
|
133 mMenuPopup.dismiss(); |
|
134 |
|
135 if (mDismissListener != null) |
|
136 mDismissListener.onDismiss(mMenu); |
|
137 } |
|
138 } |
|
139 |
|
140 @Override |
|
141 public boolean onMenuItemSelected(MenuItem item) { |
|
142 if (mClickListener != null) |
|
143 return mClickListener.onMenuItemClick(item); |
|
144 |
|
145 return false; |
|
146 } |
|
147 |
|
148 @Override |
|
149 public void openMenu() { |
|
150 show(); |
|
151 } |
|
152 |
|
153 @Override |
|
154 public void showMenu(View menu) { |
|
155 mMenuPanel.removeAllViews(); |
|
156 mMenuPanel.addView(menu); |
|
157 |
|
158 openMenu(); |
|
159 } |
|
160 |
|
161 @Override |
|
162 public void closeMenu() { |
|
163 dismiss(); |
|
164 } |
|
165 } |