Android Open Source - GreenDroid Action Bar Item






From Project

Back to project page GreenDroid.

License

The source code is released under:

Apache License

If you think the Android project GreenDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
 *//  w ww  . ja v a  2  s.c o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package greendroid.widget;

import greendroid.graphics.drawable.ActionBarDrawable;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.cyrilmottier.android.greendroid.R;

/**
 * Base class representing an {@link ActionBarItem} used in {@link ActionBar}s.
 * The base implementation exposes a single Drawable as well as a content
 * description.
 * 
 * @author Cyril Mottier
 */
public abstract class ActionBarItem {

    /**
     * The Type specifies a large set of pre-defined {@link ActionBarItem}s that
     * may be added to an {@link ActionBar}.
     * 
     * @author Cyril Mottier
     */
    public enum Type {
        /**
         * A house. This type should only be used for actions going back to the
         * main screen/menu of an application.
         * 
         * @see R.drawable#gd_action_bar_home
         */
        GoHome,
        
        /**
         * A magnifying glass
         * 
         * @see R.drawable#gd_action_bar_search
         */
        Search,
        
        /**
         * A speech bubble
         * 
         * @see R.drawable#gd_action_bar_talk
         */
        Talk,
        
        /**
         * A sheet of paper with a pen
         * 
         * @see R.drawable#gd_action_bar_compose
         */
        Compose,
        
        /**
         * A dot with an arrow
         * 
         * @see R.drawable#gd_action_bar_export
         */
        Export,
        
        /**
         * A dot with two arrows
         * 
         * @see R.drawable#gd_action_bar_share
         */
        Share,
        
        /**
         * Two curved arrows
         * 
         * @see R.drawable#gd_action_bar_refresh
         */
        Refresh,
        
        /**
         * A camera
         * 
         * @see R.drawable#gd_action_bar_take_photo
         */
        TakePhoto,
        
        // PickPhoto, // Two pictures with an arrow
        
        /**
         * The traditional GMaps pin
         * 
         * @see R.drawable#gd_action_bar_locate
         */
        Locate,
        
        /**
         * A pencil
         * 
         * @see R.drawable#gd_action_bar_edit
         */
        Edit,
        
        /**
         * A plus sign
         * 
         * @see R.drawable#gd_action_bar_add
         */
        Add,
        
        /**
         * A star
         * 
         * @see R.drawable#gd_action_bar_star
         */
        Star,
        
        /**
         * Some variable-size horizontal bars
         * 
         * @see R.drawable#gd_action_bar_sort_by_size
         */
        SortBySize,
        
        /**
         * A-Z
         * 
         * @see R.drawable#gd_action_bar_sort_alpha
         */
        SortAlphabetically,
        
        /**
         * A surrounded dot
         * 
         * @see R.drawable#gd_action_bar_locate_myself
         */
        LocateMyself,
        
        /**
         * A compass
         * 
         * @see R.drawable#gd_action_bar_compass
         */
        Compass,
        
        /**
         * A plain circle with a question mark engraved in the center
         * 
         * @see R.drawable#gd_action_bar_help
         */
        Help,
        
        /**
         * A plain circle with a 'I' engraved in the center
         * 
         * @see R.drawable#gd_action_bar_info
         */
        Info,
        
        /**
         * The Android-like 'Settings' icon
         * 
         * @see R.drawable#gd_action_bar_settings
         */
        Settings,
        
        /**
         * Some horizontal bars.
         * 
         * @see R.drawable#gd_action_bar_list
         */
        List,
        
        /**
         * A trashcan. May be used for a "delete" action.
         * 
         * @see R.drawable#gd_action_bar_trashcan
         */
        Trashcan,
        
        /**
         * An eye. May be used for a "preview" action.
         * 
         * @see R.drawable#gd_action_bar_eye
         */
        Eye,
        
        /**
         * A group of 3 people.
         * 
         * @see R.drawable#gd_action_bar_all_friends
         */
        AllFriends,
        
        /**
         * A group of 2 people.
         * 
         * @see R.drawable#gd_action_bar_group
         */
        Group,
        
        /**
         * A polaroid-like picture.
         * 
         * @see R.drawable#gd_action_bar_gallery
         */
        Gallery,
        
        /**
         * A stack of polaroid-like pictures.
         * 
         * @see R.drawable#gd_action_bar_slideshow
         */
        Slideshow,
        
        /**
         * An envelope.
         * 
         * @see R.drawable#gd_action_bar_mail
         */
        Mail
    }

    protected Drawable mDrawable;

    protected CharSequence mContentDescription;
    protected View mItemView;

    protected Context mContext;
    protected ActionBar mActionBar;

    private int mItemId;

    void setActionBar(ActionBar actionBar) {
        mContext = actionBar.getContext();
        mActionBar = actionBar;
    }

    public Drawable getDrawable() {
        return mDrawable;
    }

