/*
 *  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.util.NbBundle;
import org.openide.windows.*;


// As needed:

import java.awt.Toolkit;
//import java.io.*;
import java.net.*;
import org.openide.TopManager;
import org.openide.loaders.DataObject;
//import org.openide.actions.*;
//import org.openide.awt.UndoRedo;
//import org.openide.nodes.Node;
//import org.openide.util.HelpCtx;
//import org.openide.util.actions.*;
//import org.openide.util.datatransfer.PasteType;
//import org.openide.util.io.*;
import java.util.prefs.*;

/** An openable window available to the IDE's window manager.
 *
 * @author  devon
 */
public class InitiativeTopWindow extends TopComponent /* or CloneableTopComponent */ {
    
    // REMEMBER: You should have a public default constructor!
    // This is for externalization. If you have a nondefault
    // constructor for normal creation of the component, leave
    // in a default constructor that will put the component into
    // a consistent but unconfigured state, and make sure readExternal
    // initializes it properly. Or, be creative with writeReplace().
    public InitiativeTopWindow() {
        initComponents();
        initRest();
        setCloseOperation(CLOSE_LAST); // or CLOSE_EACH
        // Display name of this window (not needed if you use the DataObject constructor):
        setName(NbBundle.getMessage(InitiativeTopWindow.class, "LBL_component_name"));
        // You may set the icon, but often it is better to set the icon for an associated mode instead:
        try {
            setIcon (Toolkit.getDefaultToolkit ().getImage(new URL ("nbresloc:/org/evilsoft/gaming/Init/Initiative.gif")));
        }
        catch(Exception e) {
        }
        // Use the Component Inspector to set tool-tip text. This will be saved
        // automatically. Other JComponent properties you may need to save yuorself.
        // At any time you can affect the node selection:
        // setActivatedNodes (new Node[] { ... });
    }
    
    public InitiativeTopWindow(DataObject obj) {
        super(obj);
        initComponents();
        initRest();
        setCloseOperation(CLOSE_LAST); // or CLOSE_EACH
        // Display name of this window (not needed if you use the DataObject constructor):
        setName(NbBundle.getMessage(InitiativeTopWindow.class, "LBL_component_name"));
        // You may set the icon, but often it is better to set the icon for an associated mode instead:
        try {
            setIcon (Toolkit.getDefaultToolkit ().getImage(new URL ("nbresloc:/org/evilsoft/gaming/Init/Initiative.gif")));
            InitiativeDataObject dataObject = (InitiativeDataObject)obj;
            initiative.loadFromDocument(dataObject.character);
            initiative.refreshTable();
        }
        catch(Exception e) {
            TopManager tm = TopManager.getDefault();
            tm.notifyException(e);
        }
        // Use the Component Inspector to set tool-tip text. This will be saved
        // automatically. Other JComponent properties you may need to save yuorself.
        // At any time you can affect the node selection:
        // setActivatedNodes (new Node[] { ... });
    }
    
    /*
    public HelpCtx getHelpCtx () {
        return new HelpCtx (InitiativeTopWindow.class);
    }
     */
    
    /*
    // If you are using CloneableTopComponent, probably you should override:
    protected CloneableTopComponent createClonedObject () {
        return new InitiativeTopWindow ();
    }
     */
    public boolean canClose (Workspace workspace, boolean last) {
        // You might want to prompt the user first and maybe return false:
        initiative.setExitPrefs();
        initPrefs.putInt("WindowX", this.getX());
        initPrefs.putInt("WindowY", this.getY());
        initPrefs.putInt("WindowWidth", this.getSize().width);
        initPrefs.putInt("WindowHeight", this.getSize().height);
        return true;
    }
    
    // APPEARANCE
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents

        setLayout(new java.awt.BorderLayout());

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public Initiative initiative;
    private Preferences initPrefs = Preferences.userNodeForPackage(Initiative.class);

    private void initRest() {
        initiative = new Initiative();
        add(initiative);

        int iWinX = initPrefs.getInt("WindowX", 0);
        int iWinY = initPrefs.getInt("WindowY", 0);
        this.setLocation(iWinX, iWinY);
       
        int iWinWidth = initPrefs.getInt("WindowWidth", 710);
        int iWinHeight = initPrefs.getInt("WindowHeight", 653);
        this.setSize(iWinWidth, iWinHeight);
    }

    public void applyPrefs() {
        initiative.applyPrefs();
    }
    // MODES AND WORKSPACES
    
