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

📄 defaultgraphmodelfileformatxml.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				cell = createCell(type.getNodeValue(), userObject);

				if (cell != null) {
					cells.put(key.getNodeValue(), cell);

					DefaultGraphCell[] children =
						parseChildren(node, cells, cs);
					for (int i = 0; i < children.length; i++)
						cell.add(children[i]);

					Node source = node.getAttributes().getNamedItem("src");
					Node target = node.getAttributes().getNamedItem("tgt");
					if (source != null) {
						ConnectionID cid =
							new ConnectionID(cell, source.getNodeValue(), true);
						connectionSetIDs.add(cid);
					}
					if (target != null) {
						ConnectionID cid =
							new ConnectionID(
								cell,
								target.getNodeValue(),
								false);
						connectionSetIDs.add(cid);
					}

					Node boundsNode = node.getAttributes().getNamedItem("rect");
					Rectangle bounds = null;
					if (boundsNode != null) {
					    Object rectangle = decodeValue(Rectangle.class, boundsNode.getNodeValue());
					    if (rectangle instanceof Rectangle)
						bounds = (Rectangle) rectangle;
					}

					Node pointsNode = node.getAttributes().getNamedItem("pts");
					List points = null;
					if (pointsNode != null) {
					    Object pointList = decodeValue(List.class, pointsNode.getNodeValue());
					    if (pointList instanceof List)
						points = (List) pointList;
					}

					Node attr = node.getAttributes().getNamedItem("attr");
					String mapID = null;
					if (attr != null)
						mapID = attr.getNodeValue();

					if (mapID != null)
						delayedAttributes.add(
							new DelayedAttributeID(
								cell,
								bounds,
								points,
								mapID));
				}
			}
		}
		return cell;
	}

	public static DefaultGraphCell[] parseChildren(
		Node node,
		Hashtable cells,
		ConnectionSet cs) {
		List list = new LinkedList();
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			Node child = node.getChildNodes().item(i);
			DefaultGraphCell cell = parseCell(child, cells, cs);
			if (cell != null)
				list.add(cell);
		}
		DefaultGraphCell[] dgc = new DefaultGraphCell[list.size()];
		list.toArray(dgc);
		return dgc;
	}

	public static Map parseAttrs(Node node) {
		Hashtable map = new Hashtable();
		for (int i = 0; i < node.getChildNodes().getLength(); i++) {
			Node child = node.getChildNodes().item(i);
			if (child.getNodeName().toLowerCase().equals("map")) {
				Node key = child.getAttributes().getNamedItem("id");
				Node pid = child.getAttributes().getNamedItem("pid");
				Map attrs = decodeMap(child, true, false);
				if (key != null && attrs.size() > 0) {
					if (pid != null)
						attrs.put(
							PARENT,
							pid.getNodeValue());
					map.put(key.getNodeValue(), attrs);
				}
			}
		}
		return map;
	}

	/**
	 * Returns an attributeMap for the specified position and color.
	 */
	public static Map createDefaultAttributes() {
		// Create an AttributeMap
		Map map = GraphConstants.createMap();
		// Set a Black Line Border (the Border-Attribute must be Null!)
		GraphConstants.setBorderColor(map, Color.black);
		// Return the Map
		return map;
	}

	public static class DelayedAttributeID {

		protected Object cell;

		protected Rectangle bounds;

		protected List points;

		protected String mapID;

		public DelayedAttributeID(
			Object cell,
			Rectangle bounds,
			List points,
			String mapID) {
			this.cell = cell;
			this.bounds = bounds;
			this.points = points;
			this.mapID = mapID;
		}

		/**
		 * @return
		 */
		public Object getCell() {
			return cell;
		}

		/**
		 * @return
		 */
		public Rectangle getBounds() {
			return bounds;
		}

		/**
		 * @return
		 */
		public String getMapID() {
			return mapID;
		}

		/**
		 * @return
		 */
		public List getPoints() {
			return points;
		}

		/**
		 * @param rectangle
		 */
		public void setBounds(Rectangle rectangle) {
			bounds = rectangle;
		}

		/**
		 * @param object
		 */
		public void setCell(Object object) {
			cell = object;
		}

		/**
		 * @param string
		 */
		public void setMapID(String string) {
			mapID = string;
		}

		/**
		 * @param list
		 */
		public void setPoints(List list) {
			points = list;
		}

	}

	public static class ConnectionID {

		protected Object cell;

		protected String targetID;

		protected boolean source;

		public ConnectionID(Object cell, String targetID, boolean source) {
			this.cell = cell;
			this.targetID = targetID;
			this.source = source;
		}

		/**
		 * @return
		 */
		public Object getCell() {
			return cell;
		}

		/**
		 * @return
		 */
		public boolean isSource() {
			return source;
		}

		/**
		 * @return
		 */
		public String getTargetID() {
			return targetID;
		}

		/**
		 * @param object
		 */
		public void setCell(Object object) {
			cell = object;
		}

		/**
		 * @param b
		 */
		public void setSource(boolean b) {
			source = b;
		}

		/**
		 * @param string
		 */
		public void setTargetID(String string) {
			targetID = string;
		}

	}


    //
    // Write
    //


	public String toString(GPGraph graph) {
		userObjectMap.clear();
		cellMap.clear();
		attrs.clear();

		String xml = "<gpd-1.0>\n";

		GraphModel model = graph.getModel();
		xml += "<model>\n";
		xml += outputModel(model, "\t", null);
		xml += "</model>\n";

		xml += "<attrs>\n";
		xml += outputAttributes("\t");
		xml += "</attrs>\n";

		xml += "<user>\n";
		xml += encodeUserObjects("\t", userObjectMap);
		xml += "</user>\n";

		xml += "<view>\n";
		xml += outputView(graph, "\t");
		xml += "</view>\n";

		// Close main tags
		xml += "</gpd-1.0>\n";
		return xml;
	}

        public String outputView(GPGraph graph, String indent) {
		String xml = indent + "<a key=\"editable\" val=\""+graph.isEditable()+"\"/>\n"
		    + indent + "<a key=\"bendable\" val=\""+graph.isBendable()+"\"/>\n"
		    + indent + "<a key=\"cloneable\" val=\""+graph.isCloneable()+"\"/>\n"
		    + indent + "<a key=\"connectable\" val=\""+graph.isConnectable()+"\"/>\n"
		    + indent + "<a key=\"disconnectable\" val=\""+graph.isDisconnectable()+"\"/>\n"
		    + indent + "<a key=\"disconnectOnMove\" val=\""+graph.isDisconnectOnMove()+"\"/>\n"
		    + indent + "<a key=\"doubleBuffered\" val=\""+graph.isDoubleBuffered()+"\"/>\n"
		    + indent + "<a key=\"dragEnabled\" val=\""+graph.isDragEnabled()+"\"/>\n"
		    + indent + "<a key=\"dropEnabled\" val=\""+graph.isDropEnabled()+"\"/>\n"
		    + indent + "<a key=\"moveable\" val=\""+graph.isMoveable()+"\"/>\n"
		    + indent + "<a key=\"sizeable\" val=\""+graph.isSizeable()+"\"/>\n"
		    + indent + "<a key=\"selectNewCells\" val=\""+graph.isSelectNewCells()+"\"/>\n"
		    + indent + "<a key=\"gridVisible\" val=\""+graph.isGridVisible()+"\"/>\n"
		    + indent + "<a key=\"gridEnabled\" val=\""+graph.isGridEnabled()+"\"/>\n"
		    + indent + "<a key=\"gridSize\" val=\""+graph.getGridSize()+"\"/>\n"
		    + indent + "<a key=\"gridMode\" val=\""+graph.getGridMode()+"\"/>\n"
		    + indent + "<a key=\"scale\" val=\""+graph.getScale()+"\"/>\n"
		    + indent + "<a key=\"antiAlias\" val=\""+graph.isAntiAliased()+"\"/>\n";
		return xml;
        }

	public String outputModel(GraphModel model, String indent, Object parent) {
		String xml = new String("");
		int max =
			(parent != null)
				? model.getChildCount(parent)
				: model.getRootCount();
		for (int i = 0; i < max; i++) {
			Object cell =
				(parent != null)
					? model.getChild(parent, i)
					: model.getRootAt(i);
			if (cell != null)
				xml += outputCell(indent, model, cell);
		}
		return xml;
	}

	public String outputCell(String indent, GraphModel model, Object cell) {
		Map map = new Hashtable(model.getAttributes(cell));
		Rectangle r = (Rectangle) map.remove(GraphConstants.BOUNDS);
		Object value = map.remove(GraphConstants.VALUE);
		if (GraphConstants.getFont(map).equals(GraphConstants.defaultFont))
			map.remove(GraphConstants.FONT);
		Object source = model.getSource(cell);
		Object target = model.getTarget(cell);
		if (GraphConstants.getRouting(map) != null)
		    map.remove(GraphConstants.POINTS);
		String sourceID = "";
		String targetID = "";
		if (source != null)
			sourceID = " src=\"" + getID(source) + "\"";
		if (source != null)
			targetID = " tgt=\"" + getID(target) + "\"";
		String bounds = "";
		String valueS = "";
		if (r != null && !model.isEdge(cell) && !model.isPort(cell) && !r.equals(DefaultGraphCell.defaultBounds))
			bounds = " rect=\"" + encodeValue(r) + "\"";
		List p = GraphConstants.getPoints(map);
		map.remove(GraphConstants.POINTS);
		String points = "";
		if (p != null && !p.equals(DefaultEdge.defaultPoints)) {
			String tmp = encodeValue(p);
			if (tmp.length() > 0)
			    points = " pts=\"" + tmp + "\"";
		}
		if (value != null)
			valueS = " val=\"" + getUserObjectID(value) + "\"";
		String attrID = "";
		if (map.size() > 0)
			attrID = " attr=\"" + attrCol.addMap(map) + "\"";
		String xml =
			new String(
				indent
					+ "<a class=\""
					+ getType(cell)
					+ "\" id=\""
					+ getID(cell)
					+ "\""
					+ valueS
					+ sourceID
					+ targetID
					+ bounds
					+ points
					+ attrID);
		if (model.getChildCount(cell) > 0)
			xml += ">\n"
				+ outputModel(model, indent + "\t", cell)
				+ indent
				+ "</a>\n";
		else
			xml += "/>\n";
		return xml;
	}

	public int getUserObjectID(Object object) {
		Integer index = (Integer) userObjectMap.get(object);
		if (index != null)
			return index.intValue();
		index = new Integer(userObjectMap.size() + 1);
		userObjectMap.put(object, index);
		return index.intValue();
	}

	public int getID(Object object) {
		Integer index = (Integer) cellMap.get(object);
		if (index != null)
			return index.intValue();
		index = new Integer(cellMap.size() + 1);
		cellMap.put(object, index);
		return index.intValue();
	}

	public String outputAttributes(String indent) {
		Set set = new HashSet();
		set.add(PARENT);
		set.add(GraphConstants.BOUNDS);
		set.add(GraphConstants.POINTS);

		String xml = new String();
		for (int i = 0; i < attrCol.maps.size(); i++) {
			Map map = (Map) attrCol.maps.get(i);
			Object hook = map.get(PARENT);
			String hookS = "";
			if (hook != null)
				hookS = " pid=\"" + attrCol.maps.indexOf(hook) + "\"";
			xml += indent + "<map id=\"" + i + "\"" + hookS + ">\n";
			xml += encodeMap(indent + "\t", map, false, set, false);
			xml += indent + "</map>\n";
		}
		return xml;
	}

	public class AttributeCollection {

		public List maps = new LinkedList();

		public int addMap(Map attr) {
			Iterator it = maps.iterator();
			Map storeMap = new Hashtable(attr);
			Map hook = storeMap;
			while (it.hasNext()) {
				Map ref = (Map) it.next();
				Map diff = diffMap(ref, attr);
				if (diff.size() < storeMap.size()) {
					hook = ref;
					storeMap = diff;
				}
			}
			if (storeMap.size() == 0 && hook != storeMap)
				return maps.indexOf(hook);
			if (hook != storeMap)
				storeMap.put(PARENT, hook);
			maps.add(storeMap);
			return maps.indexOf(storeMap);
		}

		public void clear() {
			maps.clear();
		}

		/**
		 * Returns a new map that contains all (key, value)-pairs
		 * of <code>newState</code> where either key is not used
		 * or value is different for key in <code>oldState</code>.
		 * In other words, this method removes the common entries
		 * from oldState and newState, and returns the "difference"
		 * between the two.
		 *
		 * This method never returns null.
		 */
		public Map diffMap(Map oldState, Map newState) {
			// Augment oldState
			Stack s = new Stack();
			s.add(oldState);
			Object hook = oldState.get(PARENT);
			while (hook instanceof Map) {
				s.add(hook);
				hook = ((Map) hook).get(PARENT);
			}
			oldState = new Hashtable();
			while (!s.isEmpty()) {
				oldState.putAll((Map) s.pop());
			}
			Map diff = new Hashtable();
			Iterator it = newState.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				Object key = entry.getKey();
				Object oldValue = oldState.remove(key);
				if (key != PARENT) {
					Object newValue = entry.getValue();
					if (oldValue == null || !oldValue.equals(newValue))
						diff.put(key, newValue);
				}
			}
			it = oldState.keySet().iterator();
			while (it.hasNext()) {
				Object key = it.next();
				if (!oldState.get(key).equals(""))
					diff.put(key, "");
			}
			diff.remove(PARENT);
			return diff;
		}

	}


    //
    // Codec
    //

⌨️ 快捷键说明

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