Re: Class Loading on Different Opperating Systems



Rationem <rationem@xxxxxxxxx> wrote:
In an attempt to handle the special CMD+Q on a Mac that resides in the
application menu I had to put in the following code:

if (System.getProperty("os.name").toLowerCase().indexOf("mac")
!= -1)
{
com.apple.eawt.Application app = new
com.apple.eawt.Application();

app.addApplicationListener(new
com.apple.eawt.ApplicationAdapter() {
public void handleQuit(com.apple.eawt.ApplicationEvent
e)
{
attemptToQuit();
}
});
}

This works properly on a Mac but does not work on a windows machine
because the com.apple.eawt.* classes are not avabilible. Is there any
way I can use this code and still maintain the cross-platform aspect of
java?

Sure; reflection can do what you want. The naive way would look like
this (omitting exception handling).

if (System.getProperty("os.name").toLowerCase()
.indexOf("mac") != -1)
{
Class appc = Class.forName("com.apple.eawt.Application");
Object app = appc.newInstance();

Class lc = Class.forName(
"com.apple.eawt.ApplicationListener");
Object listener = Proxy.newProxyInstance(
getClassLoader(), new Class[] { lc },
new InvocationHandler() {
public Object invoke(
Object proxy,
Method method,
Object[] args)
{
if (method.getName().equals("handleQuit")
{
attemptToQuit();
}
}
});

Method m = appc.getMethod("addApplicationListener", lc);
m.invoke(app, listener);
}

Of course, that's the naive way. A better way would be to put all that
code into a class that implements some interface, and then do something
like this:

if (System.getProperty("os.name").toLowerCase()
.indexOf("mac") != -1)
{
AppCloseHandler h = Class.forName(
"mypackage.MacintoshCloseHandler").newInstance();
h.setupClose();
}

AppCloseHandler is an interface that you write. Inside of the class
mypackage.MacintoshCloseHandler, you can make static use of the Mac-
specific classes, since this code will only get loaded when those
classes are available.

I still find it hard to believe that WindowListener.windowClosing
doesn't work properly on the Mac... but if you've tried it, I guess I
can't argue since I don't have a Mac to try it with.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
.



Relevant Pages

  • Re: Cost of ownership: MV vs. SQL Server
    ... >>app server independent, etc, but you cannot be all of those at once. ... > mention that .NET seems to satisfy all of the business requirements ... (both Macs & Linux), so my criteria are different from yours. ... > and this market has never expressed a real need for Mac, ...
    (comp.databases.pick)
  • Re: My G5 & Leopard
    ... It offers a great deal over winders. ... It's functionality windows ain't got. ... With windows you are stuck with it's app centric menu driven ... With a Mac you _can_ use it as an app centric menu driven ...
    (comp.sys.mac.advocacy)
  • Re: Why no Newsreader.app? AND MORE!
    ... this wonderful change from that old buggy classic mac os. ... yet despite many of those extensions known to cause problems, ... it was a buggy geotagging app that i tried one day. ...
    (comp.sys.mac.apps)
  • Re: Running Windows on Mac
    ... when using any sort of virtual app such as VMWare ... I'm mildly curious: MacSpeech runs on OSX. ... use it to feed transliterated text into a Windows program running in ... I run both Windows and Mac versions of speech software. ...
    (uk.comp.sys.mac)
  • Re: My G5 & Leopard
    ... > It offers a great deal over winders. ... This is available to any app> built ... With a Mac you _can_ use it as an app centric menu driven ... > Windows: no std way to do this so I will run through several> variations. ...
    (comp.sys.mac.advocacy)