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

net.alantea.swing.tab.TabbedPane Maven / Gradle / Ivy

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

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.alantea.swing.pageelements.Redrawable;

/**
 * The Class TabbedPane.
 */
public class TabbedPane extends JTabbedPane
{
   
   /** The Constant serialVersionUID. */
   private static final long serialVersionUID = 1L;
   
   /** The listeners. */
   private List listeners = new ArrayList<>();
   
   // key is the panel, value is the title
   private Map tabTitlesMap = new HashMap<>();
   
   // key is the reference, value is the panel
   private Map panelsMap = new HashMap<>();
   
   // key is the reference, value is the panel
   private Map reversePanelsMap = new HashMap<>();
   
   private TabbedTitleComputer titleComputer = (reference) -> { return reference.toString();};
   
   public TabbedPane()
   {
      super();
      addChangeListener(new ChangeListener()
      {
         public void stateChanged(ChangeEvent e)
         {
            doRedraw(TabbedPane.this.getSelectedComponent());
         }
         
         public void doRedraw(Component component)
         {
               if (component instanceof Redrawable)
               {
                  ((Redrawable) component).doRedraw();
               }
               else if (component != null)
               {
                  component.invalidate();
                  component.repaint();
                  if (component instanceof Container)
                  {
                     for (Component comp : ((Container) component).getComponents())
                     {
                        doRedraw(comp);
                     }
                  }
               }
            
         }
     });           
   }
   
   public TabbedPane(int flag)
   {
      super(flag);
      addChangeListener(new ChangeListener()
      {
         public void stateChanged(ChangeEvent e)
         {
            for (Component comp : TabbedPane.this.getComponents())
            {
               comp.invalidate();
               comp.update(comp.getGraphics());
               comp.repaint();
            }
         }
     });         
   }
   
   public TabbedPane(TabbedTitleComputer computer)
   {
      this();
      setTitleComputer(computer);
   }
   
   public void setTitleComputer(TabbedTitleComputer computer)
   {
      if (computer != null)
      {
         titleComputer = computer;
         recomputeTitles();
      }
   }
   
   /**
    * Gets the reference.
    *
    * @param panel the panel
    * @return the reference
    */
   public Object getReference(JPanel panel)
   {
      return reversePanelsMap.get(panel);
   }
   
   public JPanel getPanel(Object reference)
   {
      return panelsMap.get(reference);
   }
   
   public JLabel getLabel(Object reference)
   {
      JPanel panel = panelsMap.get(reference);
      if (panel != null)
      {
         return tabTitlesMap.get(panel);
      }
      return null;
   }

   /**
    * Adds the to tab folder.
    *
    * @param panel the panel
    * @param reference the reference
    * @param closeable the closeable
    * @return the int
    */
   public int addToTabFolder(final JPanel panel, Object reference, boolean closeable)
   {
      add(panel);
      JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
      titlePanel.setOpaque(false);
      JLabel titleLbl = new JLabel();
      titleLbl.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
      titlePanel.add(titleLbl);
      if (closeable)
      {
         JLabel closeButton = new JLabel("  x");

         closeButton.addMouseListener(new MouseAdapter()
         {
            @Override
            public void mouseClicked(MouseEvent e)
            {
               remove(panel);
               tabTitlesMap.remove(panel);
               Object ref = reversePanelsMap.remove(panel);
               panelsMap.remove(ref);
               reversePanelsMap.remove(panel);
               for (TabFolderRemoveListener listener : listeners)
               {
                  try
                  {
                     listener.onRemoveTab(panel);
                  }
                  catch(Exception exp)
                  {
                     // do nothing
                  }
               }
            }
         });
         titlePanel.add(closeButton);
      }
      int num = indexOfComponent(panel);
      setTabComponentAt(num, titlePanel);
      
      tabTitlesMap.put(panel, titleLbl);
      String title = titleComputer.getTitle(reference);
      titleLbl.setText(title);
      panelsMap.put(reference,  panel);
      reversePanelsMap.put(panel, reference);
      return num;
   }
   
   public void recomputeTitles()
   {
      for (Object ref : panelsMap.keySet().toArray())
      {
         JPanel panel = panelsMap.get(ref);
         JLabel titleLabel = tabTitlesMap.get(panel);
         String title = titleComputer.getTitle(ref);
         titleLabel.setText(title);
      }
   }
   
   /**
    * Sets the tab title from the panel.
    *
    * @param panel the panel
    * @param title the title
    */
   public void setTabTitle(JPanel panel, String title)
   {
      JLabel label = tabTitlesMap.get(panel);
      if ((label != null) && (title != null))
      {
         label.setText(title);
      }
   }
   
   /**
    * Sets the tab title from the reference.
    *
    * @param reference the reference
    * @param title the title
    */
   public void setTabTitle(Object reference, String title)
   {

      JPanel panel = panelsMap.get(reference);
      if (panel != null)
      {
         JLabel label = tabTitlesMap.get(panel);
         if ((label != null) && (title != null))
         {
            label.setText(title);
         }
      }
   }

   /**
    * Adds the tab folder remove listener.
    *
    * @param listener the listener
    */
   public void addTabFolderRemoveListener(TabFolderRemoveListener listener)
   {
      if (!listeners.contains(listener))
      {
         listeners.add(listener);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy