Workspace Plugin Example

package com.plugins;

import java.io.File;
import java.text.SimpleDateFormat;
import com.treestar.lib.PluginHelper;
import com.treestar.lib.core.WorkspacePluginInterface;
import com.treestar.lib.fjml.types.FileTypes;
import com.treestar.lib.prefs.HomeEnv;
import com.treestar.lib.xml.SElement;
import com.treestar.lib.xml.XMLUtil;

public class ServerPluginTest implements WorkspacePluginInterface {

    private String opened;
    @Override
    public String getServerUrl() {
        return "http://localhost:8080/ServerPluginTest";
    }

    @Override
    public SElement getElement() {
        SElement result = new SElement("ServerPluginTest");
        if (opened != null && !opened.isEmpty())
            result.setString("opened", opened);
        return result;
    }

    @Override
    public boolean openWorkspace(SElement workspaceElement) {
        opened = getDateStamp();
        return true;
    }

    private File getOutputFolder(SElement workspaceElement) {
        File file = PluginHelper.getWorkspaceAnalysisFolder(workspaceElement);
        if (file != null && file.exists())
            return file;
        String home = HomeEnv.getInstance().getUserHomeFolder();
        File homeFolder = new File(home);
        if (homeFolder.exists())
            return homeFolder;
        return null;
    }
    private String getDateStamp() {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, MMM d, yyyy HH:mm:ss z");
        return dateFormatter.format(System.currentTimeMillis());
    }
    @Override
    public void save(SElement workspaceElement) {
        String name = "ServerPluginTest";
        File folder = getOutputFolder(workspaceElement);
        File saveFile = new File(folder, name + FileTypes.XML_SUFFIX);
        int ct = 1;
        while (saveFile.exists()){
            String nextName = name + "." + ct++;
            saveFile = new File(folder, nextName + FileTypes.XML_SUFFIX);
        }
        SElement out = getElement();
        SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, MMM d, yyyy HH:mm:ss z");
        out.setString("saved", dateFormatter.format(System.currentTimeMillis()));
        new XMLUtil().write(out, saveFile);
    }

    @Override
    public void endSession() { }

    @Override
    public String getVersion() {
        return "1.0";    
    }
}

results matching ""

    No results matching ""