mason.View = draw2d.Canvas.extend({

	init : function(id) {
		this._super(id);

		this.setScrollArea("#" + id);

		this.currentDropConnection = null;
	},

	/**
	 * @method Called if the DragDrop object is moving around.<br>
	 *         <br>
	 *         Graphiti use the jQuery draggable/droppable lib. Please inspect
	 *         http://jqueryui.com/demos/droppable/ for further information.
	 * 
	 * @param {HTMLElement}
	 *            droppedDomNode The dragged DOM element.
	 * @param {Number}
	 *            x the x coordinate of the drag
	 * @param {Number}
	 *            y the y coordinate of the drag
	 * 
	 * @template
	 */
	onDrag : function(droppedDomNode, x, y) {

	},

	setCurrentSelection : function(figure) {
		
		if ( this.currentSelection !== null) {
			this.currentSelection.setFocus(this, false);
		}
		
		if (figure == null) {
			$('#relationshipTypeTextLabel').hide();
			$('#relationTypeList').hide();
			$('#relationSwitchButton').hide();
		} else {
			if (figure.NAME == "mason.Relationship") {
				$('#relationshipTypeTextLabel').show();
				$('#relationTypeList').show();
				$('#relationSwitchButton').show();

				$('#relationTypeList option[value=' + figure.kind + ']').attr(
						'selected', 'selected');
			} else {
				$('#relationshipTypeTextLabel').hide();
				$('#relationTypeList').hide();
				$('#relationSwitchButton').hide();
			}
			
			figure.setFocus(this, true);
		}

		if (figure === null) {
			this.hideResizeHandles();
			this.hideLineResizeHandles();
		}

		if (figure instanceof draw2d.shape.basic.Line) {
			this.showLineResizeHandles(figure);
		} else if (figure instanceof draw2d.Figure) {
			this.showResizeHandles(figure);
		}

		this.currentSelection = figure;

		// inform all selection listeners about the new selection.
		//
		for ( var i = 0; i < this.selectionListeners.getSize(); i++) {
			var w = this.selectionListeners.get(i);
			if (typeof w.onSelectionChanged === "function") {
				w.onSelectionChanged(this.currentSelection);
			}
		}
	},

	/**
	 * @method Called if the user drop the droppedDomNode onto the canvas.<br>
	 *         <br>
	 *         Graphiti use the jQuery draggable/droppable lib. Please inspect
	 *         http://jqueryui.com/demos/droppable/ for further information.
	 * 
	 * @param {HTMLElement}
	 *            droppedDomNode The dropped DOM element.
	 * @param {Number}
	 *            x the x coordinate of the drop
	 * @param {Number}
	 *            y the y coordinate of the drop
	 * @private
	 */
	onDrop : function(droppedDomNode, x, y) {

		var this_ = this;

		var type = $(droppedDomNode).data("shape");
		var figure = eval("new " + type + "();");
		// create a command for the undo/redo support
		var command = new draw2d.command.CommandAdd(this, figure, x, y);
		figure.setLabel($(droppedDomNode).data("text"));
		figure.umlElement = $(droppedDomNode).data("id");
		figure.setId(-1);
		figure.viewId = this.viewId;

		this.getCommandStack().execute(command);

		if (figure.umlElement == -1) {
			var modelId = $('#modelId').text();

			var data = {
				value : $(droppedDomNode).data("text")
			};

			jQuery.ajax({
				type : "POST",
				dataType : "json",
				data : data,
				url : '/json/model/' + modelId + '/element',
				success : function(json, statusText) {

					var reader = new mason.Reader();
					reader.processElement(this_, json);

					figure.umlElement = json.generatedKey;
					figure.setLabel($(droppedDomNode).data("text"));
					figure.sendUpdate();
				},
				error : function(xhr, ajaxOptions, thrownError) {
					alert('FOUT: Send new Element');
				}
			});
		} else {
			figure.sendUpdate();
		}

		figure.setLabel();

	},

	loadView : function(key) {

		$('#viewId').text(key);

		if (String(key) == 'undefined') {
			return;
		}

		var this_ = this;

		jQuery.ajax({
			type : "GET",
			dataType : "json",
			url : '/json/view/' + key + '/data/all',
			success : function(data, statusText) {
				this_.clear();
				var reader = new mason.Reader();
				reader.processData(this_, data);
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert('Fout tijdens het ophalen van de content');
			}
		});
	}
});
