de.atomfrede.android.thwdroid.damageaccount.activity.DisplayDamageAccountDetailsActivity.java Source code

Java tutorial

Introduction

Here is the source code for de.atomfrede.android.thwdroid.damageaccount.activity.DisplayDamageAccountDetailsActivity.java

Source

/**
 * 
 * DisplayDamagesActivity.java
 *
 *    Copyright $2011 Frederik Hahne
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package de.atomfrede.android.thwdroid.damageaccount.activity;

import java.util.*;

import org.joda.time.format.DateTimeFormat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import com.actionbarsherlock.view.*;

import de.atomfrede.android.thwdroid.R;
import de.atomfrede.android.thwdroid.damage.activity.DisplayDamageDetailsActivity;
import de.atomfrede.android.thwdroid.damage.adapter.DamageListAdapter;
import de.usetable.ThwExtras;
import de.usetable.core.ThwService;
import de.usetable.model.*;

public class DisplayDamageAccountDetailsActivity extends ModelListActivity {

    DamageListAdapter listAdapter;
    UUID missionSectionId;
    UUID damageAccountId;
    DamageAccount damageAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        damageAccountId = (UUID) getIntent().getExtras().getSerializable(ThwExtras.ELEMENT_ID);
        missionSectionId = (UUID) getIntent().getExtras().getSerializable(ThwExtras.PARENT_ELEMENT_ID);

        getSupportActionBar().setTitle("DamageAccount");

        // header view
        getListView().addHeaderView(getLayoutInflater().inflate(R.layout.damage_account_detail, null, true));
    }

    @Override
    public void onThwServiceConnected(ThwService service) {
        super.onThwServiceConnected(service);
    }

    @Override
    public void onLoadMissionDone(Mission mission) {
        super.onLoadMissionDone(mission);
        refresh();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (listAdapter != null) {
            refresh();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            super.onBackPressed();
            return true;
        }

        if (item.getItemId() == R.id.menu_edit) {
            editDamageAccount();
            return true;
        }

        if (item.getItemId() == R.id.menu_save) {
            saveMission();
            return true;
        }
        return false;
    }

    public void refresh() {
        damageAccount = (DamageAccount) getModel(damageAccountId);

        if (damageAccount.getDamageList() != null)
            listAdapter = new DamageListAdapter(this, damageAccount.getDamageList(), false);
        else
            listAdapter = new DamageListAdapter(this, new ArrayList<Damage>(), false);

        TextView damageAccountnameTextView = (TextView) findViewById(R.id.damage_account_title);
        TextView damageAccountTimeTextView = (TextView) findViewById(R.id.time_text_view);

        damageAccountnameTextView.setText(damageAccount.getName());
        if (damageAccount.getTime() != null)
            damageAccountTimeTextView.setText(damageAccount.getTime().toString(DateTimeFormat.mediumDate()));
        else
            damageAccountTimeTextView.setText("Not available");

        setListAdapter(listAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        displayDamageDetails(position - 1);
    }

    private void displayDamageDetails(int position) {
        if (position >= 0) {
            Intent displayDamageDetailsIntent = new Intent(this, DisplayDamageDetailsActivity.class);
            displayDamageDetailsIntent.putExtra(ThwExtras.ELEMENT_ID,
                    listAdapter.getDamages().get(position).getId());
            displayDamageDetailsIntent.putExtra(ThwExtras.PARENT_ELEMENT_ID, damageAccountId);
            startActivity(displayDamageDetailsIntent);
        }
    }

    private void editDamageAccount() {
        Intent editDamageAccountIntent = new Intent(this, EditDamageAccountActivity.class);
        editDamageAccountIntent.putExtra(ThwExtras.ELEMENT_ID, damageAccountId);
        editDamageAccountIntent.putExtra(ThwExtras.PARENT_ELEMENT_ID, missionSectionId);
        startActivity(editDamageAccountIntent);
    }
}