Changeset 6953 for lang/scala

Show
Ignore:
Timestamp:
02/20/08 02:09:11 (9 months ago)
Author:
keisuken
Message:

SwingBuilder? updated. SwingCommand? trait add and framework rules fix.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/scala/sandbox/src/jp/ne/cappuccino/keisuken/scl/swing/SwingBuilder.scala

    r6877 r6953  
    1313object SwingBuilder { 
    1414 
     15  trait SwingCommand { 
     16    def exec(commands: AnyRef*): Unit = exec(commands.toArray) 
     17    def exec(commands: Array[AnyRef]): Unit 
     18  } 
     19 
    1520  class ActionAdapter(proc: ActionEvent => Unit) extends ActionListener { 
    1621    def actionPerformed(e: ActionEvent) = proc(e) 
    1722  } 
    1823 
    19   def Frame(commands: AnyRef*): JFrame = Frame(commands.toArray) 
     24  def action(proc: ActionEvent => Unit) = ('action, proc) 
    2025 
    21   def Frame(commands: Array[AnyRef]): JFrame = { 
    22     val frame = new JFrame() 
    23     for(command <- commands) command match { 
    24       case ('title, title: String) => 
    25         frame.setTitle(title) 
    26       case ('exitOnClose, b: Boolean) => 
    27         if(b) 
    28           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
    29         else 
    30           frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) 
    31       case pane: java.awt.Component => 
    32         frame.getContentPane.add(pane) 
    33       case ('size, (width: Int, height: Int)) => 
    34         frame.setSize(width, height) 
    35       case 'pack => 
    36         frame.pack 
    37       case ('visible, b: Boolean) => 
    38         frame.setVisible(b) 
    39       case command: Any => { 
    40         command match { 
    41           case (name: Symbol, value: AnyRef) => 
    42             println("NG: (" + name.getClass.getName + ", " + value.getClass.getName + ")") 
    43           case value: Any => 
    44             println("NG: " + value.getClass.getName) 
     26  class Frame extends JFrame with SwingCommand { 
     27    def this(commands: Array[AnyRef]) { 
     28      this() 
     29      exec(commands) 
     30    } 
     31    def exec(commands: Array[AnyRef]) { 
     32      for(command <- commands) command match { 
     33        case ('title, title: String) => 
     34          setTitle(title) 
     35        case 'exitOnClose => 
     36          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
     37        case pane: java.awt.Component => 
     38          getContentPane.add(pane) 
     39        case ('size, (width: Int, height: Int)) => 
     40          setSize(width, height) 
     41        case 'pack => 
     42          pack 
     43        case ('visible, b: Boolean) => 
     44          setVisible(b) 
     45        case command: Any => { 
     46          command match { 
     47            case (name: Symbol, value: AnyRef) => 
     48              println("NG: (" + name.getClass.getName + ", " + value.getClass.getName + ")") 
     49            case value: Any => 
     50              println("NG: " + value.getClass.getName) 
     51          } 
    4552        } 
    4653      } 
    4754    } 
    48     frame 
    4955  } 
     56 
     57  class Button(text: String) extends JButton(text) with SwingCommand { 
     58    def this(text: String, commands: Array[AnyRef]) { 
     59      this(text) 
     60      exec(commands) 
     61    } 
     62    def exec(commands: Array[AnyRef]) { 
     63      for(command <- commands) { 
     64        command match { 
     65          case ('action, proc: (ActionEvent => Unit)) => 
     66            addActionListener(new ActionAdapter(proc)) 
     67          case ('text, text: String) => 
     68            setText(text) 
     69          case _ => println("NG: " + command) 
     70        } 
     71      } 
     72    } 
     73  } 
     74 
     75  def Frame(commands: AnyRef*): Frame = new Frame(commands.toArray) 
    5076 
    5177  def TabbedPane(tabs: (String,TablePanel)*): JTabbedPane = { 
     
    6591    cols.map(_.asInstanceOf[java.lang.Object]).toArray 
    6692 
    67   def button(name: String): JButton = { 
    68     new JButton(name) 
    69   } 
    70  
    71   def button(name: String, action: ActionEvent => Unit): JButton = { 
    72     val button = new JButton(name) 
    73     button.addActionListener(new ActionAdapter(action)) 
    74     button 
    75   } 
     93  def Button(text: String, commands: AnyRef*): Button = 
     94    new Button(text, commands.toArray) 
    7695 
    7796  def showDialogMessage( 
     
    86105  } 
    87106 
     107  def FileChooser(commands: Any*): JFileChooser = { 
     108    val chooser = new JFileChooser() 
     109    for(command <- commands) 
     110      command match { 
     111        case ('name, name: String) => 
     112          chooser.setName(name) 
     113        case ('dialogTitle, title: String) => 
     114          chooser.setDialogTitle(title) 
     115        case ('showOpen, parent: java.awt.Component) => 
     116          chooser.showOpenDialog(parent) 
     117        case ('showSave, parent: java.awt.Component) => 
     118          chooser.showSaveDialog(parent) 
     119        case ('action, proc: (ActionEvent => Unit)) => 
     120          chooser.addActionListener(new ActionAdapter(proc)) 
     121        case _ => 
     122          println("NG: " + command) 
     123      } 
     124    chooser 
     125  } 
     126 
    88127  /** 
    89128   * Sample codes. 
     
    91130  def main(args: Array[String]) { 
    92131    val frame = Frame( 
    93       'exitOnClose -> true, 
     132      'exitOnClose, 
    94133      'title -> "Hello, world", 
    95134      'size -> (320, 240), 
     
    103142        ("Boo", 
    104143          TablePanel( 
    105             line(button("abc"), button("cde")), 
     144            line(Button("abc"), Button("cde")), 
    106145            line( 
    107               button("efg"), 
    108               button("Hello", 
    109                 {e => showDialogMessage(e.getSource, "Info", "Hello!") 
    110                 } 
     146              Button("efg"), 
     147              Button("Hello", 
     148                action(e => 
     149                  showDialogMessage(e.getSource, "Info", "Hello!") 
     150                ) 
    111151              ) 
    112152            ) 
     
    117157      'visible -> true 
    118158    ) 
     159    val fileChooser = FileChooser( 
     160      'name -> "Foo", 
     161      action(e => println(e)), 
     162      'showOpen -> frame 
     163    ) 
    119164    println(frame) 
    120165  }