Package fbench.ui

Source Code of fbench.ui.TabPanel

// Based on example at http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html

package fbench.ui;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MutationEvent;

import fbench.FBench;
import fbench.LibraryElementView;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TabPanel extends JPanel implements EventListener{
  private static final long serialVersionUID = 9130094704554958571L;

  public class TabCloseButton extends JButton implements ActionListener {
    private static final long serialVersionUID = -4901377612777070667L;
 
    private JTabbedPane parentPane;
    private Component component;
    private final int size = 17;
   
    public TabCloseButton(JTabbedPane pane, Component component){
      super();
     
      this.parentPane = pane;
      this.component = component;
     
          setPreferredSize(new Dimension(size, size));
          setToolTipText("Close this tab");
          setContentAreaFilled(false);
          setFocusable(false);
          setBorder(BorderFactory.createEtchedBorder());
          setBorderPainted(false);
          setRolloverEnabled(true);
          addActionListener(this)
          addMouseListener(new MouseAdapter() {
              public void mouseEntered(MouseEvent e) {
                  Component component = e.getComponent();
                  if (component instanceof AbstractButton) {
                      AbstractButton button = (AbstractButton) component;
                      button.setBorderPainted(true);
                  }
              }
 
              public void mouseExited(MouseEvent e) {
                  Component component = e.getComponent();
                  if (component instanceof AbstractButton) {
                      AbstractButton button = (AbstractButton) component;
                      button.setBorderPainted(false);
                  }
              }
          });
    }
 
    public void actionPerformed(ActionEvent arg0) {
      FBench.theBench.closeTab(component);
//      if( component instanceof LibraryElementView ){
//        if( ((LibraryElementView)component).safeToClose() ){
//          ((LibraryElementView)component).unload();
//          parentPane.remove(component);
//        }
//      }
//      else
//        parentPane.remove(component);
    }
   
    protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g.create();
          //shift the image for pressed buttons
          if (getModel().isPressed()) {
              g2.translate(1, 1);
          }
          g2.setStroke(new BasicStroke(2));
          g2.setColor(Color.BLACK);
          if (getModel().isRollover()) {
              g2.setColor(Color.DARK_GRAY);
          }           
          int delta = 6;
          g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
          g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
          g2.dispose();
      }
  }

  private JLabel title = null;
  private TabCloseButton closeButton = null;
  private Component component = null;
 
  public TabPanel(String title, JTabbedPane pane, Component component){
    super(new FlowLayout(FlowLayout.LEFT, 0, 0));
    this.component = component;
   
    // If the component is a LEV we can listen for changes to the name of the file
    if(component instanceof LibraryElementView){
      LibraryElementView lev = (LibraryElementView)component;
      ((EventTarget)lev.getDocument()).addEventListener("MutationEvent", this, false);
    }
   
    this.title = new JLabel(title);
    this.closeButton = new TabCloseButton(pane, component);
   
    setOpaque(false);
    add(this.title);
    this.title.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
    add(closeButton);
    setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
  }

  public void handleEvent(Event evt) {
    if (evt instanceof MutationEvent){
      LibraryElementView lev = (LibraryElementView)component;
      title.setText(lev.getFilename(false));
      invalidate();
    }
  }
}
TOP

Related Classes of fbench.ui.TabPanel

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.