001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.ui.components.gbody;
005    
006    import graphlab.platform.lang.Pair;
007    import graphlab.ui.components.gsidebar.GSideBarPanel;
008    
009    import javax.swing.*;
010    import java.awt.*;
011    import java.util.ArrayList;
012    import java.util.Iterator;
013    
014    /**
015     * this class is the Body of the GFrame, it is important that the word "Body" here means the body of program plus the
016     * side bar of the program, so it contains a Body pane, and a Split pane . which a vertical split is between them.
017     *
018     * @author azin azadi
019     */
020    public class GBody extends JPanel {
021        /**
022         *
023         */
024        private static final long serialVersionUID = 2389438929191166213L;
025        private JSplitPane splitPane = new JSplitPane();
026        private Component bodyPane;
027        //is side bar hidden
028        private boolean hidden = false;
029    
030        private ArrayList<Pair<Component, String>> showingSideBars = new ArrayList<Pair<Component, String>>();
031    
032        public void setBodyPane(Component bodyPane) {
033            this.bodyPane = bodyPane;
034            splitPane.setRightComponent(bodyPane);
035            if (hidden)
036                hideSideBar();
037    
038        }
039    
040        public void showSideBarPane(Component leftPanel, String label) {
041    //        int _ = splitPane.getDividerLocation();
042            Pair<Component, String> p = new Pair<Component, String>(leftPanel, label);
043            if (showingSideBars.contains(p)) {
044                return;
045            }
046            showingSideBars.add(p);
047    
048            updateEveryThingInsidebar();
049    
050    //        splitPane.setDividerLocation(_);
051    //        splitPane.setDividerLocation(leftPanel.getPreferredSize().width);
052    //        spitPane.setDividerSize(2);
053        }
054    
055        private void updateEveryThingInsidebar() {
056            JPanel sp = createTotalSideBarPanel();
057            hidden = false;
058            splitPane.setRightComponent(bodyPane);
059            splitPane.setLeftComponent(sp);
060            add(splitPane);
061            validate();
062        }
063    
064        private JPanel createTotalSideBarPanel() {
065            JPanel sp = new JPanel();
066            makeJPanelFlat(sp);
067            JPanel cur = sp;
068            Iterator<Pair<Component, String>> it = showingSideBars.iterator();
069            while (it.hasNext()) {
070                Pair<Component, String> _ = it.next();
071                GSideBarPanel sbp = new GSideBarPanel(this, _.first, _.second);
072                if (it.hasNext()) {
073                    cur.add(new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, sbp, cur = new JPanel()));
074                    makeJPanelFlat(cur);
075                } else {
076                    cur.add(sbp);
077                }
078            }
079            return sp;
080        }
081    
082        private void makeJPanelFlat(JPanel p) {
083            p.setBorder(null);
084            p.setLayout(new BorderLayout(0, 0));
085        }
086    
087        public void hideSideBar(Component c, String label) {
088            showingSideBars.remove(new Pair<Component, String>(c, label));
089            if (showingSideBars.isEmpty()) {
090                hideSideBar();
091            } else {
092                updateEveryThingInsidebar();
093            }
094    
095        }
096    
097        public void hideSideBar() {
098            splitPane.remove(bodyPane);
099            remove(splitPane);
100            add(bodyPane);
101    //        splitPane.setDividerLocation(0);
102            hidden = true;
103            validate();
104    //        splitPane.setDividerSize(0);
105        }
106    //    private Component rightPanel = new JPanel();
107    //    private Component leftPanel = new JPanel();
108    
109        public GBody() {
110            initComponents();
111        }
112    
113        private void initComponents() {
114            setBodyPane(new Container());
115            hideSideBar();
116    //        setBorder(new LineBorder(Color.red,1,true));//new EmptyBorder(0,0,0,0));
117            setLayout(new BorderLayout());
118    //        add(splitPane);
119            setBorder(null);
120            splitPane.setBorder(null);
121    
122    
123        }
124    }