View Javadoc

1   /*
2    * $Id: Link.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.sitegraph.model;
19  
20  import java.io.IOException;
21  
22  /***
23   */
24  public class Link implements Render, Comparable {
25      public static final int TYPE_FORM = 0;
26      public static final int TYPE_ACTION = 1;
27      public static final int TYPE_HREF = 2;
28      public static final int TYPE_RESULT = 3;
29      public static final int TYPE_REDIRECT = 4;
30  
31      private SiteGraphNode from;
32      private SiteGraphNode to;
33      private int type;
34      private String label;
35  
36      public Link(SiteGraphNode from, SiteGraphNode to, int type, String label) {
37          this.from = from;
38          this.to = to;
39          this.type = type;
40          this.label = label;
41      }
42  
43      public void render(IndentWriter writer) throws IOException {
44          writer.write(from.getFullName() + " -> " + to.getFullName() + " [label=\"" + getRealLabel() + "\"" + getColor() + "];");
45      }
46  
47      private String getRealLabel() {
48          switch (type) {
49              case TYPE_ACTION:
50                  return "action" + label;
51              case TYPE_FORM:
52                  return "form" + label;
53              case TYPE_HREF:
54                  return "href" + label;
55              case TYPE_REDIRECT:
56                  return "redirect: " + label;
57              case TYPE_RESULT:
58                  return label;
59          }
60  
61          return "";
62      }
63  
64      private String getColor() {
65          if (type == TYPE_RESULT || type == TYPE_ACTION) {
66              return ",color=\"darkseagreen2\"";
67          } else {
68              return "";
69          }
70      }
71  
72      public int compareTo(Object o) {
73          Link other = (Link) o;
74          int result = from.compareTo(other.from);
75          if (result != 0) {
76              return result;
77          }
78  
79          result = to.compareTo(other.to);
80          if (result != 0) {
81              return result;
82          }
83  
84          result = label.compareTo(other.label);
85          if (result != 0) {
86              return result;
87          }
88  
89          return new Integer(type).compareTo(new Integer(other.type));
90      }
91  }