summaryrefslogtreecommitdiff
path: root/src/swing/scala/swing/BorderPanel.scala
blob: 75bb721ffc2f817aa11703c5cbd8181c0c4c839f (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2007-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.swing

import java.awt.BorderLayout

object BorderPanel {
  /**
   * The position of a component in a <code>BorderPanel</code>
   */
  object Position extends Enumeration {
    val North = Value(BorderLayout.NORTH)
    val South = Value(BorderLayout.SOUTH)
    val West = Value(BorderLayout.WEST)
    val East = Value(BorderLayout.EAST)
    val Center = Value(BorderLayout.CENTER)
  }
  private[swing] def wrapPosition(s: String): Position.Value = s match {
    case BorderLayout.NORTH => Position.North
    case BorderLayout.SOUTH => Position.South
    case BorderLayout.WEST => Position.West
    case BorderLayout.EAST => Position.East
    case BorderLayout.CENTER => Position.Center
  }
}

/**
 * A container that arranges its children around a central component that
 * takes most of the space. The other children are placed on one of four
 * borders: north, east, south, west.
 *
 * @see javax.swing.BorderLayout
 */
class BorderPanel extends Panel with LayoutContainer {
  import BorderPanel._
  def layoutManager = peer.getLayout.asInstanceOf[BorderLayout]
  override lazy val peer = new javax.swing.JPanel(new BorderLayout) with SuperMixin

  type Constraints = Position.Value

  protected def constraintsFor(comp: Component) =
    wrapPosition(layoutManager.getConstraints(comp.peer).asInstanceOf[String])

  protected def areValid(c: Constraints): (Boolean, String) = (true, "")
  protected def add(c: Component, l: Constraints) {
    // we need to remove previous components with the same constraints as the new one,
    // otherwise the layout manager loses track of the old one
    val old = layoutManager.getLayoutComponent(l.toString)
    if(old != null) peer.remove(old)
    peer.add(c.peer, l.toString)
  }
}