📄 jrviewermod.java
字号:
/*
* ============================================================================
* The JasperReports License, Version 1.0
* ============================================================================
*
* Copyright (C) 2001-2004 Teodor Danciu (teodord@users.sourceforge.net). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by Teodor Danciu (http://jasperreports.sourceforge.net)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The name "JasperReports" must not be used to endorse or promote products
* derived from this software without prior written permission. For written
* permission, please contact teodord@users.sourceforge.net.
*
* 5. Products derived from this software may not be called "JasperReports", nor
* may "JasperReports" appear in their name, without prior written permission
* of Teodor Danciu.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ============================================================================
* GNU Lesser General Public License
* ============================================================================
*
* JasperReports - Free Java report-generating library.
* Copyright (C) 2001-2004 Teodor Danciu teodord@users.sourceforge.net
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Teodor Danciu
* 173, Calea Calarasilor, Bl. 42, Sc. 1, Ap. 18
* Postal code 030615, Sector 3
* Bucharest, ROMANIA
* Email: teodord@users.sourceforge.net
*/
/*
* Contributors:
* Ryan Johnson - delscovich@users.sourceforge.net
* Carlton Moore - cmoore79@users.sourceforge.net
*/
package net.adrianromero.tpv.reports;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.filechooser.FileFilter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRHyperlink;
import net.sf.jasperreports.engine.JRPrintAnchorIndex;
import net.sf.jasperreports.engine.JRPrintElement;
import net.sf.jasperreports.engine.JRPrintHyperlink;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRClassLoader;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRSaver;
import net.sf.jasperreports.engine.xml.JRPrintXmlLoader;
import net.sf.jasperreports.view.*;
/**
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version $Id: JRViewer.java,v 1.10 2004/08/29 18:35:49 teodord Exp $
*/
public class JRViewerMod extends javax.swing.JPanel implements JRHyperlinkListener
{
/**
*
*/
private static final int TYPE_FILE_NAME = 1;
private static final int TYPE_INPUT_STREAM = 2;
private static final int TYPE_JASPER_PRINT = 3;
protected float MIN_ZOOM = 0.5f;
protected float MAX_ZOOM = 2.5f;
protected int zooms[] = {50, 75, 100, 125, 150, 175, 200, 250};
// protected int defaultZoomIndex = 2;
private int type = TYPE_FILE_NAME;
private boolean isXML = false;
private String reportFileName = null;
private JasperPrint jasperPrint = null;
private int pageIndex = 0;
private float zoom = 0f;
private DecimalFormat zoomDecimalFormat = new DecimalFormat("#.##");
private int downX = 0;
private int downY = 0;
private static ResourceBundle m_messages = ResourceBundle.getBundle("net/adrianromero/tpv/i18n/jrmessages");
private java.util.List hyperlinkListeners = new ArrayList();
private Map linksMap = new HashMap();
private MouseListener mouseListener =
new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent evt)
{
hyperlinkClicked(evt);
}
};
/** Creates new form JRViewer */
public JRViewerMod(String fileName, boolean isXML) throws JRException
{
setZooms();
initComponents();
loadReport(fileName, isXML);
setPageIndex(0);
//force pageRefresh()
zoom = 0;
setZoomRatio(1);
addHyperlinkListener(this);
}
/** Creates new form JRViewer */
public JRViewerMod(InputStream is, boolean isXML) throws JRException
{
setZooms();
initComponents();
loadReport(is, isXML);
setPageIndex(0);
//force pageRefresh()
zoom = 0;
setZoomRatio(1);
addHyperlinkListener(this);
}
/** Creates new form JRViewer */
public JRViewerMod(JasperPrint jrPrint) throws JRException
{
setZooms();
initComponents();
loadJasperPrint(jrPrint);
addHyperlinkListener(this);
}
public JRViewerMod() throws JRException {
setZooms();
initComponents();
loadJasperPrint(null);
addHyperlinkListener(this);
}
public void loadJasperPrint(JasperPrint jrPrint) throws JRException {
loadReport(jrPrint);
setPageIndex(0);
//force pageRefresh()
zoom = 0;
setZoomRatio(1);
}
/**
*
*/
public void clear()
{
emptyContainer(this);
jasperPrint = null;
}
/**
*
*/
protected void setZooms()
{
}
public static String getIntString(String sKey) {
return m_messages.getString(sKey);
}
/**
*
*/
public void addHyperlinkListener(JRHyperlinkListener listener) throws JRException
{
hyperlinkListeners.add(listener);
}
/**
*
*/
public void removeHyperlinkListener(JRHyperlinkListener listener) throws JRException
{
hyperlinkListeners.remove(listener);
}
/**
*
*/
public void gotoHyperlink(JRPrintHyperlink hyperlink) throws JRException
{
switch(hyperlink.getHyperlinkType())
{
case JRHyperlink.HYPERLINK_TYPE_REFERENCE :
{
if (hyperlinkListeners != null && hyperlinkListeners.size() > 1)
{
System.out.println("Hyperlink reference : " + hyperlink.getHyperlinkReference());
System.out.println("Implement your own JRHyperlinkListener to manage this type of event.");
}
break;
}
case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR :
{
if (hyperlink.getHyperlinkAnchor() != null)
{
Map anchorIndexes = jasperPrint.getAnchorIndexes();
JRPrintAnchorIndex anchorIndex = (JRPrintAnchorIndex)anchorIndexes.get(hyperlink.getHyperlinkAnchor());
if (anchorIndex.getPageIndex() != pageIndex)
{
setPageIndex(anchorIndex.getPageIndex());
refreshPage();
}
Container container = pnlInScroll.getParent();
if (container instanceof JViewport)
{
JViewport viewport = (JViewport) container;
int newX = (int)(anchorIndex.getElement().getX() * zoom);
int newY = (int)(anchorIndex.getElement().getY() * zoom);
int maxX = pnlInScroll.getWidth() - viewport.getWidth();
int maxY = pnlInScroll.getHeight() - viewport.getHeight();
if (newX < 0)
{
newX = 0;
}
if (newX > maxX)
{
newX = maxX;
}
if (newY < 0)
{
newY = 0;
}
if (newY > maxY)
{
newY = maxY;
}
viewport.setViewPosition(new Point(newX, newY));
}
}
break;
}
case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE :
{
int page = pageIndex + 1;
if (hyperlink.getHyperlinkPage() != null)
{
page = hyperlink.getHyperlinkPage().intValue();
}
if (page >= 1 && page <= jasperPrint.getPages().size() && page != pageIndex + 1)
{
setPageIndex(page - 1);
refreshPage();
Container container = pnlInScroll.getParent();
if (container instanceof JViewport)
{
JViewport viewport = (JViewport) container;
viewport.setViewPosition(new Point(0, 0));
}
}
break;
}
case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR :
{
if (hyperlinkListeners != null && hyperlinkListeners.size() > 1)
{
System.out.println("Hyperlink reference : " + hyperlink.getHyperlinkReference());
System.out.println("Hyperlink anchor : " + hyperlink.getHyperlinkAnchor());
System.out.println("Implement your own JRHyperlinkListener to manage this type of event.");
}
break;
}
case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE :
{
if (hyperlinkListeners != null && hyperlinkListeners.size() > 1)
{
System.out.println("Hyperlink reference : " + hyperlink.getHyperlinkReference());
System.out.println("Hyperlink page : " + hyperlink.getHyperlinkPage());
System.out.println("Implement your own JRHyperlinkListener to manage this type of event.");
}
break;
}
case JRHyperlink.HYPERLINK_TYPE_NONE :
default :
{
break;
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -