Java.use(better,Jython=Swing)《1.2》JTextArea: 各チーム紹介

前の記事記事一覧次の記事
Java.use(better, Jython=Swing)


Jython 導入ガイド -- 2010 FIFA World Cup South Africa™

JTextArea: 各チーム紹介



無知な者の心には
常に偉大なアイデアが納まる余地がある
Oscar Wilde - Wikipedia

《関連記事》

-

  • INDEX《Swing》text
  • INDEX《Swing》lineWrap
  • wrapStyleWord
  • font
■ 課題:相手チームを知ろう

《要求仕様》予選リーグに出場した全32チームの個別情報を表示したい。

  • モジュールを起動すると、
$ jython -i wc02_JTextArea.py
>>> 

2010 FIFA World Cup 南アフリカ大会の開催国チーム South Africa (rsa) の旗とチーム名に加えて、そのプロフィールが表示されます。

  • 各チーム(リスト項目)を選択すると、

ここでは、日本チーム Japan (jpn) に関する情報が表示されます。

 ↑ TOP

》作業中です《
Created: 2003/12/29|Last updated: 2013/04/04 12:26:31

■ 余録:ソースコード

 ↑ TOP

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

## ----------------------------------------
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(
            listData = teams(),
            minimumSize = (100,50),
            valueChanged = self,
            )
        canvasPane = self._canvasPane(
            minimumSize = (100,50),
            )
        master.contentPane = self._splitPane(
            leftComponent  = listPane,
            rightComponent = canvasPane,
            )
        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, minimumSize, **keys):
        comp = JPanel(
            layout = BorderLayout(),
            )
        comp.add(
            self._labelPane(),
            BorderLayout.NORTH,
            )
        comp.add(
            self._textPane(minimumSize),
            BorderLayout.CENTER,
            )
        return comp

    def _labelPane(self, **keys):
        def view():
            self.label = \
            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, minimumSize, **keys):
        def view():
            self.textArea = \
            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 = 120,
            **keys
            )
        return comp

    ## ----------------------------------------
    def __call__(self, e):  # ListSelectionListener.valueChanged
        s = e.source.selectedValue
        self._update(s)

    def _update(self, s=None):
        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
        def contents(path):
            if exists(path):
                return "".join([e for e in open(path)])
            else:
                return None

        path = dirname(argv[-1])

        self.label.icon = imageIcon("%s/squad/%s.gif"%(path, s))

        s = name(s)
        self.label.text = s

        text = contents("%s/profile/%s.txt"%(path, s))
        self.textArea.text = text if text else "*** File not found ***"
        self.textArea.caretPosition = 0

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