Package net.xoetrope.awt

Source Code of net.xoetrope.awt.Cleanup

package net.xoetrope.awt;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.util.Hashtable;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import net.xoetrope.xui.DialogSupport;
import net.xoetrope.xui.PageSupport;
import net.xoetrope.xui.XContentHolder;

import net.xoetrope.xui.XContentPane;
import net.xoetrope.xui.XPage;
import net.xoetrope.xui.XPageManager;
import net.xoetrope.xui.build.BuildProperties;
import net.xoetrope.xui.helper.XuiUtilities;

/**
* <p>Provides support for Popups. This class extends XPage giving
* a blank panel on which you can create custom dialogs. The dialog can be shown
* as a modal dialog which will block execution of the client code till the
* dialog is dismissed.</p><p>
* This dialog differs from the built-in Dialog class as it displays an embedded
* Xpage instance. In this way the dialog content can be created like any other XUI
* page</p>
* <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
* License:      see license.txt
* @version $Revision: 2.30 $
*/
public class XDialog extends XPage implements XContentPane, XPage.IXDialog, DialogSupport
{
  /*
   * Record style information for when the contentPanel is created.
   */
  private Color backColour, foreColour;
  private Font font;

  /** A boolean value used for checking a 'true' value */
  public static final boolean trueField = true;

  /** The default dialog padding. the padding indents the content page within the dialog frame */
  public static final int DEFAULT_PADDING = 0;

  /** A state flag indicating that no button clicked so far */
  public static final int NOTHING_CLICKED_YET = 0;

  /** A state flag indicating that the OK button was clicked */
  public static final int OK_CLICKED = 1;

  /** A state flag indicating that the CANCEL button was clicked */
  public static final int CANCEL_CLICKED = 2;

  /** A state flag indicating that the CLOSE button was clicked */
  public static final int CLOSE_CLICKED = 3;

  /** A state flag indicating that the NO button was clicked */
  public static final int NO_CLICKED = 4;

  /** A flag indicating whether or not the dialog is modal */
  private boolean bIsModal = false;

  /** A flag indicating whether or not the dialog automatically saves its data when it is closed */
  protected boolean saveOnClose = true;

  /** A flag indicating whether or not the dialog uses the system headers or if it draws the header itself */
  private boolean bUseNativeHeaders = false;
 
  /** A flag indicating whether or not the dialog should be sizable */
  private boolean bResizable = false;

  /** The return value, indicating which button was clicked */
  protected int returnValue = 0;

  /** The last return value */
  protected static int lastReturnValue;

  /** The return value, a user defined value */
  public Object returnObject;

  /** The 'content' panel that holds the dialog's XPage */
  protected XPanel contentPanel;

  /** The current dialog padding */
  protected int padding = 2;

  /** The component that had focus prior to display of the dialog. The dialog attempts to restore focus to this component when dismissed */
  protected Component focusComponent = null;

  /** Was the event dispatch thread found */
  boolean QAvailable = true;

  /** The name of the callback method */
  private String callback;

  /** The owner of the callback method */
  private Component callbackParent;

  /** The dialog title */
  private String title = "";

  /** The location at which to display the dialog specified by the user */
//  private Point userLocation;

  /** Was the close button clicked */
  protected int closeButtonID = -1;

  private Window dialogWindow;

  private Point onScreenLocation;

  private static int minorVersion = 1;

  static final boolean bFireEmptyEvent = useEmptyEvent();

  final Frame clientFrame;
  final Window appWindow;

  /**
   * Creates a new dialog and adds a content panel to the page. A handler is also
   * set so that the dialog will be dismissed when the escape key is pressed.
   */
  public XDialog()
  {
    super();
    clientFrame = project.getAppFrame();
    appWindow = project.getAppWindow();
   
    padding = DEFAULT_PADDING;
    bIsModal = true;

    init();
  }

  /**
   * Creates a new dialog and adds a content panel to the page. A handler is also
   * set so that the dialog will be dismissed when the escape key is pressed.
   * @param modal true for a modal dialog
   * @param pad the amount of padding in pixels with which to surround the page within the dialog frame
   */
  public XDialog( boolean modal, int pad )
  {
    super();
    clientFrame = project.getAppFrame();
    appWindow = project.getAppWindow();
    padding = pad;
    bIsModal = modal;

    init();
  }

  private void init()
  {
    if ( !BuildProperties.BUILD_JDK_118 ) {
      Hashtable dialogList = (Hashtable)project.getObject( "DialogList" );
      if ( dialogList == null ) {
        dialogList = new Hashtable();
        project.setObject( "DialogList", dialogList );
      }
      String pageName = getPageName();
      if ( pageName != null )
        dialogList.put( pageName, new java.lang.ref.SoftReference( this ));
    }

//    userLocation = null;

    pageHelper.componentFactory.setParentComponent( this );
    contentPanel = ( XPanel )pageHelper.componentFactory.addComponent( XPage.PANEL, 0, 0, 800, 600 );
    if ( backColour != null )
      contentPanel.setBackground( backColour );
    if ( foreColour != null )
      contentPanel.setForeground( foreColour );
    if ( font != null )
      contentPanel.setFont( font );

    pageHelper.componentFactory.setParentComponent( contentPanel );
    super.setVisible( false );

    // The content should be sized or the pack method should be called prior to
    // displaying the dialog with showDialog. The null layout allows it to setup
    // according to the sizes in the XML
    setLayout( null );
    contentPanel.setLayout( null );

    contentPanel.setBackground( Color.white );
    setBackground( Color.white );
    lastReturnValue = NOTHING_CLICKED_YET;
  }

  /*
   * Record the background color so the contentPanel can be set with it when created
   * @param c the background color
   */
  public void setBackground( Color c )
  {
    super.setBackground( c );
    backColour = c;
  }

  /*
   * Record the foreground color so the contentPanel can be set with it when created
   * @param c the foreground color
   */
  public void setForeground( Color c )
  {
    super.setForeground( c );
    foreColour = c;
  }

  /*
   * Record the font so the contentPanel can be set with it when created
   * @param f the font
   */
  public void setFont( Font f )
  {
    super.setFont( f );
    font = f;
  }

  /**
   * Get the return value of the most recently dismissed dialog
   * @return a value indicating the status or the button that was used to dismiss the dialog
   */
  public static int getLastReturnValue()
  {
    return lastReturnValue;
  }


  /**
   * Get the 'content' pane
   * @return the container that should hold the dialog's XPage instance
   */
  public Object getContentPane()
  {
    return contentPanel;
  }

  /**
   * Overload the XPage XCreated event and set the caption of the dialog from
   * the title attribute. Call super if overloaded.
   */
  public void pageCreated()
  {
    String titleAttrib = (String)getAttribute( "title", null );
    if ( titleAttrib != null )
      setCaption( pageHelper.componentFactory.translate( titleAttrib ));
  }

  /**
   * Size the dialog to hold the largest components (i.e. children of the content panel)
   */
  public void pack()
  {
    if ( padding == 0 ) {
      String padStr = (String)pageHelper.attribs.get( "padding" );
      if (( padStr != null ) && ( padStr.length() > 0 ))
        padding = new Integer( padStr ).intValue();
    }

    Point size = XuiUtilities.getMaxCoordinates( contentPanel );
    if ( padding > 0 )
        setSize( size.x + 2*padding + 2, size.y + 2*padding + 4 );
    else
        setSize( size.x, size.y );
  }

  /**
   * Set the dialog caption/title
   * @param c the new caption of dialog title
   */
  public void setCaption( String c )
  {
    title = c;
  }

  /**
   * Get the minimumsize for the dialog
   * @return the minimum size
   */
  public Dimension getMinimumSize()
  {
    return (Dimension)getSize();
  }

  /**
   * Set the preferred size for the component
   * @return the preferred size
   */
  public Dimension getPreferredSize()
  {
    return (Dimension)getSize();
  }

  /**
   * Set the dialog to use the native platform decorations (title bar and borders).
   * @param bh true to use native decorations.
   */
  public void setUseNativeHeaders( boolean bh )
  {
    bUseNativeHeaders = bh;
  }

  /**
   * Set the dialog to be modal or non-modal
   * @param modal true for a modal dialog
   */
  public void setModal( boolean modal )
  {
    bIsModal = modal;
  }

  /**
   * Overrides the setVisible method to close or show the dialog
   * @param b false to hide the dialog or true to show it
   */
/*  public void setVisible( boolean b )
  {
    if ( !b )
      closeDlg();
    else
      super.setVisible( true );
  }*/

  /**
   * Set the save on close option
   * @param save true to save the data when the dialog is closed or dismissed, false to
   * discard the data.
   */
  public void setSaveOnClose( boolean save )
  {
    saveOnClose = save;
  }

  /**
   * Set the resizable property. The resizing of dialogs only works if native
   * headers are used
   * @param state true for a resizable dialog
   * @since 2.0.7
   * @see setUseNativeHeaders
   */
  public void setResizable( boolean state )
  {
    bResizable = state;
  }

  /**
   * Dismiss the dialog and discard the data.
   */
  public void cancelDlg()
  {
    saveOnClose = false;
    closeButtonID = CANCEL_CLICKED;
    closeDlg();
  }

  /**
   * Close the dialog and restore focus
   */
  public void closeDlg()
  {
    if ( dialogWindow == null )
      return;
   
    dialogWindow.setVisible( false );

    if ( callback != null ){
      try {
        Class params[] = new Class[ 1 ];
        params[ 0 ] = getClass();
        Method m = callbackParent.getClass().getMethod( callback, params );
        Object args[] = new Object[ 1 ];
        args[ 0 ] = this;
        m.invoke( callbackParent, args );
      }
      catch ( Exception ex ) {
        ex.getCause().printStackTrace();
      }
    }

    if ( focusComponent != null ) {
      if ( BuildProperties.BUILD_JDK_118 )
        focusComponent.requestFocus();
      else if ( !focusComponent.hasFocus()) {
        final Component myFocusComponent = focusComponent;
        XuiUtilities.invokeLater( new Runnable()
        {
          public void run()
          {
            myFocusComponent.requestFocus();
          }
        } );
      }
    }
    pageHelper.eventHandler.suppressFocusEvents( false );

    clientFrame.setEnabled( true );
    appWindow.setEnabled( true );
    if (( closeButtonID != CANCEL_CLICKED ) && saveOnClose ) {
      saveBoundComponentValues();
      XPageManager pm = project.getPageManager();
      int numTargets = pm.getNumTargets();
     
      // Skip any toolbars
      int targetIdx = 0;
      Object t = pm.getTarget( targetIdx );
      if ( !( t instanceof XContentHolder ))
        targetIdx++;
     
      for ( ; targetIdx < numTargets; targetIdx++ )  {
        XContentHolder targetArea = (XContentHolder)pm.getTarget( targetIdx );
        PageSupport currentPage = pm.getCurrentPage( targetArea.getName());
        if ( currentPage != null )
          currentPage.updateBoundComponentValues();
      }
    }
    setStatus( XPage.LOADED );
    if ( closeButtonID == CLOSE_CLICKED )
      lastReturnValue = CLOSE_CLICKED;
    else if ( closeButtonID == CANCEL_CLICKED )
      lastReturnValue = CANCEL_CLICKED;
    else
      lastReturnValue = OK_CLICKED;

    pageHelper.eventHandler.removeHandlers( this );
    ((Cleanup)dialogWindow).cleanup();
    if ( !BuildProperties.BUILD_JDK_118 )
      dialogWindow.dispose();
    pageHelper = null;
    dialogWindow = null;
    contentPanel = null;
  }

  /**
   * Shows the dialog. This method calls showDialog( this ) after setting the
   * title, location and after setting the dialog to a size just large enough to
   * display all its content
   *
   * @param owner The container to which the dialog is added.
   * @param title The dialog title/caption
   * @param x The x location on screen to show the dialog
   * @param y The y location on screen to show the dialog
   * @return the returnValue
   */
  public int showDialog( Object owner, String title, int x, int y )
  {
    setModal( true );
    setCaption( pageHelper.componentFactory.translate( title ));
    if ( x > -1 )
      setLocation( x, y );
    pack();
    return showDialog( owner );
  }

  public int showDialog( Object owner, String title, Point pt )
  {
    return showDialog( owner, title, pt.x, pt.y );
  }

