/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * .java
 *
 * Created on 10.02.2010, 23:54:00
 */

package csa.gui.components;

import csa.config.Configuration;
import csa.config.theme.Theme;
import java.awt.*;
import java.beans.PropertyVetoException;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicInternalFrameUI;

/** http://www.koders.com/java/fid0EF14FD482B41C1FBD92062767CBB1457E004E63.aspx
 *
 * @author Malban
 */
public class CSAMacMenuInternalFrame extends CSAInternalFrame 
{
    CSAView mParent =null;
    javax.swing.JComponent mPanel;
    protected javax.swing.JDesktopPane m_desktop;
    protected boolean m_undecorated;

    /** Creates new form JPortalInternalFrame */
    public CSAMacMenuInternalFrame() 
    {
        this.setRootPaneCheckingEnabled(false);
        initComponents();
        Theme t = Configuration.getConfiguration().getCurrentTheme();
        
        setFrameIcon(new javax.swing.ImageIcon(t.getImage("BlueIconSmall.png")));
    }

    public void setParent(CSAView p)
    {
        mParent=p;
    }
    
    /** 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 Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setClosable(true);
        setResizable(true);
        setPreferredSize(new java.awt.Dimension(800, 600));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        if (mParent != null)
            mParent.removeInternalFrame(this);
    }//GEN-LAST:event_formInternalFrameClosed

    public void addPanel(javax.swing.JComponent panel)
    {
        mPanel = panel;
        add(panel, java.awt.BorderLayout.CENTER);
    }

    public javax.swing.JPanel getPanel()
    {
        return (javax.swing.JPanel)mPanel;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    public Dimension getPreferredSize()
    {
        Dimension prefSize=super.getPreferredSize();
        javax.swing.JDesktopPane desktop=this.getDesktopPane();
        if (desktop!=null && desktop.isShowing()) {
            Dimension size=desktop.getSize();
            Border border=getBorder();
            if (border!=null) {
                Insets margin=border.getBorderInsets(this);
                size.height-=margin.top;//margin.top+margin.bottom;
            }
            prefSize.width=Math.min(size.width, prefSize.width);
            prefSize.height=Math.min(size.height, prefSize.height);
        }
        return prefSize;
    }
    
    public void setState(int state)
    {
        switch(state){
            case Frame.NORMAL:
                try {
                    this.setIcon(false);
                    this.setMaximum(false);
                } catch (PropertyVetoException e) {
                    Logger.global.throwing(getClass().getName(), "setState", e);
                }
                break;
            case Frame.ICONIFIED:
                try {
                    this.setIcon(true);
                } catch (PropertyVetoException e) {
                    Logger.global.throwing(getClass().getName(), "setState", e);
                }
                break;
            case Frame.MAXIMIZED_BOTH:
                try {
                    this.setMaximum(true);
                } catch (PropertyVetoException e) {
                    Logger.global.throwing(getClass().getName(), "setState", e);
                }
                break;
        }
    }

    public int getState()
    {
        if (isMaximum()) 
        {
            return Frame.MAXIMIZED_BOTH;
        } else if (isIcon()) {
            return Frame.ICONIFIED;
        } else {
            return Frame.NORMAL;
        }
    }
    
    public void setLocationRelativeTo(Component c)
    {
        Container root=null;

        if (c != null) {
            if (c instanceof JInternalFrame) {
               root = (Container)c;
            } else {
                Container parent;
                for(parent = c.getParent() ; parent != null ; parent = parent.getParent()) {
                    if (parent instanceof JInternalFrame) {
                        root = parent;
                        break;
                    }
                }
            }
        }

        if(root == null) {
            javax.swing.JDesktopPane desktop = this.getDesktopPane();
            Dimension paneSize = getSize();
            Dimension screenSize = desktop.getSize();
            int locationX=0;
            int locationY=0;
            if (screenSize.width>paneSize.width){
                locationX=(screenSize.width - paneSize.width)/2;
            }
            if (screenSize.width>paneSize.width){
                locationY=(screenSize.height - paneSize.height)/2;
            }
            setLocation(locationX, locationY);
        } else {
            Dimension invokerSize = c.getSize();
            Point invokerScreenLocation;

            // If this method is called directly after a call to
            // setLocation() on the "root", getLocationOnScreen()
            // may return stale results (Bug#4181562), so we walk
            // up the tree to calculate the position instead
            // (unless "root" is an applet, where we cannot walk
            // all the way up to a toplevel window)
            //
            if (root instanceof java.applet.Applet) {
                invokerScreenLocation = c.getLocationOnScreen();
            } else {
                invokerScreenLocation = new Point(0,0);
                Component tc = c;
                while (tc != null) {
                    Point tcl = tc.getLocation();
                    invokerScreenLocation.x += tcl.x;
                    invokerScreenLocation.y += tcl.y;
                    if (tc == root) {
                        break;
                    }
                    tc = tc.getParent();  
                }              

                Rectangle           dialogBounds = getBounds();
                int                 dx = invokerScreenLocation.x+((invokerSize.width-dialogBounds.width)>>1);
                int                 dy = invokerScreenLocation.y+((invokerSize.height - dialogBounds.height)>>1);
                Dimension           ss = getToolkit().getScreenSize();

                if (dy+dialogBounds.height>ss.height) {
                    dy = ss.height-dialogBounds.height;
                    dx = invokerScreenLocation.x<(ss.width>>1) ? invokerScreenLocation.x+invokerSize.width :
                        invokerScreenLocation.x-dialogBounds.width;
                }
                if (dx+dialogBounds.width>ss.width) dx = ss.width-dialogBounds.width;
                if (dx<0) dx = 0;
                if (dy<0) dy = 0;
                setLocation(dx, dy);
            }
        }
    }
    
    public boolean isUndecorated()
    {
        return m_undecorated;
    }
    
    public void setUndecorated(boolean undecorated)
    {
        if (m_undecorated!=undecorated) {
            m_undecorated=undecorated;
            BasicInternalFrameUI ui=(BasicInternalFrameUI)getUI();
            if (undecorated) {
                putClientProperty("titlePane", ui.getNorthPane());
                putClientProperty("border", getBorder());
                ui.setNorthPane(null);
                setBorder(null);
            } else {
                ui.setNorthPane((JComponent)getClientProperty("titlePane"));
                setBorder((Border)getClientProperty("border"));
                putClientProperty("titlePane", null);
                putClientProperty("border", null);
            }
        }
        setResizable(!undecorated);
    }
}
