/*
 *  Initiative - A role playing utility to track turns
 *  Copyright (C) 2002 Devon D Jones
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 */
package org.evilsoft.gaming.Init;

import org.openide.TopManager;
import org.openide.modules.ModuleInstall;
import org.openide.windows.WindowManager;
import org.openide.windows.Workspace;

/** Manages a module's lifecycle.
 * Remember that an installer is optional and often not needed at all.
 *
 * @author devon
 */
public class InitiativeInstall extends ModuleInstall {
    
    // By default, do nothing but call restored ().
    public void installed () {
        // create the Workspace:
        TopManager tm = TopManager.getDefault();
        WindowManager wm = tm.getWindowManager();
        Workspace gamingWS = wm.findWorkspace("combat"); //NOI18N
        if (gamingWS == null)
        {
            Workspace newWs = wm.createWorkspace("combat", "Combat"); //NOI18N
            // to make the Workspace visible, get an array of the
            // existing Workspaces, add our new Workspace, then
            // set the Workspaces with the new array:
            Workspace oldArray[] = wm.getWorkspaces();
            Workspace newArray[] = new Workspace[oldArray.length + 1];
            System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
            newArray[oldArray.length] = newWs;
            wm.setWorkspaces(newArray);

            // now call restored, which we are not currently overriding:
            restored ();
        }
    }
    
    // By default, do nothing.
    /*
    public void restored () {
    }
     */
    
    // By default, do nothing.
    public void uninstalled () {
        // get a TopManager, then the WindowManager:
        TopManager tm = TopManager.getDefault();
        WindowManager wm = tm.getWindowManager();

        // remove the workspace from the list.  be smart
        // if it's not there, mostly because we're creating
        // a new smaller array if it is:
        Workspace oldArray[] = wm.getWorkspaces();
        int j = -1;
        for (int i=0; i<oldArray.length; i++)
        {
            if (oldArray[i].getName().equals("combat")) //NOI18N
            {
                j = i;
            }
        }
        if (j > -1)
        {
            Workspace newArray[] = new Workspace[oldArray.length - 1];
            int newCtr = 0;
            for (int i=0; i<oldArray.length; i++)
            {
                if (i != j)
                {
                    newArray[newCtr++] = oldArray[i];
                }
            }
            wm.setWorkspaces(newArray);
        }
     }
    
    // By default, call restored ().
    /*
    public void updated (int release, String specVersion) {
    }
     */
    
    // It is no longer recommended to override Externalizable methods
    // (readExternal and writeExternal). See the Modules API section on
    // "installation-clean" modules for an explanation.
    
}
