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.example.data.CategoryTree;
23  import org.apache.myfaces.tobago.example.data.Node;
24  
25  import javax.enterprise.context.SessionScoped;
26  import javax.inject.Named;
27  import javax.swing.tree.DefaultMutableTreeNode;
28  import java.io.Serializable;
29  
30  @SessionScoped
31  @Named
32  public class TreeSelectController implements Serializable {
33  
34    private DefaultMutableTreeNode sample;
35    private String selectable = "multi";
36  
37    public TreeSelectController() {
38      sample = CategoryTree.createSample();
39    }
40  
41    public DefaultMutableTreeNode getSample() {
42      return sample;
43    }
44  
45    public String getSelectable() {
46      return selectable;
47    }
48  
49    public void setSelectable(final String selectable) {
50      this.selectable = selectable;
51      resetSelection(sample);
52    }
53  
54    public void resetSelection(final DefaultMutableTreeNode node) {
55      final Node userObject = (Node) node.getUserObject();
56      userObject.setSelected(false);
57      for (int i = 0; i < node.getChildCount(); i++) {
58        final DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
59        resetSelection(child);
60      }
61    }
62  
63    public String getSelectedNodes() {
64      final StringBuilder stringBuilder = new StringBuilder();
65      buildSelectedNodesString(stringBuilder, sample);
66      if (stringBuilder.length() > 2) {
67        return stringBuilder.substring(2); // Remove ', '.
68      } else {
69        return "";
70      }
71    }
72  
73    private void buildSelectedNodesString(final StringBuilder stringBuilder, final DefaultMutableTreeNode node) {
74      final Node userObject = (Node) node.getUserObject();
75      if (userObject.isSelected()) {
76        stringBuilder.append(", " + userObject.getName());
77      }
78      for (int i = 0; i < node.getChildCount(); i++) {
79        final DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
80        buildSelectedNodesString(stringBuilder, child);
81      }
82    }
83  }