toolBarDemo/scala/ToolBarDemo.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.BorderLayout
  8: import javax.swing.ImageIcon
  9: import javax.swing.JToolBar
 10: 
 11: import swing._
 12: import swing.event._
 13: import BorderPanel._
 14: 
 15: // ----------------------------------------
 16: object ToolBarDemo {
 17:   val version =          ToolBarDemo
 18:     .getClass.getName+": #1.0.30"
 19: }
 20: 
 21: // ----------------------------------------
 22: class ToolBarDemo extends BorderPanel {
 23: // implements ActionListener
 24: 
 25:   //Create the toolbar.
 26:   val toolBar = new JToolBar("Still draggable") {
 27:     addButtons(this)
 28:     peer.add(this, BorderLayout.PAGE_START)
 29:   }
 30: 
 31:   def addButtons(toolBar: JToolBar) = {
 32:     val seq = Array(
 33:       Array(                                //first button
 34:         "Back24",
 35:         "taken you to the previous .",
 36:         "Back to previous something-or-other",
 37:         "Previous"),
 38:       Array(                                //second button
 39:         "Up24",
 40:         "taken you up one level to .",
 41:         "Up to something-or-other",
 42:         "Up"),
 43:       Array(                                //third button
 44:         "Forward24",
 45:         "taken you to the next .",
 46:         "Forward to something-or-other",
 47:         "Next"))
 48: 
 49:     for (Array(imageName, command, toolTipText, altText) <- seq) {
 50:       var button = makeNavigationButton(
 51:         imageName, command, toolTipText, altText)
 52:       toolBar.add(button.peer)
 53:     }
 54:   }
 55: 
 56:   lazy val panel: ToolBarDemo = this
 57:   def makeNavigationButton(
 58:     imageName: String,
 59:     actionCommand: String,
 60:     toolTipText: String,
 61:     altText: String) = {
 62: 
 63:     //Create and initialize the button.
 64:     new CmdButton("") {
 65:       command = new TextCommand(panel, actionCommand)
 66:       tooltip = toolTipText
 67: 
 68:       //Look for the image.
 69:       val imgLocation = "images/%s.gif" format imageName
 70:       val imageURL = ToolBarDemo.getClass.getResource(imgLocation)
 71:       if (imageURL != null) {
 72:         this.icon = new ImageIcon(imageURL, altText)
 73:       } else {
 74:         this.peer.setText(altText)
 75:         System.err.println("Resource not found: " + imgLocation)
 76:       }
 77:     }
 78:   }
 79: 
 80:   //Create the text area used for output.  Request
 81:   //enough space for 5 rows and 30 columns.
 82:   val textArea = new TextArea(5,30) {
 83:     editable = false
 84:   }
 85:   val scrollPane = new ScrollPane(textArea) {
 86:     add(this, Position.Center)
 87:   }
 88: }
 89: 
 90: // ---------------------------------------- Command::Invoker
 91: trait CommandHolder extends Publisher {
 92:   private var _command: Command = Command.NoCommand
 93:   def command: Command = _command
 94:   def command_=(a: Command) {
 95:     _command = a
 96:     command.listenTo(this)
 97:   }
 98: }
 99: 
100: class CmdButton(override val text: String)
101:   extends Button(text) with CommandHolder
102: 
103: // ---------------------------------------- Command::Command
104: object Command {
105:   case object NoCommand extends Command {
106:     reactions += {
107:       case ButtonClicked(source) =>
108:         println(":: ButtonClicked")
109:     }
110:   }
111: }
112: class Command extends Publisher
113: 
114: // ---------------------------------------- Command::ConcreteCommand
115: // ---------------------------------------- Command::Receiver
116: class TextCommand(
117:   val panel: ToolBarDemo,
118:   val description: String) extends Command {
119: 
120:   // Handle each button.
121:   reactions += {
122:     case ButtonClicked(source) =>
123:       displayResult(
124:         "If this were a real app,\n  it would have %s"
125:         format description)
126:   }
127:   def displayResult(actionDescription: String) {
128:     val textArea = panel.textArea
129:     textArea.append("%s\n" format actionDescription)
130:     textArea.caret.position = textArea.peer.getDocument.getLength
131:   }
132: }
133: 
134: // ========================================