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

Java Open Source » ERP CRM Financial » Evaristo 4.0 
Evaristo 4.0 » com » m16e » mpbiz » xgm » XDocPrintChooser.java
/*
 * XgmChooser.java
 *
 * Created on 28/Mar/2005
 */

package com.m16e.mpbiz.xgm;

import com.m16e.mpbiz.common.MpBizAppConfig;
import com.m16e.mpbiz.docs.MpBizDoc;
import com.m16e.mpbiz.docs.print.MpBizDocPrintOptions;
import com.m16e.mpbiz.tables.DbDoc;
import com.m16e.tools.MpAppGraphicsContext;
import com.m16e.tools.datamodels.ComboData;
import com.m16e.tools.xgm.XgmComboBox;
import com.m16e.tools.xgm.XgmComponent;
import com.m16e.tools.xgm.XgmException;
import com.m16e.tools.xgm.XgmFactory;
import com.m16e.tools.xgm.XgmRootContainer;
import com.m16e.tools.xml.XmlTreeNodeable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.print.PrintService;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;


///////////////////////////////////////////////////////////////
/**
 * @author carlos
 *
 * 
 */
public class XDocPrintChooser
extends XgmRootContainer
{
  static Logger logger = Logger.getLogger( XDocPrintChooser.class );

  String layoutFile = "dialogs/xgm-doc-print-chooser.xml";
  String chooserTitle = null;
  XgmComponent[] cbOptions = null;
  Integer tipoEntId = null;
  Integer tipoDocId = null;
  MpBizDoc mpBizDoc = null;

  ///////////////////////////////////////////////////////////////
  public XDocPrintChooser( String title, Integer tipoEntId, Integer tipoDocId ) 
  {
    chooserTitle = title;
    this.tipoEntId = tipoEntId;
    this.tipoDocId = tipoDocId;
  }

  ///////////////////////////////////////////////////////////////
  public XDocPrintChooser( String title, MpBizDoc mpBizDoc ) 
  {
    chooserTitle = title;
    this.mpBizDoc = mpBizDoc;
    this.tipoEntId = mpBizDoc.getTipoEntId();
    this.tipoDocId = mpBizDoc.getTipoDocId();
  }

  ///////////////////////////////////////////////////////////////
  public void setLayoutFile( String layoutFile )
  {
    this.layoutFile = layoutFile;
  }
  
  ///////////////////////////////////////////////////////////////
  public void init() 
  throws XgmException
  {
    try
    {
      String elDialog =
        XgmFactory.getInstance().readLayout( layoutFile );
      setLayout( layoutFile, elDialog, true, (String[]) null );
      initOptions();
    }
    catch( Exception e )
    {
      String msg = e.getMessage();
      logger.error( e.getMessage(), e );
      throw new XgmException( XgmException.IO_ERROR, msg );
    }
  }
  
  ///////////////////////////////////////////////////////////////
  private void initOptions() 
  throws XgmException
  {
    XgmComponent xc = null;
    JCheckBox jcb = null;
    for( int f = 0; f < MpBizDocPrintOptions.COPY_NAMES.length; f++ )
    {
      xc = getComponentByName( MpBizDocPrintOptions.L_COPY_PREFIX + (f + 1) );
      if( xc == null )
        break;
      jcb = (JCheckBox) xc.getSwingComponent();
      jcb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            JCheckBox j = (JCheckBox) e.getSource();
            if( j.isSelected() )
            {
              setSpecialOff();
            }
          }
        }
      );
    }
    xc = getComponentByName( MpBizDocPrintOptions.L_REPRINT );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      jcb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            JCheckBox j = (JCheckBox) e.getSource();
            if( j.isSelected() )
            {
              setCopiesOff();
            }
          }
        }
      );
    }
    xc = getComponentByName( MpBizDocPrintOptions.L_DRAFT );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      jcb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            JCheckBox j = (JCheckBox) e.getSource();
            if( j.isSelected() )
            {
              setCopiesOff();
            }
          }
        }
      );
    }
    xc = getComponentByName( MpBizDocPrintOptions.L_PRINTERS );
    if( xc != null )
    {
      XgmComboBox xcb = (XgmComboBox) xc;
      java.util.List<ComboData> lcd =  new ArrayList<ComboData>();
      java.util.List<PrintService> lps = 
        MpAppGraphicsContext.getInstance().getAvailablePrinters();
      for( int f = 0; f < lps.size(); f++ )
      {
        lcd.add( new ComboData( f, lps.get( f ).getName() ) );
      }
      xcb.setComboDataModel( lcd );
      JComboBox cb = (JComboBox) xc.getSwingComponent();
      cb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            setPdfOff();
            setMailOff();
            setViewerOff();
          }
        }
      );
    }
    xc = getComponentByName( MpBizDocPrintOptions.L_PDF );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      jcb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            JCheckBox j = (JCheckBox) e.getSource();
            if( j.isSelected() )
            {
              setViewerOff();
              setMailOff();
            }
          }
        }
      );
    }
    xc = getComponentByName( MpBizDocPrintOptions.L_MAIL );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      jcb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            JCheckBox j = (JCheckBox) e.getSource();
            if( j.isSelected() )
            {
              setViewerOff();
              setPdfOff();
            }
          }
        }
      );
    }
    xc = getComponentByName( MpBizDocPrintOptions.L_VIEWER );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      jcb.addActionListener(
        new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            JCheckBox j = (JCheckBox) e.getSource();
            if( j.isSelected() )
            {
              setPdfOff();
              setMailOff();
            }
          }
        }
      );
    }
    
    xc = getComponentByName( MpBizDocPrintOptions.L_PRINT_BARCODE_LABELS );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      if( !MpBizAppConfig.getInstance().getDocPrintBarcodeLabels(
            tipoEntId, tipoDocId ) )
      {
        xc.setComponentValueFromObject( Boolean.FALSE );
        xc.setVisible( false );
      }
    }

    xc = getComponentByName( MpBizDocPrintOptions.L_PRINT_TRANSPORT_DATA );
    if( xc != null )
    {
      jcb = (JCheckBox) xc.getSwingComponent();
      xc.setComponentValueFromObject(
        MpBizAppConfig.getInstance().printTransportData( tipoEntId, tipoDocId ) );
    }
  }
  
  ///////////////////////////////////////////////////////////////
  public void setDocHeader( DbDoc doc )
  throws XgmException
  {
    StringBuffer docHeader = new StringBuffer();
    docHeader.append( "<html>" );
    docHeader.append( " <table>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    T. Ent.:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( doc.getTipoEntId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    T. Doc.:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( doc.getTipoDocId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    Cd. Doc.:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( doc.getDocId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    Entidade:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( doc.getEntId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    Data:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( doc.getDataDoc() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( " </table>" );
    docHeader.append( "</html>\n" );
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_DOC_HEADER );
    xc.setComponentValueFromObject( docHeader.toString() );
  }
  
  ///////////////////////////////////////////////////////////////
  public void setDocHeader( java.util.List<DbDoc> docs )
  throws XgmException
  {
    StringBuffer docHeader = new StringBuffer();
    docHeader.append( "<html>" );
    docHeader.append( " <table>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    T. Ent.:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( docs.get( 0 ).getTipoEntId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    T. Doc.:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( docs.get( 0 ).getTipoDocId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    Cd. Doc.:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    boolean first = true;
    for( DbDoc doc : docs )
    {
      if( first )
        first = false;
      else
        docHeader.append( ", " );
      docHeader.append( doc.getDocId() );
    }
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( "  <tr>" );
    docHeader.append( "   <th>" );
    docHeader.append( "    Entidade:" );
    docHeader.append( "   </th>" );
    docHeader.append( "   <td>" );
    docHeader.append( docs.get( 0 ).getEntId() );
    docHeader.append( "   </td>" );
    docHeader.append( "  </tr>" );
    
    docHeader.append( " </table>" );
    docHeader.append( "</html>\n" );
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_DOC_HEADER );
    xc.setComponentValueFromObject( docHeader.toString() );
  }
  
  ///////////////////////////////////////////////////////////////
  public void setNumberOfCopies( int numberOfCopies )
  {
    XgmComponent xc = null;
    try
    {
      for( int f = 0; f < MpBizDocPrintOptions.COPY_NAMES.length; f++ )
      {
        xc = getComponentByName( MpBizDocPrintOptions.L_COPY_PREFIX + (f + 1) );
        if( xc == null )
          break;
        if( f < numberOfCopies )
          xc.setComponentValueFromObject( Boolean.TRUE );
        else
          xc.setComponentValueFromObject( Boolean.FALSE );
      }
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }
    
  }
  
  ///////////////////////////////////////////////////////////////
  private void setPdfOff()
  {
    XgmComponent xc = null;
    try
    {
      xc = getComponentByName( MpBizDocPrintOptions.L_PDF );
      if( xc != null )
        xc.setComponentValueFromObject( Boolean.FALSE );
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }

  }

  ///////////////////////////////////////////////////////////////
  private void setMailOff()
  {
    XgmComponent xc = null;
    try
    {
      xc = getComponentByName( MpBizDocPrintOptions.L_MAIL );
      if( xc != null )
        xc.setComponentValueFromObject( Boolean.FALSE );
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }

  }

  ///////////////////////////////////////////////////////////////
  private void disableMail()
  {
    XgmComponent xc = null;
    try
    {
      xc = getComponentByName( MpBizDocPrintOptions.L_MAIL );
      if( xc != null )
        xc.setEditable( false );
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }

  }

  ///////////////////////////////////////////////////////////////
  private void setViewerOff()
  {
    XgmComponent xc = null;
    try
    {
      xc = getComponentByName( MpBizDocPrintOptions.L_VIEWER );
      if( xc != null )
        xc.setComponentValueFromObject( Boolean.FALSE );
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }
    
  }
  
  ///////////////////////////////////////////////////////////////
  private void setCopiesOff()
  {
    XgmComponent xc = null;
    try
    {
      for( int f = 0; f < MpBizDocPrintOptions.COPY_NAMES.length; f++ )
      {
        xc = getComponentByName( MpBizDocPrintOptions.L_COPY_PREFIX + (f + 1) );
        if( xc == null )
          break;
        xc.setComponentValueFromObject( Boolean.FALSE );
      }
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }
    
  }
  
  ///////////////////////////////////////////////////////////////
  private void setSpecialOff()
  {
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_REPRINT );
    try
    {
      xc.setComponentValueFromObject( Boolean.FALSE );
      xc = getComponentByName( MpBizDocPrintOptions.L_DRAFT );
      xc.setComponentValueFromObject( Boolean.FALSE );
    }
    catch( Exception e )
    {
      logger.error( e.getMessage(), e );
      JOptionPane.showMessageDialog( null, e.getMessage() );
    }
  }
  
  ///////////////////////////////////////////////////////////////
  public void setPrintTotalsEditable( boolean editable  )
  {
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_PRINT_TOTALS );
    xc.setEditable( editable  );
  }
  
  ///////////////////////////////////////////////////////////////
  public void setPrintValuesEditable( boolean editable  )
  {
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_PRINT_VALUES );
    xc.setEditable( editable  );
  }
  
  ///////////////////////////////////////////////////////////////
  public void setPrintTotals( Boolean selected  ) 
  throws XgmException
  {
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_PRINT_TOTALS );
    xc.setComponentValueFromObject( selected  );
  }
  
  ///////////////////////////////////////////////////////////////
  public void setPrintValues( Boolean selected  ) 
  throws XgmException
  {
    XgmComponent xc = getComponentByName( MpBizDocPrintOptions.L_PRINT_VALUES );
    xc.setComponentValueFromObject( selected  );
  }
  
  ///////////////////////////////////////////////////////////////
  public MpBizDocPrintOptions getPrintOptions() 
  throws XgmException
  {
    JOptionPane jop = new JOptionPane( getSwingComponent() );
    jop.setOptionType( JOptionPane.OK_CANCEL_OPTION );
    //jop.setWantsInput( true );
    jop.setSelectionValues( null );
    jop.setInitialSelectionValue( getSwingComponent() );
    JDialog jd = jop.createDialog( getSwingComponent(), chooserTitle );
    //jd.setSize( 600, 400 );
    jd.setResizable( true );
    jop.selectInitialValue();
    jd.setVisible( true );
    jd.dispose();
    Object val = jop.getValue();
    if( val instanceof Integer )
    {
      int op = ((Integer) val).intValue();
      if( op != JOptionPane.OK_OPTION )
        return null;
    }
    else
      return null;
    XmlTreeNodeable result = getValueAsXmlTreeNodeable();
    
    return new MpBizDocPrintOptions( result );
  }
  
  
}
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.