    public ActionBarItem setDrawable(int drawableId) {
        return setDrawable(mContext.getResources().getDrawable(drawableId));
    }

    public ActionBarItem setDrawable(Drawable drawable) {
        if (drawable != mDrawable) {
            mDrawable = drawable;
            if (mItemView != null) {
                onDrawableChanged();
            }
        }
        return this;
    }

    public CharSequence getContentDescription() {
        return mContentDescription;
    }

    public ActionBarItem setContentDescription(int contentDescriptionId) {
        return setContentDescription(mContext.getString(contentDescriptionId));
    }

    public ActionBarItem setContentDescription(CharSequence contentDescription) {
        if (contentDescription != mContentDescription) {
            mContentDescription = contentDescription;
            if (mItemView != null) {
                onContentDescriptionChanged();
            }
        }
        return this;
    }

    public View getItemView() {
        if (mItemView == null) {
            mItemView = createItemView();
            prepareItemView();
        }
        return mItemView;
    }

    protected abstract View createItemView();

    protected void prepareItemView() {
    }

    protected void onDrawableChanged() {
    }

    protected void onContentDescriptionChanged() {
    }

    protected void onItemClicked() {
    }

    void setItemId(int itemId) {
        mItemId = itemId;
    }

    public int getItemId() {
        return mItemId;
    }

    static ActionBarItem createWithType(ActionBar actionBar, ActionBarItem.Type type) {

        int drawableId = 0;
        int descriptionId = 0;

        switch (type) {
            case GoHome:
                drawableId = R.drawable.gd_action_bar_home;
                descriptionId = R.string.gd_go_home;
                break;

            case Search:
                drawableId = R.drawable.gd_action_bar_search;
                descriptionId = R.string.gd_search;
                break;

            case Talk:
                drawableId = R.drawable.gd_action_bar_talk;
                descriptionId = R.string.gd_talk;
                break;

            case Compose:
                drawableId = R.drawable.gd_action_bar_compose;
                descriptionId = R.string.gd_compose;
                break;

            case Export:
                drawableId = R.drawable.gd_action_bar_export;
                descriptionId = R.string.gd_export;
                break;

            case Share:
                drawableId = R.drawable.gd_action_bar_share;
                descriptionId = R.string.gd_share;
                break;

            case Refresh:
                return actionBar.newActionBarItem(LoaderActionBarItem.class)
                        .setDrawable(new ActionBarDrawable(actionBar.getContext(), R.drawable.gd_action_bar_refresh))
                        .setContentDescription(R.string.gd_refresh);

            case TakePhoto:
                drawableId = R.drawable.gd_action_bar_take_photo;
                descriptionId = R.string.gd_take_photo;
                break;
            //
            // case PickPhoto:
            // drawableId = R.drawable.gd_action_bar_pick_photo;
            // descriptionId = R.string.gd_pick_photo;
            // break;

            case Locate:
                drawableId = R.drawable.gd_action_bar_locate;
                descriptionId = R.string.gd_locate;
                break;

            case Edit:
                drawableId = R.drawable.gd_action_bar_edit;
                descriptionId = R.string.gd_edit;
                break;

            case Add:
                drawableId = R.drawable.gd_action_bar_add;
                descriptionId = R.string.gd_add;
                break;

            case Star:
                drawableId = R.drawable.gd_action_bar_star;
                descriptionId = R.string.gd_star;
                break;

            case SortBySize:
                drawableId = R.drawable.gd_action_bar_sort_by_size;
                descriptionId = R.string.gd_sort_by_size;
                break;

            case SortAlphabetically:
                drawableId = R.drawable.gd_action_bar_sort_alpha;
                descriptionId = R.string.gd_sort_alpha;
                break;

            case LocateMyself:
                drawableId = R.drawable.gd_action_bar_locate_myself;
                descriptionId = R.string.gd_locate_myself;
                break;

            case Compass:
                drawableId = R.drawable.gd_action_bar_compass;
                descriptionId = R.string.gd_compass;
                break;

            case Help:
                drawableId = R.drawable.gd_action_bar_help;
                descriptionId = R.string.gd_help;
                break;

            case Info:
                drawableId = R.drawable.gd_action_bar_info;
                descriptionId = R.string.gd_info;
                break;

            case Settings:
                drawableId = R.drawable.gd_action_bar_settings;
                descriptionId = R.string.gd_settings;
                break;

            case List:
                drawableId = R.drawable.gd_action_bar_list;
                descriptionId = R.string.gd_list;
                break;

            case Trashcan:
                drawableId = R.drawable.gd_action_bar_trashcan;
                descriptionId = R.string.gd_trashcan;
                break;

            case Eye:
                drawableId = R.drawable.gd_action_bar_eye;
                descriptionId = R.string.gd_eye;
                break;

            case AllFriends:
                drawableId = R.drawable.gd_action_bar_all_friends;
                descriptionId = R.string.gd_all_friends;
                break;

            case Group:
                drawableId = R.drawable.gd_action_bar_group;
                descriptionId = R.string.gd_group;
                break;

            case Gallery:
                drawableId = R.drawable.gd_action_bar_gallery;
                descriptionId = R.string.gd_gallery;
                break;

            case Slideshow:
                drawableId = R.drawable.gd_action_bar_slideshow;
                descriptionId = R.string.gd_slideshow;
                break;

            case Mail:
                drawableId = R.drawable.gd_action_bar_mail;
                descriptionId = R.string.gd_mail;
                break;

            default:
                // Do nothing but return null
                return null;
        }

        final Drawable d = new ActionBarDrawable(actionBar.getContext(), drawableId);

        return actionBar.newActionBarItem(NormalActionBarItem.class).setDrawable(d).setContentDescription(descriptionId);
    }

}




