JavaEmbeddedFrameView.st
author cg
Sat, 14 Nov 1998 19:51:35 +0000
changeset 451 50d24dc93f8b
parent 429 99990a8a3509
child 455 36fffdce84f7
permissions -rw-r--r--
checkin from browser

JavaView subclass:#JavaEmbeddedFrameView
	instanceVariableNames:'codeURL codeBaseURL documentURL archiveURL parameterDictionary
		embeddedAppletFrame applet appletID appletThread
		infoDisplayReceiver autoSetupApplet autoStartApplet
		autoDestroyApplet isNS40'
	classVariableNames:'NextSequentialAppletID'
	poolDictionaries:''
	category:'Java-Views-Support'
!


!JavaEmbeddedFrameView class methodsFor:'support'!

newAppletContext
    |jMozillaAppletContextClass|

    "/ this makes it a modzilla applet context ...
    jMozillaAppletContextClass := Java classForName:'netscape.applet.MozillaAppletContext'.
    jMozillaAppletContextClass isNil ifTrue:[
        self warn:'no netscape.applet.MozillaAppletContext class'.
        ^ nil
    ].
    jMozillaAppletContextClass instVarNamed:'debug' put:1.

    ^ jMozillaAppletContextClass new.

    "Created: / 20.10.1998 / 15:40:08 / cg"
    "Modified: / 20.10.1998 / 15:43:47 / cg"
! !

!JavaEmbeddedFrameView methodsFor:'accessing'!

appletID
    "return the value of the instance variable 'appletID' (automatically generated)"

    ^ appletID

    "Created: / 28.1.1998 / 21:08:54 / cg"
!

appletID:something
    "set the value of the instance variable 'appletID' (automatically generated)"

    appletID := something.

    "Created: / 28.1.1998 / 21:08:54 / cg"
!

appletThread
    ^ appletThread

    "Created: / 28.1.1998 / 21:39:42 / cg"
!

archiveURL:anArchiveURLString
    archiveURL := anArchiveURLString

    "Created: / 28.1.1998 / 21:00:38 / cg"
!

codeBaseURL:aCodeBaseURLString
    codeBaseURL := aCodeBaseURLString

    "Created: / 28.1.1998 / 20:59:34 / cg"
!

codeURL:aCodeURLString
    codeURL := aCodeURLString

    "Created: / 28.1.1998 / 21:10:11 / cg"
!

documentURL:aDocumentURLString
    documentURL := aDocumentURLString

    "Created: / 28.1.1998 / 21:00:03 / cg"
    "Modified: / 28.1.1998 / 21:00:28 / cg"
!

embeddedAppletFrame
    ^ embeddedAppletFrame

    "Created: / 28.1.1998 / 21:39:35 / cg"
!

infoDisplayReceiver
    "return the value of the instance variable 'infoDisplayReceiver' (automatically generated)"

    ^ infoDisplayReceiver

    "Created: / 28.1.1998 / 21:43:54 / cg"
!

infoDisplayReceiver:something
    "set the value of the instance variable 'infoDisplayReceiver' (automatically generated)"

    infoDisplayReceiver := something.

    "Created: / 28.1.1998 / 21:43:54 / cg"
!

parameterDictionary:aParameterDictionary
    parameterDictionary := aParameterDictionary

    "Created: / 28.1.1998 / 21:01:20 / cg"
! !

!JavaEmbeddedFrameView methodsFor:'applet control'!

appletDESTROY
    |ev|

    isNS40 ifTrue:[
        "/ ev = netscape.applet.AppletEvent(APPLET_DESTROY);
        ev := (Java classForName:'netscape.applet.AppletEvent')
                newWith_int:(embeddedAppletFrame class instVarNamed:'APPLET_DESTROY').
        embeddedAppletFrame
            perform:#'sendEvent(Lnetscape/applet/NEvent;)V'
            with:ev.
        ^ self
    ].
    embeddedAppletFrame
        perform:#'sendEvent(I)V' 
        with:(embeddedAppletFrame class instVarNamed:'APPLET_DESTROY').

    "Created: / 28.1.1998 / 21:41:45 / cg"
    "Modified: / 13.11.1998 / 14:19:49 / cg"
!

