imagereportelement.java

来自「iReport-0.4.1-src是iReport的源代码,iReport是一个」· Java 代码 · 共 710 行 · 第 1/2 页

JAVA
710 行
字号
/*
 * RectangleReportElement.java
 *
 *  iReport  --  Visual designer for generating JasperReports Documents
 *  Copyright (C) 2002  Giulio Toffoli gt@businesslogic.it
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Giulio Toffoli
 *  Via T.Aspetti, 233
 *  35100 Padova ITALY
 *  gt@businesslogic.it
 *
 * Created on 28 febbraio 2003, 19.20
 */
package it.businesslogic.ireport;
import it.businesslogic.ireport.gui.*;
import it.businesslogic.ireport.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.util.JRImageLoader;

public class ImageReportElement extends GraphicReportElement implements ImageObserver, HyperLinkableReportElement
{
        private Image img=null;
	static private Image defimg=null;
	private String imageExpression;
	private String imageClass;
	private String scaleImage;
	private String hyperlinkType="None";
	private String evaluationTime="Now";
	private String evaluationGroup="";
	
	private String verticalAlignment="Top";
	private String horizontalAlignment="Left";
	
	private String anchorNameExpression="";
	private String hyperlinkReferenceExpression="";
	private String hyperlinkAnchorExpression="";
	private String hyperlinkPageExpression="";
    private boolean isUsingCache;
    private File reportDirectory = null;
    private boolean triedToLoadImg = false; 
    
    private Box box = null;
    
	public ImageReportElement(int x, int y, int width, int height)
	{
		super(x,y, Math.abs(width), Math.abs(height));
                setGraphicElementPen("Thin");
		this.bgcolor = Color.WHITE;
		this.fgcolor = Color.BLACK;
                setKey("image");
                
                if (defimg == null)
                {
                    // Load default image...
                    defimg = Misc.loadImageFromResources("it/businesslogic/ireport/icons/layout/img.gif");
                }
                
                img = null;
		isUsingCache=false;
		imageExpression=""; 
		imageClass="java.lang.String";
		scaleImage="FillFrame";
		graphicElementPen = "None";
                this.hyperlinkType = "None";
                this.anchorNameExpression = "";
                box = new Box();
	}
        

	
        /*
	public void drawObject(Graphics2D g,double zoom_factor, int x_shift_origin, int y_shift_origin)
	{
                position.x -= 10;
		position.y -= 10;
		x_shift_origin -= 10;
		y_shift_origin -= 10;
		
		this.zoom_factor = zoom_factor;
                
                g.setColor( bgcolor );
                if (!getTransparent().equalsIgnoreCase("Transparent"))
                    g.fillRect( getZoomedDim(position.x)-x_shift_origin,
				getZoomedDim(position.y)-y_shift_origin,
				getZoomedDim(width),
				getZoomedDim(height));
                g.setColor( this.fgcolor );
                              
		position.x += 10;
		position.y += 10;
		x_shift_origin += 10;
		y_shift_origin += 10;
                
                drawGraphicsElement(g,this.getGraphicElementPen(), zoom_factor,  x_shift_origin,y_shift_origin, 0);
	}*/
        
