All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yaoqiang.graph.model.YGraphModel Maven / Gradle / Ivy

There is a newer version: 2.2.18
Show newest version
package org.yaoqiang.graph.model;

import java.awt.Color;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.yaoqiang.graph.util.Constants;

import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;

/**
 * YGraphModel
 * 
 * @author Shi Yaoqiang([email protected])
 */
public class YGraphModel extends mxGraphModel {

	private static final long serialVersionUID = 1L;

	protected PageFormat pageFormat;

	protected Color backgroundColor = Color.WHITE;

	protected int pageCount = Integer.parseInt(Constants.SETTINGS.getProperty("pageNumV", "1"));

	protected int horizontalPageCount = Integer.parseInt(Constants.SETTINGS.getProperty("pageNumH", "1"));

	public PageFormat getPageFormat() {
		return pageFormat;
	}

	public void setPageFormat(PageFormat pageFormat) {
		this.pageFormat = pageFormat;
	}

	public PageFormat setDefaultPageFormat() {
		PageFormat pageFormat = new PageFormat();
		Paper paper = pageFormat.getPaper();
		paper.setSize(Constants.PAGE_HEIGHT, Constants.PAGE_WIDTH);
		double margin = 5;
		paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
		pageFormat.setOrientation(Integer.parseInt(Constants.SETTINGS.getProperty("pageOrientation", "0")));
		pageFormat.setPaper(paper);
		this.pageFormat = pageFormat;
		return pageFormat;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getHorizontalPageCount() {
		return horizontalPageCount;
	}

	public void setHorizontalPageCount(int horizontalPageCount) {
		this.horizontalPageCount = horizontalPageCount;
	}

	public Color getBackgroundColor() {
		return backgroundColor;
	}

	public void setBackgroundColor(Color backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	public List getAllEdgesInOrder(Object root, List callActivities) {
		List edges = new ArrayList();
		Collection cells = getCells().values();
		for (Object cell : cells) {
			if (isEdge(cell) && isAncestor(root, cell)) {
				boolean add = true;
				for (Object parent : callActivities) {
					if (isAncestor(parent, cell)) {
						add = false;
						break;
					}
				}
				if (add) {
					edges.add(cell);
				}
			}
		}
		return edges;
	}

	public String getAttrFromStyle(String attr, String style) {
		if (style == null) {
			return "";
		}
		int index = style.lastIndexOf(";" + attr + "=");
		if (index == -1) {
			return "";
		}
		int lastindex = -1;
		for (String key : Constants.STYLE_KEYWORDS) {
			lastindex = style.indexOf(";" + key + "=", index);
			if (lastindex != -1 && lastindex != index) {
				break;
			}
		}
		if (lastindex == -1 || lastindex == index) {
			return style.substring(index + attr.length() + 2);
		} else {
			return style.substring(index + attr.length() + 2, lastindex);
		}

	}

	public Map getCells() {
		for (Object cell : new ArrayList(cells.values())) {
			if (getParent(cell) != null && !Arrays.asList(getChildren(this, getParent(cell))).contains(cell)) {
				cells.remove(((mxICell) cell).getId());
			}
		}
		return cells;
	}

	public Object getCell(String id) {
		Object result = null;
		if (cells != null && id != null) {
			result = cells.get(id);
			if (result != null && getParent(result) != null && !Arrays.asList(getChildren(this, getParent(result))).contains(result)) {
				cells.remove(id);
				result = null;
			}
		}
		return result;
	}

}