views:

309

answers:

2

Hello,

Eclipse allows you debug applets using sun.applet.AppletViewer. It will actually start the applet, run it and emulate complete browser runtime. If you have javascript manipulation code in your applet this will cause JSException because the present context is not a real JS enabled engine.

How would you solve this issue? I can see several possible paths:

  1. Wrap the exception in try {} catch () and ignore it - Not good practice.
  2. Pass and environment flag that would tell the code we are currently in debug mode, so that it won't try to execute JS code - OK but will need manual intervention from each developer.
  3. Identify current context is not a browser - How?
  4. Find another AppletViewer that can emulate a complete browser behavior, including JS - Is there ?
  5. More?

Thank for your ideas.

+1  A: 

Not a fail safe but a workable solution I've managed to come up with:

private void notifySelectionState(){
 JSObject jsObject = null;

 try {
  jsObject = JSObject.getWindow(applet);

  // An exception can be thrown here (hopefully) only is running in debug environment...
 } catch (JSException e) {
  // So actually what I'm doing here is checking (in a very lame fashion) for if I'm in a the browser
  // content or in the AppletViewer
 } 

 if (jsObject != null) {
  jsObject.call(...);
 }
}
Maxim Veksler
A: 

On certain older browsers, JSObject will throw generic RuntimeExceptions instead of JSException. So you may want to throw your net a little bit wider and leave a good comment in your wrappers.

Jon Bringhurst