ListMenuItemView.java revision 696cba573e651b0e4f18a4718627c8ccecb3bda0
1/*
2 * Copyright (C) 2006 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
17package com.android.internal.view.menu;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.widget.CheckBox;
26import android.widget.CompoundButton;
27import android.widget.ImageView;
28import android.widget.LinearLayout;
29import android.widget.RadioButton;
30import android.widget.TextView;
31
32/**
33 * The item view for each item in the ListView-based MenuViews.
34 */
35public class ListMenuItemView extends LinearLayout implements MenuView.ItemView {
36    private MenuItemImpl mItemData;
37
38    private ImageView mIconView;
39    private RadioButton mRadioButton;
40    private TextView mTitleView;
41    private CheckBox mCheckBox;
42    private TextView mShortcutView;
43
44    private Drawable mBackground;
45    private int mTextAppearance;
46    private Context mTextAppearanceContext;
47    private boolean mPreserveIconSpacing;
48
49    private int mMenuType;
50
51    private LayoutInflater mInflater;
52
53    public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
54        super(context, attrs);
55
56        TypedArray a =
57            context.obtainStyledAttributes(
58                attrs, com.android.internal.R.styleable.MenuView, defStyle, 0);
59
60        mBackground = a.getDrawable(com.android.internal.R.styleable.MenuView_itemBackground);
61        mTextAppearance = a.getResourceId(com.android.internal.R.styleable.
62                                          MenuView_itemTextAppearance, -1);
63        mPreserveIconSpacing = a.getBoolean(
64                com.android.internal.R.styleable.MenuView_preserveIconSpacing, false);
65        mTextAppearanceContext = context;
66
67        a.recycle();
68    }
69
70    public ListMenuItemView(Context context, AttributeSet attrs) {
71        this(context, attrs, 0);
72    }
73
74    @Override
75    protected void onFinishInflate() {
76        super.onFinishInflate();
77
78        setBackgroundDrawable(mBackground);
79
80        mTitleView = (TextView) findViewById(com.android.internal.R.id.title);
81        if (mTextAppearance != -1) {
82            mTitleView.setTextAppearance(mTextAppearanceContext,
83                                         mTextAppearance);
84        }
85
86        mShortcutView = (TextView) findViewById(com.android.internal.R.id.shortcut);
87    }
88
89    public void initialize(MenuItemImpl itemData, int menuType) {
90        mItemData = itemData;
91        mMenuType = menuType;
92
93        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
94
95        setTitle(itemData.getTitleForItemView(this));
96        setCheckable(itemData.isCheckable());
97        setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
98        setIcon(itemData.getIcon());
99        setEnabled(itemData.isEnabled());
100    }
101
102    public void setTitle(CharSequence title) {
103        if (title != null) {
104            mTitleView.setText(title);
105
106            if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE);
107        } else {
108            if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE);
109        }
110    }
111
112    public MenuItemImpl getItemData() {
113        return mItemData;
114    }
115
116    public void setCheckable(boolean checkable) {
117
118        if (!checkable && mRadioButton == null && mCheckBox == null) {
119            return;
120        }
121
122        if (mRadioButton == null) {
123            insertRadioButton();
124        }
125        if (mCheckBox == null) {
126            insertCheckBox();
127        }
128
129        // Depending on whether its exclusive check or not, the checkbox or
130        // radio button will be the one in use (and the other will be otherCompoundButton)
131        final CompoundButton compoundButton;
132        final CompoundButton otherCompoundButton;
133
134        if (mItemData.isExclusiveCheckable()) {
135            compoundButton = mRadioButton;
136            otherCompoundButton = mCheckBox;
137        } else {
138            compoundButton = mCheckBox;
139            otherCompoundButton = mRadioButton;
140        }
141
142        if (checkable) {
143            compoundButton.setChecked(mItemData.isChecked());
144
145            final int newVisibility = checkable ? VISIBLE : GONE;
146            if (compoundButton.getVisibility() != newVisibility) {
147                compoundButton.setVisibility(newVisibility);
148            }
149
150            // Make sure the other compound button isn't visible
151            if (otherCompoundButton.getVisibility() != GONE) {
152                otherCompoundButton.setVisibility(GONE);
153            }
154        } else {
155            mCheckBox.setVisibility(GONE);
156            mRadioButton.setVisibility(GONE);
157        }
158    }
159
160    public void setChecked(boolean checked) {
161        CompoundButton compoundButton;
162
163        if (mItemData.isExclusiveCheckable()) {
164            if (mRadioButton == null) {
165                insertRadioButton();
166            }
167            compoundButton = mRadioButton;
168        } else {
169            if (mCheckBox == null) {
170                insertCheckBox();
171            }
172            compoundButton = mCheckBox;
173        }
174
175        compoundButton.setChecked(checked);
176    }
177
178    public void setShortcut(boolean showShortcut, char shortcutKey) {
179        final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
180                ? VISIBLE : GONE;
181
182        if (newVisibility == VISIBLE) {
183            mShortcutView.setText(mItemData.getShortcutLabel());
184        }
185
186        if (mShortcutView.getVisibility() != newVisibility) {
187            mShortcutView.setVisibility(newVisibility);
188        }
189    }
190
191    public void setIcon(Drawable icon) {
192        final boolean showIcon = mItemData.shouldShowIcon();
193        if (!showIcon && !mPreserveIconSpacing) {
194            return;
195        }
196
197        if (mIconView == null && icon == null) {
198            return;
199        }
200
201        if (mIconView == null) {
202            insertIconView();
203        }
204
205        if (icon != null || mPreserveIconSpacing) {
206            mIconView.setImageDrawable(showIcon ? icon : null);
207
208            if (mIconView.getVisibility() != VISIBLE) {
209                mIconView.setVisibility(VISIBLE);
210            }
211        } else {
212            mIconView.setVisibility(GONE);
213        }
214    }
215
216    private void insertIconView() {
217        LayoutInflater inflater = getInflater();
218        mIconView = (ImageView) inflater.inflate(com.android.internal.R.layout.list_menu_item_icon,
219                this, false);
220        addView(mIconView, 0);
221    }
222
223    private void insertRadioButton() {
224        LayoutInflater inflater = getInflater();
225        mRadioButton =
226                (RadioButton) inflater.inflate(com.android.internal.R.layout.list_menu_item_radio,
227                this, false);
228        addView(mRadioButton);
229    }
230
231    private void insertCheckBox() {
232        LayoutInflater inflater = getInflater();
233        mCheckBox =
234                (CheckBox) inflater.inflate(com.android.internal.R.layout.list_menu_item_checkbox,
235                this, false);
236        addView(mCheckBox);
237    }
238
239    public boolean prefersCondensedTitle() {
240        return false;
241    }
242
243    public boolean showsIcon() {
244        return false;
245    }
246
247    private LayoutInflater getInflater() {
248        if (mInflater == null) {
249            mInflater = LayoutInflater.from(mContext);
250        }
251        return mInflater;
252    }
253}
254