⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 casedocument.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    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.
 */

/*
 * Created on 2006/8/30
 *
 * @Author: Xiaojun Chen
 * $Revision$ 1.0
 *
 */
package eti.bi.alphaminer.tools.ExportCase;

import java.awt.Component;
import java.awt.Graphics;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;
import javax.swing.filechooser.FileFilter;
import JB.Graph.JbDirectedGraph;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import eti.bi.alphaminer.core.Node.NodeLoader;
import eti.bi.alphaminer.core.handler.CaseHandler;
import eti.bi.alphaminer.operation.operator.NodeInfo;
import eti.bi.alphaminer.tools.Exporter;
import eti.bi.alphaminer.vo.BICase;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.alphaminer.vo.Node;
import eti.bi.alphaminer.vo.NodeConnection;
import eti.bi.alphaminer.vo.NodeFactory;
import eti.bi.alphaminer.vo.TaskNode;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.SysException;
import eti.bi.util.ResourceLoader;

public class CaseDocument {
	private static FileFilter filter = new FileFilter() {

		@Override
		public boolean accept(File f) {
			if(f.isDirectory()) {
				return true;
			}
			else if(f.getName().endsWith(".pdf")) {
				return true;
			}
			else {
				return false;
			}
		}

		@Override
		public String getDescription() {
			return "pdf";
		}
	};
	
	private static CasePageEvent casepageevent;
	private static BaseFont bf;
	
	private static CaseHandler m_CaseHandler;
	
	private static String BookMark;
	private static BaseFont baseFont;
	
	public static void exportCase(Component parent, String case_ID, Component caseDiagram) throws Exception{
		if(m_CaseHandler==null) {
			m_CaseHandler = CaseHandler.getInstance();
		}
		if(bf==null) {
			bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		}
		BICase aCase = m_CaseHandler.getCase(case_ID, false);
		
		if(aCase==null) {
			throw new SysException("Can't find case: "+case_ID);
		}
		
		TaskNode taskNode = (TaskNode) aCase.getNode(NodeFactory.TASK, null);
		
		String caseName = taskNode.getCaseName();
		File selectedFile=null;
		if(caseName!=null) {
			selectedFile = new File(caseName);
		}
		
		File savefile = Exporter.chooseSaveFile(parent, Resource.srcStr("ExportCaseDocument"), filter, true, selectedFile, null,".pdf");
		if(savefile==null) {
			return;
			//throw new SysException("No export file!");
		}
		
		String filename = savefile.getName();
		if(!filename.endsWith(".pdf")) {
			filename = filename.concat(".pdf");
			savefile = new File(filename);
		}
		savefile.createNewFile();
		
		if(!savefile.canWrite()) {
			throw new IOException("Can't write file: "+savefile.getAbsolutePath());
		}
		
		if(casepageevent==null) {
			casepageevent = new CasePageEvent();
		}
		
		Document document = new Document(PageSize.A4, 50, 50, 70, 70);
		
		Exception e = null;
		try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(savefile));
            writer.setPageEvent(casepageevent);
            
            document.addCreationDate();
            document.addCreator("Xiaojun Chen");
            document.addTitle(Resource.srcStr("CaseDocTitle"));
            document.addSubject("Data Mining");
            document.addProducer();
            document.open();
            
