📄 defaultgraphmodelfileformatxml.java
字号:
/*
* @(#)DefaultGraphModelFileFormatXML.java 1.0 17.02.2003
*
* Copyright (C) 2003 sven.luzar
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.jgraph.pad;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jgraph.graph.ConnectionSet;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Edge;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.jgraph.pad.resources.Translator;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.jgpd.io.JGpdModelNode;
import org.jgpd.io.ModelImportImpl;
import org.jgpd.io.jbpm.ModelImportJBpm;
import org.jgpd.jgraph.*;
/**File format for the default graph model.
* The file format writes a XML
* file with the graph as content.
*
* @author luzar
* @version 1.0
*/
public class DefaultGraphModelFileFormatXML implements GraphModelFileFormat {
public static final String EMPTY = new String("Empty");
public static final String PARENT = new String("Parent");
protected static Map cells = new Hashtable();
protected static Map attrs = new Hashtable();
protected static Map objs = new Hashtable();
protected static List delayedAttributes = new LinkedList();
protected static List connectionSetIDs = new LinkedList();
protected Map cellMap = new Hashtable();
protected static ModelImportImpl importModel;
protected AttributeCollection attrCol = new AttributeCollection();
protected Map userObjectMap = new Hashtable();
/** file filter for this file format
*/
FileFilter fileFilter;
/** accessory component. A checkbox for the
* zipped output or unzipped output
*
*/
JComponent compZipSelect;
/** a const value for the key at the properties hashtable
*/
public static final String COMPRESS_WITH_ZIP = "CompressWithZip";
/**
* Constructor for DefaultGraphModelFileFormatXML.
*/
public DefaultGraphModelFileFormatXML() {
if (importModel == null )
{
importModel = new ModelImportJBpm(); // FIXME needs to be model independant
}
fileFilter = new FileFilter() {
/**
* @see javax.swing.filechooser.FileFilter#accept(File)
*/
public boolean accept(File f) {
if (f == null)
return false;
if (f.getName() == null)
return false;
if (f.getName().endsWith(".gpd"))
return true;
if (f.isDirectory())
return true;
return false;
}
/**
* @see javax.swing.filechooser.FileFilter#getDescription()
*/
public String getDescription() {
return Translator.getString("FileDesc.JGpdDiagramXml"); /*Finished: Original="JGraphpad Diagram (*.pad_xml)"*/
}
};
compZipSelect = new JCheckBox(Translator.getString("zipCompress"));
}
/** returns <tt>pad_xml</tt>
*/
public String getFileExtension(){
return "gpd";
}
/** Returns a file filter for the <tt>pad_xml</tt> extension.
*
* @see org.jgraph.pad.GraphModelFileFormat#getFileFilter()
*/
public FileFilter getFileFilter() {
return fileFilter;
}
/** Returns null
* @see org.jgraph.pad.GraphModelFileFormat#getReadAccessory()
*/
public JComponent getReadAccessory() {
return null;
}
/** Returns the compZipSelect object.
*
* @see #compZipSelect
* @see org.jgraph.pad.GraphModelFileFormat#getWriteAccessory()
*/
public JComponent getWriteAccessory() {
return null; //compZipSelect;
}
/**
* Writes the graph as XML file
*
* @see org.jgraph.pad.GraphModelFileFormat#write(String, Hashtable, GPGraph, GraphModel)
*/
public void write(
URL file,
Hashtable properties,
GPGraph gpGraph,
GraphModel graphModel)
throws Exception {
// don't try / catch this command
// sothat we get error messages at the
// frontend.
// e.g. I you have not permissions to
// write a file you should get an error message
//try {
OutputStream out = null;
out = new FileOutputStream(file.getFile());
// Ignore
// if (properties != null &&
// properties.get(COMPRESS_WITH_ZIP) != null &&
// ((Boolean)properties.get(COMPRESS_WITH_ZIP)).booleanValue() )
// f = new GZIPOutputStream(f);
out = new BufferedOutputStream(out);
out.write(toString(gpGraph).getBytes());
out.flush();
out.close();
//} catch (Exception e) {
// e.printStackTrace();
//}
}
/**
* Puts the value from the checkbox into the properties hashtable
*
* @see org.jgraph.pad.GraphModelFileFormat#getWriteProperties(JComponent)
*/
public Hashtable getWriteProperties(JComponent accessory) {
Hashtable properties = new Hashtable();
if (!(accessory instanceof JCheckBox)){
return properties;
}
properties.put(COMPRESS_WITH_ZIP, new Boolean(((JCheckBox)accessory).isSelected() ));
return properties;
}
/**Reads the File form the XML input stream.
* Tempts to load from a zipped input stream.
* If this procedure fails the method tempts
* to load without the zipped option.
*
* @see org.jgraph.pad.GraphModelFileFormat#read(String, Hashtable, GPGraph)
*/
public GraphModel read(
URL file,
Hashtable properties,
GPGraph gpGraph)
throws Exception {
// Check file contents for new file format
boolean newFileFormat = false;
try {
InputStream f = file.openStream();
byte[] ident = new byte[4];
int length = f.read(ident);
if (length == 4 && new String(ident).toLowerCase().equals("<gpd"))
newFileFormat = true;
} catch (Exception e) {
// ignore
}
// Read old file format...
if (newFileFormat)
{
// Read as new file
// Copied from above: Use only this in future versions
try
{
read(file.openStream(), gpGraph);
}
catch (Exception ex2)
{
if (file.toString().toLowerCase().startsWith("http") ||
file.toString().toLowerCase().startsWith("ftp") )
{
// do nothing create an empty graph
}
else
{
throw ex2;
}
}
}
return gpGraph.getModel();
}
/**Returns null
*
* @see org.jgraph.pad.GraphModelFileFormat#getReadProperties(JComponent)
*/
public Hashtable getReadProperties(JComponent accessory) {
return null;
}
//
// Read
//
public static void read(InputStream in, GPGraph graph) throws Exception {
GraphModel model = graph.getModel();
// Create a DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// Create a DocumentBuilder
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the input file to get a Document object
Document doc = db.parse(in);
// Get the first child (the gpd-element)
Node modelNode = null;
Node objsNode = null;
Node attrsNode = null;
Node viewNode = null;
for (int i = 0;
i < doc.getDocumentElement().getChildNodes().getLength();
i++) {
Node node = doc.getDocumentElement().getChildNodes().item(i);
if (node.getNodeName().toLowerCase().equals("model")) {
modelNode = node;
} else if (node.getNodeName().toLowerCase().equals("user")) {
objsNode = node;
} else if (node.getNodeName().toLowerCase().equals("attrs")) {
attrsNode = node;
} else if (node.getNodeName().toLowerCase().equals("view")) {
viewNode = node;
}
}
objs = decodeUserObjects(objsNode);
attrs = parseAttrs(attrsNode);
attrs = augmentAttrs(attrs);
Map settings = decodeMap(viewNode, false, false);
ConnectionSet cs = new ConnectionSet();
Hashtable cells = new Hashtable();
DefaultGraphCell[] insert = parseChildren(modelNode, cells, cs);
// Create ConnectionSet
Iterator it = connectionSetIDs.iterator();
while (it.hasNext()) {
ConnectionID cid = (ConnectionID) it.next();
Object cell = cid.getCell();
String tid = cid.getTargetID();
if (tid != null) {
Object port = cells.get(tid);
if (port != null) {
cs.connect(cell, port, cid.isSource());
}
}
}
// Create AttributeMap
Map nested = new Hashtable();
it = delayedAttributes.iterator();
while (it.hasNext()) {
DelayedAttributeID att = (DelayedAttributeID) it.next();
Map attr = (Map) attrs.get(att.getMapID());
if (attr != null) {
attr = GraphConstants.cloneMap(attr);
if (att.getBounds() != null)
GraphConstants.setBounds(attr, att.getBounds());
if (att.getPoints() != null)
GraphConstants.setPoints(attr, att.getPoints());
nested.put(att.getCell(), attr);
}
}
// Apply settings to graph
applySettings(settings, graph);
// Insert the cells (View stores attributes)
model.insert(insert, nested, cs, null, null);
}
public static void applySettings(Map s, GPGraph graph) {
Object tmp;
tmp = s.get("editable");
if (tmp != null)
graph.setEditable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("bendable");
if (tmp != null)
graph.setBendable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("cloneable");
if (tmp != null)
graph.setCloneable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("connectable");
if (tmp != null)
graph.setConnectable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("disconnectable");
if (tmp != null)
graph.setDisconnectable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("disconnectOnMove");
if (tmp != null)
graph.setDisconnectOnMove(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("doubleBuffered");
if (tmp != null)
graph.setDoubleBuffered(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("dragEnabled");
if (tmp != null)
graph.setDragEnabled(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("dropEnabled");
if (tmp != null)
graph.setDropEnabled(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("moveable");
if (tmp != null)
graph.setMoveable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("sizeable");
if (tmp != null)
graph.setSizeable(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("selectNewCells");
if (tmp != null)
graph.setSelectNewCells(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("gridVisible");
if (tmp != null)
graph.setGridVisible(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("gridEnabled");
if (tmp != null)
graph.setGridEnabled(new Boolean(tmp.toString()).booleanValue());
tmp = s.get("gridSize");
if (tmp != null)
graph.setGridSize(Integer.parseInt(tmp.toString()));
tmp = s.get("gridMode");
if (tmp != null)
graph.setGridMode(Integer.parseInt(tmp.toString()));
tmp = s.get("scale");
if (tmp != null)
graph.setScale(Double.parseDouble(tmp.toString()));
tmp = s.get("antiAlias");
if (tmp != null)
graph.setAntiAliased(new Boolean(tmp.toString()).booleanValue());
}
public static Map augmentAttrs(Map attrs) {
Map newAttrs = new Hashtable();
Iterator it = attrs.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Map map = (Map) entry.getValue();
Stack s = new Stack();
s.add(map);
Object parentID = map.get(PARENT);
Object hook = null;
while (parentID != null) {
hook = attrs.get(parentID);
s.add(hook);
parentID = ((Map) hook).get(PARENT);
}
Map newMap = new Hashtable();
while (!s.isEmpty()) {
newMap.putAll((Map) s.pop());
}
newMap.remove(PARENT);
// Remove Empty values
Iterator it2 = newMap.entrySet().iterator();
while (it2.hasNext()) {
entry = (Map.Entry) it2.next();
if (entry.getValue() == EMPTY)
it2.remove();
}
newAttrs.put(key, newMap);
}
return newAttrs;
}
public static DefaultGraphCell parseCell(
Node node,
Hashtable cells,
ConnectionSet cs) {
DefaultGraphCell cell = null;
if (node.getNodeName().toLowerCase().equals("a")) {
Node key = node.getAttributes().getNamedItem("id");
Node type = node.getAttributes().getNamedItem("class");
if (key != null && type != null) {
Node value = node.getAttributes().getNamedItem("val");
Object userObject = "";
if (value != null)
userObject = objs.get(value.getNodeValue());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -