~/home_Jython/berry_py/worldCup/ex02/wc02a_JTabbedPane.py

INDEX Java.use(better, Jython)

》作業中です《

#! /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.44
"""
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 JTextArea
from javax.swing import ListSelectionModel

from javax.swing import JTabbedPane

## ----------------------------------------
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"},
            ],
        }

    def __init__(self, master, *args, **keys):
        self.mapping = {}
        self.label = {}
        self.textArea = {}
        
        def teams(group):
            return [e for team in self.groups[group] for e in team]

        def splitPane(group):
            listPane = self._listPane(
                listData = teams(group),
                minimumSize = (60,50),
                valueChanged = self,
                )
            canvasPane = self._canvasPane(
                group = group,
                minimumSize = (200,50),
                )
            return self._splitPane(
                leftComponent  = listPane,
                rightComponent = canvasPane,
                )
        
        panes = {}
        for e in self.groupTabs:
            panes[e] = splitPane(e)

        master.contentPane = self._tabbedPane(
            tabs = self.groupTabs,
            panes = panes,
            # panes = {e: splitPane(e) for e in self.groupTabs},
            )
        self._update("rsa")

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

        comp = JScrollPane(
            viewportView = view(listData, **keys),
            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():
            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()
        return comp

    def _textPane(self, group, minimumSize, **keys):
        def view():
            self.textArea[group] = \
            comp = JTextArea(
                lineWrap = True,
                wrapStyleWord = True,
                )
            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 = 60,
            **keys
            )
        return comp

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

        comp = JTabbedPane(
            stateChanged = stateChanged,
            )
        for e in tabs:
            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 name(key):
            for group in self.groups.values():
                for team in group:
                    if key in team:
                        return team[key]
            else:
                return 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 = name(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, name(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()

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