XgmDialogContainer.java :  » ERP-CRM-Financial » Evaristo-4.0 » com » m16e » tools » xgm » Java Open Source

Java Open Source » ERP CRM Financial » Evaristo 4.0 
Evaristo 4.0 » com » m16e » tools » xgm » XgmDialogContainer.java
/*
 * XgmDialogContainer.java
 *
 *
 */

package com.m16e.tools.xgm;

import com.m16e.tools.MpRuntimeException;
import com.m16e.tools.xml.XmlException;
import com.m16e.tools.xml.XmlTreeNode;
import com.m16e.tools.xml.XmlTreeNodeable;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
import org.apache.log4j.Logger;


///////////////////////////////////////////////////////////////
/**
 * @author carlos
 *
 * TODO insert class description
 */
public class XgmDialogContainer 
{
  XgmRootContainer xgmRootContainer = null;
  XmlTreeNodeable output = null;
  boolean cancel = false;
  boolean waiting = true;
  String okBtText = "OK";
  String okBtAction = "default";
  boolean useMainPanelOnly = true;

  Logger logger = Logger.getLogger( XgmDialogContainer.class );

  ///////////////////////////////////////////////////////////////
  //public XgmDialogContainer( XgmContainer xc ) { setXgmContainer( xc ); }

  ///////////////////////////////////////////////////////////////
  public XgmDialogContainer( String layoutFilename ) 
  throws FileNotFoundException, IOException, XgmException, XmlException
  {
    String layout = XgmFactory.getInstance().readLayout( layoutFilename );
    xgmRootContainer = new XgmRootContainer();
    xgmRootContainer.setLayout(
      layoutFilename, layout, 
      XgmContainer.ContainerType.TYPE_PANEL, false, (String[]) null );
  }
  
  ///////////////////////////////////////////////////////////////
  public XgmContainer getXgmRootContainer() { return xgmRootContainer; }
  //public void setXgmContainer( XgmRootContainer xc ) { xgmRootContainer = xc; }

  ///////////////////////////////////////////////////////////////
  public void setOkButton( String text, String action )
  {
    okBtText = text;
    okBtAction = action;
  }
  
  ///////////////////////////////////////////////////////////////
  private class BOkAction
  extends AbstractAction
  {
    String actionName = null;
    //public BOkAction() { super( "OK" ); actionName = "OK"; }
    public BOkAction( String buttonText, String action ) 
    { super( buttonText ); actionName = action; }
    
    public void actionPerformed( ActionEvent ae )
    {
      if( logger.isDebugEnabled() )
      {
        logger.debug(
          "Waiting: " + waiting +
          "; component name: " + xgmRootContainer.getComponentName() );
      }
      if( !waiting )
        return;
      waiting = false;
      output = new XmlTreeNode( XgmAction.TAG_ACTION );
      output.setAttribute( XgmAction.ATTRIB_ACTION_NAME, actionName );
      try
      {
        output.insert( xgmRootContainer.getValueAsXmlTreeNodeable() );
      }
      catch( XgmException e )
      {
        String msg = e.getLocalizedMessage();
        logger.error( e.getMessage(), e );
        throw new MpRuntimeException( e );
      }
      if( logger.isDebugEnabled() )
      {
        logger.debug(
        "output: " + output.toXml( 1 ) );
      }
    }
  }
  ///////////////////////////////////////////////////////////////
  private class BCancelAction
  extends AbstractAction
  {
    public BCancelAction() { super( "Cancel" ); }
    
    public void actionPerformed( ActionEvent ae )
    {
      if( logger.isDebugEnabled() )
      {
        logger.debug(
          "Waiting: " + waiting +
          "; component name: " + xgmRootContainer.getComponentName() );
      }
      if( !waiting )
        return;
      waiting = false;
      cancel = true;
    }
  }
  ///////////////////////////////////////////////////////////////
  /**
   * Gets the user inpot, blocking until the user chosses 
   * some action.
   * @return The user input, as an XmlTreeNodeable instance, or null if
   * user has choose the Cancel option.
   */
  public XmlTreeNodeable getInput( Component relativeTo )
  {
    waiting = true;
    JDialog jDialog = new JDialog();
    jDialog.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
    
    JComponent comp = (JComponent) xgmRootContainer.getSwingComponent();
    if( useMainPanelOnly )
    {
      XgmComponent xc = 
        xgmRootContainer.getComponentByName( xgmRootContainer.getMainPanel() );
      if( xc != null )
        comp = (JComponent) xc.getSwingComponent();
    }
    jDialog.getContentPane().add( comp, BorderLayout.CENTER );
    if( useMainPanelOnly )
    {
      Action bOkAction = new BOkAction( okBtText, okBtAction );
      JButton bOK = new JButton( bOkAction );
      Action bCancelAction = new BCancelAction();
      JButton bCancel = new JButton( bCancelAction );
      JPanel jpButtons = new JPanel();
      jpButtons.add( bOK );
      jpButtons.add( bCancel );
      String aName = "OK";
      KeyStroke ks = KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 );
      comp.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW ).put( ks, aName );
      comp.getActionMap().put( aName, bOkAction );
      aName = "Cancel";
      ks = KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 );
      comp.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW ).put( ks, aName );
      comp.getActionMap().put( aName, bCancelAction );
      jDialog.add( jpButtons, BorderLayout.SOUTH );
    }
    jDialog.pack();
    jDialog.setLocationRelativeTo( relativeTo );
    jDialog.setVisible(true);
    try
    {
      while( !cancel && output == null )
      {
        Thread.sleep( 300 );
      }
    }
    catch( InterruptedException e )
    {
      logger.info( e.getLocalizedMessage(), e );
    }
    jDialog.setVisible( false );
    jDialog.dispose();
    jDialog = null;
    return output;
  }
  
  ///////////////////////////////////////////////////////////////
//  public static void main( String[] args )
//  {
//    String layout;
//    try
//    {
//      layout = XgmFactory.getInstance().readLayout( args[0] );
//      XgmRootContainer xrc = new XgmRootContainer( args[0], layout, false );
//      XgmDialogContainer t = new XgmDialogContainer( xrc );
//      String s = t.getInput( null );
//      System.out.println( "Input: " + s );
//    }
//    catch( Exception e )
//    {
//      String msg = e.getLocalizedMessage();
//      DebugFactory.printWarning( msg );
//      e.printStackTrace();
//    }
//    System.exit( 0 );
//  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.