org.shaman.rpg.editor.project.elements.JdomElement.java Source code

Java tutorial

Introduction

Here is the source code for org.shaman.rpg.editor.project.elements.JdomElement.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.shaman.rpg.editor.project.elements;

/**
 * An implementation of {@link Element} storing all information in a JDOM-Element.
 * Warning: this class is not recommended, use {@link FileElement } if possible.
 * Only Strings are supported as user-information.
 * @author Sebastian Wei
 */
public class JdomElement implements Element {
    private final JdomGroup parent;
    private final JdomElements root; //<-- needed for autosave
    final org.jdom2.Element data;
    private final int id;
    private String name;
    private String author;
    private String coAuthor;
    private String description;
    private String license;

    JdomElement(JdomGroup parent, JdomElements root, org.jdom2.Element data, int id) {
        this.parent = parent;
        this.root = root;
        this.data = data;
        this.id = id;
    }

    /**
     * updates the jdom-element.
     */
    void upate() {
        setChildText("name", name);
        setChildText("author", author);
        setChildText("coAuthor", coAuthor);
        setChildText("descr", description);
        setChildText("license", license);
    }

    private void setChildText(String child, String text) {
        org.jdom2.Element e = data.getChild(child, JdomElements.NAMESPACE);
        if (text == null) {
            if (e != null) {
                e.detach();
            }
            return;
        }
        if (e == null) {
            e = new org.jdom2.Element(child);
            e.setNamespace(JdomElements.NAMESPACE);
            data.addContent(e);
        }
        e.setText(text);
    }

    //loads the information
    void load() {
        name = data.getChildTextNormalize("name", JdomElements.NAMESPACE);
        author = data.getChildTextNormalize("author", JdomElements.NAMESPACE);
        coAuthor = data.getChildTextNormalize("coAuthor", JdomElements.NAMESPACE);
        description = data.getChildTextNormalize("descr", JdomElements.NAMESPACE);
        license = data.getChildTextNormalize("license", JdomElements.NAMESPACE);
    }

    /**
     * deletes this element from the parent group.
     * Do not use this element after a call of method!
     */
    public void delete() {
        parent.removeSubgroup(getID());
    }

    @Override
    public JdomGroup getParent() {
        return parent;
    }

    public int getID() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    public Element setName(String name) {
        this.name = name;
        root.hasChanges = true;
        return this;
    }

    @Override
    public String getAuthor() {
        return author;
    }

    @Override
    public void setAuthor(String author) {
        this.author = author;
        root.hasChanges = true;
    }

    @Override
    public String getCoAuthor() {
        return coAuthor;
    }

    @Override
    public void setCoAuthor(String coAuthor) {
        this.coAuthor = coAuthor;
        root.hasChanges = true;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
        root.hasChanges = true;
    }

    @Override
    public String getLicense() {
        return license;
    }

    @Override
    public void setLicense(String license) {
        this.license = license;
        root.hasChanges = true;
    }

    @Override
    public String toString() {
        //return "Element{" + "id=" + id + ", name=" + name + ", author=" + author + ", coAuthor=" + coAuthor + ", description=" + description + ", license=" + license + '}';
        return getName();
    }

    @Override
    public Object getCustomInformation(String key) {
        return data.getChildText(key, JdomElements.NAMESPACE);
    }

    @Override
    public void setCustomInformation(String key, Object value) {
        if (value == null || !(value instanceof String)) {
            throw new IllegalArgumentException("only non-null string values are supported");
        }
        setChildText(key, (String) value);
    }

}