Android Open Source - adventure.datetime Fragment Node






From Project

Back to project page adventure.datetime.

License

The source code is released under:

MIT License

If you think the Android project adventure.datetime 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 ca.cmput301f13t03.adventure_datetime.view.treeView;
/* ww w  . j ava  2 s.co m*/
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.Paint.Align;
import ca.cmput301f13t03.adventure_datetime.R;
import ca.cmput301f13t03.adventure_datetime.model.StoryFragment;

/**
 * Represents a story fragment on the tree view
 * @author Jesse
 *
 */
class FragmentNode extends Region
{
  private static final int MAX_TXT_FOR_FIRST_LINE = 18;
  private static final int MAX_TXT_FOR_SECOND_LINE = 14;

  private Bitmap m_currentBackground = null;
  private Bitmap m_backgroundImage = null;
  private Bitmap m_selectedBackgroundImage = null;
  
  private StoryFragment m_fragment = null;
  private String m_lineOne = null;
  private String m_lineTwo = null;

  private static Paint s_backgroundPaint = null;
  private static Paint s_textPaint = null;

  // I love this feature!
  static
  {
    s_backgroundPaint = new Paint();

    s_textPaint = new Paint();
    s_textPaint.setColor(Color.BLACK);
    s_textPaint.setTextAlign(Align.CENTER);
    s_textPaint.setTextSize(35);
  }

  public FragmentNode(StoryFragment fragment, Resources res)
  {
    super(0, 0, 0, 0); // width and height will be updated momentarily...
    this.m_fragment = fragment;
    m_backgroundImage = BitmapFactory.decodeResource(res, R.drawable.bk_fragment_node);
    m_selectedBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.bk_fragment_selected_node);
    m_currentBackground = m_backgroundImage;
    this.width = m_backgroundImage.getWidth();
    this.height = m_backgroundImage.getHeight();

    RefreshContents();
  }
  
  public void SetIsSelected(boolean isSelected)
  {
    m_currentBackground = isSelected ? m_selectedBackgroundImage : m_backgroundImage;
  }

  public void RefreshContents()
  {
    String storyTxt = m_fragment.getStoryText();

    if(storyTxt.length() > MAX_TXT_FOR_FIRST_LINE + MAX_TXT_FOR_SECOND_LINE)
    {
      m_lineOne = ChopString(storyTxt, MAX_TXT_FOR_FIRST_LINE, 0).trim();
      m_lineTwo = ChopString(storyTxt, MAX_TXT_FOR_SECOND_LINE, m_lineOne.length()).trim() + "...";
    }
    else if(storyTxt.length() > MAX_TXT_FOR_FIRST_LINE)
    {
      m_lineOne = ChopString(storyTxt, MAX_TXT_FOR_FIRST_LINE, 0).trim();
      m_lineTwo = storyTxt.substring(m_lineOne.length(), storyTxt.length()).trim();
    }
    else
    {
      m_lineOne = storyTxt;
      m_lineTwo = "";
    }
  }

  /**
   * Helper function that finds an appropriate spot to cut the string
   * as close to the end as possible without chopping a word up
   */
  private String ChopString(String str, int length, int start)
  {
    String result = str.substring(start, length + start);
    int lastIndex = result.lastIndexOf(' ');

    if(lastIndex > 0)
    {
      return result.substring(0, lastIndex);
    }
    else
    {
      return result;
    }
  }

  public StoryFragment GetFragment()
  {
    return m_fragment;
  }

  public void Draw(Canvas surface, Camera camera)
  {
    camera.DrawLocal(surface, s_backgroundPaint, m_currentBackground, this.x, this.y);
    camera.DrawLocal(surface, s_textPaint, m_lineOne, this.x + this.width / 2, this.y + this.height * 1 / 3);
    camera.DrawLocal(surface, s_textPaint, m_lineTwo, this.x + this.width / 2, this.y + this.height * 2 / 3);
  }
}




Java Source Code List

