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

📄 functiontree.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
字号:
/**
 * 
 */
package flow.graph.gui.graph.lua.gui.function;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalToolTipUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import net.antonioshome.swing.treewrapper.TreeWrapper;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCell;

import flow.graph.gui.graph.FlowGraphConstants;
import flow.graph.gui.graph.cell.bpel.Function;
import flow.graph.gui.graph.cell.bpel.FunctionTips;
import flow.graph.gui.graph.cell.bpel.Functions;
import flow.graph.gui.graph.encoder.XMLEncoder;
import flow.graph.gui.graph.lua.GraphTemplateCell;
import flow.graph.gui.graph.lua.xml.TemplateConstants;
import flow.graph.gui.graph.template.TemplateFactory;

/**
 * @author Administrator
 *
 */

class ToolTipTreeNode extends DefaultMutableTreeNode {
	private String toolTipText;

	public ToolTipTreeNode(String str, String toolTipText) {
		super(str);
	    this.toolTipText = toolTipText;
	}

	public ToolTipTreeNode(String str, Object obj) {
		super(str);
	    this.toolTipText = obj.toString();
	}

	public String getToolTipText() {
		return toolTipText;
	}
}

public class FunctionTree {
    protected JTree             tree;
    /** Tree model. */
    protected DefaultTreeModel        treeModel;