Java Source Code List

com.cyrilmottier.android.gdcatalog.AboutActivity.java
com.cyrilmottier.android.gdcatalog.ActionBarActivity.java
com.cyrilmottier.android.gdcatalog.AsyncImageViewListActivity.java
com.cyrilmottier.android.gdcatalog.BasicItemActivity.java
com.cyrilmottier.android.gdcatalog.CatalogActivity.java
com.cyrilmottier.android.gdcatalog.CatalogApplication.java
com.cyrilmottier.android.gdcatalog.InfoTabActivity.java
com.cyrilmottier.android.gdcatalog.MapPinMapActivity.java
com.cyrilmottier.android.gdcatalog.PagedViewActivity.java
com.cyrilmottier.android.gdcatalog.QuickActionActivity.java
com.cyrilmottier.android.gdcatalog.SegmentedActivity.java
com.cyrilmottier.android.gdcatalog.SimpleAsyncImageViewActivity.java
com.cyrilmottier.android.gdcatalog.TabbedActionBarActivity.java
com.cyrilmottier.android.gdcatalog.TweakedItemViewActivity.java
com.cyrilmottier.android.gdcatalog.WebContentActivity.java
com.cyrilmottier.android.gdcatalog.XmlItemActivity.java
com.cyrilmottier.android.gdcatalog.widget.HeadedTextItemView.java
com.cyrilmottier.android.gdcatalog.widget.HeadedTextItem.java
greendroid.app.ActionBarActivity.java
greendroid.app.GDActivity.java
greendroid.app.GDApplication.java
greendroid.app.GDExpandableListActivity.java
greendroid.app.GDListActivity.java
greendroid.app.GDMapActivity.java
greendroid.app.GDTabActivity.java
greendroid.graphics.drawable.ActionBarDrawable.java
greendroid.graphics.drawable.DrawableStateSet.java
greendroid.graphics.drawable.MapPinDrawable.java
greendroid.image.ChainImageProcessor.java
greendroid.image.ImageCache.java
greendroid.image.ImageLoader.java
greendroid.image.ImageProcessor.java
greendroid.image.ImageRequest.java
greendroid.image.MaskImageProcessor.java
greendroid.image.ScaleImageProcessor.java
greendroid.util.Config.java
greendroid.util.GDUtils.java
greendroid.util.Md5Util.java
greendroid.util.Time.java
greendroid.widget.ActionBarHost.java
greendroid.widget.ActionBarItem.java
greendroid.widget.ActionBar.java
greendroid.widget.AsyncImageView.java
greendroid.widget.ItemAdapter.java
greendroid.widget.LoaderActionBarItem.java
greendroid.widget.NormalActionBarItem.java
greendroid.widget.PageIndicator.java
greendroid.widget.PagedAdapter.java
greendroid.widget.PagedView.java
greendroid.widget.QuickActionBar.java
greendroid.widget.QuickActionGrid.java
greendroid.widget.QuickActionWidget.java
greendroid.widget.QuickAction.java
greendroid.widget.SegmentedAdapter.java
greendroid.widget.SegmentedBar.java
greendroid.widget.SegmentedHost.java
greendroid.widget.item.DescriptionItem.java
greendroid.widget.item.DrawableItem.java
greendroid.widget.item.Item.java
greendroid.widget.item.LongTextItem.java
greendroid.widget.item.ProgressItem.java
greendroid.widget.item.SeparatorItem.java
greendroid.widget.item.SubtextItem.java
greendroid.widget.item.SubtitleItem.java
greendroid.widget.item.TextItem.java
greendroid.widget.item.ThumbnailItem.java
greendroid.widget.itemview.DescriptionItemView.java
greendroid.widget.itemview.DrawableItemView.java
greendroid.widget.itemview.ItemView.java
greendroid.widget.itemview.LongTextItemView.java
greendroid.widget.itemview.ProgressItemView.java
greendroid.widget.itemview.SeparatorItemView.java
greendroid.widget.itemview.SubtextItemView.java
greendroid.widget.itemview.SubtitleItemView.java
greendroid.widget.itemview.TextItemView.java
greendroid.widget.itemview.ThumbnailItemView.java