de.espirit.firstspirit.access.editor.DomTableEditorValueExample


package de.espirit.firstspirit.access.editor;

import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import de.espirit.firstspirit.access.editor.value.Table;
import de.espirit.firstspirit.access.editor.value.TableCell;
import de.espirit.firstspirit.access.editor.value.DomNode;
import de.espirit.common.xml.XmlUtilities;

import java.util.Iterator;


/**
 * Example how to get the value of a DomTableEditorValue and how to store a value into a DomTableEditorValue.
 *
 * @since 4.2.34
 */
public class DomTableEditorValueExample {

	/**
	 * A simple example how to append rows and columns to an empty DomTableEditorValue.
	 *
	 * -----------------------------
	 * | cell 0 (initial) | cell 1 |
	 * -----------------------------
	 * | cell 2           | cell 3 |
	 * -----------------------------
	 *
	 * @since 4.2.34
	 */
	public void setValueForLanguage(final DomTableEditorValue editor, final Language language) throws InvalidValueException {

		// 1. check if language is provided if editor is language dependent ("useLanguages='yes'" in gom syntax)
		if (editor.isLanguageDependent() && language == null) {
			throw new NullPointerException("Language is missing!");
		}

		// 2. get the table of the editor value
		final Table table = editor.get(language);

		// 3. by default a new table already has one table cell - retrieve the already existing table cell
		final TableCell firstCell = table.getCell(0, 0);

		// 4. append an initial text to this cell
		firstCell.appendChild("cell 0 (initial)");

		// 3. create new table structure
		table.addRow(table.getRows());			// add a new row at the end
		table.addColumn(table.getColumns());	// add a new column at the end

		// 4. retrieve table cells and append text content
		final TableCell tableCell1 = table.getCell(0, 1);
		tableCell1.appendChild("cell 1");
		final TableCell tableCell2 = table.getCell(1, 0);
		tableCell2.appendChild("cell 2");
		final TableCell tableCell3 = table.getCell(1, 1);
		tableCell3.appendChild("cell 3");

		// 5. store dom element back into the editor value
		editor.set(language, table);
	}


	/**
	 * Returns the value of the DomTableEditorValue for the given language.
	 *
	 * @since 4.2.34
	 */
	public Table getValueForLanguage(final DomTableEditorValue editor, final Language language) {

		// 1. get the stored instance in the editor value
		final Table value = editor.get(language);

		// 2. and return the value for further processing
		return value;
	}
}