| 1 | package tmcsim.cadmodels; |
|---|
| 2 | |
|---|
| 3 | import java.util.LinkedList; |
|---|
| 4 | |
|---|
| 5 | import org.w3c.dom.Document; |
|---|
| 6 | import org.w3c.dom.Element; |
|---|
| 7 | import org.w3c.dom.Node; |
|---|
| 8 | |
|---|
| 9 | import tmcsim.common.ScriptException; |
|---|
| 10 | import tmcsim.common.CADEnums.CADScreenNum; |
|---|
| 11 | import tmcsim.common.CADEnums.CADScreenType; |
|---|
| 12 | import tmcsim.common.CADProtocol.CAD_COMMANDS; |
|---|
| 13 | import tmcsim.common.CADProtocol.DATA_TAGS; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * RoutedMessageModel is a CADScreenModel object containing data that is |
|---|
| 18 | * displayed in the RoutedMessage CAD Screen. Data included in this |
|---|
| 19 | * object includes a list of CADRoutedMessages, the index of the current |
|---|
| 20 | * message item, and a boolean flag to designate whether the current message |
|---|
| 21 | * has been deleted. The addModelObject() method is used to update the model |
|---|
| 22 | * data with new information. The nextQueue(), deleteQueue(), and prevQueue() |
|---|
| 23 | * methods are used to control which message is the "current" message. <br/> |
|---|
| 24 | * <br/> |
|---|
| 25 | * This element parses and creates the following XML schema in its toXML() and |
|---|
| 26 | * fromXML() methods. The ROOT element is the parameter for those methods. |
|---|
| 27 | * See the class description for the CADScreenModel XML schema.<br/> |
|---|
| 28 | * <ROOT> |
|---|
| 29 | * <ROUTED_MESSAGE> |
|---|
| 30 | * <BASE_MODEL_INFO/> |
|---|
| 31 | * <ORIGIN/> |
|---|
| 32 | * <DESTINATION/> |
|---|
| 33 | * <MESSAGE/> |
|---|
| 34 | * </ROUTED_MESSAGE> |
|---|
| 35 | * </ROOT> |
|---|
| 36 | * |
|---|
| 37 | * @see CADScreenModel |
|---|
| 38 | * @author Matthew Cechini |
|---|
| 39 | * @version |
|---|
| 40 | */ |
|---|
| 41 | public class RoutedMessageModel extends CADScreenModel { |
|---|
| 42 | |
|---|
| 43 | /** LinkedList containing received routed messages. */ |
|---|
| 44 | private LinkedList<CADRoutedMessage> messageList = null; |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * A manually incremented and decremented iterating count. Initial value |
|---|
| 48 | * is 0 to point to the first element in the messageList. As the list is |
|---|
| 49 | * traversed, added to, and removed from, this count always contains the value |
|---|
| 50 | * of the current message that is being displayed. |
|---|
| 51 | */ |
|---|
| 52 | private int currentListItem = 0; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Flag to designate whether the current message has been deleted by the |
|---|
| 56 | * user. The currentListItem counter is not updated when a queue is |
|---|
| 57 | * deleted until nextQueue() or prevQueue() is called. This allows a |
|---|
| 58 | * message to remain on the screen after it has been deleted. |
|---|
| 59 | */ |
|---|
| 60 | private boolean messageDeleted = false; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Constructor. |
|---|
| 64 | * |
|---|
| 65 | * @param num CADScreenNum for this model. |
|---|
| 66 | * @param messages New list of messags. |
|---|
| 67 | */ |
|---|
| 68 | public RoutedMessageModel(CADScreenNum num, LinkedList<CADRoutedMessage> messages) { |
|---|
| 69 | super(CADScreenType.TO_ROUTED_MESSAGE, num); |
|---|
| 70 | |
|---|
| 71 | messageList = new LinkedList<CADRoutedMessage>(); |
|---|
| 72 | messageList.addAll(messages); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Constructor. Parse the model data from the parameter node. |
|---|
| 77 | * |
|---|
| 78 | * @param newNode XML node containing model data. |
|---|
| 79 | * @throws ScriptException if there is an error in parsing the node. |
|---|
| 80 | */ |
|---|
| 81 | public RoutedMessageModel(Node newNode) throws ScriptException { |
|---|
| 82 | super(CADScreenType.TO_ROUTED_MESSAGE, CADScreenNum.ONE); |
|---|
| 83 | |
|---|
| 84 | messageList = new LinkedList<CADRoutedMessage>(); |
|---|
| 85 | |
|---|
| 86 | fromXML(newNode); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Returns the number of routed messages received to this CAD Client model. |
|---|
| 91 | * |
|---|
| 92 | * @return int number of messages that have been sent to this terminal and |
|---|
| 93 | * retreived from the coordinator. |
|---|
| 94 | */ |
|---|
| 95 | public int getMessageCount() { |
|---|
| 96 | return messageList.size(); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Append a new message to the local linked list of CADRoutedMessages. |
|---|
| 101 | * |
|---|
| 102 | * @param updateMessages Vector of CADRoutedMessage objects that have been |
|---|
| 103 | * sent to this CAD terminal. |
|---|
| 104 | */ |
|---|
| 105 | public void addModelObject(Object o) { |
|---|
| 106 | messageList.add((CADRoutedMessage)o); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Method returns the routed message that was last viewed, or if this is the first |
|---|
| 111 | * time a method is being viewed, then the first message will be returned. |
|---|
| 112 | * This message may only be called if the numberOfMessages() returns a value |
|---|
| 113 | * greater than 0. If there are no messages, this method will throw an |
|---|
| 114 | * IndexOutOfBoundsException. |
|---|
| 115 | * |
|---|
| 116 | * @return CADRoutedMessage |
|---|
| 117 | */ |
|---|
| 118 | public CADRoutedMessage getCurrentMessage() throws IndexOutOfBoundsException { |
|---|
| 119 | |
|---|
| 120 | return messageList.get(currentListItem); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Method is called to advance to the next queued message object. Update the |
|---|
| 125 | * message list and current message counter if a message has been deleted. If the |
|---|
| 126 | * currentListItem count is at the end of the message list, then start over |
|---|
| 127 | * at the beginning, otherwise increase the count by one. |
|---|
| 128 | * This method is only to be called if numberOfMessages() returns a value |
|---|
| 129 | * greater than 0. |
|---|
| 130 | */ |
|---|
| 131 | public boolean nextQueue() { |
|---|
| 132 | |
|---|
| 133 | //update the counter if a the previous queue message was deleted |
|---|
| 134 | if(messageDeleted) { |
|---|
| 135 | messageList.remove(currentListItem); |
|---|
| 136 | currentListItem--; |
|---|
| 137 | |
|---|
| 138 | if(currentListItem > messageList.size() - 1 || |
|---|
| 139 | currentListItem < 0) { |
|---|
| 140 | currentListItem = 0; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | messageDeleted = false; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | if(messageList.size() > 0) { |
|---|
| 147 | if(currentListItem == messageList.size() - 1) |
|---|
| 148 | currentListItem = 0; |
|---|
| 149 | else |
|---|
| 150 | currentListItem++; |
|---|
| 151 | |
|---|
| 152 | return true; |
|---|
| 153 | } |
|---|
| 154 | else |
|---|
| 155 | return false; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | * If the message being deleted is currently being viewed, set the messageDeleted |
|---|
| 160 | * flag to true so that the queued message will be deleted when nextQueue() |
|---|
| 161 | * or prevQueue() is called next. Else, if the message being deleted |
|---|
| 162 | * does exist in this model's list of messages, remove that message. Update |
|---|
| 163 | * the currentListItem counter if the message deleted is at a position |
|---|
| 164 | * prior to the current message. |
|---|
| 165 | * |
|---|
| 166 | * @param delMsg Queue message being deleted |
|---|
| 167 | */ |
|---|
| 168 | public void deleteQueue(CADRoutedMessage delMsg) { |
|---|
| 169 | |
|---|
| 170 | int msgIndex = messageList.indexOf(delMsg); |
|---|
| 171 | |
|---|
| 172 | if(msgIndex == currentListItem) { |
|---|
| 173 | messageDeleted = true; |
|---|
| 174 | } |
|---|
| 175 | else if(msgIndex != -1) { |
|---|
| 176 | messageList.remove(delMsg); |
|---|
| 177 | |
|---|
| 178 | if(currentListItem > msgIndex) |
|---|
| 179 | currentListItem--; |
|---|
| 180 | } |
|---|
| 181 | //else msgIndex == -1 Msg already removed |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | * Method is called to back up to the previous queued message object. |
|---|
| 186 | * Update the message list and current message counter if a message |
|---|
| 187 | * has been deleted. If the currentListItem count is at the beginning |
|---|
| 188 | * of the message list, then move to the end, otherwise decrease the |
|---|
| 189 | * count by one. This method is only to be called if numberOfMessages() |
|---|
| 190 | * returns a value greater than 0. |
|---|
| 191 | */ |
|---|
| 192 | public boolean prevQueue() { |
|---|
| 193 | |
|---|
| 194 | //update the counter if a the previous queue message was deleted |
|---|
| 195 | if(messageDeleted) { |
|---|
| 196 | messageList.remove(currentListItem); |
|---|
| 197 | currentListItem--; |
|---|
| 198 | |
|---|
| 199 | if(currentListItem > messageList.size() - 1 || |
|---|
| 200 | currentListItem < 0){ |
|---|
| 201 | currentListItem = 0; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | messageDeleted = false; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | if(messageList.size() > 0) { |
|---|
| 208 | if(currentListItem == 0) |
|---|
| 209 | currentListItem = messageList.size() - 1; |
|---|
| 210 | else |
|---|
| 211 | currentListItem--; |
|---|
| 212 | |
|---|
| 213 | return true; |
|---|
| 214 | } |
|---|
| 215 | else |
|---|
| 216 | return false; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** |
|---|
| 220 | * Determine if this routed message is a forwarded incident update. |
|---|
| 221 | * @return True if this message is an incident update, false if not. |
|---|
| 222 | */ |
|---|
| 223 | public boolean isIncidentUpdate() { |
|---|
| 224 | return messageList.get(currentListItem).incidentUpdate; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | public void toXML(Element currElem) { |
|---|
| 228 | |
|---|
| 229 | //if this is an update, then the xmloutput is contained within |
|---|
| 230 | //the message text |
|---|
| 231 | if(getCurrentMessage().incidentUpdate) { |
|---|
| 232 | |
|---|
| 233 | } |
|---|
| 234 | //else send the current message only |
|---|
| 235 | else { |
|---|
| 236 | |
|---|
| 237 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 238 | |
|---|
| 239 | Element modelElem = theDoc.createElement(CAD_COMMANDS.ROUTED_MESSAGE.fullName); |
|---|
| 240 | |
|---|
| 241 | baseToXML(modelElem); |
|---|
| 242 | |
|---|
| 243 | Element originElem = theDoc.createElement(DATA_TAGS.ORIGIN.tag); |
|---|
| 244 | originElem.appendChild(theDoc.createTextNode(String.valueOf(getCurrentMessage().fromPosition))); |
|---|
| 245 | modelElem.appendChild(originElem); |
|---|
| 246 | |
|---|
| 247 | Element destElem = theDoc.createElement(DATA_TAGS.DESTINATION.tag); |
|---|
| 248 | destElem.appendChild(theDoc.createTextNode(String.valueOf(getCurrentMessage().toPosition))); |
|---|
| 249 | modelElem.appendChild(destElem); |
|---|
| 250 | |
|---|
| 251 | Element msgElem = theDoc.createElement(DATA_TAGS.ORIGIN.tag); |
|---|
| 252 | msgElem.appendChild(theDoc.createTextNode(getCurrentMessage().message)); |
|---|
| 253 | modelElem.appendChild(msgElem); |
|---|
| 254 | |
|---|
| 255 | currElem.appendChild(modelElem); |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | public void fromXML(Node modelNode) throws ScriptException { |
|---|
| 260 | messageList.clear(); |
|---|
| 261 | currentListItem = 0; |
|---|
| 262 | |
|---|
| 263 | CADRoutedMessage newMessage = new CADRoutedMessage(0, 0, "", false); |
|---|
| 264 | |
|---|
| 265 | modelNode = modelNode.getFirstChild(); |
|---|
| 266 | |
|---|
| 267 | baseFromXML(modelNode); |
|---|
| 268 | |
|---|
| 269 | modelNode = modelNode.getNextSibling(); |
|---|
| 270 | newMessage.fromPosition = Integer.parseInt(modelNode.getTextContent()); |
|---|
| 271 | |
|---|
| 272 | modelNode = modelNode.getNextSibling(); |
|---|
| 273 | newMessage.toPosition = Integer.parseInt(modelNode.getTextContent()); |
|---|
| 274 | |
|---|
| 275 | modelNode = modelNode.getNextSibling(); |
|---|
| 276 | newMessage.message = modelNode.getTextContent(); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | messageList.add(newMessage); |
|---|
| 280 | |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | } |
|---|