Android Open Source - sloop Bookmark Edit Activity






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;
// w  w  w .j  a v  a 2 s .  com
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.megginson.sloop.R;
import com.megginson.sloop.model.Bookmark;

/**
 * Form to create or edit a bookmark.
 * 
 * Will check the intent for "url" and "title" parameters.
 * 
 * @author David Megginson
 * 
 */
public class BookmarkEditActivity extends Activity {

  /**
   * Name of the intent extra for a new bookmark URL.
   */
  public final static String PARAM_URL = "url";

  private Bookmark mBookmark;

  private EditText mTitleField;

  private EditText mUrlField;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookmark_edit);

    doInitializeBookmark();

    setupForm();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_bookmark_edit, menu);
    return true;
  }

  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_save:
      doSubmitForm();
      return true;
    case R.id.menu_help:
      ActivitiesUtil.doHelp(this);
      return true;
    default:
      return super.onMenuItemSelected(featureId, item);
    }
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      doCancel();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  /**
   * Set up the bookmark form.
   */
  private void setupForm() {
    // fill in the title field, if supplied
    mTitleField = (EditText) findViewById(R.id.field_bookmark_title);
    mTitleField.setText(mBookmark.getTitle());

    // fill in the URL field, if supplied
    mUrlField = (EditText) findViewById(R.id.field_bookmark_url);
    mUrlField.setText(mBookmark.getUrl());
  }

  /**
   * Action: initialize the bookmark from the intent.
   */
  private void doInitializeBookmark() {
    Intent intent = getIntent();
    if (intent != null) {
      String url = intent.getStringExtra(PARAM_URL);
      String title = getSharedPreferences(
          BookmarkListActivity.PREFERENCE_GROUP_BOOKMARKS,
          MODE_PRIVATE).getString(url, url);
      mBookmark = new Bookmark(url, title);
    } else {
      mBookmark = new Bookmark();
    }

  }

  /**
   * Action: save the bookmark from the URL field.
   */
  private void doSubmitForm() {
    // TODO validate
    mBookmark.setUrl(mUrlField.getText().toString());
    mBookmark.setTitle(mTitleField.getText().toString());
    doSaveAndExit();
  }

  /**
   * Action: cancel any changes and exit the activity.
   */
  private void doCancel() {
    Toast.makeText(this, R.string.msg_bookmark_canceled, Toast.LENGTH_LONG)
        .show();
    finish();
  }

  /**
   * Action: save the bookmark and exit the activity.
   */
  private void doSaveAndExit() {
    Toast.makeText(this, R.string.msg_bookmark_saved, Toast.LENGTH_LONG)
        .show();
    SharedPreferences.Editor editor = getSharedPreferences(
        BookmarkListActivity.PREFERENCE_GROUP_BOOKMARKS, MODE_PRIVATE)
        .edit();
    editor.putString(mBookmark.getUrl(), mBookmark.getTitle());
    editor.commit();
    ActivitiesUtil.doHideKeyboard(this, mTitleField);
    finish();
  }

}




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