  /**
   * Shows the dialog. For modal dialog the showDialog method blocks till the
   * dialog is dismissed or hidden.  This method provides an alternative that
   * does not block execution of the calling thread but instead calls back a
   * method specified as an argument once the dialog has been dismissed.
   *
   * In some VMs such as the Microsoft VM it is not possible to gain access to
   * the EventQueue so as to implement blocking unless the code is loaded from a
   * signed CAB file. If this situtaion occurs an exception is thrown and a
   * non-blocking strategy is used.
   *
   * When the dialog is shown it will attempt to gain focus and upon dismissal
   * focus will be returned to the component that had focus prior to display of
   * the  dialog. A special case occurs when the dialog is displayed in response
   * to a focus event handler. The focus event will be processed as normal,
   * allowing transfer of focus but focus handler invocations related to the
   * showing and hiding of the dialog will be suppressed.
   * @param callBackParent The parent/owner for purposes of  a callback.
   * @param callBackMethod The name of a callback method in the parent (or null) to be invoked when the dialog is dismissed.
   */
  public void showDialog( Component callBackParent, String callBackMethod )
  {
    callbackParent = callBackParent;
    callback = callBackMethod;
    if ( callBackParent.getParent() == null )
      showDialog( (Container)callBackParent );
    else
      showDialog( callBackParent.getParent() );
  }

  /**
   * Shows the dialog. For modal dialog this method blocks till the dialog is
   * dismissed or hidden. A subclass can set the returnValue member to indicate
   * the status of the dialog upon dismissal.
   *
   * When the dialog is shown it will attempt to gain focus and upon dismissal
   * focus will be returned to the component that had focus prior to display of
   * the  dialog. A special case occurs when the dialog is displayed in response
   * to a focus event handler. The focus event will be processed as normal,
   * allowing transfer of focus but focus handler invocations related to the
   * showing and hiding of the dialog will be suppressed.
   *
   * @param owner The container to which the dialog is added.
   * @return the returnValue
   */
  public int showDialog( Object owner )
  {
    closeButtonID = -1;
    if ( owner != null )
      focusComponent = getFocusComponent( (Container)owner );
    if ( saveOnClose ) {
      XPage currentPage = (XPage)project.getPageManager().getCurrentPage( null );
      currentPage.saveBoundComponentValues();
    }
    updateBindings();
    updateBoundComponentValues();
    pageActivated();
    showModalWindow( this );


    // Gets around bug in the container control which doesn't disable it's child components
    requestFocus();
    repaint();

    if ( ( minorVersion < 4 ) && ( ! bUseNativeHeaders ) ) {
      synchronized ( getTreeLock() ) {
        Container parent = getParent();
        if ( ( parent != null ) && ( parent.getPeer() == null ) )
          parent.addNotify();
        if ( getPeer() == null )
          addNotify();
      }
      validate();

      if ( dialogWindow.isVisible() ) {
        if ( bIsModal ) {

          try {
            if ( QAvailable ) {
              dt = new XDialogEventDispatchThread( "AWT-Dispatch-Proxy",
                  getToolkit().getSystemEventQueue() );
              dt.start();
            }
            else
              return -1;
          }
          catch ( Exception ex ) {
            System.err.println( "error 1" );
            ex.printStackTrace();
            QAvailable = false;
            return -1;
          }

          while (( dialogWindow != null ) && ( dialogWindow.isVisible() ) ) {
            try {
              Thread.currentThread().yield();
              Thread.currentThread().sleep( 16 );
            }
            catch ( Exception e ) {}
          }

          if ( dt != null )
            try {
              dt.stopDispatching( bFireEmptyEvent );
            }
            catch ( Exception ex1 ) {
            }
        }
        else
          super.show();

        dt = null;
      }
    }

    return returnValue;
  }

  /**
   * Set the size of the dialog and centres it within the parent.
   * @param width The new width
   * @param height The new height
   */
  public void setSize( int width, int height )
  {
    contentPanel.setBounds( padding, padding, width - (padding*2), height - (padding*2) );
    super.setSize( width, height );
  }
 
  /**
   * Set the location of the dialog window
   * @param x the x coordinate, or -1 for ignore/default location, -2 for centered on screen
   * @param y the y coordinate
   */
  public void setLocation( int x, int y )
  {
    if ( x >= 0 )
      setLocation( new Point( x, y ));
    else if ( x == -2 )
      XuiUtilities.centerOnScreen( this );
  }

  /**
   * Set the location of the dialog window
   * @param location the point on screen at which the dialog is to be shown.
   */
  public void setLocation( Point location )
  {
      onScreenLocation = location;

      if ( onScreenLocation != null ) {
        Component objWindow = this;
        while (( objWindow != null ) && !( objWindow instanceof Window ))
          objWindow = objWindow.getParent();

        if ( objWindow != null )
          ((Window)objWindow).setLocation( onScreenLocation );
      }
  }

