Mediator:kotlin

気を付けたこと

  • kotlinではpublicなクラスを一つのファイルに複数書けるのでまとめた
  • 初期化するときにnullを入れる
  • nullを入れるために型名の後に?を付けてnullable型にした
  • nullable型のフィールドを参照するときに?か!!を付けてnullチェックしないとコンパイルでエラーになる
  • kotlinのプロパティはアクセサーも持てるので、javaのパッケージを使う場合にgetText()ではなくtextを使って変数のように扱う

import java.awt.*
import java.awt.event.*
import javax.print.attribute.standard.Media
import kotlin.system.exitProcess

fun main(args: Array<String>) {

    LoginFrame("Mediator Sample")
}
interface Mediator {
    abstract fun createColleagues()
    abstract fun colleagueChanged()
}
interface Colleague {
    abstract fun setMediator(mediator: Mediator)
    abstract fun setColleagueEnabled(enabled: Boolean)

}
class ColleagueButton(caption:String) : Button(caption), Colleague {
    private var mediator:Mediator? = null
    override fun setMediator(mediator: Mediator) {
        this.mediator = mediator
    }

    override fun setColleagueEnabled(enabled: Boolean) {
        isEnabled = enabled
    }

}
class ColleagueTextField(text: String, columns: Int) : TextField(text, columns), TextListener, Colleague {
    private var mediator: Mediator? = null
    override fun setMediator(mediator: Mediator) {
        this.mediator = mediator
    }
    override fun setColleagueEnabled(enabled: Boolean) {
        isEnabled = enabled
        background = if(enabled) Color.white else Color.lightGray
    }

    override fun textValueChanged(e: TextEvent?) {
        mediator?.colleagueChanged()
    }
}
class ColleagueCheckbox(caption: String, group: CheckboxGroup, state: Boolean) : Checkbox(caption, group, state),
    ItemListener, Colleague {
    private var mediator:Mediator? = null
    override fun setMediator(mediator: Mediator) {
        this.mediator = mediator
    }

    override fun setColleagueEnabled(enabled: Boolean) {
        isEnabled = enabled
    }

    override fun itemStateChanged(e: ItemEvent?) {
        mediator?.colleagueChanged()
    }
}
class LoginFrame(title: String) : Frame(title), ActionListener, Mediator {
    private var checkGuest: ColleagueCheckbox? = null
    private var checkLogin : ColleagueCheckbox? = null
    private var textUser: ColleagueTextField? = null
    private var textPass:ColleagueTextField? = null
    private var buttonOk:ColleagueButton? = null
    private var buttonCancel :ColleagueButton? = null
    init {
        background = Color.lightGray
        layout = GridLayout(4, 2)
        createColleagues()
        add(checkGuest)
        add(checkLogin)
        add(Label("Usrname"))
        add(textUser)
        add(Label("Password:"))
        add(textPass)
        add(buttonOk)
        add(buttonCancel)
        colleagueChanged()
        pack()
        show()
    }

    override fun createColleagues() {
        val g = CheckboxGroup()
        checkGuest = ColleagueCheckbox("Guest", g, true)
        checkLogin = ColleagueCheckbox("Login", g,false)
        textUser = ColleagueTextField("", 10)
        textPass = ColleagueTextField("", 10)
        textPass?.echoChar = '*'
        buttonOk = ColleagueButton("OK")
        buttonCancel = ColleagueButton("Cancel")
        //Mediatorのセット
        checkGuest?.setMediator(this)
        checkLogin?.setMediator(this)
        textUser?.setMediator(this)
        textPass?.setMediator(this)
        buttonOk?.setMediator(this)
        buttonCancel?.setMediator(this)
        //Listenerのセット
        checkGuest?.addItemListener(checkGuest)
        checkLogin?.addItemListener(checkLogin)
        textUser?.addTextListener(textUser)
        textPass?.addTextListener(textPass)
        buttonOk?.addActionListener(this)
        buttonCancel?.addActionListener(this)
    }

    override fun colleagueChanged() {
        if (checkGuest?.state!!) {
            textUser?.setColleagueEnabled(false)
            textPass?.setColleagueEnabled(false)
            buttonOk?.setColleagueEnabled(true)
        } else {
            textUser?.setColleagueEnabled(true)
            userpassChanged()
        }
    }
    private fun userpassChanged() {
        if (textUser?.text?.length!! > 0) {

            textPass?.setColleagueEnabled(true)
            if (textPass?.text?.length!! > 0) {
                buttonOk?.setColleagueEnabled(true)

            } else {
                buttonOk?.setColleagueEnabled(false)
            }
        } else {
            textPass?.setColleagueEnabled(false)
            buttonOk?.setColleagueEnabled(false)
        }


    }

    override fun actionPerformed(e: ActionEvent?) {
        println(e.toString())
        exitProcess(0)
    }
}

元のコードは以下の本からの引用です。サンプルコードは著者さんのサイトで配布されています。リンクはAmazonアソシエイトを使用しています。

増補改訂版Java言語で学ぶデザインパターン入門

ライセンス表記

Copyright (C) 2001,2004 Hiroshi Yuki.

hyuki@hyuki.com
This software is provided ‘as-is’, without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.

コメント

タイトルとURLをコピーしました