/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.plugin.editing.client.handler;

import java.util.Collections;
import java.util.List;

import org.geomajas.geometry.Coordinate;
import org.geomajas.gwt.client.handler.MapDownHandler;
import org.geomajas.gwt.client.map.RenderSpace;
import org.geomajas.plugin.editing.client.operation.GeometryOperationFailedException;
import org.geomajas.plugin.editing.client.service.GeometryEditState;
import org.geomajas.plugin.editing.client.service.GeometryIndex;
import org.geomajas.plugin.editing.client.service.GeometryIndexType;

import com.google.gwt.event.dom.client.HumanInputEvent;

/**
 * ...
 * 
 * @author Pieter De Graef
 */
public class GeometryIndexInsertHandler extends AbstractGeometryIndexMapHandler implements MapDownHandler {

	public void onDown(HumanInputEvent<?> event) {
		// This handler should only have been applied onto edges. Can't hurt to check again:
		if (service.getEditingState() == GeometryEditState.IDLE
				&& service.getIndexService().getType(index) == GeometryIndexType.TYPE_EDGE) {
			List<GeometryIndex> indices = Collections.singletonList(index);
			try {
				// If this edge is highlighted, end it:
				if (service.getIndexStateService().isHightlighted(index)) {
					service.getIndexStateService().highlightEnd(indices);
				}

				// First insert the point into the geometry:
				Coordinate location = getLocation(event, RenderSpace.WORLD);
				service.insert(indices, Collections.singletonList(Collections.singletonList(location)));

				// Then change the selection to the newly inserted point:
				service.getIndexStateService().deselectAll();
				service.getIndexStateService().select(
						Collections.singletonList(service.getIndexService().getNextVertex(index)));

				// Set status to dragging:
				service.setEditingState(GeometryEditState.DRAGGING);
			} catch (GeometryOperationFailedException e) {
			}
		}
	}
}
@@ -557,24 +557,82 @@
 Coordinate;%0A
+import org.geomajas.gwt.client.controller.MapEventParser;%0A
 import org.g
@@ -808,16 +808,87 @@
 eption;%0A
+import org.geomajas.plugin.editing.client.service.GeometryEditService;%0A
 import o
@@ -1295,16 +1295,408 @@
 dler %7B%0A%0A
+%09private final GeometryIndexDragSelectionHandler dragHandler = new GeometryIndexDragSelectionHandler();%0A%0A%09@Override%0A%09public void setService(GeometryEditService service) %7B%0A%09%09super.setService(service);%0A%09%09dragHandler.setService(service);%0A%09%7D%0A%0A%09@Override%0A%09public void setEventParser(MapEventParser eventParser) %7B%0A%09%09super.setEventParser(eventParser);%0A%09%09dragHandler.setEventParser(eventParser);%0A%09%7D%0A%0A
 %09public 
@@ -2532,16 +2532,98 @@
 tAll();%0A
+%09%09%09%09GeometryIndex insertedIndex = service.getIndexService().getNextVertex(index);%0A
 %09%09%09%09serv
@@ -2660,15 +2660,8 @@
 ect(
-%0A%09%09%09%09%09%09
 Coll
@@ -2686,56 +2686,23 @@
 ist(
-service.getIndexService().getNextVertex(i
+insertedI
 ndex))
-)
 ;%0A%0A%09
@@ -2739,58 +2739,73 @@
 %09%09%09%09
-service.setEditingState(GeometryEditState.DRAGGING
+dragHandler.setIndex(insertedIndex);%0A%09%09%09%09dragHandler.onDown(event
 );%0A%09

Add geometry splitting functionality + debugging