appletDISPOSE
    |ev|

    isNS40 ifTrue:[
        "/ ev = netscape.applet.AppletEvent(APPLET_DISPOSE);
        ev := (Java classForName:'netscape.applet.AppletEvent')
                newWith_int:(embeddedAppletFrame class instVarNamed:'APPLET_DISPOSE').
        embeddedAppletFrame
            perform:#'sendEvent(Lnetscape/applet/NEvent;)V'
            with:ev.
        ^ self
    ].
    embeddedAppletFrame
        perform:#'sendEvent(I)V' 
        with:(embeddedAppletFrame class instVarNamed:'APPLET_DISPOSE').

    "Created: / 28.1.1998 / 21:41:57 / cg"
    "Modified: / 13.11.1998 / 14:19:43 / cg"
!

appletINIT
    |ev|

    isNS40 ifTrue:[
        "/ ev = netscape.applet.AppletEvent(APPLET_INIT);
        ev := (Java classForName:'netscape.applet.AppletEvent')
                newWith_int:(embeddedAppletFrame class instVarNamed:'APPLET_INIT').
        embeddedAppletFrame
            perform:#'sendEvent(Lnetscape/applet/NEvent;)V'
            with:ev.
        ^ self
    ].
    embeddedAppletFrame
        perform:#'sendEvent(I)V' 
        with:(embeddedAppletFrame class instVarNamed:'APPLET_INIT').

    "Created: / 28.1.1998 / 21:17:51 / cg"
    "Modified: / 13.11.1998 / 14:19:35 / cg"
!

appletLOAD
    |ev|

    isNS40 ifTrue:[
        "/ ev = netscape.applet.AppletEvent(APPLET_LOAD);
        ev := (Java classForName:'netscape.applet.AppletEvent')
                newWith_int:(embeddedAppletFrame class instVarNamed:'APPLET_LOAD').
        embeddedAppletFrame
            perform:#'sendEvent(Lnetscape/applet/NEvent;)V'
            with:ev.
        ^ self
    ].
    embeddedAppletFrame
        perform:#'sendEvent(I)V' 
        with:(embeddedAppletFrame class instVarNamed:'APPLET_LOAD').

    "Created: / 28.1.1998 / 21:17:37 / cg"
    "Modified: / 13.11.1998 / 14:19:31 / cg"
!

appletSTART
    |ev|

    isNS40 ifTrue:[
        "/ ev = netscape.applet.AppletEvent(APPLET_START);
        ev := (Java classForName:'netscape.applet.AppletEvent')
                newWith_int:(embeddedAppletFrame class instVarNamed:'APPLET_START').
        embeddedAppletFrame
            perform:#'sendEvent(Lnetscape/applet/NEvent;)V'
            with:ev.
        ^ self
    ].
    embeddedAppletFrame
        perform:#'sendEvent(I)V' 
        with:(embeddedAppletFrame class instVarNamed:'APPLET_START').

    "Created: / 28.1.1998 / 21:18:01 / cg"
    "Modified: / 13.11.1998 / 14:19:21 / cg"
!

appletSTOP
    |ev|

    isNS40 ifTrue:[
        "/ ev = netscape.applet.AppletEvent(APPLET_STOP);
        ev := (Java classForName:'netscape.applet.AppletEvent')
                newWith_int:(embeddedAppletFrame class instVarNamed:'APPLET_STOP').
        embeddedAppletFrame
            perform:#'sendEvent(Lnetscape/applet/NEvent;)V'
            with:ev.
        ^ self
    ].
    embeddedAppletFrame
        perform:#'sendEvent(I)V' 
        with:(embeddedAppletFrame class instVarNamed:'APPLET_STOP').

    "Created: / 28.1.1998 / 21:41:33 / cg"
    "Modified: / 13.11.1998 / 14:19:14 / cg"
!

autoDestroyApplet:aBoolean
    "set/clear the autoDestroy flag.
     If set, the applet will be shutDown whenever the I get destroyed.
     if clear, the applet is not automatically shutDown and must be
     terminated by someone else.
     The default is false."

    autoDestroyApplet := aBoolean.

    "Created: / 29.1.1998 / 15:18:50 / cg"
    "Modified: / 29.1.1998 / 15:21:01 / cg"
!

autoSetupApplet:aBoolean
    "set/clear the autoSetup flag.
     If set, the appletFrame will be setup whenever the I get realized
     for the first time.
     If clear, the frame is not automatically setup and must be
     setup by someone else.
     The default is true."


    autoSetupApplet := aBoolean.

    "Created: / 29.1.1998 / 15:18:41 / cg"
    "Modified: / 29.1.1998 / 20:45:37 / cg"
!

