Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Class Loading on Different Opperating Systems

Thread view: 
Rationem - 31 Mar 2006 16:09 GMT
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?

Thanks,
Adam
Chris Smith - 31 Mar 2006 18:03 GMT
> 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:
[quoted text clipped - 19 lines]
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Steve W. Jackson - 31 Mar 2006 19:47 GMT
[ snip ]

> 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.

WindowListener.windowClosing works exactly the same way on a Mac as it
does in Windows and Linux.

The origins of this question are not about handling window closing
events, but about the proper way when running on a Mac to handle the
system-provided Quit and About menu items.  If you took a Swing app you
designed on Windows or Linux and moved it to a Mac, you would find those
items present whether you wanted them or not.  And the Quit menu item,
since your app wouldn't provide any handling for it, would simply
unceremoniously terminate your application without your intervention.  
The About item would display a simplistic dialog with "Java" in it
rather than using your already-built code.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Steve W. Jackson - 31 Mar 2006 19:11 GMT
> 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:
[quoted text clipped - 22 lines]
> Thanks,
> Adam

I can't imagine why you're using toLowerCase, since the value of that
property when running on Mac OS X will be *exactly* "Mac OS X" on every
system.

In what way is it failing on Windows?  Do you have code references
elsewhere (not shown here) to those Apple classes that are evaluated at
runtime on a Windows machine?

You might find more help from Apple via some useful web pages at  
<http://developer.apple.com/referencelibrary/Java/idxPorting-date.html>.  
On that page is a link entitled OSXAdapter that shows (with source code)
how to use reflection to hook these Apple classes into an application.  
Just below it is one entitled Unsolicited About Boxes which shows more
on how to properly override the About and Quit behaviors.  And just
below that is a link entitled AppleJavaExtensions where you can get stub
classes for compiling on other platforms.  I would think that one or
more of those would provide useful answers.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Rationem - 01 Apr 2006 17:59 GMT
The code below when run on a Mac works fine. It does not work on a
windows computer citing a class not found.

> > 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:
[quoted text clipped - 42 lines]
>
> = Steve =
Roedy Green - 01 Apr 2006 19:16 GMT
> com.apple.eawt.Application

is this something you invented or Apple did?

If you, you have no business using Apple's package name.
If Apple,  that package appears to be Apple only.  At the very least
you would have to arrange for the jar to be present.  

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Steve W. Jackson - 10 Apr 2006 21:47 GMT
> > com.apple.eawt.Application
>
[quoted text clipped - 3 lines]
> If Apple,  that package appears to be Apple only.  At the very least
> you would have to arrange for the jar to be present.

I have a much better idea.  Refer back to my earlier posts on the
subject.  Apple has public posted the information on how to do this
right, so that you have access to the stub classes for compiling on any
platform and making sure the Mac-specific code only executes on a Mac.
Signature

Steve W. Jackson
Montgomery, Alabama



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.