  /**
   * Gets the component that owns the focus.
   * @param cont the container to be checked for focus.
   * @return the focus component or null if the container does not have a
   * component that owns the input focus.
   */
  protected Component getFocusComponent( Container cont )
  {
    int numChildren = cont.getComponentCount();
    for ( int i = 0; i < numChildren; i++ ) {
      Component c = cont.getComponent( i );
      if ( c instanceof Container ) {
        Component cc = getFocusComponent( ( Container )c );
        if ( cc != null )
          return cc;
      }
      else {
        Window appWindow = project.getAppWindow();
        if (( appWindow != null ) && ( c == appWindow.getFocusOwner()))
          return c;
      }
    }

    return null;
  }

  /**
   * Provides access to an object representing the state of the dialog when it
   * was closed. It is the responsibility of the subclass to set this value
   * when it closes.
   * @return the return object
   */
  public Object getReturnObject()
  {
    return returnObject;
  }

  /**
   * A utility method used to determine if the last event corrseponds to a mouse
   * click. The notion of a click is extended by assuming the a mouse press and
   * release within a single component constitutes a click even if not at the
   * same coordinate. A MouseEvent.MOUSE_CLICKED is only triggered when the press
   * and release are at the same location and this is often inadequate for end-user
   * interaction.
   * @return true if the mouse was clicked
   */
  public boolean wasMouseClicked()
  {
    boolean res = ( closeButtonID == CLOSE_CLICKED ) || pageHelper.eventHandler.wasMouseClicked();
    closeButtonID = -1;
    return res;
  }

