余録:worldCup/scala/ex09/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 ex09/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.49") 16: 17: def top = new MainFrame { 18: title = "FIFA World Cup #09" 19: contents = TopPanel 20: } 21: } 22: 23: // ---------------------------------------- 24: object WcModel { 25: val groups = Map( 26: "A" -> Map( // Group A 27: "rsa" -> "South Africa", 28: "mex" -> "Mexico", 29: "uru" -> "Uruguay", 30: "fra" -> "France" 31: ), 32: "B" -> Map( // Group B 33: "arg" -> "Argentina", 34: "nga" -> "Nigeria", 35: "kor" -> "Korea Republic", 36: "gre" -> "Greece" 37: ), 38: "C" -> Map( // Group C 39: "eng" -> "England", 40: "usa" -> "USA", 41: "alg" -> "Algeria", 42: "svn" -> "Slovenia" 43: ), 44: "D" -> Map( // Group D 45: "ger" -> "Germany", 46: "aus" -> "Australia", 47: "srb" -> "Serbia", 48: "gha" -> "Ghana" 49: ), 50: "E" -> Map( // Group E 51: "ned" -> "Netherlands", 52: "den" -> "Denmark", 53: "jpn" -> "Japan", 54: "cmr" -> "Cameroon" 55: ), 56: "F" -> Map( // Group F 57: "ita" -> "Italy", 58: "par" -> "Paraguay", 59: "nzl" -> "New Zealand", 60: "svk" -> "Slovakia" 61: ), 62: "G" -> Map( // Group G 63: "bra" -> "Brazil", 64: "prk" -> "Korea DPR", 65: "civ" -> "Cote d Ivoire", 66: "por" -> "Portugal" 67: ), 68: "H" -> Map( // Group H 69: "esp" -> "Spain", 70: "sui" -> "Switzerland", 71: "hon" -> "Honduras", 72: "chi" -> "Chile" 73: ) 74: ) 75: import scala.collection.mutable 76: def teamNames = { 77: val map = mutable.Map[String,String]() 78: for { 79: group <- WcModel.groups.keys 80: teams = WcModel.groups(group) 81: team <- teams.keys 82: } map += (team -> teams(team)) 83: map 84: } 85: } 86: 87: // ---------------------------------------- 88: import javax.swing.ImageIcon 89: 90: object TopPanel extends FlowPanel { 91: def _leftComponent = { 92: def view = new ListView(WcModel.teamNames.keys.toList sortWith (_ < _)) { 93: reactions += { 94: case ListSelectionChanged(source, range, live) => 95: val value = source.selection.items(0).toString 96: println(":: %s" format WcModel.teamNames(value)) 97: } 98: listenTo(this.selection) 99: } 100: new ScrollPane { 101: viewportView = view 102: preferredSize = new Dimension(120,150) 103: } 104: } 105: def _rightComponent = { 106: def view = new Label { 107: icon = new ImageIcon("matches/fifa.png") 108: } 109: new ScrollPane { 110: viewportView = view 111: preferredSize = new Dimension(180,0) 112: } 113: } 114: contents += new SplitPane { 115: orientation = Orientation.Vertical 116: oneTouchExpandable = true 117: dividerLocation = 100 118: leftComponent = _leftComponent 119: rightComponent = _rightComponent 120: } 121: } 122: 123: // ========================================
Java の素描 #009: リファクタリング:クラスを利用する
‖記事一覧‖ 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
|関連記事|
step09: リファクタリング:クラスを利用する
- Java の素描 #009《Jython2.5》
クラスを利用すると、密接に関連するコードの断片を部品(構成要素)として扱えます。
クラスは、複数のメソッド(関数)群を意味のある構成単位として扱う手段を提供します(カプセル化)。
class WcModel: groups = { "A": [ # Group A {"rsa": "South Africa"}, {"mex": "Mexico"}, {"uru": "Uruguay"}, {"fra": "France"}, ], ... } teams = sorted([team for group in "ABCDEFGH" for teams in groups[group] for team in teams]) teamNames = dict*1 class TopPanel(JPanel): def __init__(self, master, *args, **keys): ... def leftComponent(): listData = WcModel.teams ... return comp ... def __call__(self, e): # javax.swing.event.ListSelectionEvent value = e.source.selectedValue print(":: %s"%WcModel.teamNames[value])
クラス属性 teams は、全チームの略称を整列した(アルファベット順に)リストを管理します。
クラス属性 teamNames は、全チームの略称と正式名称とを要素対に持つ、辞書を管理します。
ここでは、リファクタリングの前後で変化がないことを確認します。
↑TOP
》作業中です《
*1:k,v) for group in groups.values() for team in group for k,v in team.items(