package net.xoetrope.awt;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.MenuBar;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.Hashtable;
import javax.swing.JWindow;
import net.xoetrope.debug.AWTExceptionHandler;
import net.xoetrope.xui.PageSupport;
import net.xoetrope.xui.XApplicationContext;
import net.xoetrope.xui.XPage;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XStartupObject;
import net.xoetrope.xui.build.BuildProperties;
/**
* <p>This class is constructed with a window or frame
* and can be part of an applet or an application. The class acts as the main
* entry point to an XUI application and provides some of the infrastructure
* needed to support the application.</p>
* <p>The applet can provide support for a frameset or a single page. Page display
* functions are also supported to allow the application to display more than a
* single page or change the page that is displayed.</p>
* <p>By choosing either the AWT or Swing version of the XApplet you choose to
* have either an AWT or a Swing application/applet. In general once this choice
* has been made you should not mix toolkits.</p>
* <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License: see license.txt
* @version $Revision: 2.21 $
*/
public class XApplet extends Applet implements XStartupObject
{
protected Window appWindow;
protected Frame clientFrame;
protected Panel pageHolder;
protected boolean bUseWindow;
protected XApplicationContext applicationContext;
/**
* main method to be invoked as an application. This method is invoked as the
* entry point to the 'Application', it is not used if an Applet is being
* launched. This method establishes the frame within which the application
* runs. If overloading this method remeber to call the setup method.
* @param args the command line arguments
*/
public static void main( String args[] )
{
if ( BuildProperties.DEBUG )
AWTExceptionHandler.register();
Frame frame = new Frame();
XApplet applet = new XApplet( args, frame );
frame.add( applet );
frame.validate();
}
/**
* A default constructor. Most of the setup work is actually done by the initialize
* method and is called by the main method or the init method depending on
* whether or not an application of applet is being launched.
* @param args the application command-line arguments
* @param frame the parent frame
*/
public XApplet()
{
this( null, null );
}
/**
* Create a new application. Most of the setup work is actually done by the initialize
* method and is called by the main method or the init method depending on
* whether or not an application of applet is being launched.
* @param args the application command-line arguments
* @param frame the parent frame
*/
public XApplet( String args[], Frame f )
{
clientFrame = f;
AwtWidgetAdapter.getInstance();
applicationContext = new XApplicationContext( this, args );
}
/**
* Get the parent object
* @return the parent
*/
public Object getParentObject()
{
return getParent();
}
/**
* Get the package name for the default widget set
*/
public String getWidgetClassPackage()
{
return XPage.XUI_AWT_PACKAGE;
}
/**
* Setup frameset. This method is called prior to the addition of any target
* areas in the framset and prior to the display of any pages. Since this
* applet does not support configurable framesets, this method ignores the
* parameter values passed.
* @param params the framset parameters if any
*/
public void setupFrameset( Hashtable params )
{
}
/**
* Display a window decoration, for example a toolbar
* @param page the new page
* @param constraint a value controlling how and where the decoration is
* displayed, this value is application type specific
* @return the page being displayed
*/
public Object displayDecoration( PageSupport page, String constraint )
{
if ( constraint.equals( "NORTH" )) {
Container cont = this;
if ( bUseWindow )
cont = appWindow;
cont.add( (Component)page, BorderLayout.NORTH );
return page;
}
return null;
}
/**
* Validate and repaint the display
*/
public void refresh()
{
if ( appWindow != null ) {
appWindow.invalidate();
appWindow.validate();
appWindow.repaint();
}
}
/**
* <p>Restore the normal page views, as in the case of the docking layout where
* panels may be zoomed or minimized. The method is called prior to the
* display of a new page.</p>
* <p>In this context the method has no effect.</p>
*/
public void restoreViews()
{
}
public Object getContentPaneEx()
{
return pageHolder;
}
public void setAppTitle( String title )
{
try {
if ( clientFrame != null )
clientFrame.setTitle( title );
}
catch ( Exception ex1 ) {
ex1.printStackTrace();
}
}
/**
* Set the application icon
* @param img the image name
*/
public void setIcon( Image icon )
{
if ( icon != null )
clientFrame.setIconImage( icon );
}
/**
* Setup the windowing.
* @param context the owner application context
* @param currentProject the owner project
* @param clientWidth the desired width of the application
* @param clientHeight the desired height of the application
*/
public void setupWindow( XApplicationContext context, XProject currentProject, int clientWidth, int clientHeight )
{
currentProject.setStartupParam( "MainClass", "net.xoetrope.awt.XApplet" );
String sUseWindow = currentProject.getStartupParam( "UseWindow" );
bUseWindow = "true".equals( sUseWindow ) ? true : false;
if ( clientFrame != null )
clientFrame.setSize( clientWidth, clientHeight );
appWindow = null;
if ( bUseWindow ) {
appWindow = new JWindow( clientFrame );
appWindow.setSize( clientWidth, clientHeight );
appWindow.addWindowListener( context );
clientFrame.addWindowListener( context );
}
else {
appWindow = clientFrame;
if ( clientFrame != null )
clientFrame.addWindowListener( context );
}
appWindow.setLayout( new BorderLayout());
appWindow.add( pageHolder = new Panel(), BorderLayout.CENTER );
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if ( bUseWindow ) {
Dimension frameSize = appWindow.getSize();
appWindow.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );
appWindow.setVisible( true );
clientFrame.setLocation( screenSize.width / 2, screenSize.height / 2 );
clientFrame.setSize( 0,0 );
clientFrame.setVisible( true );
}
else if ( clientFrame != null ) {
Dimension frameSize = clientFrame.getSize();
String center = null;
try {
center = currentProject.getStartupParam( "CenterWin" );
}
catch ( Exception ex ) {
}
if ( "true".equals( center ))
clientFrame.setLocation(( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );
clientFrame.setVisible( true );
}
currentProject.setApplet( this );
currentProject.setStartupObject( this );
currentProject.setAppFrame( clientFrame );
currentProject.setAppWindow( appWindow );
}
/**
* Invoked when used as an applet. Sets up the startup file and initialises
* the application. Reads the applet parameters and calls initialize.
*/
public void init()
{
applicationContext.init();
}
/**
* Gets the Frame containing the applet.
* @return Frame which is the applet or application's parent
*/
public Frame getFrame()
{
if ( clientFrame != null )
return clientFrame;
Container parent;
for ( parent = getParent(); parent != null; parent = parent.getParent() ) {
if ( ( parent != null ) && ( parent instanceof Frame ) )
return ( Frame )parent;
}
return null;
}
/**
* Set the menubar.
* @param mb the new menu bar
*/
public void setMenuBar( MenuBar mb )
{
clientFrame.setMenuBar( mb );
}
/**
* Get the menubar, setting it up if it is not already added to the
* application frame
* @return the menu bar
*/
public Object getApplicationMenuBar()
{
return clientFrame.getMenuBar();
}
/**
* Set the menubar
* @param mb the menubar
*/
public void setApplicationMenuBar( Object mb )
{
clientFrame.setMenuBar( (MenuBar)mb );
}
}