/*
 *  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 java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
import javax.swing.JOptionPane;
import org.openide.NotifyDescriptor;
import org.openide.TopManager;
import org.openide.cookies.OpenCookie;
import org.openide.loaders.DataObject;
import org.openide.filesystems.FileObject;
import org.openide.nodes.Node;
import org.openide.windows.WindowManager;
import org.openide.windows.Workspace;
import org.openide.windows.TopComponent.Registry;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
/**
 *
 * @author  adams
 * @version 1.0
 */
public class InitiativeOpenSupport implements OpenCookie
{
    protected InitiativeDataObject dataObject;
    /** Creates new GazoogleOpenSupport */
    public InitiativeOpenSupport(InitiativeDataObject dObj)
    {
        dataObject = dObj;
    }
    /** for now, just pop a JOptionPane to prove this method is being called */
    public void open()
    {
        TopManager tm = TopManager.getDefault();
        // before doing anything, switch to our new Workspace:
        WindowManager wm = tm.getWindowManager();
        Registry reg = wm.getRegistry();
        Set components = reg.getOpened();
        Iterator compItr = components.iterator();
//        Workspace gamingWS = wm.findWorkspace("gaming"); //NOI18N
//        if (gamingWS != null) {
//            gamingWS.activate();
//        }
        // get the FileObject from the GazoogleDataObject reference
        // we saved in the constructor; this is the FileObject that
        // was associated with the OpenCookie (this support class)
        // when it was instantiated:
        FileObject fo = dataObject.getPrimaryFile();
        try {
            boolean found = false;
            while(compItr.hasNext()) {
                Object obj = compItr.next();
                if(obj.getClass().getName() == "org.evilsoft.gaming.Init.InitiativeTopWindow") {
                    InitiativeTopWindow initiativeWin = (InitiativeTopWindow)obj;
                    if(initiativeWin.isOpened(wm.getCurrentWorkspace())) {
                        initiativeWin.initiative.loadFromDocument(dataObject.character);
                        initiativeWin.initiative.refreshTable();
                        initiativeWin.open();
                        found = true;
                    }
                }
            }
            if(!found) {
                InitiativeTopWindow initiativeWin = new InitiativeTopWindow(dataObject);
                initiativeWin.open();
            }
//            Initiative.Initiative.loadFromDocument(dataObject.Character);
//            Initiative.Initiative.refreshTable();
        } catch (Exception e) {
            tm.notifyException(e);
        }
    }
}