autoStartApplet:aBoolean
    "set/clear the autoStart flag.
     If set, the applet will be loaded & started whenever the I get realized
     for the first time.
     If clear, the applet is not automatically started and must be
     started by someone else.
     The default is false."


    autoStartApplet := aBoolean.

    "Created: / 29.1.1998 / 15:18:41 / cg"
    "Modified: / 29.1.1998 / 15:20:54 / cg"
!

startApplet
    self startAppletThread.

    self appletLOAD.
    self appletINIT.
    self appletSTART

    "Modified: / 28.1.1998 / 21:30:24 / cg"
!

startAppletThread
    |appletName|

    appletThread := JavaProcess 
            for:[
                    "/ passnotifications to the documentFrame ...
                    Object informationSignal handle:[:ex |
                        JavaVM javaConsoleStream showCR:ex errorString.
                        ex proceed.
                    ] do:[
                        Object activityNotificationSignal handle:[:ex |
                            infoDisplayReceiver notNil ifTrue:[
                                infoDisplayReceiver infoDisplay:ex errorString.
                            ] ifFalse:[
                                Transcript showCR:ex errorString
                            ].
                            ex proceed.
                        ] do:[
                            [
                                embeddedAppletFrame perform:#'run()V'.
                            ] valueNowOrOnUnwindDo:[
                                appletThread terminateAllSubprocesses.
                                Transcript showCR:'JAVA applet startup finished'.
                            ]
                        ]
                    ]
                ]
            priority:(Processor activePriority - 1).

    appletName := codeURL asFilename withoutSuffix baseName.
    appletThread name:('JAVA-' , appletName , '-applet' , ' [startup]').
    
    appletThread resume.

    ^ appletThread.

    "Created: / 28.1.1998 / 21:30:32 / cg"
    "Modified: / 4.11.1998 / 17:37:14 / cg"
!

stopApplet
    |n n2|

    (appletThread notNil 
    and:[appletThread isDead not])ifTrue:[
"/        'EmbeddedAppletFrame [info]: send APPLET_STOP ...' infoPrintCR.
        self appletSTOP.

"/        'EmbeddedAppletFrame [info]: send APPLET_DESTROY ...' infoPrintCR.
        self appletDESTROY.

"/        'EmbeddedAppletFrame [info]: send APPLET_DISPOSE ...' infoPrintCR.
        self appletDISPOSE.

        "/
        "/ give it a chance to stop ...
        "/
        Processor yield.
        n2 := 0.
        [appletThread isDead] whileFalse:[
            n := 0.
            [n < 10 and:[appletThread isDead not]] whileTrue:[
                Delay waitForSeconds:0.1.
                n := n + 1.
            ].
            appletThread isDead ifFalse:[
                Transcript showCR:'EmbeddedAppletFrame [info]: JAVA thread did not stop - soft terminate ...'.
                appletThread terminate.
                n2 := n2 + 1.
                n2 > 10 ifTrue:[
                    Transcript showCR:'EmbeddedAppletFrame [info]: JAVA thread did not stop - shooting down ...'.
                    appletThread terminateNoSignal
                ]
            ].
        ].
    ].
    appletThread := nil

    "Modified: / 6.2.1998 / 02:19:18 / cg"
! !

!JavaEmbeddedFrameView methodsFor:'initialize / release'!

destroy
    autoDestroyApplet == true ifTrue:[
        self stopApplet
    ].
    super destroy.

    "Created: / 29.1.1998 / 15:21:45 / cg"
    "Modified: / 29.1.1998 / 15:28:26 / cg"
!

realize
    super realize.
 
    autoSetupApplet ~~ false ifTrue:[
        self setupAppletFrameIn:nil initializeJava:true
    ].
    autoStartApplet == true ifTrue:[
        embeddedAppletFrame notNil ifTrue:[
            self startApplet
        ]
    ]

    "Created: / 29.1.1998 / 15:21:30 / cg"
    "Modified: / 20.10.1998 / 15:46:32 / cg"
! !

!JavaEmbeddedFrameView methodsFor:'private'!

setupAppletFrameIn:anAppletContextOrNil
     |appletContext
     jEmbeddedAppletFrameClass jDerivedAppletFrameClass
     toolkit peer attribs id
     jCodeBaseURL jDocumentURL jArchiveURL|

    embeddedAppletFrame notNil ifTrue:[
        "/ already setup
        ^ self
    ].

    codeBaseURL isNil ifTrue:[
        ^ self
    ].

    Java startupJavaSystem.

    jCodeBaseURL := Java as_URL:codeBaseURL.
    documentURL notNil ifTrue:[
        jDocumentURL := Java as_URL:documentURL.
    ].
    archiveURL notNil ifTrue:[
        jArchiveURL := Java as_URL:archiveURL.
    ].

    id := appletID.
    id isNil ifTrue:[
        NextSequentialAppletID isNil ifTrue:[
            NextSequentialAppletID := 1.
        ].
        id := NextSequentialAppletID.
        NextSequentialAppletID := NextSequentialAppletID + 1
    ].

    attribs := Dictionary new.
    width notNil ifTrue:[attribs at:'width' put:width printString].
    height notNil ifTrue:[attribs at:'height' put:height printString].
    codeURL notNil ifTrue:[attribs at:'code' put:codeURL].
    parameterDictionary notNil ifTrue:[
        attribs declareAllFrom:parameterDictionary
    ].
    attribs := Java as_Hashtable:attribs.

    anAppletContextOrNil notNil ifTrue:[
        appletContext := anAppletContextOrNil
    ] ifFalse:[
        appletContext := self newAppletContext.
        appletContext isNil ifTrue:[
            self warn:'no netscape.applet.MozillaAppletContext class'.
            ^ false
        ].
    ].

    jEmbeddedAppletFrameClass := Java classForName:'netscape.applet.EmbeddedAppletFrame'.
    jEmbeddedAppletFrameClass isNil ifTrue:[
        self warn:'no netscape.applet.EmbeddedAppletFrame class'.
        ^ false
    ].
    jDerivedAppletFrameClass := Java classForName:'netscape.applet.DerivedAppletFrame'.
    jDerivedAppletFrameClass notNil ifTrue:[
        "/ ns4.0
        embeddedAppletFrame := jDerivedAppletFrameClass new.
        isNS40 := true.
    ] ifFalse:[
        "/ ns3.x
        embeddedAppletFrame := jEmbeddedAppletFrameClass new.
        isNS40 := false.
    ].
    embeddedAppletFrame instVarNamed:'pData' put:self.

    toolkit := Java classForName:'java.awt.Toolkit'.
    toolkit := toolkit perform:#getDefaultToolkit.
    peer := toolkit 
                perform:#'createFrame(Ljava/awt/Frame;)Ljava/awt/peer/FramePeer;'
                with:embeddedAppletFrame.

    (embeddedAppletFrame respondsTo:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;Z)V')
    ifTrue:[
        "/ 4.0 netscape
        embeddedAppletFrame 
            perform:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;Z)V'
            withArguments:
                (Array
                    with:jDocumentURL
                    with:jCodeBaseURL
                    with:jArchiveURL
                    with:attribs
                    with:appletContext
                    with:(Java as_Integer:id)
                    with:0 "/ reloadClasses-boolean
                ).
    ] ifFalse:[
        (embeddedAppletFrame respondsTo:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;)V')
        ifTrue:[
            "/ 3.01 netscape
            embeddedAppletFrame 
                perform:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;)V'
                with:jDocumentURL
                with:jCodeBaseURL
                with:jArchiveURL
                with:attribs
                with:appletContext
                with:(Java as_Integer:id).
        ] ifFalse:[
            "/ oldStyle netscape
            embeddedAppletFrame 
                perform:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;I)V'
                with:jDocumentURL
                with:jCodeBaseURL
                with:attribs
                with:appletContext
                with:id.
        ].
    ].

    embeddedAppletFrame instVarNamed:'peer' put:peer.

    self javaPeer:peer.

    ^ true

    "Created: / 20.10.1998 / 15:38:24 / cg"
    "Modified: / 4.11.1998 / 21:07:03 / cg"
!

setupAppletFrameIn:anAppletContextOrNil initializeJava:initializeJava
     |appletContext
     jEmbeddedAppletFrameClass jDerivedAppletFrameClass
     toolkit peer attribs id
     jCodeBaseURL jDocumentURL jArchiveURL|

    embeddedAppletFrame notNil ifTrue:[
        "/ already setup
        ^ self
    ].

    codeBaseURL isNil ifTrue:[
        ^ self
    ].

    initializeJava ifTrue:[
        Java startupJavaSystem.
    ].

    jCodeBaseURL := Java as_URL:codeBaseURL.
    documentURL notNil ifTrue:[
        jDocumentURL := Java as_URL:documentURL.
    ].
    archiveURL notNil ifTrue:[
        jArchiveURL := Java as_URL:archiveURL.
    ].

    id := appletID.
    id isNil ifTrue:[
        NextSequentialAppletID isNil ifTrue:[
            NextSequentialAppletID := 1.
        ].
        id := NextSequentialAppletID.
        NextSequentialAppletID := NextSequentialAppletID + 1
    ].

    attribs := Dictionary new.
    width notNil ifTrue:[attribs at:'width' put:width printString].
    height notNil ifTrue:[attribs at:'height' put:height printString].
    codeURL notNil ifTrue:[attribs at:'code' put:codeURL].
    parameterDictionary notNil ifTrue:[
        attribs declareAllFrom:parameterDictionary
    ].
    attribs := Java as_Hashtable:attribs.

    anAppletContextOrNil notNil ifTrue:[
        appletContext := anAppletContextOrNil
    ] ifFalse:[
        appletContext := self class newAppletContext.
        appletContext isNil ifTrue:[
            self warn:'no netscape.applet.MozillaAppletContext class'.
            ^ false
        ].
    ].

    jEmbeddedAppletFrameClass := Java classForName:'netscape.applet.EmbeddedAppletFrame'.
    jEmbeddedAppletFrameClass isNil ifTrue:[
        self warn:'no netscape.applet.EmbeddedAppletFrame class'.
        ^ false
    ].
    jDerivedAppletFrameClass := Java classForName:'netscape.applet.DerivedAppletFrame'.
    jDerivedAppletFrameClass notNil ifTrue:[
        "/ ns4.0
        embeddedAppletFrame := jDerivedAppletFrameClass new.
        isNS40 := true.
    ] ifFalse:[
        "/ ns3.x
        embeddedAppletFrame := jEmbeddedAppletFrameClass new.
        isNS40 := false.
    ].
    embeddedAppletFrame instVarNamed:'pData' put:self.

    toolkit := (Java classForName:'java.awt.Toolkit') perform:#'getDefaultToolkit'.
    peer := toolkit 
                perform:#'createFrame(Ljava/awt/Frame;)Ljava/awt/peer/FramePeer;'
                with:embeddedAppletFrame.

    (embeddedAppletFrame respondsTo:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;Z)V')
    ifTrue:[
        "/ 4.0 netscape
        embeddedAppletFrame 
            perform:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;Z)V'
            withArguments:
                (Array
                    with:jDocumentURL
                    with:jCodeBaseURL
                    with:jArchiveURL
                    with:attribs
                    with:appletContext
                    with:(Java as_Integer:id)
                    with:0 "/ reloadClasses-boolean
                ).
    ] ifFalse:[
        (embeddedAppletFrame respondsTo:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;)V')
        ifTrue:[
            "/ 3.01 netscape
            embeddedAppletFrame 
                perform:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;Ljava/lang/Integer;)V'
                with:jDocumentURL
                with:jCodeBaseURL
                with:jArchiveURL
                with:attribs
                with:appletContext
                with:(Java as_Integer:id).
        ] ifFalse:[
            "/ oldStyle netscape
            embeddedAppletFrame 
                perform:#'<init>(Ljava/net/URL;Ljava/net/URL;Ljava/util/Hashtable;Lnetscape/applet/MozillaAppletContext;I)V'
                with:jDocumentURL
                with:jCodeBaseURL
                with:attribs
                with:appletContext
                with:id.
        ].
    ].

    embeddedAppletFrame instVarNamed:'peer' put:peer.

    self javaPeer:peer.

    ^ true

    "Created: / 20.10.1998 / 15:47:04 / cg"
    "Modified: / 10.11.1998 / 12:36:09 / cg"
! !

!JavaEmbeddedFrameView methodsFor:'resizing'!

sizeChanged:how
    super sizeChanged:how.
^ self.
    embeddedAppletFrame notNil ifTrue:[
        (embeddedAppletFrame perform:#'isResizable()Z') ~~ 0 ifTrue:[
            self windowGroup sensor
                 pushDamageEvent:(WindowEvent
                                     for:self
                                     type:#configureX:y:width:height:
                                     arguments:(Array with:left with:top with:width with:height)).
"/            embeddedAppletFrame
"/                perform:#'appletResize(II)V'
"/                with:self width
"/                with:self height.
        ]
    ]

    "Modified: / 1.2.1998 / 19:15:07 / cg"
! !

!JavaEmbeddedFrameView class methodsFor:'documentation'!

version
    ^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaEmbeddedFrameView.st,v 1.14 1998/11/14 19:50:37 cg Exp $'
! !