View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.lum.common.client.lo;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import com.google.gwt.event.dom.client.ChangeHandler;
22  
23  public 
24  class OutlineNodeModel<T> {
25  	private ArrayList<OutlineNode<T>> outlineNodeList = new ArrayList<OutlineNode<T>>();
26  
27  	private final ArrayList<ChangeHandler> changeHandlerList = new ArrayList<ChangeHandler>();
28  
29  	private OutlineNode<T> currentNode;
30  
31  	public void clearNodes(){
32  		outlineNodeList = new ArrayList<OutlineNode<T>>();
33  	}
34  	public void setCurrentNode(OutlineNode<T> aNode) {
35  		currentNode = aNode;
36  	}
37  
38  	public void addChangeHandler(ChangeHandler ch) {
39  		changeHandlerList.add(ch);
40  	}
41  
42  	private void fireChangeEvents() {
43  		for (ChangeHandler ch : changeHandlerList) {
44  			ch.onChange(null);
45  		}
46  	}
47  
48  	public void moveUpCurrent() {
49  		if (this.isMoveUpable() == false) {
50  			return;
51  		}
52  		List<OutlineNode<T>> siblingList = getSiblingList();
53  		int indexInSibling = siblingList.indexOf(currentNode);
54  		OutlineNode<T> nextNodeInSibling = siblingList.get(indexInSibling - 1);
55  
56  		List<OutlineNode<T>> childList = getChildList(currentNode);
57  		childList.add(0, currentNode);// add parent
58  		for (int i = 0; i < childList.size(); i++) {
59  			OutlineNode<T> aNode = childList.get(i);
60  			outlineNodeList.remove(aNode);
61  		}
62  
63  		int moveToIndex = outlineNodeList.indexOf(nextNodeInSibling);
64  
65  		for (int i = 0; i < childList.size(); i++) {
66  			OutlineNode<T> aNode = childList.get(i);
67  			outlineNodeList.add(moveToIndex + i, aNode);
68  		}
69  		fireChangeEvents();
70  	}
71  	public void moveDownCurrent() {
72  		if (this.isMoveDownable() == false) {
73  			return;
74  		}
75  		int index = outlineNodeList.indexOf(currentNode);
76  		if (index == -1 || index == outlineNodeList.size() - 1) {
77  			return;
78  		}
79  		List<OutlineNode<T>> siblingList = getSiblingList();
80  		int indexInSibling = siblingList.indexOf(currentNode);
81  		OutlineNode<T> nextNodeInSibling = siblingList.get(indexInSibling + 1);
82  
83  		List<OutlineNode<T>> childList = getChildList(currentNode);
84  		childList.add(0, currentNode);// add parent
85  		for (int i = 0; i < childList.size(); i++) {
86  			OutlineNode<T> aNode = childList.get(i);
87  			outlineNodeList.remove(aNode);
88  		}
89  
90  		List<OutlineNode<T>> nextNodeChildList = getChildList(nextNodeInSibling);
91  		int moveToIndex = -1;
92  		if(nextNodeChildList.size() != 0){
93  			moveToIndex = outlineNodeList.indexOf(nextNodeChildList.get(nextNodeChildList.size()-1));
94  		}else{
95  			moveToIndex =outlineNodeList.indexOf(nextNodeInSibling);
96  		}
97  
98  		for (int i = 0; i < childList.size(); i++) {
99  			OutlineNode<T> aNode = childList.get(i);
100 			outlineNodeList.add(moveToIndex + 1 + i, aNode);
101 		}
102 
103 		fireChangeEvents();
104 	}
105 
106 	public void indentCurrent() {
107 		if (this.isIndentable()) {
108 			currentNode.indent();
109 			fireChangeEvents();
110 		}
111 	}
112 
113 	public void outdentCurrent() {
114 		if (this.isOutdentable()) {
115 			List<OutlineNode<T>> childList = getChildList(currentNode);
116 			childList.add(0, currentNode);// add parent
117 			for(OutlineNode<T> aNode:childList){
118 				aNode.outdent();
119 			}
120 			fireChangeEvents();
121 		}
122 	}
123 
124 	public void deleteCurrent() {
125 //		if (this.isDeletable()) {
126 			List<OutlineNode<T>> childList = getChildList(currentNode);
127 			childList.add(0, currentNode);// add parent
128 			for (int i = 0; i < childList.size(); i++) {
129 				OutlineNode<T> aNode = childList.get(i);
130 				outlineNodeList.remove(aNode);
131 			}
132 			fireChangeEvents();
133 //		}
134 	}
135 
136 	public void addPeer() {
137 		int index = outlineNodeList.indexOf(currentNode);
138 		OutlineNode<T> aNode = new OutlineNode<T>();
139 		// aNode.setUserObject(new TextBox());
140 		aNode.setIndentLevel(currentNode.getIndentLevel());
141 		outlineNodeList.add(index + 1, aNode);
142 		fireChangeEvents();
143 	}
144 
145 	public void addChild() {
146 		int index = outlineNodeList.indexOf(currentNode);
147 		OutlineNode<T> aNode = new OutlineNode<T>();
148 		// aNode.setUserObject(new TextBox());
149 		aNode.setIndentLevel(currentNode.getIndentLevel() + 1);
150 		outlineNodeList.add(index + 1, aNode);
151 		fireChangeEvents();
152 	}
153 
154 	public void addOutlineNode(OutlineNode<T> aNode) {
155 		outlineNodeList.add(aNode);
156 	}
157 
158 	@SuppressWarnings("unchecked")
159 	public OutlineNode<T>[] toOutlineNodes() {
160 		return outlineNodeList.toArray(new OutlineNode[outlineNodeList.size()]);
161 	}
162 
163 	public List<OutlineNode<T>> getOutlineNodes() {
164 		return outlineNodeList;
165 	}
166 
167 	public List<OutlineNode<T>> getChildList(OutlineNode<T> aNode) {
168 		int index = outlineNodeList.indexOf(aNode);
169 		List<OutlineNode<T>> childList = new ArrayList<OutlineNode<T>>();
170 		for (int i = index + 1; i < outlineNodeList.size(); i++) {
171 			if (outlineNodeList.get(i).getIndentLevel() > aNode.getIndentLevel()) {
172 				childList.add(outlineNodeList.get(i));
173 			} else {
174 				break;
175 			}
176 		}
177 		return childList;
178 	}
179 
180 	public List<OutlineNode<T>> getSiblingList() {
181 		List<OutlineNode<T>> siblingList = new ArrayList<OutlineNode<T>>();
182 		int index = outlineNodeList.indexOf(currentNode);
183 		// if first level
184 		if (currentNode.getIndentLevel() == 0) {
185 			for (int i = 0; i < outlineNodeList.size(); i++) {
186 				if (outlineNodeList.get(i).getIndentLevel() == 0) {
187 					siblingList.add(outlineNodeList.get(i));
188 				}
189 			}
190 			return siblingList;
191 		}
192 		// not first level
193 		// get parent first and then get all Siblings
194 		OutlineNode<T> parentNode = null;
195 		for (int i = index - 1; i >= 0; i--) {
196 			if (outlineNodeList.get(i).getIndentLevel() - currentNode.getIndentLevel() == -1) {
197 				parentNode = outlineNodeList.get(i);
198 				break;
199 			}
200 		}
201 		int parentIndex = outlineNodeList.indexOf(parentNode);
202 		for (int i = parentIndex + 1; i < outlineNodeList.size(); i++) {
203 			if (outlineNodeList.get(i).getIndentLevel() - parentNode.getIndentLevel() == 1) {
204 				siblingList.add(outlineNodeList.get(i));
205 			} else if (outlineNodeList.get(i).getIndentLevel() == parentNode.getIndentLevel()) {
206 				break;
207 			}
208 		}
209 		return siblingList;
210 	}
211 
212 	public boolean isIndentable() {
213 		int index = outlineNodeList.indexOf(currentNode);
214 		if (index == 0) {
215 			return false;
216 		}
217 		// if current node is the only child
218 
219 		List<OutlineNode<T>> siblingList = getSiblingList();
220 		if (siblingList.size() == 1) {
221 			return false;
222 		}
223 		//first kid
224 		int indexInSiblings = siblingList.indexOf(currentNode);
225 		if(indexInSiblings == 0){
226 			return false;
227 		}
228 
229 		return true;
230 	}
231 
232 	public boolean isOutdentable() {
233 		if (currentNode.getIndentLevel() == 0) {// first level
234 			return false;
235 		}
236 		return true;
237 	}
238 
239 	public boolean isMoveUpable() {
240 		List<OutlineNode<T>> list = getSiblingList();
241 		if (list.size() == 1) { // only child
242 			return false;
243 		}
244 		int index = list.indexOf(currentNode);
245 		if (index == 0) {// first child
246 			return false;
247 		}
248 		return true;
249 	}
250 
251 	public boolean isMoveDownable() {
252 		List<OutlineNode<T>> list = getSiblingList();
253 		if (list.size() == 0) { // only child
254 			return false;
255 		}
256 		int index = list.indexOf(currentNode);
257 		if (index == list.size() - 1) {// last child
258 			return false;
259 		}
260 		return true;
261 	}
262 
263 	public boolean isDeletable() {
264 //		if(outlineNodeList.size() == 1){
265 //			return false;
266 //		}
267 		return true;
268 	}
269 }