Package simtools.ui

Source Code of simtools.ui.StackPanel$StackTabAction

package simtools.ui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.AbstractAction;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import simtools.ui.PageListModel.PageListListener;
import simtools.ui.PageListModel.PageModel;

/**
* A component that stacks its children vertically, displaying only one at a time,
* with a header for each child which the user can click to display.
*
* @author zxpletran007
*/
public class StackPanel extends GridBagPanel implements PageListListener {


    /** Current selected page */
    protected PageListModel model;

    protected GridBagPanel selectedPage;

    /** The  current selected item content */
    protected JPanel content;
   
    /** An empty panel when no item is selected */
    private final JPanel emptyContentPanel = new JPanel();

    /** A panel that gather all tabs */
    protected GridBagPanel tabsPanel;
   
    protected static final Color selectedButtoncolor = new Color(184,207,229);
    /**
     * All tabs
     */
    protected List tabs;
   
   
    public StackPanel(PageListModel model){
        this.model =  model;
      
   
        // content
        content = new JPanel(new CardLayout());

        // tabs
        tabsPanel = new GridBagPanel();
        tabs = new ArrayList();

        selectedPage = new GridBagPanel();
        selectedPage.addOnCurrentRow(content, 1, true, true, true);

        // Add component to the stack panel
        addOnCurrentRow(selectedPage, 1, true, true, true);
        addOnCurrentRow(tabsPanel, 1, true, false, true);

       
        // Update content pane
        contentHasChanged();
       
        // Listen to model notifications
        model.addListener(this);
    }

    public void contentHasChanged() {

        // Create new buttons
        tabsPanel.removeAll();
        tabs.clear();
       
        content.removeAll()
        content.add(emptyContentPanel , "emptyPanel");
       
        for(int i=0; i < model.getSize(); i++){
            PageModel pm = model.getPageAt(i);
           
            JButton button = new JButton(pm.getPageTitle(), pm.getPageIcon())
           
            button.setHorizontalAlignment(SwingConstants.LEFT);
            button.addActionListener( new StackTabAction(pm));
          
            tabsPanel.addOnCurrentRow(button, 1, true, false, true);
            content.add(pm.getPageContent(), pm.getPageTitle());
            tabs.add(button);
        }
       
        selectionHasChanged();  
    }
   
   
    /**
     * get current selected page
     * @return current selected page, or null if no page selected.
     */
    public JComponent getSelectedPage(){
        JComponent res = null;
        PageModel pm = model.getSelectedPage();
        if (pm != null){
            res = pm.getPageContent();
        }
        return res;
    }

    protected class StackTabAction extends AbstractAction {
        PageModel pageModel;
        public StackTabAction(PageModel pageModel) {
            super();
            this.pageModel = pageModel;

        }
        public void actionPerformed(ActionEvent e) {
            model.setSelectedPage(pageModel);
       
    }


    /* (non-Javadoc)
     * @see simtools.ui.PageListModel.PageListListener#selectionHasChanged()
     */
    public void selectionHasChanged() {
        PageModel pm = model.getSelectedPage();

        // Update label
        if (pm != null){
            selectedPage.setBorder(pm.getPageTitle());
        }
      

        // Update content
        CardLayout cl = (CardLayout) (content.getLayout());
       
        if (pm != null){
            cl.show(content, pm.getPageTitle());
        } else {
            cl.show(content, "emptyPanel");
        }

        // Update tags status
        for(int i=0;i<tabs.size();i++){
            ((JButton)tabs.get(i)).setBackground(null);
        }
        ((JButton)tabs.get(model.getSelectedIndex())).setBackground(selectedButtoncolor);
       
       
        // repaint
        repaint();
    }
   
    public static void main(String args[]){
        // model for test
        DefaultPageListModel model = new DefaultPageListModel();

        model.addPage("test1", new JLabel("Ronan 1"));
        model.addPage("test2", new JComboBox());
        model.setSelectedPage("test2");

        JDialog test = new JDialog();
        test.getContentPane().add(new StackPanel(model));

        test.setModal(true);
        test.pack();
        test.setVisible(true);
       
        System.exit(0);
    }

}
TOP

Related Classes of simtools.ui.StackPanel$StackTabAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.