gof/Command/fullCommand/scala/FullCommand.scala
1: //..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 2: /* 3: * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. 4: */ 5: package components 6: 7: import java.awt.Color 8: import java.awt.FileDialog 9: import swing._ 10: import swing.event._ 11: 12: // ---------------------------------------- 13: object FullCommand { 14: val version = FullCommand 15: .getClass.getName+": #1.0.12" 16: } 17: 18: // ---------------------------------------- Command::Client 19: class FullCommand(val frame: Frame) extends FlowPanel { 20: 21: def createMenuBar = { 22: val menuBar = new MenuBar 23: val menu = new Menu( 24: "File") { 25: menuBar.contents += this 26: } 27: new CmdMenu( 28: "Open...") { 29: command = new FileCommand(frame) 30: menu.contents += this 31: } 32: new CmdMenu( 33: "Exit") { 34: command = new ExitCommand(frame) 35: menu.contents += this 36: } 37: menuBar 38: } 39: 40: def panel: Panel = this 41: new CmdButton( 42: "Red") { 43: command = new RedCommand(panel) 44: contents += this 45: } 46: 47: frame.title = "Frame with external commands" 48: frame.menuBar = createMenuBar 49: } 50: 51: // ---------------------------------------- Command::Invoker 52: trait CommandHolder extends Publisher { 53: private var _command: Command = Command.NoCommand 54: def command: Command = _command 55: def command_=(a: Command) { 56: _command = a 57: command.listenTo(this) 58: } 59: } 60: 61: class CmdButton(override val text: String) 62: extends Button(text) with CommandHolder 63: 64: class CmdMenu(override val text: String) 65: extends MenuItem(text) with CommandHolder 66: 67: // ---------------------------------------- Command::Command 68: object Command { 69: case object NoCommand extends Command { 70: reactions += { 71: case ButtonClicked(source) => 72: println(":: ButtonClicked") 73: } 74: } 75: } 76: class Command extends Publisher 77: 78: // ---------------------------------------- Command::ConcreteCommand 79: // ---------------------------------------- Command::Receiver 80: class RedCommand(val panel: Panel) extends Command { 81: reactions += { 82: case ButtonClicked(source) => 83: panel.background = Color.red 84: } 85: } 86: 87: class FileCommand(val frame: Frame) extends Command { 88: reactions += { 89: case ButtonClicked(source) => 90: val dialog = new FileDialog(frame.peer, "Open file") 91: dialog.setVisible(true) 92: } 93: } 94: 95: class ExitCommand(val frame: Frame) extends Command { 96: reactions += { 97: case ButtonClicked(source) => 98: frame.closeOperation 99: } 100: } 101: 102: // ========================================
gof/Command/fullCommand/scala/Tips.scala
1: //..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 2: 3: /*'****************************************'demo: scala 4: cd /Users/sketch/book/java_useBetter_1/gof/Command/fullCommand/scala/ 5: 6: scalac -d bin {Tips,FullCommand}.scala 7: scala -cp bin components.Tips; date 8: $ 9: ---------------------------------------- 10: components.Tips$: #1.0.04 11: components.ButtonDemo$: #1.0.29a 12: ---------------------------------------- 13: $ 14: '****************************************'*/ 15: package components 16: 17: import components._ 18: import scala.swing._ 19: 20: // ---------------------------------------- 21: object Tips extends SimpleSwingApplication { 22: val version = Tips 23: .getClass.getName+": #1.0.02" 24: 25: println("-"*40) 26: println(Tips.version) 27: println(FullCommand.version) 28: println("-"*40) 29: 30: def top = new MainFrame { 31: title = "FullCommand" 32: contents = new FullCommand(this) 33: 34: // peer.setLocationRelativeTo(null) 35: peer.setBounds(new Rectangle(100,100,200,100)) 36: } 37: } 38: 39: // ========================================