            writedocument(aCase, document, writer, caseDiagram);
            
		}
		catch(Exception e1) {
			e = e1;
		}
		finally {
			document.close();
			if(e!=null) {
				throw e;
			}
		}
	}

	private static void writedocument(BICase aCase, Document document, PdfWriter writer, Component caseDiagram) throws DocumentException, SysException, IOException {
		baseFont = null;
		if(Resource.equalLocale(Locale.CHINA)||Resource.equalLocale(Locale.JAPAN)||Resource.equalLocale(Locale.KOREA)){	
			baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			bf = baseFont;
		}
		else {
		}
		
		TaskNode taskNode = (TaskNode) aCase.getNode(NodeFactory.TASK, null);
		
		PdfContentByte cb = writer.getDirectContent();
		
		//first page: title
		
		Rectangle page = document.getPageSize();
		float xhalf = page.width()-document.leftMargin()-document.rightMargin();
		xhalf = (float)xhalf/2+document.leftMargin();
		float yquarter = page.height()-document.topMargin()-document.bottomMargin();
		yquarter = (float)yquarter/4;
		float yhalf = page.height()-document.topMargin()-document.bottomMargin();
		yhalf = document.bottomMargin()+yhalf/2;
		
		Paragraph caseTitle = null;
		if(baseFont==null) {
			caseTitle = new Paragraph("\n\n\n",new Font(Font.TIMES_ROMAN, 30));
		}
		else {
			caseTitle = new Paragraph("\n\n\n",new Font(baseFont, 30));
		}
		caseTitle.setAlignment(Paragraph.ALIGN_CENTER);
		caseTitle.add(Resource.srcStr("CaseDocTitle")+"\n\n");
		caseTitle.add(taskNode.getCaseName());
		document.add(caseTitle);

		cb.beginText();
		cb.setFontAndSize(bf, 20);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		format.format(new Date());
		cb.showTextAligned(PdfContentByte.ALIGN_CENTER, format.format(new Date()), xhalf, document.bottomMargin()+yquarter-20, 0);
		cb.endText();
		
		//third page: Alphaminer Logo
		document.newPage();
		Image alphaminerLogo = Image.getInstance(ResourceLoader.getsplashImage(),null);
		alphaminerLogo.setAlignment(Image.ALIGN_CENTER);
		alphaminerLogo.setAbsolutePosition(xhalf-alphaminerLogo.width()/2, yhalf-alphaminerLogo.height()/2);
		document.add(alphaminerLogo);
		
		//second page: case information
		document.newPage();
		BookMark = Resource.srcStr("m_CaseInformationButton");
		Paragraph caseinfo = null;
		if(baseFont==null) {
			caseinfo = new Paragraph("\n\n\n"+Resource.srcStr("print_caseName")+": "+taskNode.getCaseName()+"\n", new Font(Font.TIMES_ROMAN, 20));
		}
		else {
			caseinfo = new Paragraph("\n\n\n"+Resource.srcStr("print_caseName")+": "+taskNode.getCaseName()+"\n", new Font(baseFont, 20));
		}
		caseinfo.setAlignment(Paragraph.ALIGN_CENTER);
		document.add(caseinfo);
		BookMark = null;
		Paragraph caseTable = null;
		if(baseFont==null) {
			caseTable = new Paragraph("",new Font(Font.TIMES_ROMAN, 18));
		}
		else {
			caseTable = new Paragraph("",new Font(baseFont, 18));
		}
		PdfPTable table = new PdfPTable(2);
		
		if(baseFont==null) {			
			table.addCell(Resource.srcStr("m_LabelDataMiningGoal"));
			table.addCell(taskNode.getDataMiningGoal());
			
			table.addCell(Resource.srcStr("m_LabelProblemType"));
			table.addCell(taskNode.getProblemType());
			
			table.addCell(Resource.srcStr("m_LabelBusinessObjective"));
			table.addCell(taskNode.getBusinessObjective());
			
			table.addCell(Resource.srcStr("m_LabelCompany"));
			table.addCell(taskNode.getCompanyName());
			
			table.addCell(Resource.srcStr("m_LabelIndustry"));
			table.addCell(taskNode.getIndustry());
			
			table.addCell(Resource.srcStr("m_LabelDepartment"));
			table.addCell(taskNode.getDepartmentName());
		}
		else {
			Font cellFont = new Font(baseFont, 18);
			PdfPCell cell;
			Phrase phase;
			
			phase = new Phrase(Resource.srcStr("m_LabelDataMiningGoal"),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			phase = new Phrase(taskNode.getDataMiningGoal(),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			
			phase = new Phrase(Resource.srcStr("m_LabelProblemType"),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			phase = new Phrase(taskNode.getProblemType(),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			
			phase = new Phrase(Resource.srcStr("m_LabelBusinessObjective"),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			phase = new Phrase(taskNode.getBusinessObjective(),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			
			phase = new Phrase(Resource.srcStr("m_LabelCompany"),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			phase = new Phrase(taskNode.getCompanyName(),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			
			phase = new Phrase(Resource.srcStr("m_LabelIndustry"),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			phase = new Phrase(taskNode.getIndustry(),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			
			phase = new Phrase(Resource.srcStr("m_LabelDepartment"),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
			phase = new Phrase(taskNode.getDepartmentName(),cellFont);
			cell = new PdfPCell(phase);
			table.addCell(cell);
		}
		
		table.setSpacingBefore(5);
		table.setSpacingBefore(5);
		caseTable.setAlignment(Paragraph.ALIGN_CENTER);
		caseTable.add(table);
		document.add(caseTable);
		
		//case diagram
		
		document.newPage();
		//case diagram bookmark
		BookMark = Resource.srcStr("print_tile_caseDiagram");
		Paragraph catitle = null;
		if(baseFont==null) {
			catitle = new Paragraph("\n\n\n"+Resource.srcStr("print_tile_caseDiagram"),new Font(Font.TIMES_ROMAN, 20));
		}
		else {
			catitle = new Paragraph("\n\n\n"+Resource.srcStr("print_tile_caseDiagram"),new Font(baseFont, 20));
		}
		
		catitle.setAlignment(Paragraph.ALIGN_CENTER);
		document.add(catitle);
		BookMark = null;
		
		java.awt.Rectangle rec;
		if(caseDiagram instanceof BreCaseDiagramPanel) {
			rec = ((BreCaseDiagramPanel)caseDiagram).getDiagramRectangle();
		}
		else {
			rec = caseDiagram.getBounds();
		}
		java.awt.Image image = caseDiagram.createImage(rec.width, rec.height);
		Graphics g = image.getGraphics();
		caseDiagram.paint(g);
		
		Image casedg = Image.getInstance(image, null);
		casedg.setAlignment(Image.ALIGN_MIDDLE);
		//suit the page
		float imageWidth = page.width()-document.leftMargin()-document.rightMargin();
		casedg.scaleAbsolute(imageWidth, imageWidth*rec.height/rec.width);
		document.add(casedg);
		
		
		//workflow explain
		document.newPage();
		
		Node[] nodes = aCase.getOperatorNodeListArray();
		if(nodes!=null) {
			
			int size = nodes.length;
			int i;
			
			JbDirectedGraph dgp = new JbDirectedGraph(nodes.length);
			
			for(i=0;i<size;i++) {
				dgp.addVertex(nodes[i]);
			}
			
			Vector<NodeConnection> connections = aCase.getProcess().getConnectionList();
			
			NodeConnection con = null;
			
			size = connections.size();
			for(i=0;i<size;i++) {
				con = (NodeConnection) connections.get(i);
				String parent = con.getHeadNodeID();
				String child = con.getTailNodeID();
				dgp.addEdge(aCase.getNode(parent), aCase.getNode(child), 1);
			}
			
			Object[] sortNodes =  dgp.topologicalSort();
			size = sortNodes.length;
			Node aNode;
			String a_CaseID = aCase.getCaseID();
			String nodeID;
			String temp;
			float width;
			int j;
			Font captain = null;
			Font subcaptain = null;
			if(baseFont==null) {
				captain = new Font(Font.TIMES_ROMAN, 18);
				subcaptain = new Font(Font.TIMES_ROMAN, 14);
			}
			else {
				captain = new Font(baseFont, 18);
				subcaptain = new Font(baseFont, 14);
			}
			
			Paragraph tempParagraph;
			
			for(i=0;i<size;i++) {
				try {
					aNode = (Node) sortNodes[i];
					nodeID = aNode.getNodeID();
					IOperatorNode operatorNode = m_CaseHandler.getOperatorNode(a_CaseID, nodeID);
					NodeInfo nodeInfo = NodeLoader.getNodeInfo(operatorNode.getOperatorDefinitionID());
					
					BookMark = Resource.srcStr("Node")+(i+1)+": "+aNode.getNodeDescription();
					//create document
					Image operatorImage = Image.getInstance(ResourceLoader.getOperatorImage(nodeInfo.getFlowImageFileName(),nodeInfo.getJafFile()),null);
					operatorImage.setAlignment(Image.ALIGN_LEFT);
					Paragraph nodecaption = new Paragraph(Resource.srcStr("Node")+(i+1)+"\n",captain);
					nodecaption.add(operatorImage);
					nodecaption.add("\n");
					document.add(nodecaption);
					BookMark = null;
					
					temp = Resource.srcStr("Node_Name");
					width = bf.getWidthPoint(temp, 16);
					document.add(new Paragraph(temp,captain));
					tempParagraph = new Paragraph(aNode.getNodeDescription()+"\n",subcaptain);
					tempParagraph.setIndentationLeft(width);
					document.add(tempParagraph);
					
					
					temp = Resource.srcStr("Node_description");
					width = bf.getWidthPoint(temp, 16);
					document.add(new Paragraph(temp,captain));
					tempParagraph = new Paragraph(operatorNode.getName()+"\n",subcaptain);
					tempParagraph.setIndentationLeft(width);
					document.add(tempParagraph);
					
					temp = Resource.srcStr("Node_remark");
					width = bf.getWidthPoint(temp, 16);
					document.add(new Paragraph(temp,captain));
					temp = (String) operatorNode.getParameterValue("Tips");
					if(temp==null) {
						temp = "";
					}
					tempParagraph = new Paragraph(temp+"\n",subcaptain);
					tempParagraph.setIndentationLeft(width);
					document.add(tempParagraph);
					
					temp = Resource.srcStr("Node_arguments");
					width = bf.getWidthPoint(temp, 16);
					document.add(new Paragraph(temp,captain));
					String[][] pars = operatorNode.getParametersArray();
					tempParagraph = new Paragraph("", subcaptain);
					tempParagraph.setIndentationLeft(width);
					if(pars!=null) {
						int psize = pars.length;
						for(j=0;j<psize;j++) {
							tempParagraph.add(pars[j][0]+": "+pars[j][1]+"\n");
						}
					}
					else {
						tempParagraph.add("\n");
					}
					tempParagraph.add("\n\n");
					
					//end
					document.add(tempParagraph);
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static String BookMark() {
		return BookMark;
	}
	
	public static BaseFont baseFont() {
		return baseFont;
	}
}

⌨️ 快捷键说明

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