        public void drawObject(Graphics2D g, double zoom_factor, int x_shift_origin, int y_shift_origin)
	{
                
                position.x += -10 + box.getLeftPadding();
		position.y += -10 + box.getTopPadding();
                width = width - box.getLeftPadding() - box.getRightPadding();
                height = height - box.getTopPadding() - box.getBottomPadding();

		x_shift_origin -= 10;
		y_shift_origin -= 10;
                
		this.zoom_factor = zoom_factor;
		Image imgx = img;
		if (imgx==null) 
		{	
                    // Try to load the image from imageExpression...
                    
			String fname = imageExpression;
			
            if(!triedToLoadImg && imageClass.equals("java.lang.String") && fname.length()>3 && fname.charAt(0)=='\"' &&
			fname.charAt(fname.length()-1 ) == '\"'){
            	
				// Could be a file...
				fname = Misc.string_replace("\\","\\\\",fname);
				fname = Misc.string_replace("","\"",fname);
            	
            	//first try read image from file
				File file = new File(fname);
				if(!file.isAbsolute() && reportDirectory != null){
					file = new File(reportDirectory, fname);
				}
				if(file.exists()){             	
					img = Misc.loadImageFromFile(file.getAbsolutePath());
				}else{
					
					try{
						img = JRImageLoader.loadImage(JRImageLoader.loadImageDataFromURL(new URL(fname)));
					}catch(JRException jrex){
					}catch(MalformedURLException mue){}
				}
            
				imgx = img;
				triedToLoadImg = true;//try only once
            }
                        
			if (imgx==null)
				 imgx=defimg;
		}	

                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
                
		if (imgx != null)
		{
                    int imageWidth = imgx.getWidth(this);
                    int imageHeight = imgx.getHeight(this);
                    /*
			if (imgx instanceof Image)
			{
				((Image)imgx).setTransparent(true);
			}
                     */
			
			// 
			if (scaleImage.equalsIgnoreCase("RetainShape"))
			{
				if (imageWidth <= width && imageHeight<= height)
				{
					Rectangle destination = new Rectangle(-x_shift_origin+ getZoomedDim( position.x) ,-y_shift_origin+getZoomedDim(position.y), getZoomedDim( imageWidth), getZoomedDim( imageHeight));
					Rectangle source = new Rectangle(0,0,imageWidth,imageHeight);
					// Calculate y shift based on hAlign...
					int elem_height = getZoomedDim(this.height);
					elem_height -= getZoomedDim( imageHeight);
					if (verticalAlignment.equals("Middle")) destination.y += elem_height/2;
					else if (verticalAlignment.equals("Bottom")) destination.y += elem_height;
					
					// Calculate x shift based on hAlign...
					int elem_width = getZoomedDim(this.width);
					elem_width -= getZoomedDim( imageWidth);
					if (horizontalAlignment.equals("Center")) destination.x += elem_width/2;
					else if (horizontalAlignment.equals("Right")) destination.x += elem_width;
						
					g.drawImage( imgx,destination.x, destination.y, destination.x+destination.width, destination.y+destination.height, 
                                                        source.x,source.y,source.width,source.height,
                                                        null,this);
				}
				else if (width>0 && height>0)// Resize based on minor x/WIDTH... e y/HEIGHT
				{
					if ((double)((double)imageWidth/(double)width)> (double)((double)imageHeight/(double)height))
					{
						
						Rectangle source = new Rectangle(0,0,imageWidth,imageHeight);
						Rectangle destination = new Rectangle(-x_shift_origin+ getZoomedDim( position.x) ,-y_shift_origin+getZoomedDim(position.y),getZoomedDim(width) ,getZoomedDim(Math.min( (imageHeight*width)/imageWidth, height)) );						
						
						// Calculate y shift based on hAlign...
						int elem_height = getZoomedDim(this.height);
						elem_height -= getZoomedDim(Math.min( (imageHeight*width)/imageWidth, height));
						if (verticalAlignment.equals("Middle")) destination.y += elem_height/2;
						else if (verticalAlignment.equals("Bottom")) destination.y += elem_height;
						
						g.drawImage( imgx,destination.x, destination.y, destination.x+destination.width, destination.y+destination.height, 
                                                        source.x,source.y,source.width,source.height,
                                                        null,this);
					}
					else
					{
						Rectangle source = new Rectangle(0,0,imageWidth,imageHeight);
						Rectangle destination = new Rectangle(-x_shift_origin+ getZoomedDim( position.x) ,-y_shift_origin+getZoomedDim(position.y), getZoomedDim( Math.min( (imageWidth*height)/imageHeight, width)) ,getZoomedDim( height) );
						
						// Calculate x shift based on hAlign...
						int elem_width = getZoomedDim(this.width);
						elem_width -= getZoomedDim( Math.min( (imageWidth*height)/imageHeight, width));
						if (horizontalAlignment.equals("Center")) destination.x += elem_width/2;
						else if (horizontalAlignment.equals("Right")) destination.x += elem_width;
							
						
						g.drawImage( imgx,destination.x, destination.y, destination.x+destination.width, destination.y+destination.height, 
                                                        source.x,source.y,source.width,source.height,
                                                        null,this);
					}
				}
				
			}
			else if (scaleImage.equalsIgnoreCase("FillFrame"))
			{
                            
				g.drawImage(imgx, getZoomedDim(position.x)-x_shift_origin,
                                            getZoomedDim(position.y)-y_shift_origin,
                                            getZoomedDim(position.x+width)-x_shift_origin,
                                            getZoomedDim(position.y+height)-y_shift_origin,
                                            0,0,imageWidth,imageHeight,null,this);

                                //-x_shift_origin+ getZoomedDim( position.x-10)+10 ,-y_shift_origin+getZoomedDim(position.y)+10, getZoomedDim( this.width), getZoomedDim( this.height),
                                
                                
			}
			else
			{
				Rectangle destination = new Rectangle(-x_shift_origin+ getZoomedDim( position.x) ,-y_shift_origin+getZoomedDim(position.y),getZoomedDim( Math.min(imageWidth, width)),getZoomedDim( Math.min(imageHeight, height)));
				Rectangle source = new Rectangle(0,0,Math.min( imageWidth, width) ,Math.min(imageHeight,  height));

				// Calculate y shift based on hAlign...
				int elem_height = getZoomedDim( height);
				elem_height -= getZoomedDim( imageHeight );
				if (verticalAlignment.equals("Middle")) { 
					destination.y += elem_height/2;  
					if (destination.y<--y_shift_origin+getZoomedDim(position.y)+10 )
					{
						source.y += -y_shift_origin+getZoomedDim(position.y)+10-destination.y;
						destination.y = -y_shift_origin+getZoomedDim(position.y)+10;
					}
				}
				else if (verticalAlignment.equals("Bottom"))  { 
					destination.y += elem_height;  
					if (destination.y<-y_shift_origin+getZoomedDim(position.y)+10 )
					{
						source.y += -y_shift_origin+getZoomedDim(position.y)+10-destination.y;
						destination.y = -y_shift_origin+getZoomedDim(position.y)+10;
					}
				}
				
				// Calculate x shift based on hAlign...
				int elem_width = getZoomedDim( width);
				elem_width -= getZoomedDim( imageWidth );
				if (horizontalAlignment.equals("Center")) { 
					destination.x += elem_width/2;  
					if (destination.x<-x_shift_origin+ getZoomedDim( position.x-10)+10 )
					{
						source.x += -x_shift_origin+ getZoomedDim( position.x-10)+10-destination.x;
						destination.x = -x_shift_origin+ getZoomedDim( position.x-10)+10;
					}
				}
				else if (horizontalAlignment.equals("Right"))  { 
					destination.x += elem_width;  
					if (destination.x<-x_shift_origin+ getZoomedDim( position.x-10)+10 )
					{
						source.x += -x_shift_origin+ getZoomedDim( position.x-10)+10-destination.x;
						destination.x = -x_shift_origin+ getZoomedDim( position.x-10)+10;
					}
				}
					
				g.drawImage( imgx,destination.x, destination.y, destination.x+destination.width, destination.y+destination.height, 
                                                        source.x,source.y,source.width,source.height,
                                                        null,this);
			}
		}
                
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
                g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
                
                //position.x += 10;
		//position.y += 10;
                
                position.x += 10 - box.getLeftPadding();
		position.y += 10 - box.getTopPadding();
                width = width + box.getLeftPadding() + box.getRightPadding();
                height = height + box.getTopPadding() + box.getBottomPadding();

                
		x_shift_origin += 10;
		y_shift_origin += 10;
                
                super.drawBorder((Graphics2D)g, zoom_factor,  x_shift_origin,y_shift_origin);
		drawGraphicsElement((Graphics2D)g,this.getGraphicElementPen(), zoom_factor,  x_shift_origin,y_shift_origin);
                drawBorder( g, zoom_factor,  x_shift_origin,y_shift_origin,getBox());
	}
	
        
    public ReportElement cloneMe()
    {
	ImageReportElement newReportElement = new ImageReportElement(position.x, position.y, width, height);
	copyBaseReportElement(newReportElement, this);
	return newReportElement;
    }
        
