Added ActionBarSherlock v4.0.3 library for use in the android application. ActionBar...
[debian/openrocket] / android-libraries / ActionBarSherlock / src / com / actionbarsherlock / internal / view / StandaloneActionMode.java
1 /*
2  * Copyright (C) 2010 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 package com.actionbarsherlock.internal.view;
17
18 import android.content.Context;
19 import android.view.View;
20 import android.view.accessibility.AccessibilityEvent;
21
22 import java.lang.ref.WeakReference;
23
24 import com.actionbarsherlock.internal.view.menu.MenuBuilder;
25 import com.actionbarsherlock.internal.view.menu.MenuPopupHelper;
26 import com.actionbarsherlock.internal.view.menu.SubMenuBuilder;
27 import com.actionbarsherlock.internal.widget.ActionBarContextView;
28 import com.actionbarsherlock.view.ActionMode;
29 import com.actionbarsherlock.view.Menu;
30 import com.actionbarsherlock.view.MenuInflater;
31 import com.actionbarsherlock.view.MenuItem;
32
33 public class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback {
34     private Context mContext;
35     private ActionBarContextView mContextView;
36     private ActionMode.Callback mCallback;
37     private WeakReference<View> mCustomView;
38     private boolean mFinished;
39     private boolean mFocusable;
40
41     private MenuBuilder mMenu;
42
43     public StandaloneActionMode(Context context, ActionBarContextView view,
44             ActionMode.Callback callback, boolean isFocusable) {
45         mContext = context;
46         mContextView = view;
47         mCallback = callback;
48
49         mMenu = new MenuBuilder(context).setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
50         mMenu.setCallback(this);
51         mFocusable = isFocusable;
52     }
53
54     @Override
55     public void setTitle(CharSequence title) {
56         mContextView.setTitle(title);
57     }
58
59     @Override
60     public void setSubtitle(CharSequence subtitle) {
61         mContextView.setSubtitle(subtitle);
62     }
63
64     @Override
65     public void setTitle(int resId) {
66         setTitle(mContext.getString(resId));
67     }
68
69     @Override
70     public void setSubtitle(int resId) {
71         setSubtitle(mContext.getString(resId));
72     }
73
74     @Override
75     public void setCustomView(View view) {
76         mContextView.setCustomView(view);
77         mCustomView = view != null ? new WeakReference<View>(view) : null;
78     }
79
80     @Override
81     public void invalidate() {
82         mCallback.onPrepareActionMode(this, mMenu);
83     }
84
85     @Override
86     public void finish() {
87         if (mFinished) {
88             return;
89         }
90         mFinished = true;
91
92         mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
93         mCallback.onDestroyActionMode(this);
94     }
95
96     @Override
97     public Menu getMenu() {
98         return mMenu;
99     }
100
101     @Override
102     public CharSequence getTitle() {
103         return mContextView.getTitle();
104     }
105
106     @Override
107     public CharSequence getSubtitle() {
108         return mContextView.getSubtitle();
109     }
110
111     @Override
112     public View getCustomView() {
113         return mCustomView != null ? mCustomView.get() : null;
114     }
115
116     @Override
117     public MenuInflater getMenuInflater() {
118         return new MenuInflater(mContext);
119     }
120
121     public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
122         return mCallback.onActionItemClicked(this, item);
123     }
124
125     public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
126     }
127
128     public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
129         if (!subMenu.hasVisibleItems()) {
130             return true;
131         }
132
133         new MenuPopupHelper(mContext, subMenu).show();
134         return true;
135     }
136
137     public void onCloseSubMenu(SubMenuBuilder menu) {
138     }
139
140     public void onMenuModeChange(MenuBuilder menu) {
141         invalidate();
142         mContextView.showOverflowMenu();
143     }
144
145     public boolean isUiFocusable() {
146         return mFocusable;
147     }
148 }