1 /*
2 * Created on Jan 19, 2004
3 *
4 * Copyright (C) 2004 Sean Ruff
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 package org.un4gvn.editorviewer.internal.element;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 /***
30 * Abstract IViewerElement Implementation
31 * <p>
32 * @author Sean Ruff
33 */
34
35 public abstract class AbstractViewerElement implements IViewerElement {
36
37 /***
38 * Application data object
39 */
40 protected Object _data = null;
41
42 /***
43 * Map of children IViewerElement objects
44 */
45 protected Map _children = null;
46
47 /***
48 * Parent IViewerElement
49 */
50 protected IViewerElement _parent = null;
51
52 /***
53 * Name of the IViewerElement
54 */
55 protected Object _id = null;
56
57 /***
58 * Constructor
59 * @param name
60 */
61 public AbstractViewerElement(String id){
62 this(null,id);
63 }
64
65 /***
66 * Constructor
67 * @param IViewerElement parent
68 * @param id
69 */
70 public AbstractViewerElement(IViewerElement parent, Object id){
71 _parent = parent;
72 _id = id;
73 _children = new HashMap();
74 }
75
76 /***
77 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#getName()
78 */
79 public Object getId() {
80 return _id;
81 }
82
83 /***
84 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#setData(java.lang.Object)
85 */
86 public void setData(Object data) {
87 _data = data;
88 }
89
90 /***
91 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#getData()
92 */
93 public Object getData() {
94 return _data;
95 }
96
97 /***
98 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#getParent()
99 */
100 public IViewerElement getParent() {
101 return _parent;
102 }
103
104 /***
105 * @see org.un4gvn.editorviewer.internal.IViewerElement#setParent(org.un4gvn.editorviewer.internal.IViewerElement)
106 */
107 public void setParent(IViewerElement parent) {
108 _parent = parent;
109 }
110
111 /***
112 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#hasParent()
113 */
114 public boolean hasParent() {
115 return _parent != null;
116 }
117
118 /***
119 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#hasChildren()
120 */
121 public boolean hasChildren() {
122 return !isLeaf() && _children.size() > 0;
123 }
124
125 /***
126 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#getChildren()
127 */
128 public IViewerElement[] getChildren() {
129 if ( isLeaf() )
130 return new IViewerElement[]{};
131 return (IViewerElement[])_children.values().toArray(new IViewerElement[_children.size()]);
132 }
133
134 /***
135 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#getAllChildren()
136 */
137 public IViewerElement[] getAllChildren(){
138 if ( isLeaf() )
139 return new IViewerElement[]{};
140 IViewerElement[] children = getChildren();
141 List list = new ArrayList();
142 for ( int i = 0 ; i < children.length ; i++ ){
143 list.add(children[i]);
144 IViewerElement[] els = children[i].getAllChildren();
145 list.addAll(Arrays.asList(els));
146 }
147 //the children will be returned with leafs on the end so reverse to
148 //ensure leafs are first so that removal will start at the leafs, just in case
149 //TODO does this really need to be performed
150 Collections.reverse(list);
151 return (IViewerElement[])list.toArray(new IViewerElement[list.size()]);
152 }
153
154 /***
155 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#getChild(java.lang.String)
156 */
157 public IViewerElement getChild(Object id) {
158 return (IViewerElement)_children.get(id);
159 }
160
161 /***
162 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#addChild(org.un4gvn.editorviewer.internal.element.IViewerElement)
163 */
164 public void addChild(IViewerElement el) {
165 if ( isLeaf() )
166 return;
167 _children.put(el.getId(),el);
168 }
169
170 /***
171 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#removeChild(org.un4gvn.editorviewer.internal.element.IViewerElement)
172 */
173 public void removeChild(IViewerElement child) {
174 if ( isLeaf() )
175 return;
176
177 //notify all it's children to remove
178 IViewerElement[] children = child.getChildren();
179 for ( int i = 0 ; i < children.length ; i++ ){
180 child.removeChild(children[i]);
181 }
182 //remove the parent object
183 child.setParent(null);
184 _children.remove(child.getId());
185 }
186
187 /***
188 * @see org.un4gvn.editorviewer.internal.element.IViewerElement#removeChildren()
189 */
190 public void removeChildren(){
191 IViewerElement[] children = getChildren();
192 for ( int i = 0 ; i < children.length ; i++ ){
193 removeChild(children[i]);
194 }
195 }
196
197
198 }
This page was automatically generated by Maven