📄 printorga.java
字号:
/*
* OPIAM Suite
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package opiam.admin.faare.service.services.views.orgchart;
import com.lowagie.text.Document;
import com.lowagie.text.Graphic;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.log4j.Logger;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* This class is used to generate a PDF orgchart form an OrgChartPanel
* generated orgchart.
* The cells info (X,Y,w,h,attributes,photos) are conveyed in hastables for
* each node.
*
*/
public final class PrintOrga
{
/** Constructor private because this is a utility class. */
private PrintOrga()
{
}
/** Instance of logger. */
private static Logger _logger = Logger.getLogger(OrgChartPanel.class);
/** Orgchart width. */
private static int maxporgaw = 0;
/** Orgchart height. */
private static int maxporgah = 0;
/** Orgchart mode. */
private static int direction = OrgChartPanel.HORIZONTAL;
/** Factor to convert X coordinates to page size. */
private static int fXSIZE = 0;
/** Factor to convert Y coordinates to page size. */
private static int fYSIZE = 0;
/** Number of pages. */
private static int nbPageY = 1;
//DW/2615/BeginPatch
/** Default photo file name. */
private static String defaultPhoto = null;
//DW/2615/EndPatch
/** Some space. */
private static final int SPACE1 = OrgChartPanel.SPACEV + OrgChartPanel.DX;
/** Some space. */
private static final int SPACE2 = 2 * OrgChartPanel.SPACEV;
/** Some space. */
private static final int SPACE3 = SPACE2 + OrgChartPanel.DX;
/** Some space. */
private static final int SPACE4 = SPACE1 + SPACE2;
/** Font size. */
private static final int FONT_SZ = 12;
/** Conversion pixel=>PDF lib units. */
private static final int F1 = 595;
/** Conversion pixel=>PDF lib units. */
private static final int F2 = 210;
/** Line size. */
private static final float LINE_SZ = 1.5f;
/**
* Updates orgchart size with current node.
*
* @param info Node hashtable.
*/
private static void computePageSize(final Hashtable info)
{
int x = 0;
int y = 0;
if ((info.get(OrgChartPanel.WIDTH) != null) &&
(info.get(OrgChartPanel.XCOORD) != null))
{
x = ((Integer) info.get(OrgChartPanel.WIDTH)).intValue() +
((Integer) info.get(OrgChartPanel.XCOORD)).intValue();
}
if (maxporgaw < x)
{
maxporgaw = x;
}
if ((info.get(OrgChartPanel.HEIGHT) != null) &&
(info.get(OrgChartPanel.YCOORD) != null))
{
y = ((Integer) info.get(OrgChartPanel.HEIGHT)).intValue() +
((Integer) info.get(OrgChartPanel.YCOORD)).intValue();
}
if (maxporgah < y)
{
maxporgah = y;
}
}
/**
* Computes orgchart size.
*
* @param node Orgchart root node.
*/
private static void computePageSize(final DefaultMutableTreeNode node)
{
Enumeration enum = node.children();
while (enum.hasMoreElements())
{
DefaultMutableTreeNode childnode = (DefaultMutableTreeNode) enum.nextElement();
Hashtable info = (Hashtable) childnode.getUserObject();
computePageSize(info);
computePageSize(childnode);
}
}
/**
* Draws rectangle and lines.
*
* @param g PDF Graphic on which to draw.
* @param node Current node.
* @param px Page number on X coordinates.
* @param py Page number on Y coordinates.
*/
private static void drawRectangle(final Graphic g,
final DefaultMutableTreeNode node, final int px, final int py)
{
Hashtable info = (Hashtable) node.getUserObject();
int x = ((Integer) info.get(OrgChartPanel.XCOORD)).intValue();
int y = ((Integer) info.get(OrgChartPanel.YCOORD)).intValue();
int w = ((Integer) info.get(OrgChartPanel.WIDTH)).intValue();
int h = ((Integer) info.get(OrgChartPanel.HEIGHT)).intValue();
int xd = (px - 1) * fXSIZE;
int xf = px * fXSIZE;
int yd = (nbPageY - py) * fYSIZE;
g.rectangle(x - xd, maxporgah - y - h - OrgChartPanel.SPACEV - yd, w, h);
int nbchild = node.getChildCount();
if (nbchild > 0)
{
/* vertical line at bottom of node */
if (direction == OrgChartPanel.HORIZONTAL)
{
g.moveTo((x + (w / 2)) - xd, maxporgah - y - h - OrgChartPanel.SPACEV - yd);
}
else
{
g.moveTo((x + w) - xd, maxporgah - y - (h / 2) - OrgChartPanel.SPACEV - yd);
}
Hashtable finfo = (Hashtable) ((DefaultMutableTreeNode) node.getFirstChild()).getUserObject();
Hashtable linfo = (Hashtable) ((DefaultMutableTreeNode) node.getLastChild()).getUserObject();
int fx = ((Integer) finfo.get(OrgChartPanel.XCOORD)).intValue();
int lx = ((Integer) linfo.get(OrgChartPanel.XCOORD)).intValue();
int fw = ((Integer) finfo.get(OrgChartPanel.WIDTH)).intValue();
int lw = ((Integer) linfo.get(OrgChartPanel.WIDTH)).intValue();
int fh = ((Integer) finfo.get(OrgChartPanel.HEIGHT)).intValue();
int lh = ((Integer) linfo.get(OrgChartPanel.HEIGHT)).intValue();
int fy = ((Integer) finfo.get(OrgChartPanel.YCOORD)).intValue();
int ly = ((Integer) linfo.get(OrgChartPanel.YCOORD)).intValue();
if (direction == OrgChartPanel.HORIZONTAL)
{
g.lineTo((x + (w / 2)) - xd, (maxporgah - fy + SPACE1) - yd);
}
else if (direction == OrgChartPanel.TREE)
{
g.moveTo(fx - SPACE3 - xd, maxporgah - y - h - OrgChartPanel.SPACEV - yd);
g.lineTo(fx - SPACE3 - xd, maxporgah - fy - (fh / 2) - OrgChartPanel.SPACEV - yd);
}
else
{
g.lineTo(fx - SPACE3 - xd, maxporgah - y - (h / 2) - OrgChartPanel.SPACEV - yd);
}
/* horizontal line from first son to last son */
int x0 = fx + (fw / 2);
if (x0 < xd)
{
x0 = xd;
}
int x1 = lx + (lw / 2);
if (x1 > xf)
{
x1 = xf;
}
if (direction == OrgChartPanel.HORIZONTAL)
{
g.moveTo(x0 - xd, (maxporgah - fy + SPACE1) - yd);
g.lineTo(x1 - xd, (maxporgah - fy + SPACE1) - yd);
}
else
{
g.moveTo(fx - SPACE3 - xd, maxporgah - fy - (fh / 2) - OrgChartPanel.SPACEV - yd);
g.lineTo(fx - SPACE3 - xd, maxporgah - ly - (lh / 2) - OrgChartPanel.SPACEV - yd);
}
/* vertical lines at top of sons */
Enumeration enum = node.children();
while (enum.hasMoreElements())
{
DefaultMutableTreeNode childnode = (DefaultMutableTreeNode) enum.nextElement();
Hashtable cinfo = (Hashtable) childnode.getUserObject();
int cx = ((Integer) cinfo.get(OrgChartPanel.XCOORD)).intValue();
int cw = ((Integer) cinfo.get(OrgChartPanel.WIDTH)).intValue();
int cy = ((Integer) cinfo.get(OrgChartPanel.YCOORD)).intValue();
int ch = ((Integer) cinfo.get(OrgChartPanel.HEIGHT)).intValue();
if (direction == OrgChartPanel.HORIZONTAL)
{
g.moveTo((cx + (fw / 2)) - xd, (maxporgah - fy + SPACE1) - yd);
g.lineTo((cx + (lw / 2)) - xd, maxporgah - fy - OrgChartPanel.SPACEV - yd);
}
else
{
g.moveTo(fx - SPACE3 - xd, maxporgah - cy - (ch / 2) - OrgChartPanel.SPACEV - yd);
g.lineTo(fx - xd, maxporgah - cy - (ch / 2) - OrgChartPanel.SPACEV - yd);
}
}
}
Enumeration enum = node.children();
while (enum.hasMoreElements())
{
DefaultMutableTreeNode childnode = (DefaultMutableTreeNode) enum.nextElement();
drawRectangle(g, childnode, px, py);
}
}
//DW/2573/BeginPatch
/**
* Draws rectangle and lines, in Sema mode.
*
* @param g PDF Graphic on which to draw.
* @param node Current node.
* @param px Page number on X coordinates.
* @param py Page number on Y coordinates.
*/
private static void drawRectangleSema(final Graphic g,
final DefaultMutableTreeNode node, final int px, final int py)
{
Hashtable info = (Hashtable) node.getUserObject();
int x = ((Integer) info.get(OrgChartPanel.XCOORD)).intValue();
int y = ((Integer) info.get(OrgChartPanel.YCOORD)).intValue();
int w = ((Integer) info.get(OrgChartPanel.WIDTH)).intValue();
int h = ((Integer) info.get(OrgChartPanel.HEIGHT)).intValue();
int xd = (px - 1) * fXSIZE;
int xf = px * fXSIZE;
int yd = (nbPageY - py) * fYSIZE;
g.rectangle(x - xd, maxporgah - y - h - OrgChartPanel.SPACEV - yd, w, h);
int nbchild = node.getChildCount();
if (nbchild > 0)
{
/* vertical line at bottom of node */
g.moveTo((x + (w / 2)) - xd, maxporgah - y - h - OrgChartPanel.SPACEV - yd);
Hashtable finfo = (Hashtable) ((DefaultMutableTreeNode) node.getFirstChild()).getUserObject();
Hashtable linfo = (Hashtable) ((DefaultMutableTreeNode) node.getLastChild()).getUserObject();
int fx = ((Integer) finfo.get(OrgChartPanel.XCOORD)).intValue();
int lx = ((Integer) linfo.get(OrgChartPanel.XCOORD)).intValue();
int fw = ((Integer) finfo.get(OrgChartPanel.WIDTH)).intValue();
int lw = ((Integer) linfo.get(OrgChartPanel.WIDTH)).intValue();
int fh = ((Integer) finfo.get(OrgChartPanel.HEIGHT)).intValue();
int lh = ((Integer) linfo.get(OrgChartPanel.HEIGHT)).intValue();
int fy = ((Integer) finfo.get(OrgChartPanel.YCOORD)).intValue();
int ly = ((Integer) linfo.get(OrgChartPanel.YCOORD)).intValue();
g.lineTo((x + (w / 2)) - xd, (maxporgah - ly + OrgChartPanel.DX) - yd);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -