📄 orgchartpanel.java
字号:
/*
* OPIAM Suite
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package opiam.admin.faare.service.services.views.orgchart;
import Acme.JPM.Encoders.GifEncoder;
import org.apache.log4j.Logger;
import quantize.ImageFrame;
import quantize.Quantize;
import quantize.TestQuantize;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
/**
* This class allows the orgchart generation.
*
* This class uses Swing.
* Application should use PJA (both on Windows and UNIX).
*
*/
public class OrgChartPanel extends JPanel
{
/** Instance of logger. */
private static Logger _logger = Logger.getLogger(OrgChartPanel.class);
// LINE TYPES
/** Kind of line. */
public static final short HORIZONTAL_LEFT = 0;
/** Kind of line. */
public static final short HORIZONTAL_CENTER = 1;
/** Kind of line. */
public static final short HORIZONTAL_RIGHT = 2;
/** Kind of orgchart, and kind of line. */
public static final short VERTICAL = 3;
/** Kind of orgchart, and kind of line. */
public static final short HORIZONTAL = 4;
/** Kind of orgchart. */
public static final short TREE = 5;
/** Orgchart mode. */
public static final short COMPRESSED = 6;
/** Orgchart mode (deprecated). */
public static final short LARGE = 7;
/** Kind of orgchart. */
public static final short SEMALIKE = 8;
/** Kind of line. */
public static final short NONE = -1;
// node properties
/** Name of node. */
public static final String NAME = "name";
/** Dname of node. */
public static final String DN = "dn";
/** Photo of person in node. */
public static final String PHOTO = "photo";
/** Node level in hashtable. */
public static final String LEVEL = "level";
/** Cell width in hashtable. */
public static final String WIDTH = "dispwidth";
/** Cell height in hashtable. */
public static final String HEIGHT = "dispheight";
/** X coordinate of cell in hashtable. */
public static final String XCOORD = "dispx";
/** Y coordinate of cell in hashtable. */
public static final String YCOORD = "dispy";
/** Cell font size in hashtable. */
public static final String FONTSIZE = "fontsize";
/** Node attributes in hashtable. */
public static final String ATTRIBS = "attribs";
/** Computed X coordinate un hashtable. */
public static final String X = "computedx";
/** Global attribute list. */
private static List attribs;
/** Root cell. */
private static XJButton baseCell = null;
/** Orgchart kind. */
private static int direction = HORIZONTAL;
/** Orgchart mode. */
private static int mode = COMPRESSED;
/** Number of characters in longest label. */
private static int nbcar = 0;
/** Longest label size. */
private static int longest = 0;
/** Horizontal space. */
public static final int SPACEH = 50;
/** Vertical space. */
public static final int SPACEV = 10;
/** Some space. */
public static final int SSPACE = 30;
/** Maximum width. */
private static int maxX = 0;
/** Maximum height. */
private static int maxY = 0;
/** Current instance. */
private static OrgChartPanel orgchart = null;
/** Attributes per level. */
private static Hashtable levelAttribs;
/** Orgchart criteria. */
private static Object[] crits;
// colors and fonts
/** Background color. */
private Color backGround;
/** Text color. */
private Color textColor;
/** Text font. */
private Font textFont;
/** Text font. */
private Font titleFont;
/** Cell color. */
private Color cellColor;
/** List of panels with lines. */
private ArrayList horizList = null;
/** The head of the orgchart. */
private DefaultMutableTreeNode root;
/** Maximum width of a cell. */
private int maxWidth = 500;
/** Maximum height of a cell. */
private int maxHeight = 0;
/** Number of columns in Sema mode. */
private int nbCols = 2;
//DW/2615/BeginPatch
/** Default photo file name. */
private String defaultPhoto = null;
//DW/2615/EndPatch
/** Space left for photo + margin. */
public static final int DX_PHOTO = 55;
/** Space left for margin (no photo). */
public static final int DX = 5;
/** Photo size. */
public static final int PHOTO_SZ = 50;
/** Minimum width. */
private static final int MINW = 70;
/** Width factor. */
private static final int W_FACTOR = 8;
/** Maximum size supported by PDF lib. */
private static final int MAX_PDF = 14400;
/** Maximum nb of colors supported by GIF. */
private static final int MAX_GIF = 256;
/**
* Creates a new OrgChartPanel object.
*
* @param aroot Root node.
* @param level Depth level. (deprecated)
*/
public OrgChartPanel(final DefaultMutableTreeNode aroot, final int level)
{
this(aroot, level, true);
_logger.debug("OrgChartPanel constructor1 level: " + level);
}
/**
* Creates a new OrgChartPanel object.
*
* @param aroot Root node
* @param level Depth level. (deprecated)
* @param start If true, generates orgchart immediately.
*/
public OrgChartPanel(final DefaultMutableTreeNode aroot, final int level,
final boolean start)
{
super();
_logger.debug("OrgChartPanel constructor2 level: " + level);
_logger.debug("OrgChartPanel constructor2 start: " + start);
orgchart = this;
backGround = Color.white;
textColor = Color.black;
Font f;
f = getFont();
textFont = new Font(f.getName(), Font.BOLD, f.getSize());
titleFont = f;
setBackground(backGround);
cellColor = Color.lightGray;
attribs = new ArrayList();
attribs.add("photo");
attribs.add("displayname");
attribs.add("title");
setRootNode(aroot);
if (start)
{
doIt();
}
crits = null;
nbcar = 0;
longest = 0;
doCompute(this);
}
/**
* Creates a new OrgChartPanel object.
*
* @param aroot Root node.
* @param level Depth level. (deprecated)
* @param acrits Criteria.
* @param listatt Attributes per level.
*/
public OrgChartPanel(final DefaultMutableTreeNode aroot, final int level,
final Object[] acrits, final Hashtable listatt)
{
this(aroot, level, false);
crits = acrits;
levelAttribs = listatt;
_logger.debug("OrgChartPanel constructor3 level: " + level);
}
/**
* Gets orgchart kind.
*
* @return The orgchart kind.
*/
public static int getDirection()
{
return direction;
}
/**
* Sets orgchart mode.
*
* @param amode Orgchart mode.
*/
public void setMode(final int amode)
{
OrgChartPanel.mode = amode;
}
/**
* Gets orgchart mode.
*
* @return The orgchart mode.
*/
public static int getMode()
{
return mode;
}
/**
* Sets orgchart kind.
*
* @param dir Orgchart kind.
*/
public void setDirection(final int dir)
{
_logger.debug("setDirection1 dir = " + dir);
direction = dir;
_logger.debug("setDirection2 direction = " + direction);
}
/**
* Sets root node.
*
* @param aRoot Root node.
*/
public final void setRootNode(final DefaultMutableTreeNode aRoot)
{
root = aRoot;
}
/**
* Gets the root node.
*
* @return The root node.
*/
public final DefaultMutableTreeNode getRoot()
{
return root;
}
/**
* Adds an attribute to a cell.
*
* @param pan Enclosing panel.
* @param gl Gridbag layout.
* @param gc Gridbag constraints.
* @param lab Label to add.
*/
private void addAttrib(final JPanel pan, final GridBagLayout gl,
final GridBagConstraints gc, final JLabel lab)
{
_logger.debug("addAttrib");
gl.setConstraints(lab, gc);
pan.add(lab);
gc.gridy++;
}
/**
* Sets font size of a label according to cell content.
*
* @param label Label to set.
* @param base Base font.
* @param maxw Max width.
* @param info Node hashtable.
* @param isphoto Photo presence flag.
*/
private void setFont(final JLabel label, final Font base, final int maxw,
final Hashtable info, final boolean isphoto)
{
_logger.debug("setFont");
int maxw4t = maxw - DX_PHOTO;
if (!isphoto)
{
maxw4t = maxw - DX;
}
String text = label.getText();
Font curFont = new Font(base.getName(), base.getStyle(), base.getSize());
Font nextFont;
int txtsize = 0;
do
{
txtsize = (int) getFontMetrics(curFont)
.getStringBounds(text, getGraphics()).getWidth();
label.setFont(curFont);
nextFont = new Font(curFont.getName(), curFont.getStyle(),
curFont.getSize() - 1);
curFont = nextFont;
}
while ((txtsize > maxw4t) && (curFont.getSize() > 1));
if (info.get(FONTSIZE) != null)
{
int oldsize = ((Integer) info.get(FONTSIZE)).intValue();
if (oldsize > curFont.getSize())
{
info.put(FONTSIZE, new Integer(curFont.getSize()));
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -