splitPaneDemo/scala/SplitPaneDemo.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.Dimension
  8: import java.awt.Font
  9: import javax.swing.ImageIcon
 10: import swing._
 11: import swing.event._
 12: import ListView._
 13: 
 14: // ----------------------------------------
 15: object SplitPaneDemo {
 16:   val version =          SplitPaneDemo
 17:     .getClass.getName+": #1.0.22"
 18: }
 19: 
 20: // ----------------------------------------
 21: //SplitPaneDemo itself is not a visible component.
 22: class SplitPaneDemo extends FlowPanel {
 23: // implements ListSelectionListener
 24: 
 25:   val imageNames = Array(
 26:     "Bird", "Cat", "Dog", "Rabbit", "Pig", "dukeWaveRed",
 27:     "kathyCosmo", "lainesTongue", "left", "middle", "right", "stickerface")
 28: 
 29:   //Create the list of images and put it in a scroll pane.
 30:   def list = new ListView(imageNames) {
 31:     selection.intervalMode = IntervalMode.Single
 32:     peer.setSelectedIndex(0)
 33:     Command.listenTo(selection)
 34:   }
 35:   val listScrollPane = new ScrollPane(list) {
 36:     minimumSize = _minimumSize
 37:   }
 38: 
 39:   val picture = new Label() {
 40:     font = font.deriveFont(Font.ITALIC)
 41:     horizontalAlignment = Alignment.Center
 42:   }
 43:   val pictureScrollPane = new ScrollPane(picture) {
 44:     minimumSize = _minimumSize
 45:   }
 46: 
 47:   //Provide minimum sizes for the two components in the split pane.
 48:   val _minimumSize = new Dimension(100, 50)
 49: 
 50:   //Create a split pane with the two scroll panes in it.
 51:   val splitPane = new SplitPane(
 52:     Orientation.Vertical,
 53:     listScrollPane,
 54:     pictureScrollPane) {
 55:     oneTouchExpandable = true
 56:     dividerLocation = 150
 57:     preferredSize = new Dimension(400, 200)
 58:     SplitPaneDemo.this.contents += this
 59:   }
 60: 
 61:   //Provide a preferred size for the split pane.
 62:   updateLabel(imageNames(list.selection.leadIndex))
 63: 
 64:   // ----------------------------------------
 65:   //Listens to the list
 66:   //  valueChanged(ListSelectionEvent)
 67:   object Command extends Publisher {
 68:     reactions += { 
 69:       case ListSelectionChanged(source, range, live) => 
 70:         updateLabel(imageNames(source.selection.leadIndex))
 71:     }
 72:   }
 73: 
 74:   //Renders the selected image
 75:   def updateLabel(name: String) {
 76:     val icon = createImageIcon("images/%s.gif" format name)
 77:     picture.icon = icon
 78:     picture.text = if (icon != null) null else "Image not found"
 79:   }
 80: 
 81: /*'****************************************'
 82:     //Used by SplitPaneDemo2
 83:     public JList getImageList() {
 84:         return list;
 85:     }
 86:     public JSplitPane getSplitPane() {
 87:         return splitPane;
 88:     }
 89: '****************************************'*/
 90: 
 91:   /** Returns an ImageIcon, or null if the path was invalid. */
 92:   def createImageIcon(path: String): ImageIcon = {
 93:     val imgURL = SplitPaneDemo.getClass.getResource(path)
 94:     if (imgURL != null) {
 95:       return new ImageIcon(imgURL)
 96:     } else {
 97:       System.err.println("Couldn't find file: " + path)
 98:       return null
 99:     }
100:   }
101: }
102: 
103: // ========================================