mason.Reader = draw2d.io.Reader.extend({
	NAME : "mason.Reader",

	init : function() {
		this._super();
	},

	processData : function(canvas, json, rebuild) {
		
		var this_ = this;
		
		var node = null;

		var key;
		var value;

		if (json.views !== null && json.views.length !== 0) {
			$.each(json.views, function(i, element) {
				this_.processView(canvas, element);
			});
		}

		// verwerken van de Elements
		if (json.elements ==! null) {
			$.each(json.elements, function(i, element) {
				this_.processElement(canvas, element);
			});
		}

		// verwerken van de ElementContainters
		if (json.elementContainers !== null) {
			$.each(json.elementContainers, function(i, element) {
				this_.processElementContainter(canvas, element);
			});
		}

		// Verwerken van de ElementRelationships
		if (json.relationships !== null) {
			$.each(json.relationships, function(i, element) {
				this_.processRelationship(canvas, element);
			});
		}
	},
	
	processView : function (canvas, element){
		key = element.generatedKey;
		value = element.description;
		$('#viewList').append($('<option>', {
			value : key
		}).text(value));
		$('#new-element').show();
		$('#editViewButton').show();
		$('#deleteViewButton').show();
	},
	
	processElement : function (canvas, element) {
		
		if($("#" + element.generatedKey).length == 0) {
			jQuery('<div/>', {
			    id: element.generatedKey,
			    text: element.slug
			}).appendTo('#navigation');

			$("#" + element.generatedKey).attr("data-shape", "mason.ElementContainer");
			$("#" + element.generatedKey).attr("data-text", element.value);
			$("#" + element.generatedKey).attr("data-id", element.generatedKey);

			$("#" + element.generatedKey).addClass("draw2d_droppable");
			$("#" + element.generatedKey).addClass("palette_node_element");
			
			$("#" + element.generatedKey).draggable({
	            appendTo:"body",
	            stack:"body",
	            zIndex: 27000,
	            helper:"clone",
	            drag: $.proxy(function(event, ui){
	                event = canvas._getEvent(event);
	                var pos = canvas.fromDocumentToCanvasCoordinate(event.clientX, event.clientY);
	                this.onDrag(ui.draggable, pos.getX(), pos.getY());
	            },canvas),
	            stop: function(e, ui){
	            	canvas.isInExternalDragOperation=false;
	            },
	            start: function(e, ui){
	            	canvas.isInExternalDragOperation=true;
	                $(ui.helper).addClass("shadow");
	            }
	       });
			 
		} else {
			$("#" + element.generatedKey).attr("data-text", element.value);
			$("#" + element.generatedKey).text(element.slug);
			
			// werkt nog niet
			if (canvas.figures !== undefined ) {
				$.each(canvas.getFigures(), function(i, fig) {
					if (fig.NAME == "mason.ElementContainer") {
						console.log(4);
						if (fig.umlElement == element.generatedKey) {
							console.log(5);
							fig.setLabel();
						}
					}
				});
			}
		}
	},
	
	processElementContainter : function (canvas, element){
		var o = eval("new mason.ElementContainer()");

		o.setPersistentAttributes(element);
		canvas.addFigure(o);
	},
	
	processRelationship : function (canvas, element){
		var o = eval("new mason.Relationship()");

		var source = null;
		var target = null;
		
		sourceNode = canvas.getFigure(element.sourceIdGenerated);

		targetNode = canvas.getFigure(element.targetIdGenerated);
		
		if (sourceNode !== null && targetNode !== null) {
			source = sourceNode.getPort(element.sourcePort);
			target = targetNode.getPort(element.targetPort);
		}

		if (source !== null && target !== null) {
			o.setSource(source);
			o.setTarget(target);
		}

		o.setPersistentAttributes(element);
		canvas.addFigure(o);
	}

});