/**
 * 
 */
package org.corpus_tools.atomic.grideditor.configuration;

import org.corpus_tools.salt.core.SAnnotation;
import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.config.IEditableRule;
import org.eclipse.nebula.widgets.nattable.data.convert.DisplayConverter;
import org.eclipse.nebula.widgets.nattable.data.convert.IDisplayConverter;
import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
import org.eclipse.nebula.widgets.nattable.edit.editor.TextCellEditor;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;

/**
 * @author Stephan Druskat
 * 
 * // TODO Add description
 *
 */
public class GridEditConfiguration extends AbstractRegistryConfiguration {

	/* (non-Javadoc)
	 * @see org.eclipse.nebula.widgets.nattable.config.IConfiguration#configureRegistry(org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)
	 */
	@Override
	public void configureRegistry(IConfigRegistry configRegistry) {
		configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);
		configRegistry.registerConfigAttribute(CellConfigAttributes.DISPLAY_CONVERTER, getAnnotationValueConverter(), DisplayMode.NORMAL);
		configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new TextCellEditor(), DisplayMode.EDIT, null);
	}

	private IDisplayConverter getAnnotationValueConverter() {
		return new DisplayConverter() {
			
			@Override
			public Object displayToCanonicalValue(Object displayValue) {
				return displayValue;
			}
			
			@Override
			public Object canonicalToDisplayValue(Object canonicalValue) {
				if (canonicalValue instanceof SAnnotation) {
					return ((SAnnotation) canonicalValue).getValue();
				}
				else {
					return canonicalValue;
				}
			}
		};
	}

//	/**
//		 * // TODO Add description
//		 *
//		 * @author Stephan Druskat <[mail@sdruskat.net](mailto:mail@sdruskat.net)>
//		 * 
//		 */
//	public class AnnotationCellEditor extends TextCellEditor {
//
//		@Override
//		public boolean commit(MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation) {
//			if (!isClosed()) {
//				// always do the conversion
//				Object canonicalValue = getCanonicalValue();
//				if (skipValidation || (!skipValidation && validateCanonicalValue(canonicalValue))) {
//					boolean committed = new InlineEditHandler(layerCell.getLayer(), layerCell.getColumnPosition(),
//							layerCell.getRowPosition()).commit(canonicalValue, direction);
//					if (committed && closeAfterCommit) {
//						close();
//						if (direction != MoveDirectionEnum.NONE && openAdjacentEditor()) {
//							this.layerCell.getLayer().doCommand(new EditSelectionCommand(null, this.configRegistry));
//						}
//					}
//					return committed;
//				}
//				return false;
//			}
//			if (!isClosed()) {
//				close();
//			}
//			return true;
//		}
//	}

}

@@ -686,16 +686,103 @@
 Editor;%0A
+import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;%0A
 import o
@@ -1225,16 +1225,41 @@
 stry) %7B%0A
+%09%09// Make cells editable%0A
 %09%09config
@@ -1364,16 +1364,56 @@
 TABLE);%0A
+%09%09// Configure what to show in the grid%0A
 %09%09config
@@ -1537,16 +1537,44 @@
 ORMAL);%0A
+%09%09// Configure cell editing%0A
 %09%09config
@@ -1644,20 +1644,26 @@
 OR, new 
-Text
+Annotation
 CellEdit
@@ -1693,16 +1693,330 @@
  null);%0A
+%09%09// Automatically open the editor for the cell below FIXME Make this configurable%0A%09%09configRegistry.registerConfigAttribute(EditConfigAttributes.OPEN_ADJACENT_EDITOR, true);%0A%09%09// Switch off out-of-the-box multi-cell editing%0A%09%09configRegistry.registerConfigAttribute(EditConfigAttributes.SUPPORT_MULTI_EDIT, false);%0A
 %09%7D%0A%0A%09pri
@@ -2471,17 +2471,13 @@
 %09%7D%0A%0A
-//
 %09/**%0A
-//
 %09%09 *
@@ -2505,17 +2505,13 @@
 ion%0A
-//
 %09%09 *%0A
-//
 %09%09 *
@@ -2587,26 +2587,20 @@
 t)%3E%0A
-//
 %09%09 * %0A
-//
 %09%09 */%0A
-//
 %09pub
@@ -2659,13 +2659,9 @@
 r %7B%0A
-//%0A//
+%0A
 %09%09@O
@@ -2668,18 +2668,16 @@
 verride%0A
-//
 %09%09public
@@ -2777,768 +2777,72 @@
 ) %7B%0A
-//
 %09%09%09
-if (!isClosed()) %7B%0A//%09%09%09%09// always do the conversion%0A//%09%09%09%09Object canonicalValue = getCanonicalValue();%0A//%09%09%09%09if (skipValidation %7C%7C (!skipValidation && validateCanonicalValue(canonicalValue))) %7B%0A//%09%09%09%09%09boolean committed = new InlineEditHandler(layerCell.getLayer(), layerCell.getColumnPosition(),%0A//%09%09%09%09%09%09%09layerCell.getRowPosition()).commit(canonicalValue, direction);%0A//%09%09%09%09%09if (committed && closeAfterCommit) %7B%0A//%09%09%09%09%09%09close();%0A//%09%09%09%09%09%09if (direction != MoveDirectionEnum.NONE && openAdjacentEditor()) %7B%0A//%09%09%09%09%09%09%09this.layerCell.getLayer().doCommand(new EditSelectionCommand(null, this.configRegistry));%0A//%09%09%09%09%09%09%7D%0A//%09%09%09%09%09%7D%0A//%09%09%09%09%09return committed;%0A//%09%09%09%09%7D%0A//%09%09%09%09return false;%0A//%09%09%09%7D%0A//%09%09%09if (!isClosed()) %7B%0A//%09%09%09%09close();%0A//%09%09%09%7D%0A//%09%09%09return true;%0A//%09%09%7D%0A//
+return super.commit(MoveDirectionEnum.DOWN, true, true);%0A%09%09%7D%0A
 %09%7D%0A%0A

Configure grid to open adjacent cell editors after commit