project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/RoomlistAdapter.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: 7491
diff changeset
    20
package org.hedgewars.hedgeroid;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    22
import java.util.Comparator;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import org.hedgewars.hedgeroid.R;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    25
import org.hedgewars.hedgeroid.Datastructures.Room;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    26
import org.hedgewars.hedgeroid.Datastructures.RoomWithId;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    27
import org.hedgewars.hedgeroid.util.ObservableTreeMapAdapter;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.Context;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.content.res.Resources;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.view.LayoutInflater;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.view.View;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.ViewGroup;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.widget.TextView;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    36
/**
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    37
 * Displays the list of all rooms in the lobby
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    38
 */
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    39
public class RoomlistAdapter extends ObservableTreeMapAdapter<String, RoomWithId> {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    private Context context;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    public RoomlistAdapter(Context context) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
        this.context = context;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
    protected Comparator<RoomWithId> getEntryOrder() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        return RoomWithId.NEWEST_FIRST_ORDER;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    51
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
    public long getItemId(int position) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
        return getItem(position).id;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
    public boolean hasStableIds() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
        return true;
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
    private static CharSequence formatExtra(Resources res, Room room) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
        String ownermsg = res.getString(R.string.roomlist_owner, room.owner);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
        String mapmsg = res.getString(R.string.roomlist_map, room.formatMapName(res));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        String scheme = room.scheme.equals(room.weapons) ? room.scheme : room.scheme + " / " + room.weapons;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
        String schememsg = res.getString(R.string.roomlist_scheme, scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    66
        return ownermsg + ". " + mapmsg + ", " + schememsg;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
    public View getView(int position, View convertView, ViewGroup parent) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
        View v = convertView;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
        if (v == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
            LayoutInflater vi = LayoutInflater.from(context);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
            v = vi.inflate(R.layout.listview_room, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        Room room = getItem(position).room;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
        int iconRes = room.inProgress ? R.drawable.roomlist_ingame : R.drawable.roomlist_preparing;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
        if(v.findViewById(android.R.id.text1) == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
            // Tabular room list
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
            TextView roomnameView = (TextView)v.findViewById(R.id.roomname);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
            TextView playerCountView = (TextView)v.findViewById(R.id.playercount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
            TextView teamCountView = (TextView)v.findViewById(R.id.teamcount);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
            TextView ownerView = (TextView)v.findViewById(R.id.owner);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
            TextView mapView = (TextView)v.findViewById(R.id.map);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
            TextView schemeView = (TextView)v.findViewById(R.id.scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
            TextView weaponView = (TextView)v.findViewById(R.id.weapons);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
            roomnameView.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
            roomnameView.setText(room.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
            if(playerCountView != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
                playerCountView.setText(String.valueOf(room.playerCount));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    94
            if(teamCountView != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    95
                teamCountView.setText(String.valueOf(room.teamCount));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    96
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    97
            ownerView.setText(room.owner);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    98
            mapView.setText(room.formatMapName(context.getResources()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    99
            schemeView.setText(room.scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   100
            weaponView.setText(room.weapons);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   101
        } else {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   102
            // Small room list
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   103
            TextView v1 = (TextView)v.findViewById(android.R.id.text1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   104
            TextView v2 = (TextView)v.findViewById(android.R.id.text2);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   105
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   106
            v1.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   107
            v1.setText(room.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   108
            v2.setText(formatExtra(context.getResources(), room));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   109
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   110
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   111
        return v;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
   112
    }
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
}