com.kccomy.orgar.module.TreeNode.java Source code

Java tutorial

Introduction

Here is the source code for com.kccomy.orgar.module.TreeNode.java

Source

/*
 * Copyright (C) 2016. kccomy, Orgar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kccomy.orgar.module;

import org.cowboyprogrammer.org.OrgNode;
import org.cowboyprogrammer.org.OrgTimestamp;
import org.joda.time.LocalDateTime;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TreeNode implements Serializable {

    private boolean isExpand;

    private int id;
    private int pid = 0;
    private int level;
    private int icon;

    private TreeNode parent;
    private OrgNode orgNode;
    private List<TreeNode> children = new ArrayList<>();

    public TreeNode(int id, int pid, OrgNode orgNode) {
        this.id = id;
        this.pid = pid;
        this.orgNode = orgNode;
        this.level = orgNode.getLevel();
    }

    /*-------------- functional methods ---------------*/

    public boolean isRoot() {
        return parent == null;
    }

    public boolean isParentExpand() {
        if (isRoot()) {
            return false;
        }

        return parent.isExpand();
    }

    public boolean isLeaf() {
        return children.size() == 0;
    }

    /*-------------- OrgNode setters and getters ------------*/

    /**
     * Use TreeNode as wrapper of OrgNode, members wrapped is below:
     * todo
     * priority
     * timestamp
     * header
     * body
     */

    public String getTodo() {
        return orgNode.getTodo();
    }

    public void setTodo(String todo) {
        orgNode.setTodo(todo);
    }

    public String getPriority() {
        //UNIMPLEMENTED
        return null;
    }

    public void setPriority(String priority) {
        //UNIMPLEMENTED
    }

    public LocalDateTime getTimestamp(OrgTimestamp.Type type) {
        if (type == null)
            return null;

        for (OrgTimestamp t : orgNode.getTimestamps()) {
            if (type.equals(t.getType())) {
                return t.getDate();
            }
        }

        return null;
    }

    public void setTimestamp(LocalDateTime dateTime, OrgTimestamp.Type type) {
        if (dateTime == null || type == null)
            return;

        OrgTimestamp timestamp = null;

        for (OrgTimestamp t : orgNode.getTimestamps()) {
            if (type.equals(t.getType())) {
                timestamp = t;
            }
        }

        if (timestamp == null) {
            OrgTimestamp ts = new OrgTimestamp("[", OrgTimestamp.Type.SCHEDULED.toString(),
                    dateTime.toString("yyyy-MM-dd"), dateTime.toString("HH:mm"), null, null, null);
            orgNode.addTimestamp(ts);

        } else {
            timestamp.setDate(dateTime, true);
        }
    }

    public String getHeader() {
        return orgNode.getTitle();
    }

    public void setHeader(String header) {
        orgNode.setTitle(header);
    }

    public String getBody() {
        return orgNode.getBody();
    }

    public void setBody(String body) {
        orgNode.setBody(body);
    }

    /*-------------- TreeNode setters and getters ---------------*/

    public boolean isExpand() {
        return this.isExpand;
    }

    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;

        if (!isExpand) {
            for (TreeNode treeNode : children) {
                treeNode.setExpand(isExpand);
            }
        }
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPid() {
        return this.pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public int getLevel() {
        return this.level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getIcon() {
        return this.icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public TreeNode getParent() {
        return this.parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public OrgNode getOrgNode() {
        return orgNode;
    }

    public void setOrgNode(OrgNode orgNode) {
        this.orgNode = orgNode;
    }

    public List<TreeNode> getChildren() {
        return this.children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

}