xyz.putzi.slackmc.common.messaging.builder.DefaultSlackAttachmentBuilder.java Source code

Java tutorial

Introduction

Here is the source code for xyz.putzi.slackmc.common.messaging.builder.DefaultSlackAttachmentBuilder.java

Source

/*
 * SlackMc Bukkit/Bungeecord Api - Bukkit/Bungeecord plugin for Slack
 *      Copyright (C) 2016  putzi_x  (http://putzi.xyz)
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

package xyz.putzi.slackmc.common.messaging.builder;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import xyz.putzi.slackmc.common.utils.Utils;
import xyz.putzi.slackmc.common.messaging.model.SlackAttachment;

import java.awt.*;
import java.util.Objects;

public class DefaultSlackAttachmentBuilder implements SlackAttachment {

    private String title;
    private String titleLink;
    private String fallbackText;
    private String preText;
    private String text;

    private String authorName;
    private String authorLink;
    private String authorIcon;

    private JsonArray fields;
    private String color;
    private String imageUrl;
    private String thumbUrl;

    private String footerText;
    private String footerIcon;
    private Integer timeStamp;

    private JsonArray jsonArray = new JsonArray();

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setTitleLink(String titleLink) {
        this.titleLink = titleLink;
    }

    @Override
    public void setFallbackText(String fallbackText) {
        this.fallbackText = fallbackText;
    }

    @Override
    public void setPreText(String preText) {
        this.preText = preText;
    }

    @Override
    public void setText(String text) {
        this.text = text;
    }

    @Override
    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    @Override
    public void setAuthorLink(String authorLink) {
        this.authorLink = authorLink;
    }

    @Override
    public void setAuthorIcon(String authorIcon) {
        this.authorIcon = authorIcon;
    }

    @Override
    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public void setFields(JsonArray fields) {
        this.fields = fields;
    }

    public void setColor(Color color) {
        setColor(Utils.colorToHex(color));
    }

    @Override
    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    @Override
    public void setThumbUrl(String thumbUrl) {
        this.thumbUrl = thumbUrl;
    }

    @Override
    public void setFooterText(String footerText) {
        this.footerText = footerText;
    }

    @Override
    public void setFooterIcon(String footerIcon) {
        this.footerIcon = footerIcon;
    }

    @Override
    public void setTimeStamp(Integer timeStamp) {
        this.timeStamp = timeStamp;
    }

    @Override
    public JsonArray getJsonArray() {
        Objects.requireNonNull(jsonArray, "JsonArray");

        JsonArray arrayToReturn = new JsonArray();
        arrayToReturn.addAll(jsonArray);
        jsonArray = new JsonArray();
        return arrayToReturn;
    }

    public void addToJsonArray() {
        JsonObject object = new JsonObject();

        if (title != null)
            object.addProperty("title", title);
        if (titleLink != null)
            object.addProperty("title_link", titleLink);
        if (preText != null)
            object.addProperty("pretext", preText);
        if (fallbackText != null)
            object.addProperty("fallback", fallbackText);
        if (text != null)
            object.addProperty("text", text);
        if (authorName != null)
            object.addProperty("author_name", authorName);
        if (authorLink != null)
            object.addProperty("author_link", authorLink);
        if (authorIcon != null)
            object.addProperty("author_icon", authorIcon);
        if (color != null)
            object.addProperty("color", color);
        if (fields != null)
            object.add("fields", fields);
        if (imageUrl != null)
            object.addProperty("image_url", imageUrl);
        if (thumbUrl != null)
            object.addProperty("thumb_url", thumbUrl);
        if (footerText != null)
            object.addProperty("footer", footerText);
        if (footerIcon != null)
            object.addProperty("footer_icon", footerIcon);
        if (timeStamp != null)
            object.addProperty("ts", timeStamp);

        jsonArray.add(object);

    }
}