	public FunctionTree(JGraph graph){
		/*
	    String[][] strs = { { "swing", "boat" }, // 0
				{ "platf", "paltform" }, // 1
		        { "basic", "fortran" }, // 2
		        { "metal", "heavy" }, // 3
		        { "JTree", "three" } }; // 4

		ToolTipTreeNode[] nodes = new ToolTipTreeNode[strs.length];
		for (int i = 0; i < strs.length; i++) {
			nodes[i] = new ToolTipTreeNode(strs[i][0], strs[i][1]);
		}
		nodes[0].add(nodes[1]);
		nodes[1].add(nodes[2]);
		nodes[1].add(nodes[3]);
		nodes[0].add(nodes[4]);
		*/
		ToolTipTreeNode root = new ToolTipTreeNode("函数列表", "函数列表说明");
		ToolTipTreeNode systemFunction = new ToolTipTreeNode("系统函数", "系统函数列表说明");
		//systemFunction.add(new ToolTipTreeNode("系统函数1", new FunctionTips("getMessage", "", "磷矿粉代价啊;临街房地拉动司机发送打发打发第三方")));
		Functions sysfunctions = new Functions();
		File file = new File(System.getProperty("user.dir")+"/"+TemplateFactory.CACHE_PATH+"/"+TemplateFactory.FUNCTION_FILES);
		if(file.exists()){
			sysfunctions = (Functions)readGraphFile(System.getProperty("user.dir")+"/"+TemplateFactory.CACHE_PATH+"/"+TemplateFactory.FUNCTION_FILES);
		} else{
			try {
				file.createNewFile();
				if(sysfunctions == null) sysfunctions = new Functions();
				writeGraphFile(System.getProperty("user.dir")+"/"+TemplateFactory.CACHE_PATH+"/"+TemplateFactory.FUNCTION_FILES, sysfunctions);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		for(int j=0;j<sysfunctions.getFunctionTips().size();j++){
			FunctionTips functionTips = (FunctionTips)sysfunctions.getFunctionTips().get(j);
			systemFunction.add(new ToolTipTreeNode(functionTips.getName()+"("+functionTips.getParameter()+")", functionTips));
		}
		
		ToolTipTreeNode userFunction = new ToolTipTreeNode("自定义函数", "自定义函数列表说明");
		if(graph != null){
			if(graph.getRoots().length > 0){
				Object[] cells = graph.getDescendants(graph.getRoots());
				for(int i=0;i<cells.length;i++){
					//System.out.println("class="+cells[i].getClass());
					if(cells[i] instanceof GraphTemplateCell){
						//System.out.println("1------------function"+cells[i]);
						GraphTemplateCell cell = (GraphTemplateCell)cells[i];
						if(FlowGraphConstants.getTemplateType(cell.getAttributes()) != null){
							//System.out.println("2------------function");
							if(FlowGraphConstants.getTemplateType(cell.getAttributes()).toString().equals(TemplateConstants.MODEL_TEMPLATE)){
								//System.out.println("3------------function");
								if(FlowGraphConstants.getBpelFunctions(cell.getAttributes()) != null){
									Functions functions = (Functions)FlowGraphConstants.getBpelFunctions(cell.getAttributes());
									if(functions.getFunctions() != null){
										for(int j=0;j<functions.getFunctions().size();j++){
											Function function = (Function)functions.getFunctions().get(j);
											userFunction.add(new ToolTipTreeNode(function.getName()+"("+function.getParameter()+")", function));
										}
									}
									if(functions.getFunctionTips() != null){
										for(int j=0;j<functions.getFunctionTips().size();j++){
											FunctionTips functionTips = (FunctionTips)functions.getFunctionTips().get(j);
											userFunction.add(new ToolTipTreeNode(functionTips.getName()+"("+functionTips.getParameter()+")", functionTips));
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		root.add(systemFunction);
		root.add(userFunction);
		
		tree = new JTree(root) {
			public JToolTip createToolTip() {
				MultiLineToolTip tip = new MultiLineToolTip();
				tip.setComponent(this);
				return tip;
			}
				
			public String getToolTipText(MouseEvent evt) {
				if (getRowForLocation(evt.getX(), evt.getY()) == -1)
					return null;
				TreePath curPath = getPathForLocation(evt.getX(), evt.getY());
				return ((ToolTipTreeNode) curPath.getLastPathComponent()).getToolTipText();
			}
		};
		tree.setToolTipText("");
		TreeWrapper wrapper1 = new TreeWrapper( tree );
	}
	
	
	public JTree getTree(){
		return tree;
	}
	public Object readGraphFile(String s){
		try {
			XMLDecoder dec = new XMLDecoder(new BufferedInputStream(new FileInputStream(s)));
			Object obj = dec.readObject();
			dec.close();
			return obj;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		return null;
	}
	public void writeGraphFile(String s, Functions fun){
		try {
			XMLEncoder dec = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(s)));
			dec.writeObject(fun);
			dec.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
}

class MultiLineToolTip extends JToolTip {
	public MultiLineToolTip() {
		setUI(new MultiLineToolTipUI());
	    UIManager.put("ToolTip.foreground", new ColorUIResource(Color.BLUE));
	    UIManager.put("ToolTip.background", new ColorUIResource(Color.yellow));
	}
}

class MultiLineToolTipUI extends MetalToolTipUI {
	private String[] strs;

	private int maxWidth = 0;

	public void paint(Graphics g, JComponent c) {
		FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(g.getFont());
	    Dimension size = c.getSize();
	    g.setColor(c.getBackground());
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(new GradientPaint(0, 0,Color.WHITE,size.width, size.height, new Color(245, 251, 140) ));
	    g2.fillRect(0, 0, size.width, size.height);
	    g.setColor(c.getForeground());
	    if (strs != null) {
			for (int i = 0; i < strs.length; i++) {
				g.drawString(strs[i], 3, (metrics.getHeight()) * (i + 1));
			}
		}
	}

	public Dimension getPreferredSize(JComponent c) {
		FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(c.getFont());
	    String tipText = ((JToolTip) c).getTipText();
	    if (tipText == null) {
			tipText = "";
	    }
	    BufferedReader br = new BufferedReader(new StringReader(tipText));
	    String line;
	    int maxWidth = 0;
	    Vector v = new Vector();
	    try {
			while ((line = br.readLine()) != null) {
				int width = SwingUtilities.computeStringWidth(metrics, line);
				maxWidth = (maxWidth < width) ? width : maxWidth;
				v.addElement(line);
			}
	    } catch (IOException ex) {
			ex.printStackTrace();
	    }
	    int lines = v.size();
	    if (lines < 1) {
			strs = null;
			lines = 1;
	    } else {
			strs = new String[lines];
			int i = 0;
			for (Enumeration e = v.elements(); e.hasMoreElements(); i++) {
				strs[i] = (String) e.nextElement();
			}
	    }
	    int height = metrics.getHeight() * lines;
	    this.maxWidth = maxWidth;
	    return new Dimension(maxWidth + 20, height + 20);
	}
}

⌨️ 快捷键说明

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