View Javadoc

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