    public void copyBaseReportElement(ReportElement destination, ReportElement source)
        {
                super.copyBaseReportElement(destination, source);
                
                if (destination instanceof ImageReportElement &&
                    source instanceof ImageReportElement )
                {
                    ((ImageReportElement)destination).setEvaluationGroup(  new String( ((ImageReportElement)source).getEvaluationGroup() ));
                    ((ImageReportElement)destination).setEvaluationTime( new String(  ((ImageReportElement)source).getEvaluationTime() ));
                    ((ImageReportElement)destination).setHorizontalAlignment( new String(  ((ImageReportElement)source).getHorizontalAlignment() ));
                    ((ImageReportElement)destination).setImageClass( new String(  ((ImageReportElement)source).getImageClass() ));
                    ((ImageReportElement)destination).setImageExpression( new String(  ((ImageReportElement)source).getImageExpression() ));
                    ((ImageReportElement)destination).setIsUsingCache( ((ImageReportElement)source).isIsUsingCache());
                    ((ImageReportElement)destination).setScaleImage( new String(  ((ImageReportElement)source).getScaleImage() ));
                    ((ImageReportElement)destination).setVerticalAlignment(  new String(  ((ImageReportElement)source).getVerticalAlignment() ));
					((ImageReportElement)destination).setReportDirectory(   ((ImageReportElement)source).getReportDirectory() );

                    ((HyperLinkableReportElement)destination).setAnchorNameExpression(new String(  ((HyperLinkableReportElement)source).getAnchorNameExpression() ));
                    ((HyperLinkableReportElement)destination).setHyperlinkAnchorExpression(new String(  ((HyperLinkableReportElement)source).getHyperlinkAnchorExpression() ));
                    ((HyperLinkableReportElement)destination).setHyperlinkPageExpression(new String(  ((HyperLinkableReportElement)source).getHyperlinkPageExpression() ));
                    ((HyperLinkableReportElement)destination).setHyperlinkReferenceExpression(new String(  ((HyperLinkableReportElement)source).getHyperlinkReferenceExpression() ));
                    ((HyperLinkableReportElement)destination).setHyperlinkType(new String(  ((HyperLinkableReportElement)source).getHyperlinkType() ));
                }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?