📄 graphedx.java
字号:
}
public void about() {
}
public boolean writeXMLObject(JGraph obj, String fileName){
String url="rmi://localhost:2099/wkfservices";
boolean _return=false;
try {
Object o = java.rmi.Naming.lookup(url);
Services services = (Services) o;
services.writeXMLObject(obj, fileName);
_return = true;
}
catch (java.rmi.NotBoundException nbe) {
nbe.printStackTrace();
}
catch (java.rmi.RemoteException re) {
re.printStackTrace();
}
catch (java.net.MalformedURLException ure) {
ure.printStackTrace();
}
return _return;
}
public void saveFile() {
// int returnValue = JFileChooser.CANCEL_OPTION;
// initFileChooser();
// returnValue = fileChooser.showSaveDialog(graph);
// if (returnValue == JFileChooser.APPROVE_OPTION) {
XMLEncoder encoder;
Container parent = graph.getParent();
try {
String tmpstr = JOptionPane.showInputDialog("请输入保存的文件名", "name");
String urlstr = Constants.URLSTR+tmpstr;
// url = new URL(getCodeBase(), tmpstr);
// urlc = url.openConnection();
// urlc.setUseCaches(false);
// os = new FileOutputStream
// urlc.connect();
// os = new FileOutputStream("C:\\Tomcat 5.0\\webapps\\1010027\\aa.xml");
// URL url = new URL(urlstr);
// URLConnection con = url.openConnection();
// con.setUseCaches(false);
// OutputStream out = con.getOutputStream();
// ObjectOutputStream objStream;
// objStream = new ObjectOutputStream(out);
// album = (Album)objStream.readObject();
// return album;
String serviceUrl="http://127.0.0.1:8080/1010027/basicservice";
uninstallListeners(graph);
parent.remove(graph);
System.out.println("开始初始化远程接口");
writeXMLObject(graph,tmpstr);
// HessianProxyFactory factory = new HessianProxyFactory();
// Basic basic = (Basic) factory.create(Basic.class, serviceUrl);
// System.out.println("开始调用远程接口");
// basic.setXMLFileName(graph,tmpstr);
// basic.setUserName(graph, tmpstr);
// System.out.println("写对象数据成功");
// encoder = new XMLEncoder(new BufferedOutputStream(objStream));
// configureEncoder(encoder);
// encoder.writeObject(graph);
// encoder.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(graph, e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
} finally {
// Adds the component back into the component hierarchy
if (parent instanceof JViewport) {
JViewport viewPort = (JViewport) parent;
viewPort.setView(graph);
} else {
// Best effort...
parent.add(graph);
}
// And reinstalls the listener
installListeners(graph);
}
// }
}
public void openFile() {
// int returnValue = JFileChooser.CANCEL_OPTION;
// System.out.println("begin initfilechooser");
// initFileChooser();
// System.out.println("end initfilechooser");
// returnValue = fileChooser.showOpenDialog(graph);
// if (returnValue == JFileChooser.APPROVE_OPTION) {
Container parent = graph.getParent();
BasicMarqueeHandler marqueeHandler = graph.getMarqueeHandler();
try {
String urlstr = JOptionPane.showInputDialog("请输入XML的文件名", "URL");
url = new URL(Constants.URLSTR+urlstr);
urlc = url.openConnection();
urlc.connect();
is = urlc.getInputStream();
uninstallListeners(graph);
parent.remove(graph);
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(is));
graph = (JGraph) decoder.readObject();
// Take the marquee handler from the original graph and
// use it in the new graph as well.
graph.setMarqueeHandler(marqueeHandler);
// Adds the component back into the component hierarchy
if (parent instanceof JViewport) {
JViewport viewPort = (JViewport) parent;
viewPort.setView(graph);
} else {
// Best effort...
parent.add(graph);
}
initTable(graph);
graph.getModel().addGraphModelListener(new GraphModelListener() {
public void graphChanged(GraphModelEvent e) {
GraphModelEvent.GraphModelChange c = e.getChange();
System.out.println("graph change");
if (c.getRemoved() == null && c.getInserted() == null) {
Map previousAttributes = c.getPreviousAttributes();
Set keySet = previousAttributes.keySet();
Iterator iter = keySet.iterator();
Object attribute_prev=null;
while (iter.hasNext()) {
attribute_prev = iter.next();
System.out.println("Prev Key " + String.valueOf(attribute_prev));
Object value = c.getPreviousAttributes().get(attribute_prev);
System.out.println("\t" + String.valueOf(value));
}
Map attributes = c.getAttributes();
keySet = attributes.keySet();
iter = keySet.iterator();
Vector ver_table = new Vector();
while (iter.hasNext()) {
Object attribute = iter.next();
System.out.println("Curr Key " + String.valueOf(attribute));
Object value = c.getAttributes().get(attribute);
System.out.println("\t" + String.valueOf(value));
String valuestr = String.valueOf(value);
String newstr = "";
if(valuestr!=null){
newstr = valuestr.substring(valuestr.indexOf("=")+1,valuestr.length()-1);
}
if(attribute!=null){
updatecellvalue(attribute, newstr);
}
}
System.out.println("\n\n");
}
}
});
installListeners(graph);
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(graph, e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}catch(MalformedURLException mfe){
JOptionPane.showMessageDialog(null,"url,异常为: " + mfe.getMessage());
}catch(IOException ioe){
JOptionPane.showMessageDialog(null,"IO Exception! " + ioe.getMessage());
}
// }
}
/**
* Utility method that ensures the file chooser is created. Start-up time
* is improved by lazily instaniating choosers.
*
*/
protected void initFileChooser() {
if (fileChooser == null) {
System.out.println("begin new jfilechooser");
fileChooser = new JFileChooser("c:\\");
System.out.println("end new jfilechooser");
FileFilter 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(".xml"))
return true;
if (f.getName().endsWith(".ser"))
return true;
if (f.isDirectory())
return true;
return false;
}
/**
* @see javax.swing.filechooser.FileFilter#getDescription()
*/
public String getDescription() {
return "打开文件 (.xml, .ser)";
}
};
fileChooser.setFileFilter(fileFilter);
}
}
public void configureEncoder(XMLEncoder encoder) {
// Better debugging output, in case you need it
encoder.setExceptionListener(new ExceptionListener() {
public void exceptionThrown(Exception e) {
e.printStackTrace();
}
});
encoder.setPersistenceDelegate(DefaultGraphModel.class,
new DefaultPersistenceDelegate(new String[] { "roots",
"attributes" }));
encoder.setPersistenceDelegate(MyGraphModel.class,
new DefaultPersistenceDelegate(new String[] { "roots",
"attributes" }));
// Note: In the static initializer the marquee handler of the
// MyGraph class is made transient to avoid being written out.
encoder.setPersistenceDelegate(MyGraph.class,
new DefaultPersistenceDelegate(new String[] { "model",
"graphLayoutCache" }));
encoder
.setPersistenceDelegate(GraphLayoutCache.class,
new DefaultPersistenceDelegate(new String[] { "model",
"factory", "cellViews", "hiddenCellViews",
"partial" }));
encoder.setPersistenceDelegate(DefaultGraphCell.class,
new DefaultPersistenceDelegate(new String[] { "userObject" }));
encoder.setPersistenceDelegate(DefaultEdge.class,
new DefaultPersistenceDelegate(new String[] { "userObject" }));
encoder.setPersistenceDelegate(DefaultPort.class,
new DefaultPersistenceDelegate(new String[] { "userObject" }));
encoder.setPersistenceDelegate(AbstractCellView.class,
new DefaultPersistenceDelegate(new String[] { "cell",
"attributes" }));
encoder.setPersistenceDelegate(DefaultEdge.DefaultRouting.class,
new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance,
Encoder out) {
return new Expression(oldInstance,
GraphConstants.class, "getROUTING_SIMPLE", null);
}
});
encoder.setPersistenceDelegate(DefaultEdge.LoopRouting.class,
new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance,
Encoder out) {
return new Expression(oldInstance,
GraphConstants.class, "getROUTING_DEFAULT",
null);
}
});
encoder.setPersistenceDelegate(JGraphShadowBorder.class,
new PersistenceDelegate() {
protected Expression instantiate(Object oldInstance,
Encoder out) {
return new Expression(oldInstance,
JGraphShadowBorder.class, "getSharedInstance",
null);
}
});
encoder.setPersistenceDelegate(ArrayList.class, encoder
.getPersistenceDelegate(List.class));
}
/**
* Main method
*/
public static void main(String[] args) {
try {
// Construct Frame
System.out.println("now is here!");
JFrame frame = new JFrame("流程编辑");
// Set Close Operation to Exit
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Fetch URL to Icon Resource
URL jgraphUrl = GraphEdX.class.getClassLoader().getResource(
"com/softwarematch/tools/resources/jgraph.gif");
// If Valid URL
if (jgraphUrl != null) {
// Load Icon
ImageIcon jgraphIcon = new ImageIcon(jgraphUrl);
// Use in Window
frame.setIconImage(jgraphIcon.getImage());
}
// Add an Editor Panel
System.out.println("now is here2!");
GraphEdX app = new GraphEdX();
frame.getContentPane().add(app);
app.init();
// Set Default Size
frame.setSize(640, 480);
// Show Frame
frame.setVisible(true);
System.out.println("now is here33!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Makes all fields but <code>cell</code> and <code>attributes</code>
* transient in the bean info of <code>clazz</code>.
*
* @param clazz
* The cell view class who fields should be made transient.
*/
public static void makeCellViewFieldsTransient(Class clazz) {
try {
BeanInfo info = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] propertyDescriptors = info
.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; ++i) {
PropertyDescriptor pd = propertyDescriptors[i];
if (!pd.getName().equals("cell")
&& !pd.getName().equals("attributes")) {
pd.setValue("transient", Boolean.TRUE);
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
/**
* Encodable graph model with related constructor. Note: This class must be
* static for the XML encoding to work.
*/
public static class MyGraphModel extends DefaultGraphModel {
public MyGraphModel() {
super();
}
public MyGraphModel(List roots, AttributeMap attributes) {
super(roots, attributes);
}
public boolean acceptsSource(Object edge, Object port) {
return (port != null);
}
public boolean acceptsTarget(Object edge, Object port) {
return (port != null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -