project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/WeaponsetListActivity.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import java.io.IOException;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.util.ArrayList;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.Collections;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.util.List;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import java.util.Map;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import org.hedgewars.hedgeroid.Datastructures.Weaponset;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.app.ListActivity;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.content.Intent;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.os.Bundle;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.view.ContextMenu;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.view.MenuItem;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.view.View;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
import android.view.View.OnClickListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
import android.widget.AdapterView;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
import android.widget.AdapterView.AdapterContextMenuInfo;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
import android.widget.AdapterView.OnItemClickListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
import android.widget.Button;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
import android.widget.ListAdapter;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
import android.widget.SimpleAdapter;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
import android.widget.Toast;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
public class WeaponsetListActivity extends ListActivity implements OnItemClickListener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    private List<Weaponset> userWeaponsets;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
    private Button addButton;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    protected void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
        setContentView(R.layout.activity_weaponsetlist);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        addButton = (Button)findViewById(R.id.addButton);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        addButton.setOnClickListener(new OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
            public void onClick(View v) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
                editWeaponset(null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
    public void onResume() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        super.onResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        updateList();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
        getListView().setOnItemClickListener(this);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
        registerForContextMenu(getListView());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
    private List<Map<String, ?>> weaponsetsToMap(List<Weaponset> weaponsets) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
        List<Map<String, ?>> result = new ArrayList<Map<String, ?>>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
        for(Weaponset weaponset : weaponsets) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
            result.add(Collections.singletonMap("txt", weaponset.name));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
    public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        editWeaponset(userWeaponsets.get(position).name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
        menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.edit);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
        menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
    public boolean onContextItemSelected(MenuItem item){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
        int position = menuInfo.position;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        Weaponset weaponset = userWeaponsets.get(position);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
        switch(item.getItemId()){
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
        case 0:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
            editWeaponset(weaponset.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
        case 1:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
            try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
                Weaponsets.deleteUserWeaponset(this, weaponset.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
            } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
                Toast.makeText(this.getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_SHORT).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
            updateList();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
        return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
    private void updateList() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
            userWeaponsets = Weaponsets.loadUserWeaponsets(this);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
        } catch (IOException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   113
            Toast.makeText(this, R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   114
            finish();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   115
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   116
        Collections.sort(userWeaponsets, Weaponset.NAME_ORDER);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   117
        ListAdapter adapter = new SimpleAdapter(this, weaponsetsToMap(userWeaponsets), android.R.layout.simple_list_item_1, new String[]{"txt"}, new int[]{android.R.id.text1});
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   118
        setListAdapter(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   119
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   120
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   121
    private void editWeaponset(String weaponsetName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   122
        Intent i = new Intent(this, WeaponsetCreatorActivity.class);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   123
        i.putExtra(WeaponsetCreatorActivity.PARAMETER_EXISTING_WEAPONSETNAME, weaponsetName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   124
        startActivity(i);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   125
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
}