Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SVN, XCode, and pre-existing code...





Michael Hall wrote:
I seem to remember someone claiming sometime that you didn't actually need the reflection.
You don't need to use any reflection to use the ApplicationAdapter on non-Mac platforms without including the stubs. All you need to do is make sure that the code path that executes when running on non-macs doesn't actually use any classes from the stub. I do it with a simple check of the os system property.

It appears that Java's late binding requires this to work.


e.g.

public class MyMainGUI extends JFrame
{
...
// inside some initialization method
if (mac) {
// do stuff with application adapter like instantiate classes implementing ApplicationListener
// WITHOUT using reflection
OSXAdapter.registerMacOSXApplication(myApp);
OSXAdapter.enablePrefs( true );
}
...
}


class OSXAdapter extends ApplicationAdapter
{
private static OSXAdapter theAdapter;
private static com.apple.eawt.Application theApplication;
private MyAppClass mainApp;
private OSXAdapter( MyAppClass app )
{
mainApp = app;
}
@Override
public void handleAbout( ApplicationEvent ae )
{
if( mainApp != null )
{
ae.setHandled( true );
mainApp.getGUI().doAboutBox();
}
else
{
throw new IllegalStateException("handleAbout: KlamathController detached from listener");
}
}


@Override
public void handlePreferences( ApplicationEvent ae )
{
if( mainApp != null )
{
mainApp.doPreferences();
ae.setHandled( true );
}
else
{
throw new IllegalStateException("handleAbout: KlamathController detached from listener");
}
}


@Override
public void handleQuit( ApplicationEvent ae )
{
if( mainApp != null )
{
ae.setHandled( false ); // must do this to delay or cancel the quit
mainApp.exit();
}
else
{
throw new IllegalStateException("handleAbout: KlamathController detached from listener");
}
}


public static void registerMacOSXApplication( MyAppClass app )
{
if( theApplication == null)
{
theApplication = new Application();
}
if( theAdapter == null )
{
theAdapter = new OSXAdapter( app);
}
theApplication.addApplicationListener( theAdapter );
}
public static void enablePrefs( boolean enabled )
{
if( theApplication == null )
{
theApplication = new Application();
}
theApplication.setEnabledPreferencesMenu( enabled );
}
}



Using the above code OSXAdapter will never load on a non-Mac and all the references to the Apple specific classes are in it.. so you're good to go.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden


This email sent to email@hidden
References: 
 >Re: SVN, XCode, and pre-existing code... (From: Doug Zwick <email@hidden>)
 >Re: SVN, XCode, and pre-existing code... (From: Michael Hall <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.