alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Container.scala)

This example Scala source code file (Container.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

bufferwrapper, component, component, container, container, content, content, int, int, publisher, seq, uielement, wrapper

The Scala Container.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2007-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */



package scala.swing

import event._
import scala.collection.mutable.Buffer

object Container {
  /**
   * Utility trait for wrapping containers. Provides an immutable 
   * implementation of the contents member.
   */
  trait Wrapper extends Container with Publisher {
    override def peer: javax.swing.JComponent
    
    protected val _contents = new Content
    def contents: Seq[Component] = _contents
  
    protected class Content extends BufferWrapper[Component] {
      override def clear() { peer.removeAll() }
      override def remove(n: Int): Component = { 
        val c = peer.getComponent(n)
        peer.remove(n)
        UIElement.cachedWrapper[Component](c)
      }
      protected def insertAt(n: Int, c: Component) { peer.add(c.peer, n) }
      def +=(c: Component): this.type = { peer.add(c.peer) ; this }
      def length = peer.getComponentCount
      def apply(n: Int) = UIElement.cachedWrapper[Component](peer.getComponent(n))
    }
  
    peer.addContainerListener(new java.awt.event.ContainerListener {
      def componentAdded(e: java.awt.event.ContainerEvent) { 
        publish(ComponentAdded(Wrapper.this, 
          UIElement.cachedWrapper[Component](e.getChild.asInstanceOf[javax.swing.JComponent]))) 
      }
      def componentRemoved(e: java.awt.event.ContainerEvent) { 
        publish(ComponentRemoved(Wrapper.this, 
          UIElement.cachedWrapper[Component](e.getChild.asInstanceOf[javax.swing.JComponent]))) 
      }
    })
  }
}

/**
 * The base traits for UI elements that can contain <code>Components.
 * 
 * @note [Java Swing] This is not the wrapper for java.awt.Container but a trait 
 * that extracts a common interface for components, menus, and windows.
 */
trait Container extends UIElement {
  /**
   * The child components of this container.
   */
  def contents: Seq[Component]
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Container.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.