Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions src/main/scala/scala/swing/group/Alignments.scala

This file was deleted.

33 changes: 0 additions & 33 deletions src/main/scala/scala/swing/group/BaselineAnchors.scala

This file was deleted.

52 changes: 52 additions & 0 deletions src/main/scala/scala/swing/group/BaselineCustomization.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2007-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */


package scala.swing.group

import swing._
import javax.swing.{ GroupLayout, JPanel }
import java.awt.{ Component => AWTComp }
import scala.annotation.tailrec

/** Allows to assign a baseline reference, i.e. a component that provides the
* baseline of the panel instead of the panel itself (which has no meaningful
* baseline by default).
*
* @author Andreas Flierl
*/
trait BaselineCustomization { this: Panel =>
private[this] var customBaseline: Option[Component] = None

object theBaselineReference {
def is(c: Component): Unit = {
if (! peer.isAncestorOf(c.peer)) throw new IllegalArgumentException(
"component to be used as baseline reference must be a descendant of this GroupPanel")
customBaseline = Some(c)
}
}

def theDefaultBaselineIsUsed(): Unit = customBaseline = None

/** The swing `JPanel` wrapped by this `Panel`. */
override lazy val peer: JPanel = new JPanel with SuperMixin {
override def getBaseline(w: Int, h: Int): Int = {
customBaseline.map { c =>
setSize(w, h)
validate
sumOfOffsets(c.peer, c.peer.getBaseline(c.peer.getWidth, c.peer.getHeight))
}.getOrElse(super.getBaseline(w, h))
}

@tailrec
private[this] def sumOfOffsets(c: AWTComp, y: Int): Int = {
if (c == this || ! c.getParent.isInstanceOf[AWTComp]) y
else sumOfOffsets(c.getParent.asInstanceOf[AWTComp], y + c.getY)
}
}
}
142 changes: 61 additions & 81 deletions src/main/scala/scala/swing/group/ComponentsInGroups.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2007-2010, LAMP/EPFL **
** / __/ __// _ | / / / _ | (c) 2007-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
Expand All @@ -18,32 +18,28 @@ import javax.swing.GroupLayout
*
* @author Andreas Flierl
*/
trait ComponentsInGroups extends SizeTypes { this: GroupPanel =>
/**
* Implicit conversion that puts a component into the correct context on demand.
*/
trait ComponentsInGroups { this: GroupPanel =>
/** Implicit conversion that puts a component into the correct context on demand. */
protected final implicit def add[A <: G](comp: Component) =
new ComponentInGroup[A](comp)

/** Triplet of minimum, preferred and maximum size. */
private[group] case class Sizes(min: Size, pref: Size, max: Size)

/**
* Wraps an arbitrary component so that it may appear within a group of
* type `A`.
*/
/** Wraps an arbitrary component so that it may appear within a group of
* type `A`.
*/
protected final class ComponentInGroup[A <: G](comp: Component)
extends InGroup[A] with SizeHelpers[A] {

override private[group] def build(parent: A) = parent.addComponent(comp.peer)

/**
* Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
/** Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
def sized(min: Size, pref: Size, max: Size) =
new ComponentWithSize[A](comp, Sizes(min, pref, max))

Expand All @@ -57,18 +53,15 @@ trait ComponentsInGroups extends SizeTypes { this: GroupPanel =>
*/
def asBaseline = new ComponentInSequential(comp, None, true)

/**
* Specifies an alignment for this component. May only be used inside a
* parallel group.
*
* @param newAlign the alignment to use
*/
/** Specifies an alignment for this component. May only be used inside a
* parallel group.
*
* @param newAlign the alignment to use
*/
def aligned(newAlign: Alignment) = new ComponentInParallel(comp, None, newAlign)
}

/**
* Wraps an arbitrary component to allow for custom size constraints.
*/
/** Wraps an arbitrary component to allow for custom size constraints. */
protected final class ComponentWithSize[A <: G](comp: Component,
sizes: Sizes) extends InGroup[A] {
override private[group] def build(parent: A) =
Expand All @@ -85,21 +78,19 @@ trait ComponentsInGroups extends SizeTypes { this: GroupPanel =>
*/
def asBaseline = new ComponentInSequential(comp, Some(sizes), true)

/**
* Specifies an alignment for this component. May only be used inside a
* parallel group.
*
* @param newAlign the alignment to use
*/
/** Specifies an alignment for this component. May only be used inside a
* parallel group.
*
* @param newAlign the alignment to use
*/
def aligned(newAlign: Alignment) =
new ComponentInParallel(comp, Some(sizes), newAlign)
}

/**
* Wraps a GUI component so that it may appear in a sequential group.
*
* @see javax.swing.GroupLayout.SequentialGroup
*/
/** Wraps a GUI component so that it may appear in a sequential group.
*
* @see javax.swing.GroupLayout.SequentialGroup
*/
protected final class ComponentInSequential(comp: Component,
sizes: Option[Sizes], useAsBaseline: Boolean)
extends InSequential with SizeHelpers[GroupLayout#SequentialGroup] {
Expand All @@ -110,22 +101,20 @@ trait ComponentsInGroups extends SizeTypes { this: GroupPanel =>
sizes.get.pref.pixels, sizes.get.max.pixels)
else parent.addComponent(useAsBaseline, comp.peer)

/**
* Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
/** Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
def sized(min: Size, pref: Size, max: Size) =
new ComponentInSequential(comp, Some(Sizes(min, pref, max)), useAsBaseline)
}

/**
* Wraps a GUI component so that it may appear in a parallel group.
*
* @see javax.swing.GroupLayout.ParallelGroup
*/
/** Wraps a GUI component so that it may appear in a parallel group.
*
* @see javax.swing.GroupLayout.ParallelGroup
*/
protected final class ComponentInParallel(comp: Component,
sizes: Option[Sizes], align: Alignment)
extends InParallel with SizeHelpers[GroupLayout#ParallelGroup] {
Expand All @@ -136,53 +125,44 @@ trait ComponentsInGroups extends SizeTypes { this: GroupPanel =>
sizes.get.pref.pixels, sizes.get.max.pixels)
else parent.addComponent(comp.peer, align.wrapped)

/**
* Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
/** Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
def sized(min: Size, pref: Size, max: Size) =
new ComponentInParallel(comp, Some(Sizes(min, pref, max)), align)
}

/**
* Additional methods for nicer control over resizing behaviour.
*/
/** Additional methods for nicer control over resizing behaviour. */
private[group] sealed trait SizeHelpers[A <: G] {
/**
* Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
/** Specifies size constraints for this component.
*
* @param min minimum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param pref preferred size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
* @param max maximum size >= 0 (or one of `UseDefault`, `UsePreferred` and `Infinite`)
*/
def sized(min: Size, pref: Size, max: Size): InGroup[A]

/**
* Fixes the size of this component to its default size.
*/
/** Fixes the size of this component to its default size. */
def fixedToDefaultSize = sized(UsePreferred, UseDefault, UsePreferred)

/**
* Fixes the size of this component to the specified size.
*
* @param size the desired size in pixels
*/
/** Fixes the size of this component to the specified size.
*
* @param size the desired size in pixels
*/
def sized(size: Size): InGroup[A] = sized(UsePreferred, size, UsePreferred)

/**
* Forces this component to be resizable (useful e.g. for buttons). Its
* minimum size is set to its default size.
*/
/** Forces this component to be resizable (useful e.g. for buttons). Its
* minimum size is set to its default size.
*/
def resizable(min: Size = UseDefault, pref: Size = UseDefault, max: Size = Infinite) =
sized(min, pref, max)

/**
* Forces this component to be resizable (useful e.g. for buttons). Its
* minimum size is set to 0 pixels.
*/
/** Forces this component to be resizable (useful e.g. for buttons). Its
* minimum size is set to 0 pixels.
*/
def fullyResizable(pref: Size = UseDefault) = sized(0, pref, Infinite)
}
}
Loading