ca.cmput301f13t03.adventure_datetime.controller.AuthorControllerTest.java
ca.cmput301f13t03.adventure_datetime.controller.AuthorController.java
ca.cmput301f13t03.adventure_datetime.controller.UserControllerTest.java
ca.cmput301f13t03.adventure_datetime.controller.UserController.java
ca.cmput301f13t03.adventure_datetime.model.AccountServiceTests.java
ca.cmput301f13t03.adventure_datetime.model.AccountService.java
ca.cmput301f13t03.adventure_datetime.model.BookmarkTest.java
ca.cmput301f13t03.adventure_datetime.model.Bookmark.java
ca.cmput301f13t03.adventure_datetime.model.ChoiceTest.java
ca.cmput301f13t03.adventure_datetime.model.Choice.java
ca.cmput301f13t03.adventure_datetime.model.CommentTest.java
ca.cmput301f13t03.adventure_datetime.model.Comment.java
ca.cmput301f13t03.adventure_datetime.model.ES.java
ca.cmput301f13t03.adventure_datetime.model.ImageTest.java
ca.cmput301f13t03.adventure_datetime.model.Image.java
ca.cmput301f13t03.adventure_datetime.model.StoryDBTest.java
ca.cmput301f13t03.adventure_datetime.model.StoryDB.java
ca.cmput301f13t03.adventure_datetime.model.StoryFragmentTest.java
ca.cmput301f13t03.adventure_datetime.model.StoryFragment.java
ca.cmput301f13t03.adventure_datetime.model.StoryManager.java
ca.cmput301f13t03.adventure_datetime.model.StoryTest.java
ca.cmput301f13t03.adventure_datetime.model.Story.java
ca.cmput301f13t03.adventure_datetime.model.ThreadPool.java
ca.cmput301f13t03.adventure_datetime.model.WebStorageCleaner.java
ca.cmput301f13t03.adventure_datetime.model.WebStorageTest.java
ca.cmput301f13t03.adventure_datetime.model.WebStorage.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IAllFragmentsListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IBookmarkListListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICommentsListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICurrentFragmentListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ICurrentStoryListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ILocalStorage.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.ILocalStoriesListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IOnlineStoriesListener.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IStoryModelDirector.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IStoryModelPresenter.java
ca.cmput301f13t03.adventure_datetime.model.Interfaces.IWebStorage.java
ca.cmput301f13t03.adventure_datetime.serviceLocator.Locator.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit_Edit.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit_Overview.java
ca.cmput301f13t03.adventure_datetime.view.AuthorEdit.java
ca.cmput301f13t03.adventure_datetime.view.AuthorStories.java
ca.cmput301f13t03.adventure_datetime.view.AuthorStoryDescription.java
ca.cmput301f13t03.adventure_datetime.view.AuthorViewPager.java
ca.cmput301f13t03.adventure_datetime.view.BrowseFragment.java
ca.cmput301f13t03.adventure_datetime.view.BrowseView.java
ca.cmput301f13t03.adventure_datetime.view.CommentsView.java
ca.cmput301f13t03.adventure_datetime.view.ContinueView.java
ca.cmput301f13t03.adventure_datetime.view.FragmentViewActivity.java
ca.cmput301f13t03.adventure_datetime.view.FragmentView.java
ca.cmput301f13t03.adventure_datetime.view.FullScreen_Image.java
ca.cmput301f13t03.adventure_datetime.view.IFragmentSelected.java
ca.cmput301f13t03.adventure_datetime.view.MainViewTest.java
ca.cmput301f13t03.adventure_datetime.view.MainView.java
ca.cmput301f13t03.adventure_datetime.view.StoryDescription.java
ca.cmput301f13t03.adventure_datetime.view.treeView.Camera.java
ca.cmput301f13t03.adventure_datetime.view.treeView.ConnectionPlacer.java
ca.cmput301f13t03.adventure_datetime.view.treeView.FragmentConnection.java
ca.cmput301f13t03.adventure_datetime.view.treeView.FragmentNode.java
ca.cmput301f13t03.adventure_datetime.view.treeView.GridSegment.java
ca.cmput301f13t03.adventure_datetime.view.treeView.InputHandler.java
ca.cmput301f13t03.adventure_datetime.view.treeView.NodeGrid.java
ca.cmput301f13t03.adventure_datetime.view.treeView.NodePlacer.java
ca.cmput301f13t03.adventure_datetime.view.treeView.Region.java
ca.cmput301f13t03.adventure_datetime.view.treeView.TreeView.java