/*
 * 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 explorer;
import com.ultraswing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 * This demo creates a simple UI similar to windows explorer 
 */
public class Explorer 
{
    private static TitlePanel _treePanel;
    private static CFrame _frame;
    private static CSplitPane _splitPane;
    private static ToolBarToggleButton _folderList;   
    
    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( "Explorer demo" );
        JMenuBar menuBar = createMenus();
        frame.setJMenuBar( menuBar );
        
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        frame.getContentPane().add( content, BorderLayout.CENTER );

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

        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.addStatusBar(status);
    }

    public static final int LARGE_ICON = 0;
    public static final int SMALL_ICON = 1;
    public static final int LIST_ICON  = 2;
    public static final int DETAIL_ICON = 3;

    private static CToolBar createToolBar()
    {
        CToolBar toolbar = new CToolBar();
        toolbar.setFloatable(false);
        DropDownButton dropBtn = new DropDownButton(null, getImage("tree.jpg"));
        dropBtn.setSeparateButton(false);
        JPopupMenu popup = new JPopupMenu();
        JMenuItem large = popup.add(new JMenuItem("Large Icons"));
        JMenuItem small = popup.add(new JMenuItem("Small Icons"));
        JMenuItem list  = popup.add(new JMenuItem("List"));
        JMenuItem details = popup.add(new JMenuItem("Details"));
        
        large.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)  
            {
                setPaneStyle(LARGE_ICON);
                System.out.println( "'Large Icons' menu item clicked" );
            }
        });
        small.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)  
            {
                setPaneStyle(SMALL_ICON);
            }
        });
        list.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)  
            {
                setPaneStyle(LIST_ICON);
            }
        });
        details.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)  
            {
                setPaneStyle(DETAIL_ICON);
            }
        });

        dropBtn.setPopupMenu(popup);

        _folderList = new ToolBarToggleButton("Folder List");
        _folderList.setState(false);
        _folderList.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println( "treePanel isVisible=" + _treePanel.isVisible());
                if(!_treePanel.isRemoved())
                {
                    _treePanel.removeFromParentPanel();
                }
                else
                {
                    _treePanel.restore();
                }
            }
        });
        toolbar.add(_folderList); 
        toolbar.add(dropBtn);
        return toolbar;
    }

    private static JPanel createMainWindow()
    {
        CSplitPane panel = new CSplitPane(true, 4);
        panel.setRemoveFromParent(false);//never remove this CSplitPane from its parent container
        _splitPane = panel;
        panel.addPane(createFolderList(panel, 1), 1);
        panel.addPane(createList(IconList.LARGE_ICON_VIEW_STYLE), 4);
        // panel.addPane(createDetailPanel(), 4);
        // panel.addPane(createLargeIconPanel(), 4);
        return panel;
    }
 
    private static void setPaneStyle(int style)
    {
         Component[] comps = _splitPane.getPanes();
         _splitPane.removePane(comps[comps.length - 1]);
         JComponent comp = null;
         if(style == LARGE_ICON)
         {
             comp = createList(IconList.LARGE_ICON_VIEW_STYLE);
         }
         else if(style == SMALL_ICON)
         {
             comp = createList(IconList.SMALL_ICON_VIEW_STYLE);
         }
         else if(style == LIST_ICON)
         {
             comp = createList(IconList.LIST_VIEW_STYLE);
         }
         else if(style == DETAIL_ICON)
         {
             comp = createDetailPanel();
         }

         _splitPane.addPane(comp, 4);
         _splitPane.revalidate();
         _splitPane.repaint();
    }
    
    private static JScrollPane _SMALL_ICON_LIST_PANE;
    private static JScrollPane _LARGE_ICON_LIST_PANE;
    private static JScrollPane _LIST_ICON_LIST_PANE;
    private static JScrollPane createList(int style) 
    {
        if(style == IconList.LARGE_ICON_VIEW_STYLE && _LARGE_ICON_LIST_PANE != null) return _LARGE_ICON_LIST_PANE;
        if(style == IconList.SMALL_ICON_VIEW_STYLE && _SMALL_ICON_LIST_PANE != null) return _SMALL_ICON_LIST_PANE;
        if(style == IconList.LIST_VIEW_STYLE && _LIST_ICON_LIST_PANE != null) return _LIST_ICON_LIST_PANE;

        DefaultIconText [] items =
        {
            new DefaultIconText(getImage("new.gif"), "1.new"),
            new DefaultIconText( getImage("open.gif"), "2.open"),
            new DefaultIconText( getImage("save.gif"), "3.save"),
            new DefaultIconText(getImage("new.gif"), "4.new"),
            new DefaultIconText( getImage("open.gif"), "5.open"),
            new DefaultIconText(getImage("new.gif"), "1.new"),
            new DefaultIconText( getImage("open.gif"), "2.open"),
            new DefaultIconText( getImage("save.gif"), "3.save"),
            new DefaultIconText(getImage("new.gif"), "4.new"),
            new DefaultIconText( getImage("open.gif"), "5.open"),
            new DefaultIconText(getImage("new.gif"), "1.new"),
            new DefaultIconText( getImage("open.gif"), "2.open"),
            new DefaultIconText( getImage("save.gif"), "3.save"),
            new DefaultIconText(getImage("new.gif"), "4.new"),
            new DefaultIconText( getImage("open.gif"), "5.open"),
            new DefaultIconText(getImage("new.gif"), "1.new"),
            new DefaultIconText( getImage("open.gif"), "2.open"),
            new DefaultIconText( getImage("save.gif"), "3.save"),
            new DefaultIconText(getImage("new.gif"), "4.new"),
            new DefaultIconText( getImage("open.gif"), "5.open"),
            new DefaultIconText(getImage("new.gif"), "1.new"),
            new DefaultIconText( getImage("open.gif"), "2.open"),
            new DefaultIconText( getImage("save.gif"), "3.save"),
            new DefaultIconText(getImage("new.gif"), "4.new"),
            new DefaultIconText( getImage("open.gif"), "5.open"),
            new DefaultIconText(getImage("new.gif"), "1.new"),
            new DefaultIconText( getImage("open.gif"), "2.open"),
            new DefaultIconText( getImage("save.gif"), "3.save"),
            new DefaultIconText(getImage("new.gif"), "4.new"),
            new DefaultIconText( getImage("open.gif"), "5.open")
        };
        IconList list = new IconList(items);
        list.setBackground(Color.white);
        list.setStyle(style);
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
        JScrollPane ret = new JScrollPane(list);

        if(style == IconList.LARGE_ICON_VIEW_STYLE) _LARGE_ICON_LIST_PANE = ret;
        if(style == IconList.SMALL_ICON_VIEW_STYLE) _SMALL_ICON_LIST_PANE = ret;
        if(style == IconList.LIST_VIEW_STYLE) _LIST_ICON_LIST_PANE = ret;

        return ret;
    }

    private static JPanel createFolderList(CSplitPane split, int weight)
    {
        TitlePanel panel = new TitlePanel(split, "Folder List", true, weight)
        {
            public void onClose()
            {
                _folderList.setState(true);
            }
        };
        JTree tree = new JTree();
        tree.setBorder(SwingUtil.createRaisedBorder());
        panel.add(tree, BorderLayout.CENTER);
        _treePanel = panel;
        return panel;
    }
    
    private static JPanel _DETAIL_PANE;
    private static JPanel createDetailPanel()
    {
        if(_DETAIL_PANE != null) return _DETAIL_PANE;
        CListView listview = new CListView();
        CListReportCtrl ctrl = listview.getReportCtrl();
        ctrl.setSortEnabled(true);
        ctrl.setHorizontalScrollBarEnabled(true);
        ctrl.addColumnHeader("Name", SwingConstants.LEFT, 120);
        ctrl.addColumnHeader("Size", SwingConstants.RIGHT, 60);
        ctrl.addColumnHeader("Type", SwingConstants.LEFT, 60);
        ctrl.addColumnHeader("Modified", SwingConstants.LEFT, 100);
        for (int i = 1; i <= 4 ; i++) 
        {
            ctrl.addItem("Name " + i, null);
            ctrl.addItem("" + i + "GB", null, SwingConstants.RIGHT);
            ctrl.addItem("Type " + i, null);
            ctrl.addItem("1/" + i +"/2003", null);
        }

        ctrl.addEventListener(new CListCtrlEventListenerImpl());
        _DETAIL_PANE = listview;
        return listview;
    }

    static class CListCtrlEventListenerImpl implements CListReportCtrlEventListener
    {
       public  void valueChanged(CListReportCtrlEvent e)  
       {
            CListReportCtrl ctrl = (CListReportCtrl) e.getSource();
            int [] selected = ctrl.getSelectedRows();
            for (int i = 0; i < selected.length ; i++) 
            {
                String text = ((JLabel)ctrl.getCellAt(selected[i], 3)).getText();
                _frame.getStatusBar().setStatusMessage(text);
            }
       }
    }
    
    /**
     * 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(Explorer.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 void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            System.out.println( "setLookAndFeel failed ..........................." );
        }
        _frame = createFrame();
    }

    public static CFrame getFrame()
    {
        return _frame;
    }
    
    public static void setFrame(CFrame f)
    {
        _frame = f;
    }
}// end of class Explorer