    /*
    // If you want it to open in a specific mode:
    public static final String InitiativeTopWindow_MODE = "InitiativeTopWindow";
    public void open (Workspace ws) {
        super.open (ws);
        if (ws == null) ws = TopManager.getDefault ().getWindowManager ().getCurrentWorkspace ();
        Mode m = ws.findMode (InitiativeTopWindow_MODE);
        if (m == null) {
            try {
                m = ws.createMode (InitiativeTopWindow_MODE, // code name
                                   NbBundle.getMessage (InitiativeTopWindow.class, "LBL_mode_name"), // display name
                                   new URL ("nbresloc:/org/evilsoft/gaming/Init/InitiativeTopWindowIcon.gif"));
            } catch (MalformedURLException mfue) {
                TopManager.getDefault ().notifyException (mfue);
                return;
            }
            // If you want it in a specific position:
            // m.setBounds (...ws.getBounds ()...);
        }
        m.dockInto (this);
    }
     */
    
    /*
    // If you are not specifying a mode you may wish to use:
    public Dimension getPreferredSize () {
        return ...TopManager.getDefault ().getWindowManager ().getCurrentWorkspace ().getBounds ()...;
    }
     */
    
    /*
    // If you want it to open on a specific workspace:
    public static final String InitiativeTopWindow_WORKSPACE = NbBundle.getMessage (InitiativeTopWindow.class, "LBL_workspace_name");
    public void open () {
        WindowManager wm = TopManager.getDefault ().getWindowManager ();
        Workspace ws = wm.findWorkspace (InitiativeTopWindow_WORKSPACE);
        if (ws == null)
            ws = wm.createWorkspace (InitiativeTopWindow_WORKSPACE);
        open (ws);
        ws.activate ();
    }
     */
    
    // PERSISTENCE
    
    // private static final long serialVersionUID = ...;
    
    /*
    // If you wish to keep any state between IDE restarts, put it here:
    public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException {
        super.readExternal (in);
        setSomeState ((SomeType) in.readObject ());
    }
    public void writeExternal (ObjectOutput out) throws IOException {
        super.writeExternal (out);
        out.writeObject (getSomeState ());
    }
     */
    
    /*
    // The above assumes that the SomeType is safely serializable, e.g. String or Date.
    // If it is some class of your own that might change incompatibly, use e.g.:
    public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException {
        super.readExternal (in);
        NbMarshalledObject read = (NbMarshalledObject) in.readObject ();
        if (read != null) {
            try {
                setSomeState ((SomeType) read.get ());
            } catch (Exception e) {
                TopManager.getDefault ().getErrorManager ().notify (e);
                // If the problem would make this component inconsistent, use:
                // throw new SafeException (e);
            }
        }
    }
    public void writeExternal (ObjectOutput out) throws IOException {
        super.writeExternal (out);
        Object toWrite;
        try {
            toWrite = new NbMarshalledObject (getSomeState ());
        } catch (Exception e) {
            TopManager.getDefault ().getErrorManager ().notify (e);
            toWrite = null;
            // Again you may prefer to use:
            // throw new SafeException (e);
        }
        out.writeObject (toWrite);
    }
     */
    
    /*
    // Use this to discard the component after restarts (make it nonpersistent):
    private Object readResolve () throws ObjectStreamException {
        return null;
        // If you wish to conditionally discard it, make readExternal set
        // or clear some flag acc. to the condition, then use:
        // return discardFlag ? null : this;
        // Singleton component using private static InitiativeTopWindow theInstance:
        // if (theInstance == null) theInstance = this;
        // return theInstance;
    }
     */
    
    // ACTIONS
    
    /*
    // If you wish to have extra actions appear in the window's
    // popup menu, they can go here:
    public SystemAction[] getSystemActions () {
        SystemAction[] supe = super.getSystemActions ();
        SystemAction[] mine = new SystemAction[supe.length + 1];
        System.arraycopy (supe, 0, mine, 0, supe.length);
        mine[supe.length] = SystemAction.get (SomeActionOfMine.class);
        return mine;
    }
     */
    
    /*
    // Associate implementations with copying, searching, etc.:
    protected void componentActivated () {
        ((CallbackSystemAction) SystemAction.get (FindAction.class)).setActionPerformer (new ActionPerformer () {
                public void performAction (SystemAction action) {
                    // search this component somehow
                }
            });
        // similar for CopyAction, CutAction, DeleteAction, GotoAction, ReplaceAction, etc.
        // for PasteAction, use:
        // ((PasteAction) SystemAction.get (PasteAction.class)).setPasteTypes (new PasteType[] { ... });
    }
    protected void componentDeactivated () {
        // FindAction will be turned off by itself
        // ((PasteAction) SystemAction.get (PasteAction.class)).setPasteTypes (null);
    }
     */
    
    /*
    // If you want UndoAction and RedoAction to be enabled on this component:
    public UndoRedo getUndoRedo () {
        return new MyUndoRedo (this);
    }
     */
    
    // Printing, saving, compiling, etc.: use cookies on some appropriate node and
    // use this node as the node selection.
    
}
