余録:worldCup/scala/ex08/wcFrame.scala

  1: //..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
  2: 
  3: /* ---------------------------------------- demo: scala
  4: cd /Users/sketch/home_sketch/worldCup/scala/
  5: 
  6: scalac ex08/wcFrame.scala
  7: scala WorldCup
  8: */
  9: 
 10: // ----------------------------------------
 11: import swing._
 12: import swing.event._
 13: 
 14: object WorldCup extends SimpleSwingApplication {
 15:   println("version: #1.0.47")
 16: 
 17:   def top = new MainFrame {
 18:     title = "FIFA World Cup #08"
 19:     contents = TopPanel
 20:   }
 21: }
 22: 
 23: // ----------------------------------------
 24: import javax.swing.ImageIcon
 25: 
 26: object TopPanel extends FlowPanel {
 27:   val groups = Map(
 28:     "A" -> Map(                    // Group A
 29:       "rsa" -> "South Africa",
 30:       "mex" -> "Mexico",
 31:       "uru" -> "Uruguay",
 32:       "fra" -> "France"
 33:     ),
 34:     "B" -> Map(                    // Group B
 35:       "arg" -> "Argentina",
 36:       "nga" -> "Nigeria",
 37:       "kor" -> "Korea Republic",
 38:       "gre" -> "Greece"
 39:     ),
 40:     "C" -> Map(                    // Group C
 41:       "eng" -> "England",
 42:       "usa" -> "USA",
 43:       "alg" -> "Algeria",
 44:       "svn" -> "Slovenia"
 45:     ),
 46:     "D" -> Map(                    // Group D
 47:       "ger" -> "Germany",
 48:       "aus" -> "Australia",
 49:       "srb" -> "Serbia",
 50:       "gha" -> "Ghana"
 51:     ),
 52:     "E" -> Map(                    // Group E
 53:       "ned" -> "Netherlands",
 54:       "den" -> "Denmark",
 55:       "jpn" -> "Japan",
 56:       "cmr" -> "Cameroon"
 57:     ),
 58:     "F" -> Map(                    // Group F
 59:       "ita" -> "Italy",
 60:       "par" -> "Paraguay",
 61:       "nzl" -> "New Zealand",
 62:       "svk" -> "Slovakia"
 63:     ),
 64:     "G" -> Map(                    // Group G
 65:       "bra" -> "Brazil",
 66:       "prk" -> "Korea DPR",
 67:       "civ" -> "Cote d Ivoire",
 68:       "por" -> "Portugal"
 69:     ),
 70:     "H" -> Map(                    // Group H
 71:       "esp" -> "Spain",
 72:       "sui" -> "Switzerland",
 73:       "hon" -> "Honduras",
 74:       "chi" -> "Chile"
 75:     )
 76:   )
 77:   def _leftComponent = {
 78:     import scala.collection.mutable
 79:     def teamNames = {
 80:       val map = mutable.Map[String,String]()
 81:       for {
 82:         (group, teams) <- groups
 83:         (team, name) <- teams
 84:       } map += (team -> name)
 85:       map
 86:     }
 87:     def view = new ListView(teamNames.keys.toList sortWith (_ < _)) {
 88:       reactions += { 
 89:         case ListSelectionChanged(source, range, live) => 
 90:           val value = source.selection.items(0).toString
 91:           println(":: %s" format teamNames(value))
 92:       }
 93:       listenTo(this.selection)
 94:     }
 95:     new ScrollPane {
 96:       viewportView = view
 97:       preferredSize = new Dimension(120,150)
 98:     }
 99:   }
100:   def _rightComponent = {
101:     def view = new Label {
102:       icon = new ImageIcon("matches/fifa.png")
103:     }
104:     new ScrollPane {
105:       viewportView = view
106:       preferredSize = new Dimension(180,0)
107:     }
108:   }
109:   contents += new SplitPane {
110:     orientation = Orientation.Vertical
111:     oneTouchExpandable = true
112:     dividerLocation = 100
113:     leftComponent  = _leftComponent
114:     rightComponent = _rightComponent
115:   }
116: }
117: 
118: // ========================================

Java の素描 #008: Map を利用する:scala.collection.Map

記事一覧 Java.use(better, Swing=Scala) #FIFA World Cup への道《Scala2.8.0》

Java の素描 Scala 弾丸ツアー:時短プログラミング生活のすすめ 〜

《著》小粒ちゃん@湘南組《監修》タマゴ倶楽部

第1版♪2003/05/23 ● 第2版♪2006/04/03 ● 第3版♪2010/06/11● 第4版♪2010/07/14

》作業中です《

step08: Map を利用する:scala.collection.Map

辞書を利用して、各チームの略号と正式名称とを対応させます。

■ 要求仕様: 全チームの一覧表を作成する

全32チームの一覧表(リスト項目)を作成したいとします。各項目を選択すると、チームの情報が得られるようにします。それには、JList を利用すると便利です。

■ JList の動作を確認する

任意の項目を提示して、各項目を選択したときの動作を規定します。チームの略号を列挙した項目を選択すると、その正式名称が得られるようにします。

■ プログラムを変更する
## ---------------------------------------- wcFrame.py
class TopPanel(JPanel):
    groups = {
        "A": [                          # Group A
            {"rsa": "South Africa"},
            {"mex": "Mexico"},
            {"uru": "Uruguay"},
            {"fra": "France"},
            ],
        ...
        }

    def __init__(self, master, *args, **keys):
        ...
        def teams():
            return sorted(team
                for group in "ABCDEFGH"
                for teams in self.groups[group]
                for team in teams)

    def __call__(self, e):  # javax.swing.event.ListSelectionEvent
        value = e.source.selectedValue
        teamNames = dict*1
        print(":: %s"%teamNames[value])
■ プログラムを実行する
$ jython -i ex08/wcFrame.py
>>> :: Algeria
Algeria
Japan
Japan

リスト項目には、各チームの略号が整列して(アルファベット順に)表示されます。ここで、リスト項目から "jpn" を選択すると、日本チームの正式名称 Japan が出力されます。

》作業中です《

  • 組み込み関数 sorted を利用すると、
  • 組み込み関数 dict を利用すると、


関連記事

TOP

Last updated♪2010/07/23

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