com.cmput301.classproject.UI.SubmissionViewActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.cmput301.classproject.UI.SubmissionViewActivity.java

Source

/**
License: GPL 2.0
    
Copyright (C) 2012 Benson Trinh, Thomas Polasek, Remco Uittenbogerd, Clinton Wong
    
This program is free software; you can redistribute it and/or modify it under the 
terms of the GNU General Public License as published by the Free Software Foundation;
    
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License along with this program; 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
MA 02110-1301, USA.
 **/
package com.cmput301.classproject.UI;

import java.util.*;

import com.cmput301.classproject.R;
import com.cmput301.classproject.Model.Submission;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class SubmissionViewActivity extends Activity implements Observer {

    private Submission submission = null; // this is passed to from
    // TaskView Activity
    private ArrayList<Bitmap> submissionPhotos;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submission_view);
        getActionBar().setDisplayHomeAsUpEnabled(false);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            this.submission = (Submission) extras.getSerializable("Submission");
        }

        if (submission == null) {
            finish();
        } else {

            submissionPhotos = submission.getImages();

            // Start recognizing the Layout elements.
            GridView gridView = (GridView) findViewById(R.id.submissionPhotoList);
            gridView.setAdapter(new ImageAdapter(this));

            // Fill out activity view using the submission object
            TextView authorText = (TextView) findViewById(R.id.view_author_name);
            authorText.setText(submission.getAuthor());

            TextView submissionSummary = (TextView) findViewById(R.id.view_submission_summary);
            submissionSummary.setText(submission.getSummary());

            TextView submissionText = (TextView) findViewById(R.id.view_submission_text);
            submissionText.setText(submission.getTextSubmission());

            TextView access = (TextView) findViewById(R.id.view_submission_access);
            access.setText(submission.getAccess().toString());

            // TODO display timestamp, at the moment we only save it.
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_submission_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Name: okayClicked Description: User is finished viewing the submission
     * this will redirect them back to the ViewTasksActivity
     * 
     * @param v
     */
    public void okayClicked(View v) {
        finish();
    }

    /**
     * Name: update Description: Model has called to update the view with new
     * data. This will update the Submission Activity with the new data.
     * 
     * @param s
     * @param arg
     */
    public void update(Observable s, Object arg) {
        // Do nothing.
    }

    /**
     * ImageAdapter Class to support putting thumbnails into list views. Will
     * also have a modified implementation into ViewSubmission activity. This is
     * tailored to fit ListViews.
     */
    public class ImageAdapter extends BaseAdapter {
        private Context context;

        public ImageAdapter(Context C) {
            context = C;
        }

        public int getCount() {
            return submissionPhotos.size();
        }

        public Object getItem(int arg0) {
            return submissionPhotos.get(arg0);
        }

        public long getItemId(int arg0) {
            return arg0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(400, 400));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(5, 5, 5, 5);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(submissionPhotos.get(position));
            return imageView;
        }
    }
}