package com.actionbarsherlock.internal.view.menu;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.actionbarsherlock.R;
public class extends LinearLayout implements MenuView.ItemView {
private MenuItemImpl mItemData;
private ImageView mIconView;
private RadioButton mRadioButton;
private TextView mTitleView;
private CheckBox mCheckBox;
private TextView mShortcutView;
private Drawable mBackground;
private int mTextAppearance;
private Context mTextAppearanceContext;
private boolean mPreserveIconSpacing;
private LayoutInflater mInflater;
private boolean mForceShowIcon;
final Context mContext;
public (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
mContext = context;
TypedArray a =
context.obtainStyledAttributes(
attrs, R.styleable.SherlockMenuView, defStyle, 0);
mBackground = a.getDrawable(R.styleable.SherlockMenuView_itemBackground);
mTextAppearance = a.getResourceId(R.styleable.
SherlockMenuView_itemTextAppearance, -1);
mPreserveIconSpacing = a.getBoolean(
R.styleable.SherlockMenuView_preserveIconSpacing, false);
mTextAppearanceContext = context;
a.recycle();
}
public (Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
@Override
super.onFinishInflate();
setBackgroundDrawable(mBackground);
mTitleView = (TextView) findViewById(R.id.abs__title);
if (mTextAppearance != -1) {
mTitleView.setTextAppearance(mTextAppearanceContext,
mTextAppearance);
}
mShortcutView = (TextView) findViewById(R.id.abs__shortcut);
}
public void (MenuItemImpl itemData, int menuType) {
mItemData = itemData;
setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
setTitle(itemData.getTitleForItemView(this));
setCheckable(itemData.isCheckable());
setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
setIcon(itemData.getIcon());
setEnabled(itemData.isEnabled());
}
mPreserveIconSpacing = mForceShowIcon = forceShow;
}
public void setTitle(CharSequence title) {
if (title != null) {
mTitleView.setText(title);
if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE);
} else {
if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE);
}
}
return mItemData;
}
if (!checkable && mRadioButton == null && mCheckBox == null) {
return;
}
if (mRadioButton == null) {
insertRadioButton();
}
if (mCheckBox == null) {
insertCheckBox();
}
final CompoundButton compoundButton;
final CompoundButton otherCompoundButton;
if (mItemData.isExclusiveCheckable()) {
compoundButton = mRadioButton;
otherCompoundButton = mCheckBox;
} else {
compoundButton = mCheckBox;
otherCompoundButton = mRadioButton;
}
if (checkable) {
compoundButton.setChecked(mItemData.isChecked());
final int newVisibility = checkable ? VISIBLE : GONE;
if (compoundButton.getVisibility() != newVisibility) {
compoundButton.setVisibility(newVisibility);
}
if (otherCompoundButton.getVisibility() != GONE) {
otherCompoundButton.setVisibility(GONE);
}
} else {
mCheckBox.setVisibility(GONE);
mRadioButton.setVisibility(GONE);
}
}
CompoundButton compoundButton;
if (mItemData.isExclusiveCheckable()) {
if (mRadioButton == null) {
insertRadioButton();
}
compoundButton = mRadioButton;
} else {
if (mCheckBox == null) {
insertCheckBox();
}
compoundButton = mCheckBox;
}
compoundButton.setChecked(checked);
}
public void setShortcut(
boolean showShortcut,
char shortcutKey) {
final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
? VISIBLE : GONE;
if (newVisibility == VISIBLE) {
mShortcutView.setText(mItemData.getShortcutLabel());
}
if (mShortcutView.getVisibility() != newVisibility) {
mShortcutView.setVisibility(newVisibility);
}
}
public void setIcon(Drawable icon) {
final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
if (!showIcon && !mPreserveIconSpacing) {
return;
}
if (mIconView == null && icon == null && !mPreserveIconSpacing) {
return;
}
if (mIconView == null) {
insertIconView();
}
if (icon != null || mPreserveIconSpacing) {
mIconView.setImageDrawable(showIcon ? icon : null);
if (mIconView.getVisibility() != VISIBLE) {
mIconView.setVisibility(VISIBLE);
}
} else {
mIconView.setVisibility(GONE);
}
}
@Override
protected void onMeasure(
int widthMeasureSpec,
int heightMeasureSpec) {
if (mIconView != null && mPreserveIconSpacing) {
ViewGroup.LayoutParams lp = getLayoutParams();
LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
if (lp.height > 0 && iconLp.width <= 0) {
iconLp.width = lp.height;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
LayoutInflater inflater = getInflater();
mIconView = (ImageView) inflater.inflate(R.layout.abs__list_menu_item_icon,
this, false);
addView(mIconView, 0);
}
LayoutInflater inflater = getInflater();
mRadioButton =
(RadioButton) inflater.inflate(R.layout.abs__list_menu_item_radio,
this, false);
addView(mRadioButton);
}
LayoutInflater inflater = getInflater();
mCheckBox =
(CheckBox) inflater.inflate(R.layout.abs__list_menu_item_checkbox,
this, false);
addView(mCheckBox);
}
return false;
}
return mForceShowIcon;
}
if (mInflater == null) {
mInflater = LayoutInflater.from(mContext);
}
return mInflater;
}
}