1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
nextobjects Copyright (C) 2001-2005 Emmanuel Florent
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.JTabbedPane;
/**
* A JTabbedPane which has a close ('X') icon on each tab.
*
* To add a tab, use the method addTab(String, Component)
*
* To have an extra icon on each tab use
* the method addTab(String, Component, Icon).
* Only clicking the 'X' closes the tab.
*/
public class ClosableTabbedPane extends JTabbedPane implements MouseListener
{
/**
* constructor
*/
public ClosableTabbedPane(EdtMainFrame mainFrame)
{
super();
this.mainFrame = mainFrame;
addMouseListener(this);
}
/**
* add a tabbed pane
* @param component the component
* @param title the title
*/
public void addTab(String title, Component component)
{
this.addTab(title, component, null);
}
/**
* add a tabbed pane
* @param title title
* @param component component
* @param extraIcon icon
*/
public void addTab(String title, Component component, Icon extraIcon)
{
super.addTab(title, new CloseTabIcon(extraIcon), component);
bubu.ui.UITools.setBackground(this, EdtMainFrame.AppColor);
}
/**
* What to do when mouse clicked
* @param e <code>MouseEvent</code>
*/
public void mouseClicked(MouseEvent e)
{
int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
if (tabNumber < 0)
return;
Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
if (rect.contains(e.getX(), e.getY()))
{
mainFrame.tabs.removeObject(this.getComponentAt(tabNumber));
this.remove(tabNumber);
System.gc();
}
}
/**
* What to di when mouse entered
* actually do nothing
* @param e <code>MouseEvent</code>
*/
public void mouseEntered(MouseEvent e)
{
}
/**
* What to di when mouse exited
* actually do nothing
* @param e <code>MouseEvent</code>
*/
public void mouseExited(MouseEvent e)
{
}
/**
* What to di when mouse pressed
* actually do nothing
* @param e <code>MouseEvent</code>
*/
public void mousePressed(MouseEvent e)
{
}
/**
* What to di when mouse released
* actually do nothing
* @param e <code>MouseEvent</code>
*/
public void mouseReleased(MouseEvent e)
{
}
EdtMainFrame mainFrame;
}
/**
* The class which generates the 'X' icon for the tabs. The constructor
* accepts an icon which is extra to the 'X' icon, so you can have tabs
* like in JBuilder. This value is null if no extra icon is required.
*/
class CloseTabIcon implements Icon
{
/**
* X position
*/
private int x_pos;
/**
* Y position
*/
private int y_pos;
/**
* Width
*/
private int width;
/**
* Height
*/
private int height;
/**
* Icon
*/
private Icon fileIcon;
/**
* CloseTabIcon
* @param fileIcon the file icon
*/
public CloseTabIcon(Icon fileIcon)
{
this.fileIcon = fileIcon;
width = 16;
height = 16;
}
/**
* Paint icon
* @param c component
* @param g graphics g
* @param x x
* @param y y
*/
public void paintIcon(Component c, Graphics g, int x, int y)
{
this.x_pos = x;
this.y_pos = y;
Color col = g.getColor();
g.setColor(Color.black);
int y_p = y + 2;
g.drawLine(x + 1, y_p, x + 12, y_p);
g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);
g.drawLine(x, y_p + 1, x, y_p + 12);
g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);
g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);
g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);
g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);
g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);
g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);
g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);
g.setColor(col);
if (fileIcon != null)
{
fileIcon.paintIcon(c, g, x + width, y_p);
}
}
/**
* get the icon width
* @return the width
*/
public int getIconWidth()
{
return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
}
/**
* get icon height
* @return height
*/
public int getIconHeight()
{
return height;
}
/**
* get the boundaries
* @return bounds
*/
public Rectangle getBounds()
{
return new Rectangle(x_pos, y_pos, width, height);
}
} |
Partager