forked from Coded/SIPRP
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							201 lines
						
					
					
						
							6.9 KiB
						
					
					
				
			
		
		
	
	
							201 lines
						
					
					
						
							6.9 KiB
						
					
					
				/*
 | 
						|
 * EtiquetaPrinter.java
 | 
						|
 *
 | 
						|
 * Created on 27 de Fevereiro de 2006, 14:41
 | 
						|
 *
 | 
						|
 * To change this template, choose Tools | Template Manager
 | 
						|
 * and open the template in the editor.
 | 
						|
 */
 | 
						|
 | 
						|
package siprp.clientes;
 | 
						|
 | 
						|
import java.io.*;
 | 
						|
import java.text.*;
 | 
						|
import java.util.*;
 | 
						|
 | 
						|
import com.evolute.utils.fop.*;
 | 
						|
import com.evolute.utils.ui.*;
 | 
						|
import com.evolute.utils.xml.*;
 | 
						|
 | 
						|
import siprp.data.*;
 | 
						|
 | 
						|
/**
 | 
						|
 *
 | 
						|
 * @author Frederico
 | 
						|
 */
 | 
						|
public class EtiquetaPrinter
 | 
						|
{
 | 
						|
	protected static final DecimalFormat N_F = new DecimalFormat();
 | 
						|
	
 | 
						|
	static
 | 
						|
	{
 | 
						|
		DecimalFormatSymbols dfs = new DecimalFormatSymbols( new Locale( "pt", "PT" ) );
 | 
						|
		dfs.setDecimalSeparator( '.' );
 | 
						|
		N_F.setDecimalFormatSymbols( dfs );
 | 
						|
		N_F.setMaximumFractionDigits( 4 );
 | 
						|
		N_F.setMinimumFractionDigits( 0 );
 | 
						|
	}
 | 
						|
	
 | 
						|
	protected Double altura;
 | 
						|
	protected Double largura;
 | 
						|
	protected Double margemEsquerda;
 | 
						|
	protected Double margemCima;
 | 
						|
	protected Integer colunas;
 | 
						|
	protected Integer linhas;
 | 
						|
	protected boolean continua;
 | 
						|
	protected Double alturaFolha;
 | 
						|
	protected Double larguraFolha;
 | 
						|
	protected Double margemVerticalFolha;
 | 
						|
	protected Double margemHorizontalFolha;
 | 
						|
	
 | 
						|
	/** Creates a new instance of EtiquetaPrinter */
 | 
						|
	public EtiquetaPrinter( EtiquetaData etiqueta )
 | 
						|
	{
 | 
						|
		altura = ( Double ) etiqueta.get( EtiquetaData.ALTURA );
 | 
						|
		largura = ( Double ) etiqueta.get( EtiquetaData.LARGURA );
 | 
						|
		margemEsquerda = ( Double ) etiqueta.get( EtiquetaData.MARGEM_ESQUERDA );
 | 
						|
		margemCima = ( Double ) etiqueta.get( EtiquetaData.MARGEM_CIMA );
 | 
						|
		colunas = ( Integer ) etiqueta.get( EtiquetaData.COLUNAS );
 | 
						|
		linhas = ( Integer ) etiqueta.get( EtiquetaData.LINHAS );
 | 
						|
		continua = "y".equals( etiqueta.get( EtiquetaData.CONTINUA ) );
 | 
						|
		alturaFolha = ( Double ) etiqueta.get( EtiquetaData.ALTURA_FOLHA );
 | 
						|
		larguraFolha = ( Double ) etiqueta.get( EtiquetaData.LARGURA_FOLHA );
 | 
						|
		margemVerticalFolha = ( Double ) etiqueta.get( EtiquetaData.MARGEM_VERTICAL_FOLHA );
 | 
						|
		margemHorizontalFolha = ( Double ) etiqueta.get( EtiquetaData.MARGEM_HORIZONTAL_FOLHA );
 | 
						|
	}
 | 
						|
	
 | 
						|
	public void print( Vector<Object[]> data )
 | 
						|
	{
 | 
						|
		int pos[] = new EtiquetaChooserDialog( null, linhas.intValue(), colunas.intValue() ).choose();
 | 
						|
		if( pos == null )
 | 
						|
		{
 | 
						|
			return;
 | 
						|
		}
 | 
						|
		int row = pos[ 0 ];
 | 
						|
		int col = pos[ 1 ];
 | 
						|
		String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
 | 
						|
		SimpleXMLElement styleSheet = 
 | 
						|
			new SimpleXMLElement( "xsl:stylesheet" );
 | 
						|
		styleSheet.addAttribute( "version", "1.1" );
 | 
						|
		styleSheet.addAttribute( "xmlns:xsl", "http://www.w3.org/1999/XSL/Transform" );
 | 
						|
		styleSheet.addAttribute( "xmlns:fo", "http://www.w3.org/1999/XSL/Format" );
 | 
						|
		styleSheet.addAttribute( "exclude-result-prefixes", "fo" );
 | 
						|
		styleSheet.setCompact( false );
 | 
						|
 | 
						|
		SimpleXMLElement output = 
 | 
						|
			new SimpleXMLElement( "xsl:output" );
 | 
						|
		styleSheet.addElement( output );
 | 
						|
		output.addAttribute( "method", "xml" );
 | 
						|
		output.addAttribute( "version", "1.0" );
 | 
						|
		output.addAttribute( "omit-xml-declaration", "no" );
 | 
						|
		output.addAttribute( "indent", "yes" );
 | 
						|
		
 | 
						|
		SimpleXMLElement template = 
 | 
						|
			new SimpleXMLElement( "xsl:template" );
 | 
						|
		styleSheet.addElement( template );
 | 
						|
		template.addAttribute( "match", "ETIQUETA" );
 | 
						|
		
 | 
						|
		SimpleXMLElement root = 
 | 
						|
			new SimpleXMLElement( "fo:root" );
 | 
						|
		template.addElement( root );
 | 
						|
		
 | 
						|
		SimpleXMLElement layoutMasterSet = 
 | 
						|
			new SimpleXMLElement( "fo:layout-master-set" );
 | 
						|
		root.addElement( layoutMasterSet );
 | 
						|
 | 
						|
		SimpleXMLElement simplePageMaster = 
 | 
						|
			new SimpleXMLElement( "fo:simple-page-master" );
 | 
						|
		layoutMasterSet.addElement( simplePageMaster );
 | 
						|
		simplePageMaster.addAttribute( "master-name", "simpleA4" );
 | 
						|
		simplePageMaster.addAttribute( "page-height", N_F.format( alturaFolha ) + "cm" );
 | 
						|
		simplePageMaster.addAttribute( "page-width", N_F.format( larguraFolha ) + "cm" );
 | 
						|
		simplePageMaster.addAttribute( "margin-top", N_F.format( ( row == 0 ? margemVerticalFolha : 0.0 ) + row * ( altura + margemCima ) ) + "cm" );
 | 
						|
		simplePageMaster.addAttribute( "margin-bottom", "0.5cm" );
 | 
						|
		simplePageMaster.addAttribute( "margin-left", N_F.format( ( col == 0 ? margemHorizontalFolha : 0.0 ) + col * ( largura + margemEsquerda ) ) + "cm" );
 | 
						|
		simplePageMaster.addAttribute( "margin-right", N_F.format( col == colunas - 1 ? margemHorizontalFolha : 0.0 ) );
 | 
						|
		
 | 
						|
		SimpleXMLElement regionBody = 
 | 
						|
			new SimpleXMLElement( "fo:region-body" );
 | 
						|
		simplePageMaster.addElement( regionBody );
 | 
						|
		
 | 
						|
		SimpleXMLElement pageSequence = 
 | 
						|
			new SimpleXMLElement( "fo:page-sequence" );
 | 
						|
		root.addElement( pageSequence );
 | 
						|
		pageSequence.addAttribute( "master-reference", "simpleA4" );
 | 
						|
		
 | 
						|
		SimpleXMLElement flow = 
 | 
						|
			new SimpleXMLElement( "fo:flow" );
 | 
						|
		pageSequence.addElement( flow );
 | 
						|
		flow.addAttribute( "flow-name", "xsl-region-body" );
 | 
						|
		
 | 
						|
		for( Object line[] : data )
 | 
						|
		{
 | 
						|
			SimpleXMLElement lineXML = createLine( line, largura - ( col == 0 || col == colunas - 1 ? margemHorizontalFolha : 0.0 ) );
 | 
						|
			flow.addElement( lineXML );
 | 
						|
		}
 | 
						|
//System.out.println( "XSL: " + styleSheet.toString() );
 | 
						|
		String xml = "<ETIQUETA></ETIQUETA>";
 | 
						|
		ByteArrayOutputStream out = new ByteArrayOutputStream();
 | 
						|
		FOPCreator creator = FOPCreator.getFOPCreator();
 | 
						|
		InputStream xmlIn = new ByteArrayInputStream( xml.getBytes() );
 | 
						|
		try
 | 
						|
		{
 | 
						|
			creator.createFOfromXML( xmlIn,	new ByteArrayInputStream( (header+styleSheet).getBytes() ), out );
 | 
						|
			ByteArrayInputStream in = new ByteArrayInputStream( out.toString().getBytes() );
 | 
						|
			Hashtable printOptions = new Hashtable();
 | 
						|
//System.out.println( "OUT: " + out.toString() );
 | 
						|
			FOPPrinter printer = FOPPrinter.getFOPPrinter();
 | 
						|
			printer.printFO( in, false, true, printOptions );
 | 
						|
		}
 | 
						|
		catch( Exception ex )
 | 
						|
		{
 | 
						|
			DialogException.showExceptionMessage( ex, "Erro a imprimir", true );
 | 
						|
		}
 | 
						|
	}
 | 
						|
	
 | 
						|
	protected SimpleXMLElement createLine( Object[] line, Double larguraDisponival )
 | 
						|
	{
 | 
						|
		SimpleXMLElement table = 
 | 
						|
			new SimpleXMLElement( "fo:table" );
 | 
						|
		table.addAttribute( "table-layout", "fixed" );
 | 
						|
		table.addAttribute( "width", N_F.format( larguraDisponival ) + "cm" );
 | 
						|
		table.addAttribute( "space-before.optimum", "0pt" );
 | 
						|
		table.addAttribute( "space-after.optimum", "0pt" );
 | 
						|
		
 | 
						|
		for( int n = 1; n < line.length; n += 2 )
 | 
						|
		{
 | 
						|
			SimpleXMLElement tableColumn = 
 | 
						|
				new SimpleXMLElement( "fo:table-column" );
 | 
						|
			table.addElement( tableColumn );
 | 
						|
			tableColumn.addAttribute( "column-width", N_F.format( ( ( Double ) line[ n ] ) * larguraDisponival ) + "cm" );
 | 
						|
		}
 | 
						|
		
 | 
						|
		SimpleXMLElement tableBody = 
 | 
						|
			new SimpleXMLElement( "fo:table-body" );
 | 
						|
		table.addElement( tableBody );
 | 
						|
 | 
						|
		SimpleXMLElement tableRow = 
 | 
						|
			new SimpleXMLElement( "fo:table-row" );
 | 
						|
		tableBody.addElement( tableRow );
 | 
						|
		
 | 
						|
		for( int n = 0; n < line.length; n += 2 )
 | 
						|
		{
 | 
						|
			SimpleXMLElement tableCell = 
 | 
						|
				new SimpleXMLElement( "fo:table-cell" );
 | 
						|
			tableRow.addElement( tableCell );
 | 
						|
 | 
						|
			SimpleXMLElement block = 
 | 
						|
				new SimpleXMLElement( "fo:block" );
 | 
						|
			tableCell.addElement( block );
 | 
						|
			block.addAttribute( "font-size", "10pt" );
 | 
						|
			block.addAttribute( "text-align", "left" );
 | 
						|
//			block.addAttribute( "wrap-option", "no-wrap" );
 | 
						|
//			block.addAttribute( "overflow", "scroll" );
 | 
						|
 | 
						|
			block.addTextElement( ( String ) line[ n ] );
 | 
						|
		}
 | 
						|
		
 | 
						|
		return table;
 | 
						|
	}
 | 
						|
}
 |