/*
 * 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 demo creates a simple UI similar to some IDEs
 */
public class AutoHide
{
    private static final Color BACKGROUND_COLOR = new Color(245, 245, 245);
    private static CFrame _frame;
    private static CSplitPane _splitPane1;
    private static CSplitPane _splitPane2;
    private static VerticalBar _verticalBar1;
    private static VerticalBar _verticalBar2;
    private static CToolBar _horizontalBar;
    private static AutoHideDockablePanel _dock1;
    private static AutoHideDockablePanel _dock2;
    private static AutoHideDockablePanel _dock3;
    private static AutoHideDockablePanel _dock4;

    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( "AutoHide demo" );
        JMenuBar menuBar = createMenus();
        frame.setJMenuBar( menuBar );
         
        CToolBar toolbar = createToolBar();
        // toolbar.add(new ToolBarButton("open"));
        frame.getContentPane().add(toolbar, BorderLayout.NORTH);
        
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        frame.getContentPane().add( content, BorderLayout.CENTER );

        content.add(createMainWindow(), BorderLayout.CENTER);

        _verticalBar1 = new VerticalBar();
        _verticalBar2 = new VerticalBar();  
        _verticalBar1.setBackground(BACKGROUND_COLOR);
        _verticalBar1.setBorder(null);
        _verticalBar2.setBackground(BACKGROUND_COLOR);
        _verticalBar2.setBorder(null);
        
