package de.espirit.firstspirit.access.editor;
import de.espirit.firstspirit.access.Language;
import de.espirit.firstspirit.access.UserService;
import de.espirit.firstspirit.access.editor.value.DefaultDomNode;
import de.espirit.firstspirit.access.editor.value.DomElement;
import de.espirit.firstspirit.access.editor.value.DomNode;
import de.espirit.firstspirit.access.editor.value.InvalidValueException;
import de.espirit.firstspirit.access.editor.value.TableCell;
import de.espirit.firstspirit.access.editor.value.TableDomNode;
import de.espirit.firstspirit.access.editor.value.TableRowDomNode;
import de.espirit.firstspirit.access.link.Link;
import de.espirit.firstspirit.access.store.templatestore.FormatTemplate;
import de.espirit.firstspirit.access.store.templatestore.FormatTemplates;
import de.espirit.firstspirit.access.store.templatestore.LinkTemplate;
import de.espirit.firstspirit.access.store.templatestore.LinkTemplates;
import de.espirit.firstspirit.access.store.templatestore.TableFormatTemplate;
import de.espirit.firstspirit.access.store.templatestore.TemplateStoreRoot;
/**
* Example how to get the value of a DomEditorValue and how to store a value into a DomEditorValue.
*
* @since 4.2.34
*/
public class DomEditorValueExample {
/**
* A simple example how to append a paragraph containing some text and a link to the DomEditorValue.
*
* @since 4.2.34
*/
public void appendParagraphForLanguage(final DomEditorValue editor, final Language language, final UserService userService) 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 dom element of the editor value
final DomElement domElement = editor.get(language);
// 3. get the root dom node of the dom element
final DefaultDomNode rootDomNode = domElement.getRoot();
// 4. retrieve some templates (format, link) we need to append content to dom
final TemplateStoreRoot templateStore = userService.getTemplateStore();
final FormatTemplates formatTemplates = templateStore.getFormatTemplates();
final FormatTemplate paragraphTemplate = formatTemplates.getFormatTemplate("p");
if (paragraphTemplate == null) {
throw new NullPointerException("Format template not found!");
}
final LinkTemplates linkTemplates = templateStore.getLinkTemplates();
final LinkTemplate linkTemplate = linkTemplates.getTemplate("myLinkTemplate");
if (linkTemplate == null) {
throw new NullPointerException("Link template not found!");
}
// 5. append a paragraph by using the paragraphTemplate
final DefaultDomNode paragraph = (DefaultDomNode) rootDomNode.appendChild(paragraphTemplate);
// 6. append some text to the paragraph node
paragraph.appendChild("sample text with ");
// 7. create a link by using the link template retrieved in step 4
final Link link = linkTemplate.createLink(language);
// 8. set the text to be displayed for link
link.setText("link");
// 9. append created link to the paragraph node
paragraph.appendChild(link);
// 10. store dom element back into the editor value
editor.set(language, domElement);
}
/**
* A simple example how to append an inline table of 2 rows to the DomEditorValue.
* <b>Note:</b> <code>CMS_INPUT_DOM</code> configuration has to be configured to allow usage of inline tables (<code>table="yes"</code>).
*
* -------------------
* | cell 0 | cell 1 |
* -------------------
* | cell 2 |
* -------------------
*
* @since 4.2.34
*/
public void appendTableForLanguage(final DomEditorValue editor, final Language language, final UserService userService) 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 dom element of the editor value
final DomElement domElement = editor.get(language);
// 3. get the root dom node of the dom element
final DefaultDomNode rootDomNode = domElement.getRoot();
// 4. retrieve some templates (format, link) we need to append content to dom
final TemplateStoreRoot templateStore = userService.getTemplateStore();
final FormatTemplates formatTemplates = templateStore.getFormatTemplates();
final TableFormatTemplate tableFormatTemplate = formatTemplates.getTableFormatTemplate("table");
final FormatTemplate rowTemplate = formatTemplates.getFormatTemplate("tr");
final FormatTemplate cellTemplate = formatTemplates.getFormatTemplate("td");
// 5. append an inline by using the tableFormatTemplate
final TableDomNode table = rootDomNode.appendChild(tableFormatTemplate);
// 6. append a row to table and two cells to the created row
final TableRowDomNode tableRow0 = (TableRowDomNode) table.appendChild(rowTemplate);
final TableCell tableCell0 = (TableCell) tableRow0.appendChild(cellTemplate);
tableCell0.appendChild("cell 0");
final TableCell tableCell1 = (TableCell) tableRow0.appendChild(cellTemplate);
tableCell1.appendChild("cell 1");
// 7. append another row to table and one cell (with a colspan of 2) to the created row
final TableRowDomNode tableRow1 = (TableRowDomNode) table.appendChild(rowTemplate);
final TableCell tableCell2 = (TableCell) tableRow1.appendChild(cellTemplate);
tableCell2.setColSpan(2);
tableCell2.appendChild("cell 2");
// 8. store dom element back into the editor value
editor.set(language, domElement);
}
/**
* A simple example how to remove all children of a dom node.
*
* @since 4.2.34
*/
public void clearValueForLanguage(final DomEditorValue 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 dom element of the editor value
final DomElement domElement = editor.get(language);
// 3. get the root dom node of the dom element
final DefaultDomNode rootDomNode = domElement.getRoot();
// 4. remove all children from root dom node
for (final DomNode child : rootDomNode.getChildren()) {
rootDomNode.removeChild(child);
}
// 5. store dom element back into the editor value
editor.set(language, domElement);
}
/**
* Returns the value of the DomEditorValue for the given language.
*
* @since 4.2.34
*/
public DomElement getValueForLanguage(final DomEditorValue editor, final Language language) {
// 1. get the stored instance in the editor value
final DomElement value = editor.get(language);
// 2. and return the value for further processing
return value;
}
}