📄 templet.java
字号:
/***********************************************************************
* Module: Templet.java
* Author: juny
* Created: 2006年7月13日 13:27:52
* Purpose: Defines the Class Templet
***********************************************************************/
package net.excel.report.base.element;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.excel.report.Logger;
import net.excel.report.base.BaseBand;
import net.excel.report.base.BaseElement;
import net.excel.report.base.ITempletContainer;
import net.excel.report.base.Parameter;
import jxl.Cell;
import jxl.CellView;
import jxl.Range;
import jxl.write.WritableCell;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.biff.RowsExceededException;
/**
* 容器类的实现,所有的模板定义都被抽象成一个个模板定义对象而放到容器中。
*
* @author juny
*/
public class Templet implements ITempletContainer {
private static Logger log = Logger.getLogger(Templet.class);
public Templet(ITempletContainer parentContainer){
this.parentContainer = parentContainer;
elements = new HashMap();
}
/* 取得容器包含的模板元素列表
* @see excel.report.util.ITempletContainer#getContainElements()
*/
public List getContainElements() {
return null;
}
/**
* 取得顶层容器
* @return
*/
public ITempletContainer getTopContainer(){
if(null != topContainer){
return topContainer;
}
topContainer = parentContainer;
while(topContainer != null){
if(topContainer instanceof BaseElement){
topContainer = ((BaseElement)topContainer).getParentContainer();
}else{
break;
}
}
if(null == topContainer){
topContainer = this;
}
return topContainer;
}
private ITempletContainer topContainer = null;
/*
* @see excel.report.util.ITempletContainer#getElement(java.lang.String)
*/
public BaseElement getElement(String name) {
BaseElement element = null;
Iterator ite = elements.entrySet().iterator();
Entry entry = null;
while(ite.hasNext()){
entry = (Entry)ite.next();
element = (BaseElement)entry.getValue();
if(element instanceof BaseBand ){
element = ((BaseBand)element).getElement(name);
}
if(null != element && element.getName().equals(name)){
return element;
}
}
return null;
}
/*
* @see excel.report.util.ITempletContainer#addElement(excel.report.util.BaseElement)
*/
public void addElement(BaseElement element, int col, int row) {
if(null != element){
elements.put(getKey(col,row), element);
}
}
/**
* 取得map键值
* @param col
* @param row
* @return
*/
private String getKey(int col, int row){
StringBuffer key = new StringBuffer();
key.append(col).append("#").append(row);
return key.toString();
}
/*
* @see excel.report.util.ITempletContainer#getElement(int, int)
*/
public BaseElement getElement(int col, int row) {
return (BaseElement)elements.get(getKey(col, row));
}
/*
* @see excel.report.util.ITempletContainer#getBeginCol()
*/
public int getBeginCol() {
return this.beginCol;
}
/*
* @see excel.report.util.ITempletContainer#getBeginRow()
*/
public int getBeginRow() {
return this.beginRow;
}
/*
* @see excel.report.util.ITempletContainer#getEndCol()
*/
public int getEndCol() {
return this.endCol;
}
/*
* @see excel.report.util.ITempletContainer#getEndRow()
*/
public int getEndRow() {
return this.endRow;
}
/* (non-Javadoc)
* @see excel.report.util.ITempletContainer#setBegin(int, int)
*/
public void setBegin(int beginCol, int beginRow) {
this.beginCol = beginCol;
this.beginRow = beginRow;
}
/* (non-Javadoc)
* @see excel.report.util.ITempletContainer#setEnd(int, int)
*/
public void setEnd(int endCol, int endRow) {
this.endCol = endCol;
this.endRow = endRow;
}
/* (non-Javadoc)
* @see excel.report.util.ITempletContainer#addMergedCell(jxl.Range)
*/
public boolean addMergedCell(Range range) {
Iterator itr = elements.entrySet().iterator();
Entry entry = null;
BaseElement element = null;
while(itr.hasNext()){
entry = (Entry)itr.next();
element = (BaseElement)entry.getValue();
if(null != element){
if(element instanceof BaseBand){
if(((BaseBand)element).addMergedCell(range)){
return true;
}
}
}
}
//如果没有找到则将合并单元格信息保存在本模板中。
int topRow = range.getTopLeft().getRow();
int bottomRow = range.getBottomRight().getRow();
//当定义模板带自身合并的单元格则忽略掉
if(topRow == beginRow || bottomRow == endRow ||
topRow == endRow || bottomRow == beginRow){
if(log.isDebugEnable()){
log.debug("Ignore merged cell:" + range.toString());
}
return true;
}
if(topRow > beginRow && bottomRow < endRow ){
if(null == mergedCells){
mergedCells = new ArrayList();
}
mergedCells.add(range);
return true;
}
return false;
}
/*
* (non-Javadoc)
* @see excel.report.base.ITempletContainer#addImage(jxl.write.WritableImage)
*/
public boolean addImage(WritableImage image) {
Iterator itr = elements.entrySet().iterator();
Entry entry = null;
BaseElement element = null;
while(itr.hasNext()){
entry = (Entry)itr.next();
element = (BaseElement)entry.getValue();
if(null != element){
if(element instanceof BaseBand){
if(((BaseBand)element).addImage(image)){
return true;
}
}
}
}
//如果没有找到则将图片信息保存在本模板中。
double topRow = image.getRow() - 1;
double endRow = topRow + image.getHeight();
if(topRow >= this.beginRow && endRow <= this.getEndRow()){
if(null == images){
images = new ArrayList();
}
images.add(image);
return true;
}
return false;
}
/*
* (non-Javadoc)
* @see excel.report.base.ITempletContainer#getImages()
*/
public List getImages() {
return images;
}
/* (non-Javadoc)
* @see excel.report.util.ITempletContainer#getMergedCells()
*/
public List getMergedCells() {
return mergedCells;
}
/*
* (non-Javadoc)
* @see excel.report.base.ITempletContainer#removeAllTempletImages()
*/
public boolean removeAllTempletImages(WritableSheet sheet) {
Iterator itr = elements.entrySet().iterator();
Entry entry = null;
BaseElement element = null;
while(itr.hasNext()){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -