Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/graph/impl/DirectedEdgeFactory.java


1   //
2   // Copyright(c) 2002, Chris Leung
3   //
4   
5   package com.port80.graph.impl;
6   
7   import java.awt.Color;
8   import java.util.Comparator;
9   
10  import com.port80.graph.GraphException;
11  import com.port80.graph.IEdge;
12  import com.port80.graph.IEdgeFactory;
13  import com.port80.graph.IGraph;
14  import com.port80.graph.IVertex;
15  import com.port80.graph.dot.impl.RouteFactory;
16  import com.port80.util.msg;
17  import com.port80.util.attr.AttrRegistry;
18  import com.port80.util.attr.AttrTable;
19  import com.port80.util.attr.BooleanAttrFactory;
20  import com.port80.util.attr.ColorFactory;
21  import com.port80.util.attr.DoubleAttrFactory;
22  import com.port80.util.attr.IntAttrFactory;
23  import com.port80.util.attr.RectFactory;
24  import com.port80.util.attr.StringAttrFactory;
25  
26  /** 
27   * Factory class that produce edges for directed graph.The factory
28   * also provide an IAttrTable interface for accessing default
29   * attributes and method to get the IAttrRegistry interface for
30   * client to check for unqiue attribute names.
31   * 
32   * 'weight' and 'xpenalty' are supposed to be initialized from the graph description.
33   * If they are not specified, default values are used by DotEdge and VirutalEdge ... etc.
34   * (The attribute values stay undefined).
35   * 
36   * DotEdge should use "weight_default" and "weight_critical" in the EdgeFactory table.
37   * VirtualEdge use "position_weight_default", "position_weight_stub", "position_weight_critical" ... etc.
38   */
39  public class DirectedEdgeFactory extends AttrTable implements IEdgeFactory {
40  
41    private static final String NAME = "DirectedEdgeFactory";
42  
43    // Constructors ////////////////////////////////////////////////////////
44    //
45  
46    public DirectedEdgeFactory() {
47      super(null, new AttrRegistry());
48      //
49      // Setup registry.
50      //
51      StringAttrFactory aString = StringAttrFactory.getInstance();
52      BooleanAttrFactory aBoolean = BooleanAttrFactory.getInstance();
53      IntAttrFactory aInt = IntAttrFactory.getInstance();
54      DoubleAttrFactory aDouble = DoubleAttrFactory.getInstance();
55      RectFactory aRect = RectFactory.getInstance();
56      ColorFactory aColor = ColorFactory.getInstance();
57      StrokeFactory aStroke = StrokeFactory.getInstance();
58      ArrowFactory aArrow = ArrowFactory.getInstance();
59      RouteFactory aRoute = RouteFactory.getInstance();
60      //
61      super.initAttr("label", aString);
62      super.initAttr("weight", aInt);
63      super.initAttr("xpenalty", aInt);
64      super.initAttr("minlen", aDouble);
65      super.initAttr("style", aString, "solid");
66      super.initAttr("color", aColor, Color.BLACK);
67      super.initAttr("fontname", "Verdana");
68      super.initAttr("fontsize", 12.0);
69      super.initAttr("fontcolor", aColor, Color.BLACK);
70      //
71      super.initAttr("headarrow", aString, "normal");
72      super.initAttr("tailarrow", aString);
73      super.initAttr("pos", aString);
74      //
75      // These are used by dot but not used or implemented here.
76      //
77      super.initAttr("arrowsize", aDouble);
78      super.initAttr("tailclip", true);
79      super.initAttr("headclip", true);
80      super.initAttr("decorate", aBoolean);
81      super.initAttr("headlabel", aString);
82      super.initAttr("taillabel", aString);
83      super.initAttr("labelangle", aDouble);
84      super.initAttr("labeldistance", aDouble);
85      super.initAttr("portlabeldistance", aDouble);
86      super.initAttr("samehead", false);
87      super.initAttr("sametail", false);
88      // dot arrow direction attribute, not used here.
89      // Arrow direction is implicit from the edge object.
90      super.initAttr("dir", aString);
91      super.initAttr("constraint", aBoolean); /** false=not used for ranking. */
92      super.initAttr("layer", aString);
93      //
94      // Addition to dot.
95      //
96      super.initAttr("critical", false);
97      super.initAttr("weight_default", 1);
98      super.initAttr("weight_critical", 8);
99      super.initAttr("linewidth", aInt); // Line width in pixels.
100     super.initAttr("debug", aBoolean); // Debug this edge.
101     //
102     // Transient attribute use by LayoutManager.
103     //
104     super.initAttr("-bounds", aRect);
105     super.initAttr("-style", aStroke);
106     super.initAttr("-headarrow", aArrow);
107     super.initAttr("-tailarrow", aArrow);
108     super.initAttr("-pos", aRoute);
109   }
110 
111   // IAttrTable interface ////////////////////////////////////////////////
112   //
113 
114   /**
115    * Factory tables are read only. Override setAttr methods to warn user.
116    */
117 
118   public Object setAttrFromString(String name, String value) {
119     msg.err(NAME + ".setAttrFromString(): Factory table is read only: attrname=" + name);
120     return null;
121   }
122   public Object setAttr(String name, Object value) {
123     msg.err(NAME + ".setAttr(Object): Factory table is read only: attrname=" + name);
124     return null;
125   }
126   public Object setAttr(String name, boolean value) {
127     msg.err(NAME + ".setAttr(boolean): Factory table is read only: attrname=" + name);
128     return null;
129   }
130   public Object setAttr(String name, int value) {
131     msg.err(NAME + ".setAttr(int): Factory table is read only: attrname=" + name);
132     return null;
133   }
134   public Object setAttr(String name, long value) {
135     msg.err(NAME + ".setAttr(long): Factory table is read only: attrname=" + name);
136     return null;
137   }
138   public Object setAttr(String name, float value) {
139     msg.err(NAME + ".setAttr(float): Factory table is read only: attrname=" + name);
140     return null;
141   }
142   public Object setAttr(String name, double value) {
143     msg.err(NAME + ".setAttr(double): Factory table is read only: attrname=" + name);
144     return null;
145   }
146   public Object setAttrFromString(String name, double value) {
147     msg.err(NAME + ".setAttr(double): Factory table is read only: attrname=" + name);
148     return null;
149   }
150   public Object removeAttr(String name) {
151     msg.err(NAME + ".removeAttr(String): Factory table is read only: attrname=" + name);
152     return null;
153   }
154   public void removeUnregistered() {
155     msg.err(NAME + ".removeUnregistered(): Factory table is read only");
156   }
157   public void clearAttrs() {
158     msg.err(NAME + ".clearAttrs(): Factory table is read only");
159   }
160 
161   // IAttrFactory interface //////////////////////////////////////////////
162   //
163 
164   public IEdge newEdge(IVertex tail, IVertex head, String name, Object data, IGraph parent)
165     throws GraphException {
166     return new Edge(tail, head, name, data, parent);
167   }
168 
169   ////////////////////////////////////////////////////////////////////////
170 
171   public static class EdgeRenderingComparator implements Comparator {
172     public int compare(Object a, Object b) {
173       IEdge ea = (IEdge) a;
174       IEdge eb = (IEdge) b;
175       boolean ac = ea.getAttrBool("critical");
176       boolean bc = eb.getAttrBool("critical");
177       if (ac && !bc)
178         return -1;
179       if (!ac && bc)
180         return 1;
181       return ea.getName().compareTo(eb.getName());
182     }
183   }
184 
185   ////////////////////////////////////////////////////////////////////////
186 }