■ 余録:ソースコード

 ↑ TOP

#! /usr/bin/env python
# coding: utf-8
## ----------------------------------------
##
## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
##
## ----------------------------------------
## History: Swing Example "2010 FIFA World Cup South Africa"
##      2003/07, Java/Jython
##      2006/07, Jython
##      2008/02, Jython 2.2.1
##      2010/06, Jython 2.5.0
#...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
"""
>>> tips()

>>> ## ----------------------------------------
>>> None
version: #1.0.10
"""
from __init__ import *

## ----------------------------------------
## 気軽に Swing〔2〕JTextArea: 各チーム紹介
## Java.use(better,Jython=Swing) 〜 萬プログラマーのための Python 導入ガイド
## ----------------------------------------
## ----------------------------------------
## 2008/03/17《2.1》今回の課題:相手チームを知ろう
## ----------------------------------------
from os.path import dirname, exists
from sys import argv

from java.awt import BorderLayout
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 JTabbedPane
from javax.swing import JTextArea
from javax.swing import ListSelectionModel

## ----------------------------------------
class GroupListPanel(JPanel):

    groupTabs = "ABCDEFGH"
    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"},
            ],
        }
    teamNames = dict*1

    ## ----------------------------------------
    def __init__(self, master, *args, **keys):
        self.label = {}
        self.textArea = {}
        
        def splitPane(group):
            controlPane = self._controlPane(
                group = group,
                minimumSize = (60,50),
                )
            canvasPane = self._canvasPane(
                group = group,
                minimumSize = (200,50),
                )
            return self._splitPane(
                leftComponent  = controlPane,
                rightComponent = canvasPane,
                )
        
        master.contentPane = self._tabbedPane(
            panes = dict*2 for e in self.groupTabs),
            )
        self._update("rsa")

    ## ----------------------------------------
    def _controlPane(self, group, minimumSize, **keys):

        def teams(group):
            return [e for team in self.groups[group] for e in team]

        def view(group):
            comp = JList(
                teams(group),
                selectionMode = ListSelectionModel.SINGLE_SELECTION,
                selectedIndex = 0,
                valueChanged = self,
                **keys
                )
            return comp

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

    ## ----------------------------------------
    def _canvasPane(self, group, minimumSize, **keys):
        comp = JPanel(
            layout = BorderLayout(),
            )
        for layout, e in [
            ("NORTH" , self._labelPane(group),           ),
            ("CENTER", self._textPane(group, minimumSize)),
            ]:
            comp.add(e, getattr(BorderLayout, layout))
        return comp

    def _labelPane(self, group, **keys):
        def view(group):
            self.label[group] = \
            comp = JLabel(
                horizontalAlignment = JLabel.CENTER,
                horizontalTextPosition = JLabel.CENTER,
                verticalTextPosition = JLabel.BOTTOM,
                )
            comp.font = comp.font.deriveFont(Font.ITALIC)
            return comp        

        comp = view(group)
        return comp

    def _textPane(self, group, minimumSize, **keys):
        def view(group):
            self.textArea[group] = \
            comp = JTextArea(
                lineWrap = True,
                wrapStyleWord = True,
                )
            comp.font = comp.font.deriveFont(Font.ITALIC)
            return comp        

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

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

    def _tabbedPane(self, panes, **keys):
        def stateChanged(e):
            index = e.source.selectedIndex
            self.tab = self.groupTabs[index]

        comp = JTabbedPane(
            stateChanged = stateChanged,
            )
        for e in self.groupTabs:
            comp.addTab(e, panes[e])
        return comp
        
    ## ----------------------------------------
    def __call__(self, e):  # ListSelectionListener.valueChanged
        value = e.source.selectedValue
        self._update(value)

    def _update(self, value=None):
        def imageIcon(path):
            return ImageIcon(path) if exists(path) else None

        def update_label(tab, path):
            comp = self.label[tab]
            comp.icon = imageIcon("%s/squad/%s.gif"%(path, value))
            comp.text = self.teamNames[value]

        def contents(path):
            return "".join([e for e in open(path)]) if exists(path) else None

        def update_textArea(tab, path):
            comp = self.textArea[tab]
            text = contents(
                "%s/profile/%s.txt"%(path, self.teamNames[value]))
            comp.text = text if text else "*** File not found ***"
            comp.caretPosition = 0

        tab = self.tab
        path = dirname(argv[-1])
        update_label(tab, path)
        update_textArea(tab, path)

def tips():
    frame = JFrame(
        title = u"2010 FIFA World Cup South Africa™",
        size = (350,250),
        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
        locationRelativeTo = None,
        )
    GroupListPanel(frame)
    frame.show()

## ----------------------------------------
from doctest import testmod

if __name__=='__main__':
    inform()
    testmod()

## ========================================

*1:k,v) for group in groups.values() for team in group for k,v in team.items(

*2:e, splitPane(e