        FlatButton vbtn = new FlatButton("Project", getImage("project.gif"));
        vbtn.setBackground(BACKGROUND_COLOR);
        vbtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                onClick1(e); 
            }
        });
        _verticalBar1.add(vbtn);

        vbtn = new FlatButton("Structure", getImage("structure.gif"));
        vbtn.setBackground(BACKGROUND_COLOR);
        vbtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                onClick2(e);
            }
        });
        _verticalBar1.add(vbtn);

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

        vbtn = new FlatButton("Commander", getImage("commander.gif"));
        vbtn.setBackground(BACKGROUND_COLOR);
        vbtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                onClick3(e);
            }
        });
        _verticalBar2.add(vbtn);

        frame.getContentPane().add(_verticalBar2, BorderLayout.EAST); 
        
        _horizontalBar = new CToolBar();
        _horizontalBar.setFloatable(false);
        _horizontalBar.setBackground(BACKGROUND_COLOR);
        _horizontalBar.setBorder(null);
        FlatButton btn = new FlatButton("Find", getImage("find.gif"));
        btn.setBackground(BACKGROUND_COLOR);
        btn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                onClick4(e);
            }
        });
        _horizontalBar.add(btn);

        content.add(_horizontalBar, BorderLayout.SOUTH);
        StatusBar status = new StatusBar( new JComponent[]
                {StatusBar.INDICATOR_STATUS,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_DATE,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_TIME,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_JVM
                 }, true);
        
        frame.getContentPane().add(status, BorderLayout.SOUTH);
    }


    private static void onClick1(ActionEvent e)
    {
        if(_dock1 == null)
        {
            _dock1 = createDockPanel("Project", true);
            _dock1.addDockingListener(new DockActionListener(e.getSource()));
            Point pt = _verticalBar1.getLocationOnScreen();
            int width = _verticalBar1.getSize().width;
            int height = _verticalBar1.getSize().height;
            _dock1.doFloat(new Point(pt.x + width, pt.y), new Dimension(200, height - _horizontalBar.getSize().height));
        }
        else
        {
            if(_dock1.isShowing())
            {
                _dock1.close();
            }
            else
            {
                _dock1.restore();
            }
        }
    }

    private static void onClick2(ActionEvent e)
    {
        if(_dock2 == null)
        {
            _dock2 = createDockPanel("Structure", true);
            _dock2.addDockingListener(new DockActionListener(e.getSource()));
            Point pt = _verticalBar1.getLocationOnScreen();
            int width = _verticalBar1.getSize().width;
            int height = _verticalBar1.getSize().height;
            _dock2.doFloat(new Point(pt.x + width, pt.y), new Dimension(200, height - _horizontalBar.getSize().height));
        }
        else
        {
            if(_dock2.isShowing())
            {
                _dock2.close();
            }
            else
            {
                _dock2.restore();
            }
        }
    }

    private static void onClick3(ActionEvent e)
    {
        if(_dock3 == null)
        {
            _dock3 = createDockPanel("Commander", false);
            _dock3.addDockingListener(new DockActionListener(e.getSource()));
            Point pt = _verticalBar2.getLocationOnScreen();
            int height = _verticalBar2.getSize().height;
            _dock3.doFloat(new Point(pt.x - 200, pt.y), new Dimension(200, height - _horizontalBar.getSize().height));
        }
        else
        {
            if(_dock3.isShowing())
            {
                _dock3.close();
            }
            else
            {
                _dock3.restore();
            }
        }
    }

    private static void onClick4(ActionEvent e)
    {
        if(_dock4 == null)
        {
            _dock4 = createDockPanel2();
            _dock4.addDockingListener(new DockActionListener(e.getSource()));
            Point pt = _horizontalBar.getLocationOnScreen();
            int width = _horizontalBar.getSize().width;
            _dock4.doFloat(new Point(pt.x , pt.y - 200), new Dimension(width, 200));
        }
        else
        {
            if(_dock4.isShowing())
            {
                _dock4.close();
            }
            else
            {
                _dock4.restore();
            }
        }
    }

    static class DockActionListener implements ActionListener
    {
        JComponent comp;
        Color old;
        public DockActionListener(Object src)
        {
            comp = (JComponent)src;
            old = comp.getBackground();
        }
        
        public void actionPerformed(ActionEvent e)  
        {
            DockingEvent de = (DockingEvent) e;
            int id = de.getID();
            if(id == DockingEvent.COMPONENT_HIDDEN)
            {
                comp.setBackground(old);
            }
            else 
            {
                comp.setBackground(Color.gray.brighter());
            }
        }
    }
    
    private static AutoHideDockablePanel createDockPanel(String title, boolean isFirst)
    {
        JTextArea edit = new JTextArea();
        edit.setBorder(SwingUtil.createLoweredBorder());
        AutoHideDockablePanel dock = new AutoHideDockablePanel(edit,
                title, _splitPane1, 1, isFirst);
        return dock;
    }

    private static AutoHideDockablePanel createDockPanel2()
    {
        JTextArea edit = new JTextArea();
        edit.setBorder(SwingUtil.createLoweredBorder());
        AutoHideDockablePanel dock = new AutoHideDockablePanel(edit,
                "Search Result", _splitPane2, 1, false);
        return dock;
    }

    private static JPanel createMainWindow()
    {
        CSplitPane panel = new CSplitPane(true);
        panel.setRemoveFromParent(false);//never remove this CSplitPane from its parent container
        _splitPane1 = panel;
        JTextArea text = new JTextArea();
        text.setBackground(new Color(233, 233, 233));
        panel.addPane(text, 3);

        _splitPane2 = new CSplitPane(false);
        _splitPane2.addPane(panel, 3);
        return _splitPane2;
    }

    /**
     * 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(AutoHide.class.getResource( "/images/" + imageFileName ) );
    }

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

    private static CToolBar createToolBar()
    {
        CToolBar toolbar = new CToolBar();
        toolbar.setBorder(null);
        toolbar.add(new ToolBarButton(getImage("new.gif")));
        toolbar.add(new ToolBarButton(getImage("open.gif")));
        toolbar.add(new ToolBarButton(getImage("save.gif")));
        toolbar.addSeparator();
        toolbar.add(new ToolBarButton(getImage("cut.gif")));
        toolbar.add(new ToolBarButton(getImage("copy.gif")));
        toolbar.add(new ToolBarButton(getImage("paste.gif")));
        toolbar.addSeparator();
        toolbar.add(new ToolBarButton(getImage("refresh.gif")));
        toolbar.add(new ToolBarButton(getImage("print.gif")));
        return toolbar;
    }
    
    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 AutoHide