examples/chat/ChatWindow.st
author Claus Gittinger <cg@exept.de>
Wed, 30 May 2018 19:52:13 +0200
branchcvs_MAIN
changeset 3766 d96171d623c2
parent 2772 ba792256b173
permissions -rw-r--r--
*** empty log message ***

"{ Package: 'stx:libjava/examples/chat' }"

ApplicationModel subclass:#ChatWindow
	instanceVariableNames:'client reader conversationHolder buddyHolder messageHolder'
	classVariableNames:''
	poolDictionaries:''
	category:'stx-libjava-examples-chat'
!


!ChatWindow class methodsFor:'interface specs'!

windowSpec
    "This resource specification was automatically generated
     by the UIPainter of ST/X."

    "Do not manually edit this!! If it is corrupted,
     the UIPainter may not be able to read the specification."

    "
     UIPainter new openOnClass:ChatWindow andSelector:#windowSpec
     ChatWindow new openInterface:#windowSpec
     ChatWindow open
    "

    <resource: #canvas>

    ^ 
    #(FullSpec
       name: windowSpec
       window: 
      (WindowSpec
         label: 'NewApplication'
         name: 'NewApplication'
         bounds: (Rectangle 0 0 592 525)
       )
       component: 
      (SpecCollection
         collection: (
          (LabelSpec
             label: 'Buddy...'
             name: 'Label1'
             layout: (LayoutFrame 0 0 0 0 0 1 30 0)
             translateLabel: true
             labelChannel: buddyHolder
           )
          (TextEditorSpec
             name: 'TextEditor1'
             layout: (LayoutFrame 0 0 30 0 0 1 -30 1)
             model: conversationHolder
             hasHorizontalScrollBar: true
             hasVerticalScrollBar: true
             hasKeyboardFocusInitially: false
           )
          (InputFieldSpec
             name: 'EntryField1'
             layout: (LayoutFrame 0 0 -25 1 0 1 0 1)
             model: messageHolder
             acceptOnLeave: false
             acceptOnReturn: true
             acceptOnTab: false
             acceptOnLostFocus: false
             acceptOnPointerLeave: false
             valueChangeCallBackSelector: send
           )
          )
        
       )
     )
! !

!ChatWindow methodsFor:'accessing'!

buddy: aString
    self buddyHolder value: aString

    "Created: / 23-09-2013 / 23:27:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

client: jclient
    client := jclient.

    "Created: / 23-09-2013 / 23:31:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!ChatWindow methodsFor:'actions'!

receive: message

    conversationHolder value:
        (conversationHolder value , Character cr, message getFrom , ': ', message getBody)

    "Created: / 23-09-2013 / 23:34:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

send
    | m |

    m := JAVA org jivesoftware smack packet Message new.
    m setTo: self buddyHolder value.
    m setBody: self messageHolder value.
    client send: m.
    conversationHolder value:
            (conversationHolder value , Character cr, 'me: ', self messageHolder value).
    self messageHolder setValue: ''.

    "Created: / 23-09-2013 / 23:30:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!ChatWindow methodsFor:'aspects'!

buddyHolder
    <resource: #uiAspect>

    "automatically generated by UIPainter ..."

    "*** the code below creates a default model when invoked."
    "*** (which may not be the one you wanted)"
    "*** Please change as required and accept it in the browser."
    "*** (and replace this comment by something more useful ;-)"

    buddyHolder isNil ifTrue:[
        buddyHolder := ValueHolder new.
"/ if your app needs to be notified of changes, uncomment one of the lines below:
"/       buddyHolder addDependent:self.
"/       buddyHolder onChangeSend:#buddyHolderChanged to:self.
    ].
    ^ buddyHolder.
!

conversationHolder
    <resource: #uiAspect>

    "automatically generated by UIPainter ..."

    "*** the code below creates a default model when invoked."
    "*** (which may not be the one you wanted)"
    "*** Please change as required and accept it in the browser."
    "*** (and replace this comment by something more useful ;-)"

    conversationHolder isNil ifTrue:[
        conversationHolder := '' asValue.
"/ if your app needs to be notified of changes, uncomment one of the lines below:
"/       conversationHolder addDependent:self.
"/       conversationHolder onChangeSend:#conversationHolderChanged to:self.
    ].
    ^ conversationHolder.
!

messageHolder
    <resource: #uiAspect>

    "automatically generated by UIPainter ..."

    "*** the code below creates a default model when invoked."
    "*** (which may not be the one you wanted)"
    "*** Please change as required and accept it in the browser."
    "*** (and replace this comment by something more useful ;-)"

    messageHolder isNil ifTrue:[
        messageHolder := ValueHolder new.
"/ if your app needs to be notified of changes, uncomment one of the lines below:
"/       messageHolder addDependent:self.
"/       messageHolder onChangeSend:#messageHolderChanged to:self.
    ].
    ^ messageHolder.
! !

!ChatWindow methodsFor:'hooks'!

closeDownViews
    reader terminate.
    super closeDownViews.

    "Created: / 23-09-2013 / 23:35:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

commonPostOpen
    reader := [
         [
            | m |        

            client await.
            [ (m := client receive: self buddyHolder value) notNil ] whileTrue:[
                self receive: m.
            ].
         ] loop.
    ] fork.

    "Created: / 23-09-2013 / 23:34:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!ChatWindow class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !