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