001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.server.web.utils;
020
021import java.util.ArrayList;
022import java.util.List;
023
024/**
025 * This is just a helper class to make it a lot easier to add the data onto the
026 * treeview in html form the data gets sent to one of these first.
027 *
028 * @author Antonio
029 */
030public class TreeNode<T>
031{
032        private String name; // the name of this node
033        private T data; // some data attached to it
034
035        private TreeNode<T> parent;
036        private List<TreeNode<T>> children;
037
038        public TreeNode(String name, T data, TreeNode<T> parent)
039        {
040                this.name = name;
041                this.data = data;
042
043                this.parent = parent;
044                this.children = new ArrayList<TreeNode<T>>();
045        }
046
047        /**
048         * @return the name
049         */
050        public String getName()
051        {
052                return name;
053        }
054
055        /**
056         * @param name the name to set
057         */
058        public void setName(String name)
059        {
060                this.name = name;
061        }
062
063        /**
064         * @return the data
065         */
066        public T getData()
067        {
068                return data;
069        }
070
071        /**
072         * @param data the data to set
073         */
074        public void setData(T data)
075        {
076                this.data = data;
077        }
078
079        /**
080         * @return the parent
081         */
082        public TreeNode<T> getParent()
083        {
084                return parent;
085        }
086
087        /**
088         * @return the child count
089         */
090        public int getChildCount()
091        {
092                return children.size();
093        }
094
095        /**
096         * Returns the i-child.
097         *
098         * @param i the index of the child to get.
099         * @return a TreeNode object or null if i is not a valid index.
100         */
101        public TreeNode<T> getChild(int i)
102        {
103                // if it's not a valid index return null
104                if ((i < 0) || (i >= children.size()))
105                        return null;
106
107                return children.get(i);
108        }
109
110        /**
111         * Returns the child with the specified name.
112         *
113         * @param name the name of the child to get.
114         * @return a TreeNode object or null if the name is not a valid/found child.
115         */
116        public TreeNode<T> getChild(String name)
117        {
118                // if there are no children
119                if (children.size() < 1)
120                        return null;
121
122                for (TreeNode<T> child : children)
123                {
124                        if (child.getName().equals(name))
125                                return child;
126                }
127
128                // child not found
129                return null;
130        }
131
132        /**
133         * Adds a child to this node.
134         *
135         * @param name the name of the child.
136         * @param data the data of the child.
137         * @return the child inserted.
138         */
139        public TreeNode<T> addChild(String name, T data)
140        {
141                TreeNode<T> child = new TreeNode<T>(name, data, this);
142                this.children.add(child);
143                return child;
144        }
145
146        /**
147         * Based on a path-alike set of dir entries, returns the final node of it.
148         * If the path doesn't exist, it's created.
149         *
150         * @param names a set of TreeNode entries.
151         * @return the last TreeNode of the path.
152         */
153        public TreeNode<T> getTreeNode(String... names)
154        {
155                TreeNode<T> node = this;
156
157                for (String aName : names)
158                {
159                        // get the child node with this name was found
160                        TreeNode<T> child = node.getChild(aName);
161
162                        // if it was not found the create it
163                        if (child == null)
164                        {
165                                child = node.addChild(aName, null);
166                        }
167
168                        // now that we are absolutely sure that the child node exists move onto it, so we can continuing parsing the next part of the "path"
169                        node = child;
170                }
171
172                return node;
173        }
174}