Changeset 6913 for lang/scala

Show
Ignore:
Timestamp:
02/19/08 15:13:24 (9 months ago)
Author:
kmizu
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/scala/sandbox/src/jp/gr/java_conf/mizu/gui/GUIBuilder.scala

    r6906 r6913  
    22 
    33import scala.util.DynamicVariable 
    4 import java.awt._ 
    5 import javax.swing._ 
     4import java.awt.Container 
     5import java.awt.event.ActionEvent 
     6import javax.swing.JFrame 
     7import javax.swing.JButton 
     8import javax.swing.Action 
     9import javax.swing.AbstractAction 
     10import jp.gr.java_conf.mizu.util.ReflectionWrapper._ 
    611 
    7 trait GUIBuilder { 
    8   val container = new DynamicVariable[Container](null) 
    9   def Frame(params :Any*)(thunk : => Unit) : JFrame = { 
     12class GUIBuilder { 
     13  val context :DynamicVariable[AnyRef] = new DynamicVariable[AnyRef](null) 
     14        def setProperty(target :AnyRef, property :Symbol, value :Any) { 
     15          invoke(context, "set" + property.name.capitalize, value) 
     16        } 
     17        def getProperty(target :AnyRef, property :Symbol, value :Any) :Any = { 
     18          invoke(context, "set" + property.name.capitalize, value) 
     19        } 
     20  def frame(params :Any*)(thunk : => Unit) : JFrame = { 
    1021    val frame = new JFrame 
    1122    params.foreach { 
    1223    case title:String => frame.setTitle(title) 
    13     case (property:Symbol, value:Any) => () 
    14     case _ => () 
     24    case (property:Symbol, value:Any) => setProperty(frame, property, value) 
    1525    } 
    16     container.withValue(frame)(thunk) 
     26    context.withValue(frame)(thunk) 
    1727    frame 
    1828  } 
     29        def button(params :Any*) :JButton = { 
     30          val button = new JButton 
     31                context.value match { 
     32                case frame:JFrame => frame.getContentPane.add(button) 
     33                case container:Container => container.add(button) 
     34                } 
     35                params.foreach { 
     36                case text:String => button.setText(text) 
     37                case (property:Symbol, value:Any) => setProperty(button, property, value) 
     38                } 
     39                button 
     40        } 
     41        def action(handler :ActionEvent => Unit) :Action = { 
     42          new AbstractAction { 
     43                  def actionPerformed(e :ActionEvent) = handler(e) 
     44                } 
     45        } 
    1946}