1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.Component ; 33 import java.awt.Graphics ; 34 import java.awt.event.MouseAdapter ; 35 import java.awt.event.MouseEvent ; 36 import java.net.URL ; 37 38 import javax.swing.Icon ; 39 import javax.swing.ImageIcon ; 40 import javax.swing.JTabbedPane ; 41 42 public class DjTabbedPane extends JTabbedPane 43 { 44 private static final long serialVersionUID = 1L; 45 protected ImageIcon _closingIcon; 46 47 public DjTabbedPane() 48 { 49 this(getImageIcon("close.gif")); 50 } 51 52 public DjTabbedPane(ImageIcon closingIcon) 53 { 54 this._closingIcon = closingIcon; 55 56 addMouseListener(new ClosingListener()); 57 } 58 59 public void addTab(String title, ImageIcon icon, Component component, String tip) 60 { 61 if (component instanceof DjCloseable) super.addTab(title, new ClosingIcon(_closingIcon, icon), component, tip); 62 else super.addTab(title, icon, component, tip); 63 } 64 65 public void addTab(String title, ImageIcon icon, Component component) 66 { 67 addTab(title, icon, component, null); 68 } 69 70 public void addTab(String title, Component component) 71 { 72 addTab(title, null, component, null); 73 } 74 75 public static ImageIcon getImageIcon(String fileName) 76 { 77 URL url = DjTabbedPane.class.getResource("images/" + fileName); 78 if (url != null) 79 { 80 return new ImageIcon (url); 81 } 82 else return null; 83 } 84 85 protected class ClosingListener extends MouseAdapter 86 { 87 88 public void mouseReleased(MouseEvent e) 89 { 90 int i = getSelectedIndex(); 91 92 if (i == -1) return; 93 94 if (getIconAt(i) instanceof ClosingIcon) 95 { 96 ClosingIcon icon = (ClosingIcon) getIconAt(i); 97 98 if (icon != null && icon.contains(e.getX(), e.getY())) 99 { 100 Component c = getComponentAt(i); 101 102 if (c instanceof DjCloseable) 103 { 104 DjCloseable closeable = (DjCloseable) c; 105 if (closeable.canClose()) closeable.close(); 106 } 107 } 108 } 109 } 110 111 } 112 113 protected class ClosingIcon implements Icon 114 { 115 private Icon _closeIcon; 116 private Icon _icon; 117 private int _x = 0; 118 private int _y = 0; 119 private int _height = 10; 120 private int _width = 10; 121 private int _combinedWidth = 10; 122 123 public ClosingIcon(ImageIcon closeIcon, ImageIcon icon) 124 { 125 _closeIcon = closeIcon; 126 _icon = icon; 127 128 if (closeIcon != null) 129 { 130 _height = closeIcon.getIconHeight(); 131 _width = closeIcon.getIconWidth(); 132 _combinedWidth = _width; 133 } 134 if (icon != null) 135 { 136 _combinedWidth += 2 + icon.getIconWidth(); 137 _height = Math.max(_height, icon.getIconHeight()); 138 } 139 } 140 141 public int getIconHeight() 142 { 143 return _height; 144 } 145 146 public int getIconWidth() 147 { 148 return _combinedWidth; 149 } 150 151 public void paintIcon(Component c, Graphics g, int x, int y) 152 { 153 _x = x; 154 _y = y; 155 156 if (_closeIcon != null) 157 { 158 _closeIcon.paintIcon(c, g, x, y + 1); 159 if (_icon != null) _icon.paintIcon(c, g, x + _closeIcon.getIconWidth() + 2, y + 1); 160 } 161 else 162 { 163 System.err.println("DjTabbedPane: no icon"); 164 g.drawRect(x, y + 1, _width, _height); 165 } 166 } 167 168 public boolean contains(int xEvent, int yEvent) 169 { 170 if (!(xEvent >= _x) || !(xEvent <= _x + _width)) 171 { 172 return false; 173 } 174 if (!(yEvent >= _y) || !(yEvent <= _y + _height)) 175 { 176 return false; 177 } 178 179 return true; 180 } 181 } 182 } | Popular Tags |