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.model.ExpandedState;
23  import org.apache.myfaces.tobago.model.SelectedState;
24  import org.apache.myfaces.tobago.model.TreePath;
25  import org.apache.myfaces.tobago.model.TreeState;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.annotation.PostConstruct;
30  import javax.enterprise.context.SessionScoped;
31  import javax.enterprise.event.Observes;
32  import javax.faces.context.FacesContext;
33  import javax.inject.Inject;
34  import javax.inject.Named;
35  import java.io.Serializable;
36  import java.lang.invoke.MethodHandles;
37  
38  @SessionScoped
39  @Named
40  public class NavigationState implements Serializable {
41  
42    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
43  
44    @Inject
45    private NavigationTree tree;
46  
47    private NavigationNode currentNode;
48  
49    private TreeState state = new TreeState(new ExpandedState(1), new SelectedState());
50  
51    private boolean viewSource = false;
52  
53    @PostConstruct
54    public void init() {
55      currentNode = tree.findByViewId(FacesContext.getCurrentInstance().getViewRoot().getViewId());
56      initState();
57    }
58  
59    private void initState() {
60      if (currentNode != null) {
61        final TreePath treePath = currentNode.getTreePath();
62        state.getSelectedState().clearAndSelect(treePath);
63        if (!treePath.isRoot()) {
64        state.getExpandedState().collapseAllButRoot();
65          state.getExpandedState().expand(treePath, true);
66        }
67      }
68    }
69  
70    public NavigationNode getCurrentNode() {
71      return currentNode;
72    }
73  
74    public String gotoFirst() {
75      return gotoNode(tree.getTree().getNextNode()) + "?faces-redirect=true"; // the first after the root node
76    }
77  
78    public String gotoPrevious() {
79      if (currentNode == null) {
80        return gotoFirst();
81      } else {
82        final NavigationNode previousNode = currentNode.getPreviousNode();
83        if (previousNode != null) {
84          return gotoNode(previousNode);
85        } else {
86          LOG.warn("Strange navigation behavior");
87          return null;
88        }
89      }
90    }
91  
92    public String gotoNext() {
93      if (currentNode == null) {
94        return gotoFirst();
95      } else {
96        final NavigationNode nextNode = currentNode.getNextNode();
97        if (nextNode != null) {
98          return gotoNode(nextNode);
99        } else {
100         LOG.warn("Strange navigation behavior");
101         return null;
102       }
103     }
104   }
105 
106   public String gotoNode(@Observes final NavigationNode node) {
107     if (node == null) {
108       return gotoFirst();
109     } else {
110       if (!node.equals(currentNode)) {
111         currentNode = node;
112         LOG.info("Navigate to '" + currentNode.getOutcome() + "'");
113       }
114       initState();
115       return currentNode.getOutcome();
116     }
117   }
118 
119   public boolean isFirst() {
120     if (currentNode == null) {
121       return false;
122     }
123     final NavigationNode previousNode = currentNode.getPreviousNode();
124     return previousNode == null || previousNode.isRoot();
125   }
126 
127   public boolean isLast() {
128     if (currentNode == null) {
129       return false;
130     }
131     final NavigationNode nextNode = currentNode.getNextNode();
132     return nextNode == null;
133   }
134 
135   public TreeState getState() {
136     return state;
137   }
138 
139   public String toggleViewSource() {
140     viewSource = !viewSource;
141     return null;
142   }
143 
144   public boolean isViewSource() {
145     return viewSource;
146   }
147 
148   public void setViewSource(final boolean viewSource) {
149     this.viewSource = viewSource;
150   }
151 }