All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alantea.swing.panels.VerticalTabFolder Maven / Gradle / Ivy

The newest version!
package net.alantea.swing.panels;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EtchedBorder;

import net.alantea.swing.misc.ResourceManager;
import net.alantea.utils.MultiMessages;

@SuppressWarnings("serial")
public class VerticalTabFolder extends JPanel
{
   private List names = new LinkedList<>();
   private List tabs = new LinkedList<>();
   private Map map = new HashMap<>();
   
   private JPanel composite;
   private JPanel group;
   private ButtonGroup bgroup;
   private CardLayout stackLayout;

   /**
    * Create the composite.
    */
   public VerticalTabFolder()
   {
      super();
      setLayout(new BorderLayout(10,4));
      
      group = new JPanel();
      add(group, BorderLayout.WEST);
      group.setLayout(new BoxLayout(group, BoxLayout.Y_AXIS));
      group.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
      
      bgroup = new ButtonGroup();
      
      stackLayout = new CardLayout();
      composite = new JPanel();
      composite.setLayout(stackLayout);
      add(composite, BorderLayout.CENTER);

   }
   
   public final int addFolder(Component control, String name, Image image, String tooltip)
   {
      return addFolder(control, name, image, tooltip, null);
   }
   
   public final int addFolder(Component control, String name, Image image, String tooltip, Runnable onShow)
   {
      composite.add(control, name);
      names.add(name);
      tabs.add(control);
      JToggleButton  button = new JToggleButton ();
      button.setOpaque(true) ;
      if (tooltip != null)
      {
         button.setToolTipText(MultiMessages.get(tooltip));
      }
      button.setBackground(new Color(230,230,230));
      bgroup.add(button);
      button.setIcon(ResourceManager.getIcon(ResourceManager.getImage((BufferedImage)image, 30)));
      button.setPreferredSize(new Dimension(34,30));
      group.add(button);
      
      button.addActionListener(new ActionListener()
      {
         @Override
         public void actionPerformed(ActionEvent arg0)
         {
            if (onShow != null)
            {
               onShow.run();
            }
            stackLayout.show(composite, name);
         }
      });
      
      map.put(control, button);
      return tabs.size() - 1;
   }
   
   public final int addFolder(JComponent control, Image image, String tooltip)
   {
      return addFolder(control, "" + new Date().getTime() + names.size(), image, tooltip);
   }
   
   public final int addFolder(JComponent control, Image image, String tooltip, Runnable onShow)
   {
      return addFolder(control, "" + new Date().getTime() + names.size(), image, tooltip, onShow);
   }
   
   public void select(String name)
   {
      int num = names.indexOf(name);
      if ((num >= 0) && (num < names.size()))
      {
         Component component = tabs.get(num);
         stackLayout.show(composite, name);
         map.get(component).setSelected(true);
      }
   }
   
   public void select(Component component)
   {
      int num = tabs.indexOf(component);
      if ((num >= 0) && (num < names.size()))
      {
         stackLayout.show(composite, names.get(num));
         map.get(component).setSelected(true);
      }
   }
   
   public void select(int num)
   {
      if ((num >= 0) && (num < names.size()))
      {
         stackLayout.show(composite, names.get(num));
         map.get(map.keySet().toArray()[num]).setSelected(true);
      }
   }
   
   public JPanel getFolderComposite()
   {
      return composite;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy