project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamAddDialog.java
author Vittorio Giovara <vittorio.giovara@gmail.com>
Tue, 10 Nov 2015 13:53:40 +0100
changeset 11370 7ab863821864
parent 10017 de822cd3df3a
permissions -rw-r--r--
videorec: Drop a commented out option
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: 7485
diff changeset
    20
package org.hedgewars.hedgeroid;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import java.util.ArrayList;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import java.util.Collection;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.util.Collections;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.util.List;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import org.hedgewars.hedgeroid.R;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import org.hedgewars.hedgeroid.Datastructures.Team;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.app.Activity;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.app.AlertDialog;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.app.Dialog;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.content.DialogInterface;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.content.DialogInterface.OnClickListener;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.os.Bundle;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
import android.support.v4.app.DialogFragment;
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
public class TeamAddDialog extends DialogFragment {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    private static final String STATE_TEAMS_ALREADY_IN_GAME = "teamAlreadyInGame";
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
    private ArrayList<String> teamsAlreadyInGame;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    private List<Team> availableTeams;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
    private Listener listener;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
    public static interface Listener {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
        void onTeamAddDialogSubmitted(Team newTeam);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    public TeamAddDialog() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
        // Only for reflection-based instantiation by the framework
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
    TeamAddDialog(Collection<String> teamsAlreadyInGame) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        this.teamsAlreadyInGame = new ArrayList<String>(teamsAlreadyInGame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
    public void onAttach(Activity activity) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
        super.onAttach(activity);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
        try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
            listener = (Listener) activity;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
        } catch(ClassCastException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
            throw new ClassCastException("Activity " + activity + " must implement TeamAddDialog.Listener to use TeamAddDialog.");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    public void onDetach() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
        super.onDetach();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
        listener = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    public void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        if(savedInstanceState != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
            teamsAlreadyInGame = savedInstanceState.getStringArrayList(STATE_TEAMS_ALREADY_IN_GAME);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        availableTeams = new ArrayList<Team>();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
        List<Team> teams = FrontendDataUtils.getTeams(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        for(Team team : teams) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
            if(!teamsAlreadyInGame.contains(team.name)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
                availableTeams.add(team);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
        Collections.sort(availableTeams, Team.NAME_ORDER);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
    // TODO use icons for the teams (corresponding to botlevel)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
    public Dialog onCreateDialog(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
        builder.setTitle(R.string.dialog_addteam_title);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
        builder.setIcon(R.drawable.human);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
        String[] teamNames = new String[availableTeams.size()];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
        for(int i=0; i<availableTeams.size(); i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
            teamNames[i] = availableTeams.get(i).name;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
        builder.setItems(teamNames, new OnClickListener() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
            public void onClick(DialogInterface dialog, int which) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
                listener.onTeamAddDialogSubmitted(availableTeams.get(which));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
        return builder.create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
    public void onSaveInstanceState(Bundle outState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
        super.onSaveInstanceState(outState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
        outState.putStringArrayList(STATE_TEAMS_ALREADY_IN_GAME, teamsAlreadyInGame);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
    }
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
}