| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 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 | |
import org.kuali.student.lum.common.client.lo.OutlineNode; |
| 23 | |
|
| 24 | |
public |
| 25 | 0 | class OutlineNodeModel<T> { |
| 26 | 0 | private ArrayList<OutlineNode<T>> outlineNodeList = new ArrayList<OutlineNode<T>>(); |
| 27 | |
|
| 28 | 0 | private ArrayList<ChangeHandler> changeHandlerList = new ArrayList<ChangeHandler>(); |
| 29 | |
|
| 30 | |
private OutlineNode<T> currentNode; |
| 31 | |
|
| 32 | |
public void clearNodes(){ |
| 33 | 0 | outlineNodeList = new ArrayList<OutlineNode<T>>(); |
| 34 | 0 | } |
| 35 | |
public void setCurrentNode(OutlineNode<T> aNode) { |
| 36 | 0 | currentNode = aNode; |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
public void addChangeHandler(ChangeHandler ch) { |
| 40 | 0 | changeHandlerList.add(ch); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
private void fireChangeEvents() { |
| 44 | 0 | for (ChangeHandler ch : changeHandlerList) { |
| 45 | 0 | ch.onChange(null); |
| 46 | |
} |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
public void moveUpCurrent() { |
| 50 | 0 | if (this.isMoveUpable() == false) { |
| 51 | 0 | return; |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 0 | List<OutlineNode<T>> siblingList = getSiblingList(); |
| 58 | 0 | int indexInSibling = siblingList.indexOf(currentNode); |
| 59 | 0 | OutlineNode<T> nextNodeInSibling = siblingList.get(indexInSibling - 1); |
| 60 | |
|
| 61 | 0 | List<OutlineNode<T>> childList = getChildList(currentNode); |
| 62 | 0 | childList.add(0, currentNode); |
| 63 | 0 | for (int i = 0; i < childList.size(); i++) { |
| 64 | 0 | OutlineNode<T> aNode = childList.get(i); |
| 65 | 0 | outlineNodeList.remove(aNode); |
| 66 | |
} |
| 67 | |
|
| 68 | 0 | int moveToIndex = outlineNodeList.indexOf(nextNodeInSibling); |
| 69 | |
|
| 70 | 0 | for (int i = 0; i < childList.size(); i++) { |
| 71 | 0 | OutlineNode<T> aNode = childList.get(i); |
| 72 | 0 | outlineNodeList.add(moveToIndex + i, aNode); |
| 73 | |
} |
| 74 | 0 | fireChangeEvents(); |
| 75 | 0 | } |
| 76 | |
public void moveDownCurrent() { |
| 77 | 0 | if (this.isMoveDownable() == false) { |
| 78 | 0 | return; |
| 79 | |
} |
| 80 | 0 | int index = outlineNodeList.indexOf(currentNode); |
| 81 | 0 | if (index == -1 || index == outlineNodeList.size() - 1) { |
| 82 | 0 | return; |
| 83 | |
} |
| 84 | 0 | List<OutlineNode<T>> siblingList = getSiblingList(); |
| 85 | 0 | int indexInSibling = siblingList.indexOf(currentNode); |
| 86 | 0 | OutlineNode<T> nextNodeInSibling = siblingList.get(indexInSibling + 1); |
| 87 | |
|
| 88 | 0 | List<OutlineNode<T>> childList = getChildList(currentNode); |
| 89 | 0 | childList.add(0, currentNode); |
| 90 | 0 | for (int i = 0; i < childList.size(); i++) { |
| 91 | 0 | OutlineNode<T> aNode = childList.get(i); |
| 92 | 0 | outlineNodeList.remove(aNode); |
| 93 | |
} |
| 94 | |
|
| 95 | 0 | List<OutlineNode<T>> nextNodeChildList = getChildList(nextNodeInSibling); |
| 96 | 0 | int moveToIndex = -1; |
| 97 | 0 | if(nextNodeChildList.size() != 0){ |
| 98 | 0 | moveToIndex = outlineNodeList.indexOf(nextNodeChildList.get(nextNodeChildList.size()-1)); |
| 99 | |
}else{ |
| 100 | 0 | moveToIndex =outlineNodeList.indexOf(nextNodeInSibling); |
| 101 | |
} |
| 102 | |
|
| 103 | 0 | for (int i = 0; i < childList.size(); i++) { |
| 104 | 0 | OutlineNode<T> aNode = childList.get(i); |
| 105 | 0 | outlineNodeList.add(moveToIndex + 1 + i, aNode); |
| 106 | |
} |
| 107 | |
|
| 108 | 0 | fireChangeEvents(); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
public void indentCurrent() { |
| 112 | 0 | if (this.isIndentable()) { |
| 113 | 0 | currentNode.indent(); |
| 114 | 0 | fireChangeEvents(); |
| 115 | |
} |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public void outdentCurrent() { |
| 119 | 0 | if (this.isOutdentable()) { |
| 120 | 0 | List<OutlineNode<T>> childList = getChildList(currentNode); |
| 121 | 0 | childList.add(0, currentNode); |
| 122 | 0 | for(OutlineNode<T> aNode:childList){ |
| 123 | 0 | aNode.outdent(); |
| 124 | |
} |
| 125 | 0 | fireChangeEvents(); |
| 126 | |
} |
| 127 | 0 | } |
| 128 | |
|
| 129 | |
public void deleteCurrent() { |
| 130 | 0 | if (this.isDeletable()) { |
| 131 | 0 | List<OutlineNode<T>> childList = getChildList(currentNode); |
| 132 | 0 | childList.add(0, currentNode); |
| 133 | 0 | for (int i = 0; i < childList.size(); i++) { |
| 134 | 0 | OutlineNode<T> aNode = childList.get(i); |
| 135 | 0 | outlineNodeList.remove(aNode); |
| 136 | |
} |
| 137 | 0 | fireChangeEvents(); |
| 138 | |
} |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
public void addPeer() { |
| 142 | 0 | int index = outlineNodeList.indexOf(currentNode); |
| 143 | 0 | OutlineNode<T> aNode = new OutlineNode<T>(); |
| 144 | |
|
| 145 | 0 | aNode.setIndentLevel(currentNode.getIndentLevel()); |
| 146 | 0 | outlineNodeList.add(index + 1, aNode); |
| 147 | 0 | fireChangeEvents(); |
| 148 | 0 | } |
| 149 | |
|
| 150 | |
public void addChild() { |
| 151 | 0 | int index = outlineNodeList.indexOf(currentNode); |
| 152 | 0 | OutlineNode<T> aNode = new OutlineNode<T>(); |
| 153 | |
|
| 154 | 0 | aNode.setIndentLevel(currentNode.getIndentLevel() + 1); |
| 155 | 0 | outlineNodeList.add(index + 1, aNode); |
| 156 | 0 | fireChangeEvents(); |
| 157 | 0 | } |
| 158 | |
|
| 159 | |
public void addOutlineNode(OutlineNode<T> aNode) { |
| 160 | 0 | outlineNodeList.add(aNode); |
| 161 | 0 | } |
| 162 | |
|
| 163 | |
@SuppressWarnings("unchecked") |
| 164 | |
public OutlineNode<T>[] toOutlineNodes() { |
| 165 | 0 | return outlineNodeList.toArray(new OutlineNode[outlineNodeList.size()]); |
| 166 | |
} |
| 167 | |
|
| 168 | |
public List<OutlineNode<T>> getOutlineNodes() { |
| 169 | 0 | return outlineNodeList; |
| 170 | |
} |
| 171 | |
|
| 172 | |
public List<OutlineNode<T>> getChildList(OutlineNode<T> aNode) { |
| 173 | 0 | int index = outlineNodeList.indexOf(aNode); |
| 174 | 0 | List<OutlineNode<T>> childList = new ArrayList<OutlineNode<T>>(); |
| 175 | 0 | for (int i = index + 1; i < outlineNodeList.size(); i++) { |
| 176 | 0 | if (outlineNodeList.get(i).getIndentLevel() > aNode.getIndentLevel()) { |
| 177 | 0 | childList.add(outlineNodeList.get(i)); |
| 178 | |
} else { |
| 179 | |
break; |
| 180 | |
} |
| 181 | |
} |
| 182 | 0 | return childList; |
| 183 | |
} |
| 184 | |
|
| 185 | |
public List<OutlineNode<T>> getSiblingList() { |
| 186 | 0 | List<OutlineNode<T>> siblingList = new ArrayList<OutlineNode<T>>(); |
| 187 | 0 | int index = outlineNodeList.indexOf(currentNode); |
| 188 | |
|
| 189 | 0 | if (currentNode.getIndentLevel() == 0) { |
| 190 | 0 | for (int i = 0; i < outlineNodeList.size(); i++) { |
| 191 | 0 | if (outlineNodeList.get(i).getIndentLevel() == 0) { |
| 192 | 0 | siblingList.add(outlineNodeList.get(i)); |
| 193 | |
} |
| 194 | |
} |
| 195 | 0 | return siblingList; |
| 196 | |
} |
| 197 | |
|
| 198 | |
|
| 199 | 0 | OutlineNode<T> parentNode = null; |
| 200 | 0 | for (int i = index - 1; i >= 0; i--) { |
| 201 | 0 | if (outlineNodeList.get(i).getIndentLevel() - currentNode.getIndentLevel() == -1) { |
| 202 | 0 | parentNode = outlineNodeList.get(i); |
| 203 | 0 | break; |
| 204 | |
} |
| 205 | |
} |
| 206 | 0 | int parentIndex = outlineNodeList.indexOf(parentNode); |
| 207 | 0 | for (int i = parentIndex + 1; i < outlineNodeList.size(); i++) { |
| 208 | 0 | if (outlineNodeList.get(i).getIndentLevel() - parentNode.getIndentLevel() == 1) { |
| 209 | 0 | siblingList.add(outlineNodeList.get(i)); |
| 210 | 0 | } else if (outlineNodeList.get(i).getIndentLevel() == parentNode.getIndentLevel()) { |
| 211 | 0 | break; |
| 212 | |
} |
| 213 | |
} |
| 214 | 0 | return siblingList; |
| 215 | |
} |
| 216 | |
|
| 217 | |
public boolean isIndentable() { |
| 218 | 0 | int index = outlineNodeList.indexOf(currentNode); |
| 219 | 0 | if (index == 0) { |
| 220 | 0 | return false; |
| 221 | |
} |
| 222 | |
|
| 223 | |
|
| 224 | 0 | List<OutlineNode<T>> siblingList = getSiblingList(); |
| 225 | 0 | if (siblingList.size() == 1) { |
| 226 | 0 | return false; |
| 227 | |
} |
| 228 | |
|
| 229 | 0 | int indexInSiblings = siblingList.indexOf(currentNode); |
| 230 | 0 | if(indexInSiblings == 0){ |
| 231 | 0 | return false; |
| 232 | |
} |
| 233 | |
|
| 234 | 0 | return true; |
| 235 | |
} |
| 236 | |
|
| 237 | |
public boolean isOutdentable() { |
| 238 | 0 | if (currentNode.getIndentLevel() == 0) { |
| 239 | 0 | return false; |
| 240 | |
} |
| 241 | 0 | return true; |
| 242 | |
} |
| 243 | |
|
| 244 | |
public boolean isMoveUpable() { |
| 245 | 0 | List<OutlineNode<T>> list = getSiblingList(); |
| 246 | 0 | if (list.size() == 1) { |
| 247 | 0 | return false; |
| 248 | |
} |
| 249 | 0 | int index = list.indexOf(currentNode); |
| 250 | 0 | if (index == 0) { |
| 251 | 0 | return false; |
| 252 | |
} |
| 253 | 0 | return true; |
| 254 | |
} |
| 255 | |
|
| 256 | |
public boolean isMoveDownable() { |
| 257 | 0 | List<OutlineNode<T>> list = getSiblingList(); |
| 258 | 0 | if (list.size() == 0) { |
| 259 | 0 | return false; |
| 260 | |
} |
| 261 | 0 | int index = list.indexOf(currentNode); |
| 262 | 0 | if (index == list.size() - 1) { |
| 263 | 0 | return false; |
| 264 | |
} |
| 265 | 0 | return true; |
| 266 | |
} |
| 267 | |
|
| 268 | |
public boolean isDeletable() { |
| 269 | 0 | if(outlineNodeList.size() == 1){ |
| 270 | 0 | return false; |
| 271 | |
} |
| 272 | 0 | return true; |
| 273 | |
} |
| 274 | |
} |