■ 余録:ソースコード

 ↑ TOP

from os.path import exists

from java.awt import Font
from javax.swing import ImageIcon
from javax.swing import JFrame
from javax.swing import JLabel
from javax.swing import JList
from javax.swing import JPanel
from javax.swing import JScrollPane
from javax.swing import JSplitPane
from javax.swing import ListSelectionModel

## ----------------------------------------
class TeamsListPanel(JPanel):

    groups = {
        "A": [                          # Group A
            {"rsa": "South Africa"},
            {"mex": "Mexico"},
            {"uru": "Uruguay"},
            {"fra": "France"},
            ],
        "B": [                          # Group B
            {"arg": "Argentina"},
            {"nga": "Nigeria"},
            {"kor": "Korea Republic"},
            {"gre": "Greece"},
            ],
        "C": [                          # Group C
            {"eng": "England"},
            {"usa": "USA"},
            {"alg": "Algeria"},
            {"svn": "Slovenia"},
            ],
        "D": [                          # Group D
            {"ger": "Germany"},
            {"aus": "Australia"},
            {"srb": "Serbia"},
            {"gha": "Ghana"},
            ],
        "E": [                          # Group E
            {"ned": "Netherlands"},
            {"den": "Denmark"},
            {"jpn": "Japan"},
            {"cmr": "Cameroon"},
            ],
        "F": [                          # Group F
            {"ita": "Italy"},
            {"par": "Paraguay"},
            {"nzl": "New Zealand"},
            {"svk": "Slovakia"},
            ],
        "G": [                          # Group G
            {"bra": "Brazil"},
            {"prk": "Korea DPR"},
            {"civ": "Cote d Ivoire"},
            {"por": "Portugal"},
            ],
        "H": [                          # Group H
            {"esp": "Spain"},
            {"sui": "Switzerland"},
            {"hon": "Honduras"},
            {"chi": "Chile"},
            ],
        }

    def __init__(self, master, *args, **keys):
        def teams():
            return [e for group in "ABCDEFGH"
                for team in self.groups[group] for e in team]
        
        listPane = self._listPane(
            teams(),
            minimumSize = (100,50),
            valueChanged = self,
            )
        picturePane = self._picturePane(
            minimumSize = (100,50),
            )
        master.contentPane = self._splitPane(
            leftComponent  = listPane,
            rightComponent = picturePane,
            )
        self._update()

    ## ----------------------------------------
    def _listPane(self, listData, minimumSize, **keys):
        def view(listData, **keys):
            self.list = \
            comp = JList(
                listData,
                selectionMode = ListSelectionModel.SINGLE_SELECTION,
                selectedIndex = 0,
                **keys
                )
            return comp

        comp = JScrollPane(
            viewportView = view(listData, **keys),
            minimumSize = minimumSize,
            )
        return comp

    def _picturePane(self, minimumSize):
        def view():
            self.picture = \
            comp = JLabel(
                horizontalAlignment = JLabel.CENTER,
                verticalTextPosition = JLabel.BOTTOM,
                horizontalTextPosition = JLabel.CENTER,
                )
            comp.font = comp.font.deriveFont(Font.ITALIC)
            return comp        

        comp = JScrollPane(
            viewportView = view(),
            minimumSize = minimumSize,
            )
        return comp

    def _splitPane(self, **keys):
        comp = JSplitPane(
            orientation = JSplitPane.HORIZONTAL_SPLIT,
            oneTouchExpandable = True,
            dividerLocation = 120,
            **keys
            )
        return comp        

    ## ----------------------------------------
    def __call__(self, e):  # ListSelectionListener.valueChanged
        self._update()

    def _update(self):
        def imageIcon(path):
            return ImageIcon(path) if exists(path) else None
        def name(key):
            for group in self.groups.values():
                for team in group:
                    if key in team:
                        return team[key]
            else:
                return None

        e = self.list.selectedValue
        icon = imageIcon("squad/%s.gif"%e)
        self.picture.icon = icon
        self.picture.text = name(e) if icon else "Image not found"

## ----------------------------------------
def tips():
    frame = JFrame(
        title = "2010 FIFA World Cup South Africa",
        size = (340,200),
        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
        locationRelativeTo = None,
        )
    TeamsListPanel(frame)
    frame.show()

 ↑ UP