// File: ActionOpen.java // Classes: ActionOpen // Original Author: Kedar Patankar - Alberto Medina // Date 21 Jan 1997 package uci.graphedit; import java.io.FileInputStream; import java.io.FilenameFilter; import java.io.File; import java.io.FileNotFoundException; import java.awt.FileDialog; import java.util.Vector; import java.util.Enumeration; /** * Action to open a file and reading the sentence that it holds, creating * the correponding graph structutre. */ public class ActionOpen extends Action implements FilenameFilter { private Document _document; private int _idd=0; public ActionOpen(Editor editor) {super(editor);} public String name() { return "Open file with input Object"; } public boolean accept(File dir,String name){return name.endsWith(".cd");} public void doIt() { FileInputStream in; FileDialog fd = new FileDialog(_editor, "Select Input File", FileDialog.LOAD); fd.setFilenameFilter(this); fd.setDirectory(_editor.getIoDir()); fd.show(); String filename = fd.getFile(); if (filename == null) {return;} filename=fd.getDirectory()+filename; if(_editor.isPresent(filename)) { _editor.showDocument(filename); return; } System.out.println("Opening file " + filename); try {in = new FileInputStream(filename);} catch (FileNotFoundException de){return ;} UGraph _graph; if (filename.endsWith(".gcd")) { try { _graph = UGraph.parse(in); in.close(); }catch (Exception fe) { System.out.println("Error reading file "+filename); return; } System.out.println("Finished building Graph"); } else if (filename.endsWith(".cd")) { Program p; try { p = Program.parse(in); in.close(); }catch (Exception fe) { System.out.println("Error reading file "+filename); return; } System.out.println("Finished Building Program"); _graph = p.print_edges(); System.out.println("Finished Building Graph"); } else { System.out.println("Invalid file type");return;} _document=new Document(_editor); _document.setDocName(filename); _editor.attach(filename,_document); /* Traverse trough the graph and read all the infromation required for drawing the vertices on the screen*/ VertexContainer vc = _graph.GetAllVertices(); System.out.println("Finished traversing the vertex list"); addConstructionVertices(vc); addAlternationVertices(vc); /* Traverse trough the graph and read all the infromation required for drawing the edges on the screen */ EdgeContainer ec = _graph.GetAllEdges(); addConstructionEdges(ec); addAlternationEdges(ec); /* After adding all the elements to view and the net list draw them */ // _document.damageAll(); // _document.repairDamage(); _document.isSaved(true); _document.setId(_idd); } private void addConstructionVertices(VertexContainer VC) { Vector construction_vertices = VC.get_construction(); int a = construction_vertices.size(); System.out.println("Construction vertices read "+a); buildAndaddVertices(construction_vertices,"uci.graphedit.ConstClass"); } private void addAlternationVertices(VertexContainer VC) { Vector alternation_vertices = VC.get_alternation(); int a = alternation_vertices.size(); System.out.println("Alternation vertices read "+a); buildAndaddVertices(alternation_vertices,"uci.graphedit.AltClass"); } private void buildAndaddVertices(Vector v,String className) { NetNode newNode; Class _nodeClass; String _nodeClassName=className; Enumeration elm = v.elements(); try { _nodeClass = Class.forName(_nodeClassName); } catch (java.lang.ClassNotFoundException ignore) { System.out.println("Class " + _nodeClassName + " could not be found!"); return; } while (elm.hasMoreElements()) { try { newNode = (NetNode) _nodeClass.newInstance(); } catch (java.lang.IllegalAccessException ignore) { return; } catch (java.lang.InstantiationException ignore) { return; } VertexInfo vf=(VertexInfo)elm .nextElement(); int id=vf.get_id().get_id().intValue(); _idd=Math.max(_idd,id); newNode.initialize(vf,_document,id); Perspective defaultPerspective = newNode.get_Perspective(); _document.add(defaultPerspective); } } private void addConstructionEdges(EdgeContainer EC) { Vector construction_edges = EC.get_construction(); int a = construction_edges.size(); System.out.println("Construction edges read "+a); buildAndaddEdges(construction_edges); } private void addAlternationEdges(EdgeContainer EC) { Vector alternation_edges = EC.get_alternation(); int a = alternation_edges.size(); System.out.println("Alternation edges read "+a); buildAndaddEdges(alternation_edges); } private void buildAndaddEdges(Vector v) { Enumeration elm=v.elements(); while (elm.hasMoreElements()) { EdgeInfo edgeinfo= (EdgeInfo)elm.nextElement(); /** Extracting the information from EdgeInfo */ int id=edgeinfo.get_id().get_id().intValue(); _idd=Math.max(_idd,id); int from=edgeinfo.get_from().get_id().intValue(); int to=edgeinfo.get_to().get_id().intValue(); String edge_name= edgeinfo.get_name(); String cardinality = edgeinfo.get_card(); /** Get the source and destination vertices so that their height and width can be obtained */ Perspective sourcePerspective= (Perspective)_document.pick(from); Perspective destPerspective= (Perspective)_document.pick(to); if(sourcePerspective==null) { System.out.println("sors null");return;} if(destPerspective==null) { System.out.println("dest null");return;} NetNode startNode= (NetNode)sourcePerspective.owner(); NetNode destNode= (NetNode)destPerspective.owner(); NetArc newArc= new NetArc(); if(startNode==null){ System.out.println("start Node is null");return;} if(destNode==null){ System.out.println("dst Node is null"); return;} newArc.connect(startNode, destNode); newArc.setId(id); ArcPerspective ap; if(edge_name==null) ap= new ArcPerspective(newArc); else ap= new ArcPerspective(newArc,edge_name,cardinality); _document.add(ap); ap.reorder(ActionReorder.SEND_TO_BACK, _document.view()); } } public void undoIt() {} } /* end class ActionOpen */