de.atomfrede.android.thwdroid.mission.activity.EditMissionActivity.java Source code

Java tutorial

Introduction

Here is the source code for de.atomfrede.android.thwdroid.mission.activity.EditMissionActivity.java

Source

/**
 * 
 * EditMissionActivity.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.mission.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.missionsection.activity.EditMissionSectionActivity;
import de.atomfrede.android.thwdroid.missionsection.adapter.MissionSectionListAdapter;
import de.usetable.ThwExtras;
import de.usetable.core.ThwService;
import de.usetable.model.*;

public class EditMissionActivity extends ModelListActivity {

    UUID missionId;
    Mission currentMission;
    MissionSectionListAdapter listAdapter;
    List<MissionSection> removedMissionSections;
    List<MissionSection> addedMissionSections;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        missionId = (UUID) getIntent().getExtras().getSerializable(ThwExtras.ELEMENT_ID);

        // currentMission = getMission();
        // header view
        getListView().addHeaderView(getLayoutInflater().inflate(R.layout.mission_edit, null, true));

        addedMissionSections = new ArrayList<MissionSection>();
        removedMissionSections = new ArrayList<MissionSection>();

        // refresh();
    }

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

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

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

    private void saveLocalChanges() {
        String newMissionName = ((EditText) findViewById(R.id.mission_title)).getText().toString();
        String newMissionDescription = ((EditText) findViewById(R.id.description_edit_text_view)).getText()
                .toString();

        currentMission.setName(newMissionName);
        currentMission.setDescription(newMissionDescription);

        for (MissionSection deletedSection : removedMissionSections) {
            deletedSection.delete();
        }
        for (MissionSection addedSection : addedMissionSections) {
            store(addedSection);
            for (DamageAccount addedDac : addedSection.getDamageAccountList()) {
                store(addedDac);
            }
        }

        onBackPressed();
    }

    private void discardChanges() {
        currentMission.getMissionSectionList().removeAll(addedMissionSections);
        currentMission.getMissionSectionList().addAll(removedMissionSections);
        addedMissionSections.clear();
        removedMissionSections.clear();
        onBackPressed();
    }

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

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

        if (item.getItemId() == R.id.menu_discard) {
            discardChanges();
            return true;
        }

        if (item.getItemId() == R.id.menu_ok) {
            saveLocalChanges();
            return true;
        }
        return false;
    }

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

    private void editMissionSection(int position) {
        if (position >= 0) {
            saveLocalChanges();
            Intent editMissionSectionIntent = new Intent(this, EditMissionSectionActivity.class);

            editMissionSectionIntent.putExtra(ThwExtras.ELEMENT_ID,
                    currentMission.getMissionSectionList().get(position).getId());
            startActivity(editMissionSectionIntent);
        }
    }

    public void onDeleteClicked(View view) {
        int position = listAdapter.getPositionForView(view);
        MissionSection removedMissionSection = currentMission.getMissionSectionList().get(position);
        if (addedMissionSections.contains(removedMissionSection)) {
            addedMissionSections.remove(removedMissionSection);
        }
        removedMissionSections.add(removedMissionSection);
        currentMission.getMissionSectionList().remove(position);
        listAdapter = new MissionSectionListAdapter(this, currentMission.getMissionSectionList(), true);
        setListAdapter(listAdapter);
    }

    public void onAddClicked(View view) {
        MissionSection newMissionSection = new MissionSection();
        newMissionSection.setId(UUID.randomUUID().toString());
        newMissionSection.setName("New MissionSection");

        newMissionSection.setDamageAccountList(new ArrayList<DamageAccount>());
        addedMissionSections.add(newMissionSection);
        currentMission.getMissionSectionList().add(newMissionSection);
        listAdapter = new MissionSectionListAdapter(this, currentMission.getMissionSectionList(), true);
        setListAdapter(listAdapter);
    }

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

    public void refresh() {
        currentMission = (Mission) getModel(missionId);

        // mission data
        ((EditText) findViewById(R.id.mission_title)).setText(currentMission.getName());
        ((EditText) findViewById(R.id.description_edit_text_view)).setText(currentMission.getDescription());
        ((TextView) findViewById(R.id.start_time_text_view))
                .setText(currentMission.getStartDate().toString(DateTimeFormat.mediumDate()));

        listAdapter = new MissionSectionListAdapter(this, currentMission.getMissionSectionList(), true);
        setListAdapter(listAdapter);

    }

}