Android Open Source - sloop Address Action Provider






From Project

Back to project page sloop.

License

The source code is released under:

NON-LICENSE The Sloop data-browser source code is hereby released into the Public Domain. The original author, David Megginson, Megginson Technologies Ltd., and Acclar Open Ltd. provide no warranty:...

If you think the Android project sloop 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

package com.megginson.sloop.activities;
//from   w ww. j  a v  a2  s. c om
import android.content.Context;
import android.view.ActionProvider;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.megginson.sloop.R;

/**
 * Collapsible URL address field for the action bar.
 * 
 * This action provider edits and displays URLs: it consists of a text field and
 * a clear/cancel button.
 * 
 * @author David Megginson
 */
public class AddressActionProvider extends ActionProvider {

  // Contextual items
  private MainActivity mActivity;
  private MenuItem mMenuItem;
  private Context mContext;

  // Child views
  private View mContentView;
  private EditText mUrlField;
  private Button mCancelButton;

  // Internal state
  private String mUrl;

  /**
   * Standard constructor.
   * 
   * @param context
   *            the context for the action provider.
   */
  public AddressActionProvider(Context context) {
    super(context);
    mContext = context;
    System.err.println(context);
  }

  /**
   * Setup the action provider with information about the main activity.
   * 
   * We can't call the constructor directly, so we have to set extra arguments
   * here instead.
   * 
   * @param activity
   *            the referring activity
   * @param menuItem
   *            the menu item that owns this provider.
   */
  public void setUp(MainActivity activity, MenuItem menuItem) {
    mActivity = activity;
    mMenuItem = menuItem;
  }

  @Override
  public View onCreateActionView() {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    mContentView = inflater.inflate(R.layout.widget_address_bar, null);
    setupUrlField();
    setupCancelButton();
    return mContentView;
  }

  /**
   * Set the URL for the action provider.
   * 
   * We can't simply use the state of the EditText view, because we need to be
   * able to restore the URL on a cancel.
   * 
   * @param url
   *            the URL in the address bar.
   */
  public void setUrl(String url) {
    mUrl = url;
    doUpdateStatus();
  }

  /**
   * Set up the text field with a callback.
   */
  private void setupUrlField() {
    mUrlField = (EditText) mContentView.findViewById(R.id.urlField);
    // Enter key submits the URL
    mUrlField
        .setOnEditorActionListener(new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView v, int actionId,
              KeyEvent event) {
            mActivity.doLoadDataCollection(mUrl, true);
            return true;
          }
        });

  }

  /**
   * Set up the cancel button with a callback.
   */
  private void setupCancelButton() {
    mCancelButton = (Button) mContentView
        .findViewById(R.id.button_url_clear);
    // Cancel button clears or closes
    mCancelButton.setOnClickListener(new Button.OnClickListener() {
      @Override
      public void onClick(View v) {
        doCancel();
      }
    });
  }

  /**
   * Update the loading status display.
   */
  private void doUpdateStatus() {
    // update the URL field
    if (mUrlField != null) {
      mUrlField.setText(mUrl);
    }
  }

  /**
   * Do a cancel action, depending on context.
   * 
   * If the field is not empty, empty it; otherwise, close the action
   * provider.
   */
  private void doCancel() {
    if (mUrlField.getText() != null && mUrlField.getText().length() > 0) {
      mUrlField.setText(null);
    } else {
      doUpdateStatus();
      ActivitiesUtil.doHideKeyboard(mContext, mUrlField);
      mMenuItem.collapseActionView();
    }
  }

}




Java Source Code List

com.megginson.sloop.activities.ActivitiesUtil.java
com.megginson.sloop.activities.AddressActionProvider.java
com.megginson.sloop.activities.BookmarkEditActivity.java
com.megginson.sloop.activities.BookmarkListActivity.java
com.megginson.sloop.activities.InfoBarFragment.java
com.megginson.sloop.activities.MainActivity.java
com.megginson.sloop.activities.MainDisplayFragment.java
com.megginson.sloop.activities.TextFilterFragment.java
com.megginson.sloop.activities.package-info.java
com.megginson.sloop.model.Bookmark.java
com.megginson.sloop.model.DataCollection.java
com.megginson.sloop.model.DataEntry.java
com.megginson.sloop.model.DataRecord.java
com.megginson.sloop.model.Util.java
com.megginson.sloop.model.ValueFilter.java
com.megginson.sloop.model.impl.ContainsStringFilter.java
com.megginson.sloop.model.impl.DataCollectionIO.java
com.megginson.sloop.model.impl.DataCollectionImpl.java
com.megginson.sloop.model.impl.DataEntryImpl.java
com.megginson.sloop.model.impl.DataRecordImpl.java
com.megginson.sloop.model.impl.EqualsStringFilter.java
com.megginson.sloop.model.impl.package-info.java
com.megginson.sloop.model.package-info.java
com.megginson.sloop.ui.BookmarkListAdapter.java
com.megginson.sloop.ui.DataCollectionLoader.java
com.megginson.sloop.ui.DataCollectionPagerAdapter.java
com.megginson.sloop.ui.DataCollectionResult.java
com.megginson.sloop.ui.DataRecordFragment.java
com.megginson.sloop.ui.DataRecordListAdapter.java
com.megginson.sloop.ui.package-info.java