1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.ports;
15  
16  import java.util.Map;
17  
18  import y.base.YCursor;
19  import y.base.YList;
20  import y.geom.YPoint;
21  import y.io.gml.DefaultParserFactory;
22  import y.io.gml.GraphParser;
23  import y.io.gml.ItemParser;
24  import y.io.gml.LineParser;
25  import y.io.gml.GMLTokenizer.Callback;
26  import y.view.Graph2D;
27  import y.view.ImageNodeRealizer;
28  import y.view.NodeRealizer;
29  import y.view.Port;
30  
31  
32  /** This class is an implementation of the ParserFactory interface.
33   * This implementation is capable of parsing the basic gml features.
34   * The work is delegated to instances of GraphParser, NodeParser and EdgeParser
35   */
36  public class ParserFactory extends DefaultParserFactory
37  {
38    public Callback createNodeParser(Graph2D graph, Callback graphParser)
39    {
40      GraphParser gp = (GraphParser) graphParser;
41      ItemParser parser = new NodeParser(graph, gp.getId2Node());
42      return parser;
43    }
44    
45    private static final class NodeParser extends y.io.gml.NodeParser {
46      
47      private LineParser lineParser;
48      
49      NodeParser(Graph2D graph, Map id2NodeMap){
50        super(graph, id2NodeMap);
51        this.nodeGraphics.addChild("PortCandidates", lineParser = new LineParser());
52      }
53      
54      /** this method is called when the parsing of the node section
55       * has ended. This implementation calls
56       * <CODE>super.end()</CODE> and then tries to set the additional
57       * FixedPortsNodeRealizer attributes: paintingPorts and portCandidates
58       */  
59      public void end(){
60        super.end();
61        NodeRealizer item = (NodeRealizer) nodeGraphics.getItem();
62        if (item instanceof ImageNodeRealizer){ // maybe fixedportnr
63          FixedPortsNodeRealizer fpnr = new FixedPortsNodeRealizer(item);
64          fpnr.setPaintingPorts(nodeGraphics.getBoolean("paintingPorts"));
65          YList list = fpnr.getPortCandidates();
66          list.clear();
67          for (YCursor c = lineParser.getPointList().cursor(); c.ok(); c.next()){
68            YPoint p = (YPoint) c.current();
69            list.add(new Port(p.getX(), p.getY()));
70          }
71          graph.setRealizer(this.nr.getNode(), fpnr);
72        }
73        
74      }
75    }
76  }
77  
78