1 package com.sshtools.ui.swing; 2 3 import java.awt.Component ; 4 import java.awt.Graphics ; 5 import java.awt.Rectangle ; 6 import java.awt.event.MouseEvent ; 7 import java.awt.event.MouseListener ; 8 import java.util.Enumeration ; 9 import java.util.Vector ; 10 11 import javax.swing.Action ; 12 import javax.swing.Icon ; 13 import javax.swing.JPopupMenu ; 14 import javax.swing.JTabbedPane ; 15 16 22 public class ClosableTabbedPane extends JTabbedPane implements MouseListener { 23 private Vector actions; 24 private JPopupMenu popup; 25 26 public ClosableTabbedPane() { 27 this(TOP); 28 } 29 30 public ClosableTabbedPane(int tabPlacement) { 31 super(tabPlacement); 32 init(); 33 } 34 35 public void setIconAt(int index, Icon icon) { 36 super.setIconAt(index, new CloseTabIcon(icon)); 37 } 38 39 private void init() { 40 actions = new Vector (); 41 addMouseListener(this); 42 } 43 44 public void removeAllActions() { 45 actions.removeAllElements(); 46 } 47 48 public void addAction(Action action) { 49 if (!actions.contains(action)) { 50 actions.add(action); 51 } 52 } 53 54 public void removeAction(Action action) { 55 actions.remove(action); 56 } 57 58 public void addTab(String title, Component component) { 59 this.addTab(title, component, null); 60 } 61 62 public void addTab(String title, Component component, Icon extraIcon) { 63 super.addTab(title, extraIcon == null ? null : new CloseTabIcon(extraIcon), component); 64 } 65 66 public void addTab(String title, Icon extraIcon, Component component, String toolTip) { 67 super.addTab(title, extraIcon == null ? null : new CloseTabIcon(extraIcon), component, toolTip); 68 } 69 70 public void insertTab(String title, Component component, int idx) { 71 this.insertTab(title, component, null, idx); 72 } 73 74 public void insertTab(String title, Component component, Icon extraIcon, int idx) { 75 super.insertTab(title, extraIcon == null ? null : new CloseTabIcon(extraIcon), component, null, idx); 76 } 77 78 public void mouseClicked(MouseEvent e) { 79 int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY()); 80 if (tabNumber < 0) { 81 return; 82 } 83 CloseTabIcon cti = ((CloseTabIcon) getIconAt(tabNumber)); 84 if (cti != null) { 85 Rectangle rect = cti.getBounds(); 86 if (rect.contains(e.getPoint())) { 87 Component c = (Component ) getSelectedComponent(); 88 if (c != null) { 89 if (popup == null) { 90 popup = new JPopupMenu (""); } 92 popup.setLabel(getTitleAt(getSelectedIndex())); 93 popup.invalidate(); 94 popup.removeAll(); 95 Action action; 96 for (Enumeration en = actions.elements(); en.hasMoreElements();) { 97 action = (Action ) en.nextElement(); 98 if (action != null) 99 popup.add(action); 100 } 101 popup.validate(); 102 Rectangle r = getUI().getTabBounds(this, tabNumber); 103 popup.show(ClosableTabbedPane.this, r.x, r.y + r.height); 104 } 105 } 106 } 107 } 108 109 public void mouseEntered(MouseEvent e) { 110 } 111 112 public void mouseExited(MouseEvent e) { 113 } 114 115 public void mousePressed(MouseEvent e) { 116 } 117 118 public void mouseReleased(MouseEvent e) { 119 } 120 121 class CloseTabIcon implements Icon { 122 private int x_pos; 123 124 private int y_pos; 125 126 private int width; 127 128 private int height; 129 130 private Icon fileIcon; 131 132 public CloseTabIcon(Icon fileIcon) { 133 this.fileIcon = fileIcon; 134 width = fileIcon == null ? 16 : fileIcon.getIconWidth(); 135 height = fileIcon == null ? 16 : fileIcon.getIconHeight(); 136 } 137 138 public void paintIcon(Component c, Graphics g, int x, int y) { 139 this.x_pos = x; 140 this.y_pos = y; 141 if (fileIcon != null) { 142 fileIcon.paintIcon(c, g, x, y); 143 } 144 } 145 146 public int getIconWidth() { 147 return width; 148 } 149 150 public int getIconHeight() { 151 return height; 152 } 153 154 public Rectangle getBounds() { 155 return new Rectangle (x_pos, y_pos, width, height); 156 } 157 } 158 } | Popular Tags |