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: // ========================================