import driftwood.data.*;
import driftwood.gui.*;
import driftwood.r3.*;
import driftwood.util.*;
import king.*;
import king.core.*;

help() {
    print("==== PREDEFINED VARIABLES ====");
    print("parent                   ToolBox containing the BeanKing plugin");
    print("kMain                    KingMain for this application");
    print("kCanvas                  KinCanvas for drawing operations");
    print("services                 ToolServices for picking, etc.");
    print("");
    print("==== PREDEFINED FUNCTIONS ====");
    print("help()                   this help text");
    print("editor()                 GUI for writing short scripts");
    print("setAccessibility(true)   allow access to non-public fields and methods");
    print("");
    print("bleach()                 removes point colors for all visible");
    print("delete(name?)            deletes all groups/subgroups/lists with given name");
    print("makeMaster(name?)        creates master for named group/subgroup/list");
    print("killMaster(name?)        removes all uses of named master");
    print("killAllMasters()         removes all masters from the kinemage completely");
    print("");
    print("source(getResource(\"/movies.bsh\"));");
    print("");
}
help(); // display this message on startup!

bleach() {
    kin = kMain.getKinemage();
    if(kin == null) return;
    for(pt : KIterator.visiblePoints(kin))
        pt.setColor(null);
}

delete()
{ delete(JOptionPane.showInputDialog("Name to delete:")); }

delete(String delName)
{
    if(delName == null) return;
    kin = kMain.getKinemage();
    if(kin == null) return;
    for(iter = KIterator.allNonPoints(kin); iter.hasNext(); )
    {
        age = iter.next();
        if(delName.equals(age.getName())) iter.remove();
    }
}

makeMaster()
{ makeMaster(JOptionPane.showInputDialog("Name to create master for:")); }

makeMaster(String masterName)
{
    if(masterName == null) return;
    kin = kMain.getKinemage();
    if(kin == null) return;
    for(age : KIterator.allNonPoints(kin))
    {
        if(masterName.equals(age.getName())) age.addMaster(masterName);
    }
}

killMaster()
{ killMaster(JOptionPane.showInputDialog("Name of master to delete:")); }

killMaster(String masterName)
{
    if(masterName == null) return;
    kin = kMain.getKinemage();
    if(kin == null) return;
    for(age : KIterator.allNonPoints(kin))
    {
        age.removeMaster(masterName);
    }
    kin.removeUnusedMasters();
}

killAllMasters()
{
    kin = kMain.getKinemage();
    if(kin == null) return;
    masters = new ArrayList(kin.masterList());
    for(m : masters)
    {
        killMaster(m.getName());
    }
}