  /**
   * Shows the dialog as a modal window
   * @param contents the contents of the new dialog
   */
  public void showModalWindow( Component contents )
  {
    class HidingWindow extends Window implements Cleanup, MouseListener, MouseMotionListener
    {
      Point startPoint;
      Button b = new Button( "X" );
      ActionListener al;
      KeyListener kl;

      public HidingWindow( Frame frame )
      {
        super( frame );

        addMouseListener( this );
        addMouseMotionListener( this );
        dialogWindow = this;
        setBackground( SystemColor.control );
        setLayout( new BorderLayout() );
        contentPanel.setVisible( true );

        b.addActionListener( al = new ActionListener()
        {
          public void actionPerformed( ActionEvent e )
          {
            closeButtonID = CLOSE_CLICKED;
            closeDlg();
          }
        } );

        b.addKeyListener( kl = new KeyListener()
        {
          public void keyPressed( KeyEvent e )
          {
            if ( e.getKeyCode() == e.VK_ESCAPE ) {
              closeButtonID = CANCEL_CLICKED;
              closeDlg(); //setVisible( false );
            }
          }

          public void keyReleased( KeyEvent e )
          {}

          public void keyTyped( KeyEvent e )
          {}
        } );

        b.setBackground( Color.red );
        b.setForeground( Color.white );
        b.setSize( 10, 10 );

        Panel titlePanel = new Panel( new BorderLayout());
        Label titleText = new Label( title );
        Panel closePanel = new Panel( new FlowLayout( FlowLayout.RIGHT, 1, 1 ));
        closePanel.setBackground( SystemColor.activeCaption );
        closePanel.add( b );

        titlePanel.setBackground( SystemColor.activeCaption );
        titleText.setBackground( SystemColor.activeCaption );
        titleText.setForeground( SystemColor.activeCaptionText );
        titleText.setAlignment( titleText.LEFT );

        titlePanel.add( titleText, BorderLayout.CENTER );
        titlePanel.add( closePanel, BorderLayout.EAST );
        add( titlePanel, BorderLayout.NORTH );

        pack();
        doLayout();

        // Note: JDK 1.1.x requires the this pointer here
        if ( onScreenLocation != null )
          this.setLocation( onScreenLocation );
      }

      public void cleanup()
      {
        removeMouseListener( this );
        removeMouseMotionListener( this );
        b.removeActionListener( al );
        b.removeKeyListener( kl );
        al = null;
        kl = null;
        b = null;
      }

      public Insets getInsets()
      {
        return new Insets( 4, 4, 4, 4 );
      }

      public void setVisible( boolean show )
      {
        if ( show )
          dialogWindow.doLayout();
        super.setVisible( show );
        if ( !show ) {
          clientFrame.setEnabled( true );
          appWindow.setEnabled( true );
          clientFrame.toFront();

        }
        else {
          toFront();
          b.requestFocus();
        }
      }

      public void paint( Graphics g )
      {
        Dimension size = getSize();
        Color c = g.getColor();
        g.setColor( SystemColor.activeCaption );
        g.fill3DRect( 0, 0, size.width, size.height, true );
        g.setColor( c );

        contentPanel.repaint();
        super.paint( g );
      }

      // Start of mouse methods-----------------------------------------------------
      public void mouseClicked(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}
      public void mouseMoved(MouseEvent e) {}

      public void mousePressed(MouseEvent e)    {
        startPoint = e.getPoint();
      }

      public void mouseReleased(MouseEvent e)
      {
        Point endPoint = e.getPoint();
        Point oldPos = getLocation();
        // Note: JDK 1.1.x requires the this pointer here
        this.setLocation( oldPos.x + endPoint.x - startPoint.x, oldPos.y + endPoint.y - startPoint.y );
      }

      public void mouseDragged( MouseEvent e )
      {
        Point endPoint = e.getPoint();
        Point oldPos = getLocation();
        // Note: JDK 1.1.x requires the this pointer here
        this.setLocation( oldPos.x + endPoint.x - startPoint.x, oldPos.y + endPoint.y - startPoint.y );
        startPoint = endPoint;
      }
      // End of mouse methods-------------------------------------------------------
    };

    class HidingDialog extends Dialog implements Cleanup, MouseListener, MouseMotionListener
    {
      Point startPoint;
      Button b = new Button( "X" );
      ActionListener al;
      KeyListener kl;
      WindowListener wl;

      public HidingDialog( Frame frame, boolean bIsDlgModal )
      {
        super( frame, bIsDlgModal );
        setResizable( bResizable );

        addMouseListener( this );
        addMouseMotionListener( this );
        dialogWindow = this;
        contentPanel.setVisible( true );

        if ( !bUseNativeHeaders ) {
          setBackground( SystemColor.control );
          setLayout( new BorderLayout() );

          callDecorationMethod( this, true );
          b.addActionListener( al = new ActionListener()
          {
            public void actionPerformed( ActionEvent e )
            {
              closeButtonID = CLOSE_CLICKED;
              closeDlg();
            }
          } );

          b.addKeyListener( kl = new KeyListener()
          {
            public void keyPressed( KeyEvent e )
            {
              if ( e.getKeyCode() == e.VK_ESCAPE ) {
                closeButtonID = CLOSE_CLICKED;
                closeDlg(); //setVisible( false );
              }
            }

            public void keyReleased( KeyEvent e )
            {}

            public void keyTyped( KeyEvent e )
            {}
          } );

          b.setBackground( Color.red );
          b.setForeground( Color.white );
          b.setSize( 10, 10 );

          Panel titlePanel = new Panel( new BorderLayout());
          Label titleText = new Label( title );
          Panel closePanel = new Panel( new FlowLayout( FlowLayout.RIGHT, 1, 1 ));
          closePanel.setBackground( SystemColor.activeCaption );
          closePanel.add( b );

          titlePanel.setBackground( SystemColor.activeCaption );
          titleText.setBackground( SystemColor.activeCaption );
          titleText.setForeground( SystemColor.activeCaptionText );
          titleText.setAlignment( titleText.LEFT );

          titlePanel.add( titleText, BorderLayout.CENTER );
          titlePanel.add( closePanel, BorderLayout.EAST );
          add( titlePanel, BorderLayout.NORTH );
        }
        else
          super.setTitle( title );

        addWindowListener( wl = new WindowListener()
        {
          public void windowClosing( WindowEvent e )
          {
            closeButtonID = CLOSE_CLICKED;
            closeDlg();
          }
          public void windowActivated( WindowEvent e ){}
          public void windowDeactivated( WindowEvent e ){}
          public void windowClosed( WindowEvent e ){}
          public void windowOpened( WindowEvent e ){}
          public void windowDeiconified( WindowEvent e ){}
          public void windowIconified( WindowEvent e ){}
        } );

        pack();
        doLayout();
      }

      public void cleanup()
      {
        removeMouseListener( this );
        removeMouseMotionListener( this );
        removeWindowListener( wl );
        b.removeActionListener( al );
        b.removeKeyListener( kl );
        wl = null;
        al = null;
        kl = null;
        b = null;
      }

      public Insets getInsets()
      {
        if ( bUseNativeHeaders )
          return super.getInsets();
        return new Insets( 4, 4, 4, 4 );
      }

      public void paint( Graphics g )
      {
        if ( !bUseNativeHeaders ) {
          Dimension size = getSize();
          Color c = g.getColor();
          g.setColor( SystemColor.activeCaption );
          g.fill3DRect( 0, 0, size.width, size.height, true );
          g.setColor( c );
        }

        super.paint( g );
      }

      // Start of mouse methods-----------------------------------------------------
      public void mouseClicked(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}
      public void mouseMoved(MouseEvent e) {}

      public void mousePressed(MouseEvent e)
      {
        startPoint = e.getPoint();
      }

      public void mouseReleased(MouseEvent e)
      {
        Point endPoint = e.getPoint();
        Point oldPos = getLocation();
        // Note: JDK 1.1.x requires the this pointer here
        if ( startPoint != null )
          this.setLocation( oldPos.x + endPoint.x - startPoint.x, oldPos.y + endPoint.y - startPoint.y );
      }

      public void mouseDragged(MouseEvent e)
      {
        Point endPoint = e.getPoint();
        Point oldPos = getLocation();
        // Note: JDK 1.1.x requires the this pointer here
        if ( startPoint != null )
          this.setLocation( oldPos.x + endPoint.x - startPoint.x, oldPos.y + endPoint.y - startPoint.y );
      }
      // End of mouse methods-------------------------------------------------------
    };

    dialogWindow = ( ( !bUseNativeHeaders && ( minorVersion < 4 ) ) ? ( Window )new HidingWindow( clientFrame ) : new HidingDialog( clientFrame, bIsModal ) );

    dialogWindow.add( contents, BorderLayout.SOUTH );
    setStatus ( XPage.ACTIVATED );
    contents.setVisible( true );

    dialogWindow.pack();
    Dimension wSize = dialogWindow.getSize();
    if ( onScreenLocation == null )
      dialogWindow.setLocation( appWindow.getLocation().x + ( appWindow.getSize().width / 2 - wSize.width / 2 ),
                     appWindow.getLocation().y + ( appWindow.getSize().height / 2 - wSize.height / 2 ) );
    else
      dialogWindow.setLocation( onScreenLocation );

    /**
     * @todo This needs to be retested with different JDKs. Suspect that the
     * check should be if ( ! bUseNativeHeaders && bIsModal )
     */
    if ( bUseNativeHeaders && bIsModal ) {
      clientFrame.setEnabled( false );
      appWindow.setEnabled( false );
    }
    dialogWindow.doLayout();
    dialogWindow.setVisible( true );
    if (( dialogWindow != null ) && dialogWindow.isValid())
      dialogWindow.repaint();
  }

