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