summaryrefslogtreecommitdiff
path: root/src/swing/scala/swing/ScrollPane.scala
blob: 966d5889e263223f560c46fa03f05e685481b67e (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2007-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.swing

import javax.swing.{JScrollPane, ScrollPaneConstants}

object ScrollPane {
  object BarPolicy extends Enumeration {
    import ScrollPaneConstants._
    val AsNeeded = new Value(HORIZONTAL_SCROLLBAR_AS_NEEDED,
                             VERTICAL_SCROLLBAR_AS_NEEDED)
    val Never = new Value(HORIZONTAL_SCROLLBAR_NEVER,
                          VERTICAL_SCROLLBAR_NEVER)
    val Always = new Value(HORIZONTAL_SCROLLBAR_ALWAYS,
                           VERTICAL_SCROLLBAR_ALWAYS)

    def wrap(id: Int) = id match {
      case HORIZONTAL_SCROLLBAR_AS_NEEDED | VERTICAL_SCROLLBAR_AS_NEEDED => AsNeeded
      case HORIZONTAL_SCROLLBAR_NEVER | VERTICAL_SCROLLBAR_NEVER => Never
      case HORIZONTAL_SCROLLBAR_ALWAYS | VERTICAL_SCROLLBAR_ALWAYS => Always
    }
    class Value(val horizontalPeer: Int, val verticalPeer: Int) extends super.Val {
      override def id = horizontalPeer
    }
  }
}

/**
 * Can have at most a single child component, which will be put inside a canvas (the viewport)
 * that can be scrolled.
 *
 * @see javax.swing.JScrollPane
 */
class ScrollPane extends Component with Container {
  import ScrollPane._

  override lazy val peer: JScrollPane = new JScrollPane with SuperMixin
  def this(c: Component) = {
    this()
    contents = c
  }
  def contents: Seq[Component] =
    List(UIElement.cachedWrapper[Component](peer.getViewport.getView.asInstanceOf[javax.swing.JComponent]))

  /**
   * Sets the single child.
   */
  def contents_=(c: Component) { peer.setViewportView(c.peer) }

  /**
   * The component being displayed in this pane's row header.
   *
   * If you want to create a row header for lists or tables, you probably
   * want to let the row header be a list view with the same row height as
   * the viewport component.
   */
  def rowHeaderView: Option[Component] =
    Option(peer.getRowHeader.getView) map UIElement.cachedWrapper[Component]
  def rowHeaderView_=(c: Component) = peer.setRowHeaderView(c.peer)
  def rowHeaderView_=(c: Option[Component]) = peer.setRowHeaderView(c.map(_.peer).orNull)

  def columnHeaderView: Option[Component] =
    Option(peer.getColumnHeader.getView) map UIElement.cachedWrapper[Component]
  def columnHeaderView_=(c: Component) = peer.setColumnHeaderView(c.peer)
  def columnHeaderView_=(c: Option[Component]) = peer.setColumnHeaderView(c.map(_.peer).orNull)

  def viewportView: Option[Component] =
    Option(peer.getViewport.getView) map UIElement.cachedWrapper[Component]
  def viewportView_=(c: Component) = peer.setViewportView(c.peer)
  def viewportView_=(c: Option[Component]) = peer.setViewportView(c.map(_.peer).orNull)

  def verticalScrollBarPolicy = BarPolicy.wrap(peer.getVerticalScrollBarPolicy)
  def verticalScrollBarPolicy_=(p: BarPolicy.Value) = peer.setVerticalScrollBarPolicy(p.verticalPeer)

  def horizontalScrollBarPolicy = BarPolicy.wrap(peer.getHorizontalScrollBarPolicy)
  def horizontalScrollBarPolicy_=(p: BarPolicy.Value) = peer.setHorizontalScrollBarPolicy(p.horizontalPeer)

  def horizontalScrollBar = ScrollBar.wrap(peer.getHorizontalScrollBar)
  def verticalScrollBar = ScrollBar.wrap(peer.getVerticalScrollBar)
}