1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import    scala.swing.{BorderPanel, Button, ListView, FlowPanel, MainFrame, Menu, MenuBar, MenuItem, ScrollPane, SimpleGUIApplication}
import    scala.swing.event.{ActionEvent, ButtonClicked}

class HandGrepFrame(var lines:List[String]) extends MainFrame {
    
    title = "Hand Grep"
    
    menuBar = new MenuBar
    
    val    menu:Menu = new Menu("file")
    val    quitMenu:MenuItem = new MenuItem("quit")
    
    menu.contents += quitMenu
    menuBar.contents += menu
    
    val    list:ListView[String] = new ListView(lines)
    val    quit:Button = new Button("quit")
    
    contents = new BorderPanel {
        
        import BorderPanel.Position._
        
        layout(new ScrollPane(list)) = Center
        layout(new FlowPanel(FlowPanel.Alignment.Right) { contents += quit }) = South
    }
    
    listenTo(quitMenu, quit)
    reactions += {
        case ActionEvent(`quitMenu`) | ButtonClicked(`quit`) => quitHandler
    }
    
    preferredSize = (600, 480)
    pack
    
    def quitHandler:Unit = {
        list.selection.items.foreach(Console.println)
        System.exit(0)
    }
}

object HandGrep extends SimpleGUIApplication {
    val    lines:List[String] = readLines
    def readLines:List[String] = Console.in.ready match {
            case false => List[String]()
            case _ => Console.readLine :: readLines
        }
    def top = new HandGrepFrame(lines)
}