// File: ActionSave.java // Classes: ActionSave // Original Author: Kedar Patankar // Date 21 Jan 1997 package uci.graphedit; import java.io.FilenameFilter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Vector; import java.util.Enumeration; import java.util.Hashtable; import java.awt.FileDialog; import java.awt.Point; import demeter.Ident; /** * Action to take the graph structure and write it to a file selected by user */ public class ActionSave extends Action implements FilenameFilter { public ActionSave(Editor editor,Document d) {super(editor,d); } public String name() { return "Save Graph Structure"; } public boolean accept(File dir,String name) { return name.endsWith(".gcd"); } public void doIt() { if(_document.isSaved()&&(!_document.firstTime())) return; String firstName=_document.getDocName(); if(firstName==null) return; String filename=firstName; if (_document.firstTime()) { FileDialog fd = new FileDialog(_editor, "Select Output File", FileDialog.SAVE ); fd.setDirectory(_editor.getIoDir()); fd.setFilenameFilter(this); fd.setFile(firstName); fd.show(); filename = fd.getFile(); if (filename == null) {return;} filename=fd.getDirectory()+filename; int a = filename.lastIndexOf(".*.*"); if (a>0) filename = filename.substring(0,a); if (!(filename.endsWith(".gcd"))) filename = filename + ".gcd"; _document.firstTime(false); _editor.updateDocname(firstName,filename); } System.out.println("Saving file "+filename); UGraph graph=new UGraph(); Vector nodes = _document.net().nodes(); Enumeration elm = nodes.elements(); Hashtable hTable=new Hashtable(); while (elm.hasMoreElements()) { NetNode n=(NetNode)elm.nextElement(); Point p=n.get_Perspective().position(); Coordinates c=new Coordinates(new X(new Integer(p.x)),new Y(new Integer(p.y))); String name=n.get_Perspective().get_first().get_label(); UID uid=new UID(new Integer(n.getId())); UVertex u; if (n.get_Perspective().get_first() instanceof FigAltVert) u=new UAltVertex(); else if (n.outSize()==0) u=new UTerm(); else u=new UConstVertex(); u.set_id(uid); u.set_vertexname(new UVertexName(new Ident(name))); u.set_position(c); Vector in=n.inList(); Enumeration inEdges = in.elements(); while(inEdges.hasMoreElements()) { UID id=new UID(new Integer(((NetArc)inEdges.nextElement()).getId())); u.add_incoming(id); } Vector out=n.outList(); Enumeration outEdges = out.elements(); while(outEdges.hasMoreElements()) { UID id=new UID(new Integer(((NetArc)outEdges.nextElement()).getId())); u.add_outgoing(id); } hTable.put(name,u); graph.add_uvertex(u); } Enumeration elm1 = nodes.elements(); while (elm1.hasMoreElements()) { NetNode n=(NetNode)elm1.nextElement(); String source=n.get_Perspective().get_first().get_label(); Vector out=n.outList(); Enumeration outEdges = out.elements(); while(outEdges.hasMoreElements()) { NetArc na=(NetArc)(outEdges.nextElement()); ArcPerspective ap=na.get_perspective(); Fig f=ap.get_figure(); String dest=na.destNode().get_Perspective().get_first().get_label(); UID id=new UID(new Integer(na.getId())); UEdge u; if (f instanceof FigConstEdge) { u=new UConstEdge(); ((UConstEdge)u).set_edgename(new UEdgeName(new Ident(f.get_label()))); Cardinality c; String card=((FigConstEdge)f).get_cardinality(); if (card.equals("1")) c=new Cardinality(new Lower(new Integer(1)),null); else if (card.equals("0..1")) c=new Cardinality(new Lower(new Integer(0)), new Upper(new String("1"))); else if (card.equals("0..*")) c=new Cardinality(new Lower(new Integer(0)), new Upper(new String("*"))); else c=new Cardinality(new Lower(new Integer(1)), new Upper(new String("*"))); ((UConstEdge)u).set_card(c); } else u=new UAltEdge(); u.set_id(id); if(hTable.get(source)==null) {System.out.println("problem source");return;} if(hTable.get(dest)==null) {System.out.println("problem");return;} u.set_fromVertex((UVertex)hTable.get(source)); u.set_toVertex((UVertex)hTable.get(dest)); graph.add_uedge(u); } } if(graph!=null) { String graphString=graph.GetGraphString(); try { FileOutputStream out = new FileOutputStream(filename); byte[] buf = new byte[graphString.length()]; graphString.getBytes(0,graphString.length(),buf,0); out.write(buf); out.close(); }catch (IOException eb){eb.printStackTrace();} } _document.isSaved(true); } public void undoIt() { } } /* end class ActionSave */