Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Applet with embedded SWT impossible under Mac OS X ?
Applet with embedded SWT impossible under Mac OS X ? [message #516496] Wed, 24 February 2010 08:59 Go to next message
Eclipse UserFriend
Originally posted by: ludovic.maitre.softbooking.com

Hi all,

I didn't have found a lot of references on the net about this, so I
would like to know if it is possible to make applets with SWT components
under Mac OS X (Snow Leopard 64 bits) ?

Because we have an application which use an applet with SWT components
inside (GanttCharts) which work well under Windows but not under Mac OS
X. I have try to debug the problem and i see that if the code is not
executed on the main thread then the SWT crash:
org.eclipse.swt.widgets.Display:
....
void createDisplay (DeviceData data) {
...

NSThread nsthread = NSThread.currentThread();

if (!NSThread.isMainThread()) {
System.out.println ("***WARNING: Display must be created on main
thread due to Cocoa restrictions."); //$NON-NLS-1$
error(SWT.ERROR_THREAD_INVALID_ACCESS);
}

I have nothing against this apart that i believe it's not possible to
execute the code on the main thread in the context of an applet. I have
tried to execute the code on the AWT-EventQueue thread with
SwingUtilities.invokeLater( Runnable ) but no luck, it's not the main
thread. (if someone has a snippet of code to execute code on the main
thread i would be happy but AFAIK it's not possible from an applet)

If i remove the above check, my components displays but it is frozen
after, perhaps if i amend my code to always does graphical operation
inside display.asyncExec( Runnable ) (or
SwingUtilities.invoke[AndWait|Later] ?) did it will work ?

PS: i have done a little bit of dev under IPhone OS/Mac OS X and it's
true that every graphical operation should be performed on the main
thread (with [self performSelectorOnMainThread:...] ), wouldn't it be
possible for the Cocoa bindings to take care of perform the operation on
the main thread and not impose any restriction to the developer ? how do
AWT to work well in applets in Macs ? (there should but something wich
prevent it i guess as it has not been done)

Thanks in advance for any answers/link/documentation to more information,
Best regards and have a good day,
--

Ludovic Maitre
Developpeur
---------------------------------------
Softbooking Sarl
Les Algorithmes - Thalès A - 2000 route des Lucioles
06410 BIOT

Adresse postale :
BP 10358
06906 SOPHIA ANTIPOLIS Cedex

Tél. : +33 (0)4 92 94 10 04
Fax : +33 (0)4 92 94 10 01
Web : http://www.resalys.com
Re: Applet with embedded SWT impossible under Mac OS X ? [message #516509 is a reply to message #516496] Wed, 24 February 2010 09:39 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Did you try this

(what an SWT developer using a non display thread should do i did that...)

import java.applet.Applet;

public class Snippet157 extends Applet
{

    org.eclipse.swt.widgets.Display display;

    org.eclipse.swt.widgets.Shell swtParent;

    java.awt.Canvas awtParent;

    @Override
    public void init()
    {
        Thread thread = new Thread(new Runnable()
        {
            public void run()
            {
                setLayout(new java.awt.GridLayout(1, 1));
                awtParent = new java.awt.Canvas();
                add(awtParent);
                display = new org.eclipse.swt.widgets.Display();
                swtParent = org.eclipse.swt.awt.SWT_AWT.new_Shell(display, awtParent);
                swtParent.setLayout(new org.eclipse.swt.layout.FillLayout());
                display.syncExec(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        org.eclipse.swt.ole.win32.OleFrame frame = new org.eclipse.swt.ole.win32.OleFrame(
                                swtParent, org.eclipse.swt.SWT.NONE);
                        org.eclipse.swt.ole.win32.OleClientSite site;
                        try
                        {
                            site = new org.eclipse.swt.ole.win32.OleClientSite(frame,
                                    org.eclipse.swt.SWT.NONE, "Word.Document");
                        }
                        catch (org.eclipse.swt.SWTException e)
                        {
                            String str = "Create OleClientSite Error" + e.toString();
                            System.out.println(str);
                            return;
                        }
                        setSize(500, 500);
                        validate();
                        site.doVerb(org.eclipse.swt.ole.win32.OLE.OLEIVERB_SHOW);

                        while (swtParent != null && !swtParent.isDisposed())
                        {
                            if (!display.readAndDispatch()) display.sleep();
                        }
                    }
                });
            }
        });
        thread.start();
    }

    @Override
    public void stop()
    {
        if (display != null && !display.isDisposed())
        {
            display.syncExec(new Runnable()
            {
                public void run()
                {
                    if (swtParent != null && !swtParent.isDisposed()) swtParent.dispose();
                    swtParent = null;
                    display.dispose();
                    display = null;
                }
            });
            remove(awtParent);
            awtParent = null;
        }
    }
}


i moved the whole SWT code into display.syncExec method...


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay

[Updated on: Wed, 24 February 2010 10:06]

Report message to a moderator

Re: Applet with embedded SWT impossible under Mac OS X ? [message #516518 is a reply to message #516509] Wed, 24 February 2010 10:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ludovic.maitre.softbooking.com

Hi Vijay,

Thanks for your answer. I haven't tried (yet) to put all my code in
display.asyncExec, because it is when i instantiate the display that the
exception is thrown (either with new Display() or Display.getDefault()).
So to answer Q1) the snippet should not work because the code is not
running on the main thread. (we can check with this for instance:
NSThread nsthread = NSThread.currentThread();
if (NSThread.isMainThread()) {
mLogger.info("creating display on main thread (" +
SwingUtilities.isEventDispatchThread() +")");
}
else
{
mLogger.info("creating display on aux thread (" +
SwingUtilities.isEventDispatchThread() +")");
}
). (I'm not able to deploy on an x64 mac os x right now so i cannot check)

For Q2) we cannot really run the code in display.syncExec() without
patching SWT because the exception occur before/when the display is
instantiated. If i run the code with the patched SWT it display most of
the components, perhaps it could work but i have not gone far in this
way because before i would like to find a better solution than to patch SWT.

For reference here is what we (try) to do to instantiate our applet and
swt components (works well under Windows) :

public class LP extends Applet implements Runnable {
....
private Logger mLogger = Logger.getLogger("lp");

private Canvas cCanvas;

private Display dDisplay;

private Thread thSWTThread;

private Shell sShell;
....
@Override
public void destroy() {
// destroy SWT shell
dDisplay.asyncExec(new Runnable() {
@Override
public void run() {
sShell.dispose();
}
});

// remove canvas
this.removeAll();
super.destroy();
}
....
@Override
public void init() {
cCanvas = new Canvas();
this.setLayout(new BorderLayout());
this.add(cCanvas, BorderLayout.CENTER);
// TODO: should be done in SwingUtilities.invokeLater ?
this.doLayout();
thSWTThread = new Thread(this, "Applet SWT Thread");
thSWTThread.start();
super.init();
}
....
public void run() {
if( dDisplay == null )
{
// Exception thrown here
dDisplay = Display.getDefault();
// Exception also occur with :
//dDisplay = new org.eclipse.swt.widgets.Display();
sShell = SWT_AWT.new_Shell(dDisplay, cCanvas);
mLogger.info("Display is created");
try {
// build UI - should be executed with
// display.syncExec and/or SwingUtilities.invokeAndWait?
buildWS(sShell,dDisplay);
} catch (Exception e) {
e.printStackTrace();
}
}
}
....
}

So if anybody has a clue on how we should do, we would be more than
happy, else i will try with a patched version of SWT without the running
on main thread check but i worry to do this (running a modified SWT and
overriding this check - it has not been put here without reason but it
clearly break my use case (running in an applet - without access ot the
main thread AFAIK) ).

Best regards,

Le 2/24/10 10:39 AM, vijay a écrit :
> I tryed this applet thing by using Snippet 157
> /*********************************************************** ********************
>
> * Copyright (c) 2000, 2004 IBM Corporation and others.
> * All rights reserved. This program and the accompanying materials
> * are made available under the terms of the Eclipse Public License v1.0
> * which accompanies this distribution, and is available at
> * http://www.eclipse.org/legal/epl-v10.html
> *
> * Contributors:
> * IBM Corporation - initial API and implementation
> ************************************************************ *******************/
>
> package org.eclipse.swt.snippets;
>
> /*
> * example snippet: Embed Word in an applet (win32 only)
> *
> * For a list of all SWT example snippets see
> * http://www.eclipse.org/swt/snippets/
> * * @since 3.0
> */
>
> import java.applet.*;
> public class Snippet157 extends Applet {
>
> org.eclipse.swt.widgets.Display display;
> org.eclipse.swt.widgets.Shell swtParent;
> java.awt.Canvas awtParent;
>
> public void init () {
> Thread thread = new Thread (new Runnable () {
> public void run () {
> setLayout(new java.awt.GridLayout (1, 1));
> awtParent = new java.awt.Canvas ();
> add (awtParent);
> display = new org.eclipse.swt.widgets.Display ();
> swtParent = org.eclipse.swt.awt.SWT_AWT.new_Shell (display, awtParent);
> swtParent.setLayout (new org.eclipse.swt.layout.FillLayout ());
> org.eclipse.swt.ole.win32.OleFrame frame = new
> org.eclipse.swt.ole.win32.OleFrame (swtParent, org.eclipse.swt.SWT.NONE);
> org.eclipse.swt.ole.win32.OleClientSite site;
> try {
> site = new org.eclipse.swt.ole.win32.OleClientSite (frame,
> org.eclipse.swt.SWT.NONE, "Word.Document");
> } catch (org.eclipse.swt.SWTException e) {
> String str = "Create OleClientSite Error" + e.toString ();
> System.out.println (str);
> return;
> }
> setSize (500, 500);
> validate ();
> site.doVerb (org.eclipse.swt.ole.win32.OLE.OLEIVERB_SHOW);
>
> while (swtParent != null && !swtParent.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
> }
> });
> thread.start ();
> }
> public void stop (){
> if (display != null && !display.isDisposed ()){
> display.syncExec(new Runnable () {
> public void run () {
> if (swtParent != null && !swtParent.isDisposed ()) swtParent.dispose ();
> swtParent = null;
> display.dispose ();
> display = null;
> }
> });
> remove (awtParent);
> awtParent = null;
> }
> }
> }
>
> and i tryed to run it in eclipse by run as applet action...
>
> it thru invalid thread access exception...
>
> So what an SWT developer using a non display thread should do i did that...
>
> import java.applet.Applet;
>
> public class Snippet157 extends Applet
> {
>
> org.eclipse.swt.widgets.Display display;
>
> org.eclipse.swt.widgets.Shell swtParent;
>
> java.awt.Canvas awtParent;
>
> @Override
> public void init()
> {
> Thread thread = new Thread(new Runnable()
> {
> public void run()
> {
> setLayout(new java.awt.GridLayout(1, 1));
> awtParent = new java.awt.Canvas();
> add(awtParent);
> display = new org.eclipse.swt.widgets.Display();
> swtParent = org.eclipse.swt.awt.SWT_AWT.new_Shell(display, awtParent);
> swtParent.setLayout(new org.eclipse.swt.layout.FillLayout());
> display.syncExec(new Runnable()
> {
>
> @Override
> public void run()
> {
> org.eclipse.swt.ole.win32.OleFrame frame = new
> org.eclipse.swt.ole.win32.OleFrame(
> swtParent, org.eclipse.swt.SWT.NONE);
> org.eclipse.swt.ole.win32.OleClientSite site;
> try
> {
> site = new org.eclipse.swt.ole.win32.OleClientSite(frame,
> org.eclipse.swt.SWT.NONE, "Word.Document");
> }
> catch (org.eclipse.swt.SWTException e)
> {
> String str = "Create OleClientSite Error" + e.toString();
> System.out.println(str);
> return;
> }
> setSize(500, 500);
> validate();
> site.doVerb(org.eclipse.swt.ole.win32.OLE.OLEIVERB_SHOW);
>
> while (swtParent != null && !swtParent.isDisposed())
> {
> if (!display.readAndDispatch()) display.sleep();
> }
> }
> });
> }
> });
> thread.start();
> }
>
> @Override
> public void stop()
> {
> if (display != null && !display.isDisposed())
> {
> display.syncExec(new Runnable()
> {
> public void run()
> {
> if (swtParent != null && !swtParent.isDisposed()) swtParent.dispose();
> swtParent = null;
> display.dispose();
> display = null;
> }
> });
> remove(awtParent);
> awtParent = null;
> }
> }
> }
>
>
> i moved the whole SWT code into display.syncExec method...
>
> Q1)Why is the snippet not working?
> Q2)Did you try your display or SWT control creation in display.syncexec
> method

--

Ludovic Maitre
Developpeur
---------------------------------------
Softbooking Sarl
Les Algorithmes - Thalès A - 2000 route des Lucioles
06410 BIOT

Adresse postale :
BP 10358
06906 SOPHIA ANTIPOLIS Cedex

Tél. : +33 (0)4 92 94 10 04
Fax : +33 (0)4 92 94 10 01
Web : http://www.resalys.com
Re: Applet with embedded SWT impossible under Mac OS X ? [message #516531 is a reply to message #516518] Wed, 24 February 2010 11:07 Go to previous messageGo to next message
Vijay RajFriend
Messages: 608
Registered: July 2009
Senior Member
Hi Ludovic,

I downloaded
Mac OSX (Mac/Cocoa/x86_64) (Supported Versions)
SWT src and i did not find the code u specified...

did i download the correct src???


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Applet with embedded SWT impossible under Mac OS X ? [message #516540 is a reply to message #516531] Wed, 24 February 2010 11:29 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ludovic.maitre.softbooking.com

Hi Vijay,

Thanks for your time, and sorry i should have explained which version i
use, it is the SWT 3.6M5 for Cocoa x64 available here :
http://download.eclipse.org/eclipse/downloads/drops/S-3.6M5- 201001291300/download.php?dropFile=swt-3.6M5-cocoa-macosx-x8 6_64.zip

If you download this file and unzip the src.zip file, the code which
cause the problem is in the class :
swt-3.6M5-cocoa-macosx-x86_64/src/org/eclipse/swt/widgets/Di splay.java

In the method void createDisplay (DeviceData data) at line 779, and more
specifically the lines 788 to 791 :
if (!NSThread.isMainThread()) {
System.out.println ("***WARNING: Display must be created on main
thread due to Cocoa restrictions."); //$NON-NLS-1$
error(SWT.ERROR_THREAD_INVALID_ACCESS);
}

Notice that the applet snippet you mention in your mail is specifically
for Win32, however i guess you can derivate this source code (or the one
i have put in reference in my last mail) to make a simple applet which
illustrate the case (instantiate a display (AFAIK KO without commenting
the above code) then instantiate a shell would be enough).

I will do a code sample like this if eventually i open an issue in
Eclipse bug tracker but before doing so i would like to know the feeling
of other devs on the subject.

Perhaps it would suffice to write explicitely that applets with SWT are
not and will not be supported under Mac OS X ? However, it would be sad
and a deal breaker for some projects (and AWT applets works fine so why
not SWT...).

Best regards and many thanks to have take time to look into this,

Le 2/24/10 12:07 PM, vijay a écrit :
> Hi Ludovic,
>
> I downloaded Mac OSX (Mac/Cocoa/x86_64) (Supported Versions)
> SWT src and i did not find the code u specified...
>
> did i download the correct src???
Re: Applet with embedded SWT impossible under Mac OS X ? [message #516632 is a reply to message #516540] Wed, 24 February 2010 15:55 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
Hi,

I've asked SSQ about this and he does not believe that this scenario can
currently be made to work on OS X. Aside from the threads issue, there
could be other problems because in the context of applets the event loop
gets run by AWT instead of by SWT. Your case can work on Windows because
SWT can utilize a second Display there under the covers, but SWT's OS X
implementation does not currently have this support. I found a few OS X
bugs in bugzilla dealing with SWT/AWT integration, but it wasn't clear
whether any of them dealt with a case like yours, so if you want you can log
a new report with SWT at
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform &component=SWT .

Grant


"Ludovic Maitre" <ludovic.maitre@softbooking.com> wrote in message
news:hm32hv$vqt$1@build.eclipse.org...
> Hi Vijay,
>
> Thanks for your time, and sorry i should have explained which version i
> use, it is the SWT 3.6M5 for Cocoa x64 available here :
>
http://download.eclipse.org/eclipse/downloads/drops/S-3.6M5- 201001291300/download.php?dropFile=swt-3.6M5-cocoa-macosx-x8 6_64.zip
>
> If you download this file and unzip the src.zip file, the code which
> cause the problem is in the class :
> swt-3.6M5-cocoa-macosx-x86_64/src/org/eclipse/swt/widgets/Di splay.java
>
> In the method void createDisplay (DeviceData data) at line 779, and more
> specifically the lines 788 to 791 :
> if (!NSThread.isMainThread()) {
> System.out.println ("***WARNING: Display must be created on main
> thread due to Cocoa restrictions."); //$NON-NLS-1$
> error(SWT.ERROR_THREAD_INVALID_ACCESS);
> }
>
> Notice that the applet snippet you mention in your mail is specifically
> for Win32, however i guess you can derivate this source code (or the one
> i have put in reference in my last mail) to make a simple applet which
> illustrate the case (instantiate a display (AFAIK KO without commenting
> the above code) then instantiate a shell would be enough).
>
> I will do a code sample like this if eventually i open an issue in
> Eclipse bug tracker but before doing so i would like to know the feeling
> of other devs on the subject.
>
> Perhaps it would suffice to write explicitely that applets with SWT are
> not and will not be supported under Mac OS X ? However, it would be sad
> and a deal breaker for some projects (and AWT applets works fine so why
> not SWT...).
>
> Best regards and many thanks to have take time to look into this,
>
> Le 2/24/10 12:07 PM, vijay a
Re: Applet with embedded SWT impossible under Mac OS X ? [message #516804 is a reply to message #516632] Thu, 25 February 2010 09:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ludovic.maitre.softbooking.com

Hi Grant,

Thank to have asked SSQ (i don't know what it is but i guess there are
knowledgeable on SWT), i have opened issue 303869. (
https://bugs.eclipse.org/bugs/show_bug.cgi?id=303869 )
I have also read a little bit the other reports of problems like this
related to the SWT_AWT bridge under Mac OS X and it looks like peoples
have excluded our use case (not running in the main thread) too quickly,
probably because it looks too hard to code, a least for my eyes :-).
However it is required for applets (and perhaps other environments?).
Hope to see a great coder implementing it ;-),
Best regards,

Le 2/24/10 4:55 PM, Grant Gayed a écrit :
> Hi,
>
> I've asked SSQ about this and he does not believe that this scenario can
> currently be made to work on OS X. Aside from the threads issue, there
> could be other problems because in the context of applets the event loop
> gets run by AWT instead of by SWT. Your case can work on Windows because
> SWT can utilize a second Display there under the covers, but SWT's OS X
> implementation does not currently have this support. I found a few OS X
> bugs in bugzilla dealing with SWT/AWT integration, but it wasn't clear
> whether any of them dealt with a case like yours, so if you want you can log
> a new report with SWT at
> https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform &component=SWT .
>
> Grant
>
>
> "Ludovic Maitre"<ludovic.maitre@softbooking.com> wrote in message
> news:hm32hv$vqt$1@build.eclipse.org...
>> Hi Vijay,
>>
>> Thanks for your time, and sorry i should have explained which version i
>> use, it is the SWT 3.6M5 for Cocoa x64 available here :
>>
> http://download.eclipse.org/eclipse/downloads/drops/S-3.6M5- 201001291300/download.php?dropFile=swt-3.6M5-cocoa-macosx-x8 6_64.zip
>>
>> If you download this file and unzip the src.zip file, the code which
>> cause the problem is in the class :
>> swt-3.6M5-cocoa-macosx-x86_64/src/org/eclipse/swt/widgets/Di splay.java
>>
>> In the method void createDisplay (DeviceData data) at line 779, and more
>> specifically the lines 788 to 791 :
>> if (!NSThread.isMainThread()) {
>> System.out.println ("***WARNING: Display must be created on main
>> thread due to Cocoa restrictions."); //$NON-NLS-1$
>> error(SWT.ERROR_THREAD_INVALID_ACCESS);
>> }
>>
>> Notice that the applet snippet you mention in your mail is specifically
>> for Win32, however i guess you can derivate this source code (or the one
>> i have put in reference in my last mail) to make a simple applet which
>> illustrate the case (instantiate a display (AFAIK KO without commenting
>> the above code) then instantiate a shell would be enough).
>>
>> I will do a code sample like this if eventually i open an issue in
>> Eclipse bug tracker but before doing so i would like to know the feeling
>> of other devs on the subject.
>>
>> Perhaps it would suffice to write explicitely that applets with SWT are
>> not and will not be supported under Mac OS X ? However, it would be sad
>> and a deal breaker for some projects (and AWT applets works fine so why
>> not SWT...).
>>
>> Best regards and many thanks to have take time to look into this,
>>
>> Le 2/24/10 12:07 PM, vijay a écrit :
>>> Hi Ludovic,
>>>
>>> I downloaded Mac OSX (Mac/Cocoa/x86_64) (Supported Versions)
>>> SWT src and i did not find the code u specified...
>>>
>>> did i download the correct src???
>>
>
>
Re: Applet with embedded SWT impossible under Mac OS X ? [message #644212 is a reply to message #516804] Thu, 09 December 2010 23:07 Go to previous message
No real name is currently offline No real nameFriend
Messages: 1
Registered: December 2010
Junior Member
Try Java VM option: -XstartOnFirstThread

/Jan
Previous Topic:Re: How to enlarge a line in a Table (SWT)
Next Topic:Browser - Progress and Location Events on top.location.reload
Goto Forum:
  


Current Time: Tue Apr 23 14:41:13 GMT 2024

Powered by FUDForum. Page generated in 0.03949 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top