* @param editedElement
* @return
*/
private boolean handleEditedElement(String editedElement) {
try {
JSONObject jsonEditedElement = new JSONObject(editedElement);
Element element = getElementObject(jsonEditedElement);
boolean isLoaded = false;
if (!elementMap.isEmpty() && loadedElementMap.get(element.getId()) != null && !loadedElementMap.isEmpty()) {
isLoaded = isEqual(element.getJSON(), loadedElementMap.get(element.getId()).getJSON());
}
// If the edited element is not from file loaded content this clause executes
if (!isLoaded) {
// Adding the element creation/editing is add to the undo snapshots
if (snapShot == null && snapShotCreation == null) {
snapShot = new ArrayList<Element>();
snapShotCreation = new ArrayList<Boolean>();
}
if (elementMap.containsKey(element.getId()) && !elementMap.isEmpty()) {
snapShot.add(elementMap.get(element.getId()));
snapShotCreation.add(false);
} else {
snapShot.add(element);
snapShotCreation.add(true);
}
if (Type.PointFree != element.getType()) {
if (undoSnapshots.size() == 20) {
undoSnapshots.pollFirst();
undoSnapshotCreationList.pollFirst();
}
if (Type.PencilCurve == element.getType()) {
List<Element> lastElementSnapshot = undoSnapshots.peekLast();
if (lastElementSnapshot != null) {
Element lastSnapshotElement = lastElementSnapshot.get(lastElementSnapshot.size() - 1);
if ((lastSnapshotElement instanceof PencilCurve) && (lastSnapshotElement.getId() == element.getId())) {
List<Boolean> lastCreationSnapshot = undoSnapshotCreationList.getLast();
for (int i = 0; i < snapShot.size(); i++) {
lastElementSnapshot.add(snapShot.get(i));
lastCreationSnapshot.add(snapShotCreation.get(i));
}
} else {
undoSnapshots.addLast(snapShot);
undoSnapshotCreationList.addLast(snapShotCreation);
isElementSnapshotList.addLast(true);
}
}
} else if (Type.ClipArt == element.getType()) {
List<Element> snapShotTemp = undoSnapshots.pollLast();
List<Boolean> snapShotCreationTemp = undoSnapshotCreationList.pollLast();
for (int i = 0; i < snapShotTemp.size(); i++) {
snapShot.add(snapShotTemp.get(i));
snapShotCreation.add(snapShotCreationTemp.get(i));
}
undoSnapshots.addLast(snapShot);
undoSnapshotCreationList.addLast(snapShotCreation);
isElementSnapshotList.addLast(true);
} else {
undoSnapshots.addLast(snapShot);
undoSnapshotCreationList.addLast(snapShotCreation);
isElementSnapshotList.addLast(true);
}
snapShot = null;
snapShotCreation = null;
}
// Synchronizing newly added/edited element between whiteboard clients
if (element != null) {
elementMap.put(element.getId(), element);
IWebSocketConnectionRegistry reg = WebSocketSettings.Holder.get(Application.get()).getConnectionRegistry();
for (IWebSocketConnection c : reg.getConnections(Application.get())) {
try {
JSONObject jsonObject = new JSONObject(editedElement);
c.sendMessage(getAddElementMessage(jsonObject).toString());
} catch (Exception e) {
log.error("Unexpected error while sending message through the web socket", e);
}
}