/******************************************************************************
A collection of window utility static methods so they can be called
from anywhere.

----------------------------------------------------
Copyright (c) Gunnar Gotshalks. All Rights Reserved.

Permission to use, copy, modify, and distribute this software
and its documentation for NON-COMMERCIAL purposes and
without fee is hereby granted. 
*******************************************************************************/

package FlexOr.utility;
import java.awt.*;

public class WindowUtility {

/******************************************************************************
Create a window in response to a button press.  Very useful when applets
are big and you want it to run independently of a browser.

@param windowTitle Title for the window.
@param windowClass The class to run within the window.
@param windowWidth Width of the window in pixels.
@param windowHeight Height of the window in pixels.
@param button The botton creating the window so it can be disabled when
the window is created.  Enter null if there no such button or you do not
want button status to change.

@exception ClassNotFoundException when the windowClass passed could
not be found.
@exception NotFrameSubclassException when windowClass is not a Frame subclass.
@exception IllegalAccessException when windowClass or its initializer
is not accessible.  For example, when the no parameter class
constructor is not declared pooublic.
@exception InstantiationException when the class is abstract, an
interface or primitive type; or because of some other reason.
@exception NoSuchMethodException when the class does not have a
no parameter constructor.
*/

@SuppressWarnings("unchecked")
public static void createWindow(String windowTitle, String windowClass,
                         int windowWidth, int windowHeight, Button button)
       throws ClassNotFoundException, IllegalAccessException,
              InstantiationException, NoSuchMethodException {
  Class <Object> windowClassObject = null;
  Class <Object> tmp = null;
  String name = null;
  // Does the window class exists.
  try { windowClassObject = (Class<Object>) Class.forName(windowClass); }
  catch (ClassNotFoundException e) {
    if (button != null) button.setEnabled(false);
    throw new ClassNotFoundException("Can't create window:" + 
          " Couldn't find class " + windowClass);
  }

  // Is the class a frame?
  for (tmp = windowClassObject, name = tmp.getName();
       !( name.equals("java.lang.Object") ||
          name.equals("java.awt.Frame") ); ) {
    tmp = (Class<Object>) tmp.getSuperclass();
    name = tmp.getName();
  }
  if ((name == null) || name.equals("java.lang.Object")) {
    if (button != null) button.setEnabled(false);
    throw new NotFrameSubclassException(
      "Can't create window: " + windowClass + " isn't a Frame subclass.");
  }
  else if (name.equals("java.awt.Frame")) { 
           Frame window = null;

           try { window = (Frame)windowClassObject.newInstance(); }
           catch (IllegalAccessException e) {
                if (button != null) button.setEnabled(false);
                throw new IllegalAccessException(
                  "Illegal access to class " + windowClass);
           }
           catch(InstantiationException e) {
                if (button != null) button.setEnabled(false);
                throw new InstantiationException(
                  "Couldn't create instance of class " + windowClass);
           }
           catch(Exception e) {
                if (button != null) button.setEnabled(false);
                throw new NoSuchMethodException(
                  "No creation method of type expected " + windowClass);
           }
           
           window.setTitle(windowTitle);

           window.pack();
           if ((windowWidth > 0) | (windowHeight > 0)) {
               window.setSize(Math.max(windowWidth,
                             window.getSize().width),
                             Math.max(windowHeight,
                             window.getSize().height));
           }

           window.setVisible(true);
           }
}

}
