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.tobago.example.demo;
21  
22  import org.apache.myfaces.tobago.internal.util.StringUtils;
23  import org.apache.myfaces.tobago.model.TreePath;
24  
25  import javax.swing.tree.DefaultMutableTreeNode;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  public class NavigationNode extends DefaultMutableTreeNode implements Comparable<NavigationNode> {
30  
31    private final String name;
32    private final String label;
33    private final String branch;
34    private final String outcome;
35  
36    private NavigationTree tree;
37  
38    /** Cache the TreePath for optimization. */
39    private TreePath treePath;
40  
41    public NavigationNode(final String path, final NavigationTree tree) {
42  
43      this.tree = tree;
44      outcome = path;
45      final Pattern pattern = Pattern.compile("(.*)/([^/]*)\\.(xhtml)");
46  //      final Pattern pattern = Pattern.compile("([\\d\\d/]*\\d\\d[^/]*)/([^/]*)\\.(xhtml)");
47      final Matcher matcher = pattern.matcher(path);
48      matcher.find();
49      branch = matcher.group(1);
50      name = matcher.group(2);
51  //    final String extension = matcher.group(3);
52      label = StringUtils.firstToUpperCase(name.replaceAll("[_]", " "));
53    }
54  
55    @Override
56    public int compareTo(final NavigationNode other) {
57      return branch.compareTo(other.getBranch());
58    }
59  
60    public String action() {
61      tree.gotoNode(this);
62      return outcome;
63    }
64  
65    public void evaluateTreePath() {
66      treePath = new TreePath(this);
67    }
68  
69    @Override
70    public NavigationNode getNextNode() {
71      return (NavigationNode) super.getNextNode();
72    }
73  
74    @Override
75    public NavigationNode getPreviousNode() {
76      return (NavigationNode) super.getPreviousNode();
77    }
78  
79    public String getName() {
80      return name;
81    }
82  
83    public String getBranch() {
84      return branch;
85    }
86  
87    public String getLabel() {
88      return label;
89    }
90  
91    public String getOutcome() {
92      return outcome;
93    }
94  
95    public TreePath getTreePath() {
96      return treePath;
97    }
98  
99    @Override
100   public String toString() {
101     return outcome;
102   }
103 
104 }