mobile/android/base/widget/GeckoPopupMenu.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial