View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.custom.tree2;
21  
22  import java.util.List;
23  import java.util.ArrayList;
24  
25  public class TreeNodeBase implements TreeNode, Comparable
26  {
27      private static final long serialVersionUID = 278589014441538822L;
28      private List children = new ArrayList();
29      private String type;
30      private String description;
31      private boolean leaf;
32      private String identifier;
33  
34      public TreeNodeBase()
35      {}
36  
37      public TreeNodeBase(String type, String description, boolean leaf)
38      {
39          this(type, description, null, leaf);
40      }
41  
42      public TreeNodeBase(String type, String description, String identifier, boolean leaf)
43      {
44          this.type = type;
45          this.description = description;
46          this.identifier = identifier;
47          this.leaf = leaf;
48      }
49  
50      public boolean isLeaf()
51      {
52          return leaf || (getChildCount() == 0);
53      }
54  
55      public void setLeaf(boolean leaf)
56      {
57          this.leaf = leaf;
58      }
59  
60      public List getChildren()
61      {
62          return children;
63      }
64  
65      public String getType()
66      {
67          return type;
68      }
69  
70      public void setType(String type)
71      {
72          this.type = type;
73      }
74  
75      public void setDescription(String description)
76      {
77          this.description = description;
78      }
79  
80      public String getDescription()
81      {
82          return description;
83      }
84  
85      public void setIdentifier(String identifier)
86      {
87          this.identifier = identifier;
88      }
89  
90      public String getIdentifier()
91      {
92          return identifier;
93      }
94  
95      public int getChildCount()
96      {
97          return getChildren().size();
98      }
99  
100     public int compareTo(Object obj)
101     {
102         // branches come before leaves, after this criteria nodes are sorted alphabetically
103         TreeNode otherNode = (TreeNode)obj;
104 
105         if (isLeaf() && !otherNode.isLeaf())
106         {
107             // leaves come after branches
108             return 1;
109         }
110         else if (!isLeaf() && otherNode.isLeaf())
111         {
112             // branches come before leaves
113             return -1;
114         }
115         else
116         {
117             // both nodes are leaves or both node are branches, so compare the descriptions
118             return getDescription().compareTo(otherNode.getDescription());
119         }
120     }
121 }