daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.calpoly.android.lab4;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.text.TextUtils.TruncateAt;
  8.  
  9. import android.view.LayoutInflater;
  10.  
  11. import android.view.View;
  12.  
  13. import android.view.View.OnClickListener;
  14.  
  15. import android.widget.Button;
  16.  
  17. import android.widget.Checkable;
  18.  
  19. import android.widget.RadioButton;
  20.  
  21. import android.widget.RadioGroup;
  22.  
  23. import android.widget.RelativeLayout;
  24.  
  25. import android.widget.TextView;
  26.  
  27. import android.widget.RadioGroup.OnCheckedChangeListener;
  28.  
  29.  
  30.  
  31. public class JokeView extends RelativeLayout implements OnClickListener, OnCheckedChangeListener, Checkable{
  32.  
  33.  
  34.  
  35.     private Button m_vwExpandButton;
  36.  
  37.     private RadioButton m_vwLikeButton;
  38.  
  39.     private RadioButton m_vwDislikeButton;
  40.  
  41.     private RadioGroup m_vwLikeGroup;
  42.  
  43.     private TextView m_vwJokeText;
  44.  
  45.     private Joke m_joke;
  46.  
  47.     private OnJokeChangeListener m_onJokeChangeListener;
  48.  
  49.  
  50.  
  51.     public static final String EXPAND = " + ";
  52.  
  53.     public static final String COLLAPSE = " - ";
  54.  
  55.  
  56.  
  57.     /**
  58.  
  59.      * Basic Constructor that takes only takes in an application Context.
  60.  
  61.      *
  62.  
  63.      * @param context
  64.  
  65.      *            The application Context in which this view is being added.
  66.  
  67.      *            
  68.  
  69.      * @param joke
  70.  
  71.      *            The Joke this view is responsible for displaying.
  72.  
  73.      */
  74.  
  75.     public JokeView(Context context, Joke joke) {
  76.  
  77.         super(context);
  78.  
  79.        
  80.  
  81.         m_onJokeChangeListener = null;
  82.  
  83.         // Inflate the layout and set this JokeView as the root ViewGroup.
  84.  
  85.         ((LayoutInflater)context
  86.  
  87.          .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
  88.  
  89.          .inflate(R.layout.joke_view, this, true);
  90.  
  91.        
  92.  
  93.         // Initialize View member variables
  94.  
  95.         m_vwExpandButton = (Button)findViewById(R.id.expandButton);
  96.  
  97.         m_vwExpandButton.setOnClickListener(this);
  98.  
  99.        
  100.  
  101.         m_vwLikeGroup = (RadioGroup)findViewById(R.id.ratingRadioGroup);
  102.  
  103.         m_vwLikeGroup.setOnCheckedChangeListener(this);
  104.  
  105.  
  106.  
  107.         m_vwLikeButton = (RadioButton)findViewById(R.id.likeButton);
  108.  
  109.  
  110.  
  111.         m_vwDislikeButton = (RadioButton)findViewById(R.id.dislikeButton);
  112.  
  113.        
  114.  
  115.         m_vwJokeText= (TextView)findViewById(R.id.jokeTextView);
  116.  
  117.        
  118.  
  119.         setJoke(joke);
  120.  
  121.        
  122.  
  123.         collapseJokeView();
  124.  
  125.     }
  126.  
  127.  
  128.  
  129.     /**
  130.  
  131.      * Mutator method for changing the Joke object this View displays. This View
  132.  
  133.      * will be updated to display the correct contents of the new Joke.
  134.  
  135.      *
  136.  
  137.      * @param joke
  138.  
  139.      *            The Joke object which this View will display.
  140.  
  141.      */
  142.  
  143.     public void setJoke(Joke joke) {
  144.  
  145.         m_joke = joke;
  146.  
  147.         m_vwJokeText.setText(m_joke.getJoke());
  148.  
  149.         if (m_joke.getRating() == Joke.LIKE) {
  150.  
  151.             m_vwLikeButton.setChecked(true);
  152.  
  153.         }
  154.  
  155.         else if (m_joke.getRating() == Joke.DISLIKE) {
  156.  
  157.             m_vwDislikeButton.setChecked(true);
  158.  
  159.         }
  160.  
  161.         else {
  162.  
  163.             m_vwLikeGroup.clearCheck();
  164.  
  165.         }
  166.  
  167.     }
  168.  
  169.  
  170.  
  171.     /**
  172.  
  173.      * This method encapsulates the logic necessary to update this view so that
  174.  
  175.      * it displays itself in its "Expanded" form:
  176.  
  177.      *
  178.  
  179.      *  - Shows the complete text of the joke.
  180.  
  181.      *
  182.  
  183.      *  - Brings the RadioGroup of rating Buttons back into view.
  184.  
  185.      */
  186.  
  187.     private void expandJokeView() {
  188.  
  189.         m_vwJokeText.setEllipsize(null);
  190.  
  191.         m_vwExpandButton.setText(COLLAPSE);
  192.  
  193.         m_vwLikeGroup.setVisibility(VISIBLE);
  194.  
  195.         requestLayout();
  196.  
  197.     }
  198.  
  199.  
  200.  
  201.     /**
  202.  
  203.      * This method encapsulates the logic necessary to update this view so that
  204.  
  205.      * it displays itself in its "Collapsed" form:
  206.  
  207.      *
  208.  
  209.      *  - Shows only the first line of text of the joke.
  210.  
  211.      *  
  212.  
  213.      *  - If the joke is longer than one line, it appends an ellipsis to the end.
  214.  
  215.      *  
  216.  
  217.      *  - Removes the RadioGroup of rating Buttons from view.
  218.  
  219.      */
  220.  
  221.     private void collapseJokeView() {
  222.  
  223.         m_vwJokeText.setEllipsize(TruncateAt.END);
  224.  
  225.         m_vwExpandButton.setText(EXPAND);
  226.  
  227.         m_vwLikeGroup.setVisibility(GONE);
  228.  
  229.         requestLayout();
  230.  
  231.     }
  232.  
  233.  
  234.  
  235.     @Override
  236.  
  237.     public void onClick(View v) {
  238.  
  239.         if (m_vwExpandButton.getText().toString()== EXPAND) {
  240.  
  241.             expandJokeView();
  242.  
  243.         }
  244.  
  245.         else {
  246.  
  247.             collapseJokeView();
  248.  
  249.         }
  250.  
  251.     }
  252.  
  253.  
  254.  
  255.     @Override
  256.  
  257.     public void onCheckedChanged(RadioGroup arg0, int checkedId) {
  258.  
  259.         if (checkedId == R.id.likeButton) {
  260.  
  261.             m_joke.setRating(Joke.LIKE);
  262.  
  263.         }
  264.  
  265.         else if (checkedId == R.id.dislikeButton) {
  266.  
  267.             m_joke.setRating(Joke.DISLIKE);
  268.  
  269.         }
  270.  
  271.         else if (checkedId == -1) {
  272.  
  273.             m_joke.setRating(Joke.UNRATED);
  274.  
  275.         }
  276.  
  277.         notifyOnJokeChangeListener();
  278.  
  279.     }
  280.  
  281.  
  282.  
  283.     @Override
  284.  
  285.     public boolean isChecked() {
  286.  
  287.         if (m_vwExpandButton.getText().toString()== EXPAND) {
  288.  
  289.             return false;
  290.  
  291.         }
  292.  
  293.         else {
  294.  
  295.             return true;
  296.  
  297.         }
  298.  
  299.     }
  300.  
  301.  
  302.  
  303.     @Override
  304.  
  305.     public void setChecked(boolean checked) {
  306.  
  307.         if (checked) {
  308.  
  309.             expandJokeView();
  310.  
  311.         }
  312.  
  313.         else {
  314.  
  315.             collapseJokeView();
  316.  
  317.         }
  318.  
  319.     }
  320.  
  321.  
  322.  
  323.     @Override
  324.  
  325.     public void toggle() {
  326.  
  327.         setChecked(!isChecked());
  328.  
  329.     }
  330.  
  331.  
  332.  
  333.     /**
  334.  
  335.      * Mutator method for changing the OnJokeChangeListener object this JokeView
  336.  
  337.      * notifies when the state its underlying Joke object changes.
  338.  
  339.      *
  340.  
  341.      * It is possible and acceptable for m_onJokeChangeListener to be null, you
  342.  
  343.      * should allow for this.
  344.  
  345.      *
  346.  
  347.      * @param listener
  348.  
  349.      *            The OnJokeChangeListener object that should be notified when
  350.  
  351.      *            the underlying Joke changes state.
  352.  
  353.      */
  354.  
  355.     public void setOnJokeChangeListener(OnJokeChangeListener listener) {
  356.  
  357.         // TODO
  358.  
  359.         m_onJokeChangeListener = listener;
  360.  
  361.     }
  362.  
  363.  
  364.  
  365.     /**
  366.  
  367.      * This method should always be called after the state of m_joke is changed.
  368.  
  369.      *
  370.  
  371.      * It is possible and acceptable for m_onJokeChangeListener to be null, you
  372.  
  373.      * should test for this.
  374.  
  375.      *
  376.  
  377.      * This method should not be called if setJoke(...) is called, since the
  378.  
  379.      * internal state of the Joke object that m_joke references is not be
  380.  
  381.      * changed. Rather, m_joke reference is being changed to reference a
  382.  
  383.      * different Joke object.
  384.  
  385.      */
  386.  
  387.     protected void notifyOnJokeChangeListener() {
  388.  
  389.         // TODO
  390.  
  391.     }
  392.  
  393.  
  394.  
  395.     /**
  396.  
  397.      * Interface definition for a callback to be invoked when the underlying
  398.  
  399.      * Joke is changed in this JokeView object.
  400.  
  401.      *
  402.  
  403.      */
  404.  
  405.     public static interface OnJokeChangeListener {
  406.  
  407.  
  408.  
  409.         /**
  410.  
  411.          * Called when the underlying Joke in a JokeView object changes state.
  412.  
  413.          *
  414.  
  415.          * @param view
  416.  
  417.          *            The JokeView in which the Joke was changed.
  418.  
  419.          * @param joke
  420.  
  421.          *            The Joke that was changed.
  422.  
  423.          */
  424.  
  425.         public void onJokeChanged(JokeView view, Joke joke);
  426.  
  427.     }
  428.  
  429.  
  430.  
  431. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top