  /**
   * The event dispatch mechanism inserts an empty event when stopping dispatching.
   * This mechanism fails on some VMs. This method determines whether or not to
   * use the mechanism. It does not work for the MS VM (1.1.4) for instance.
   * @return true to use the empty event mechanism.
   */
  private static boolean useEmptyEvent()
  {
    minorVersion = XuiUtilities.getMinorVersion();
//    int revision = new Integer( prop.substring( ++pos2 )).intValue();

    if ( minorVersion < 2 )
      return true;//false;

    return true;
  }

  /**
   * Call setUndecorated via reflection as it is only in JDK1.4+
   * @param obj a reference to the dialog instance
   * @param value the boolean argument
   * @return true if the call succeeds
   */
  private boolean callDecorationMethod( Object obj, boolean value )
  {
    try {
      Class c = obj.getClass();
      Field f = getClass().getField( "trueField" );
      Class[] params = new Class[1];
      params[ 0 ] = f.getType();
      Method theMethod = c.getMethod( "setUndecorated", params );
      //String methodString = theMethod.getName();
      Object args[] = new Object[ 1 ];
      args[ 0 ] = f.get( this );
      theMethod.invoke( obj, args );
      return true;
    }
    catch ( Exception ex ) {
      ex.getCause().printStackTrace();
    }
     return false;
  }

  private XDialogEventDispatchThread dt = null;
}

interface Cleanup
{
  public void cleanup();
}
TOP

Related Classes of net.xoetrope.awt.Cleanup

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.