/*
 * Copyright (c) 2002 iReasoning Networks. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by iReasoning Networks as part
 * of an iReasoning Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of iReasoning Networks.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD IREASONING SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */



package dockAndFloat;
import com.ultraswing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 * This class demonstrates  dockable/floatable panels
 */
public class Dock
{
    private static CFrame _frame;
    private static VerticalBar _verticalBar;
    private static SideBar _sidebar1;
    private static SideBar _sidebar2;
    private static DockablePanel _dock1;

    private static CFrame createFrame()
    {
        CFrame frame = new CFrame();
        createFrame(frame);
        frame.setSize(800, 600);
        frame.centerOnScreen();
        frame.setVisible(true);
        frame.setExitOnClose();
        return frame;
    }

    private static void createFrame(CFrame frame)
    {
        frame.setTitle( "Docking Demo" );
        JMenuBar menuBar = createMenus();
        frame.setJMenuBar( menuBar );

        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        frame.getContentPane().add( content, BorderLayout.CENTER );

        content.add(createMainWindow(), BorderLayout.CENTER);
        _verticalBar = new VerticalBar();
        _verticalBar.setBackground(Color.white);
        _verticalBar.setBorder(null);
        FlatButton vbtn = new FlatButton("Restore sidebar 1", getImage("project.gif"));
        vbtn.setBackground(Color.white);
        vbtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _sidebar1.restore();
            }
        });
        _verticalBar.add(vbtn);

        vbtn = new FlatButton("Restore sidebar 2", getImage("structure.gif"));
        vbtn.setBackground(Color.white);
        vbtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _sidebar2.restore();
            }
        });
        _verticalBar.add(vbtn);

        vbtn = new FlatButton("Restore Dock 1", getImage("outline.gif"));
        vbtn.setBackground(Color.white);
        vbtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                _dock1.restore();
            }
        });
        _verticalBar.add(vbtn);

        frame.getContentPane().add(_verticalBar, BorderLayout.WEST);

        StatusBar status = new StatusBar( new JComponent[]
                {StatusBar.INDICATOR_STATUS,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_DATE,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_TIME,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_JVM
                 });
        frame.addStatusBar(status);
    }

    private static JPanel createMainWindow()
    {
        CSplitPane bigpane = new CSplitPane(false);
        CSplitPane paneUp = new CSplitPane(true);
        paneUp.addPane(new JTextArea("You can click on the title bars to move all the windowns"));
        bigpane.addPane(paneUp);

        CSplitPane paneDown = new CSplitPane(true);
        _sidebar1 = new SideBar("Sidebar 1", getImage("project.gif"), false);
        _sidebar1.setTabPlacement(JTabbedPane.BOTTOM);
        JPanel p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        p1.add(new JTextArea("text"), BorderLayout.CENTER);
        _sidebar1.addPlugin(p1, "tab 1", getImage("structure.gif"));
        _sidebar1.addPlugin(new CPanel(), "tab 2", getImage("new2.gif"));
        _sidebar2 = new SideBar("Sidebar 2", getImage("structure.gif"), true);
        _sidebar2.addPlugin(new CPanel(), "tab 3", getImage("structure.gif"));
        _sidebar2.addPlugin(new CPanel(), "tab 4", getImage("new2.gif"));
        paneDown.addPane(_sidebar1);
        _dock1 = new DockablePanel(new JTextArea(), "Title Bar", getImage("outline.gif"), false)
        {
            public Dimension getPreferredSize()
            {
                return new Dimension(240, 160);
            }
        };
        paneDown.addPane(_dock1);
        paneDown.addPane(_sidebar2, 3);
        bigpane.addPane(paneDown);

        return bigpane;
    }

    /**
     * Create menus
     */
    private static JMenuBar createMenus()
    {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = menuBar.add( new JMenu( "File" ) );
        createMenuItem( fileMenu, "new", "new.gif");
        createMenuItem( fileMenu, "open", "open.gif");
        createMenuItem( fileMenu, "save", "save.gif");
        return menuBar;
    }

    private static ImageIcon getImage(String imageFileName)
    {
        return new ImageIcon(Dock.class.getResource( "/images/" + imageFileName ) );
    }

    /**
     * Creates a generic menu item
     */
    private static JMenuItem createMenuItem( JMenu menu, String label, String icon)
    {
        JMenuItem mi = menu.add( new JMenuItem( label  ) );
        mi.setIcon(getImage(icon));
        return mi;
    }

    public static CFrame getFrame()
    {
        return _frame;
    }

    public static void setFrame(CFrame f)
    {
        _frame = f;
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            System.out.println( "setLookAndFeel failed ..........................." );
        }
        _frame = createFrame();
    }
}// end of class Dock