001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Graphics;
046: import java.awt.Rectangle;
047: import java.awt.event.MouseEvent;
048: import java.awt.event.MouseListener;
049:
050: import javax.swing.Icon;
051: import javax.swing.JPanel;
052: import javax.swing.JTabbedPane;
053:
054: /**
055: * A JTabbedPane which has a close ('X') icon on each tab. To add a tab, use the method
056: * addTab(String, Component) To have an extra icon on each tab (e.g. like in JBuilder,
057: * showing the file type) use the method addTab(String, Component, Icon). Only clicking
058: * the 'X' closes the tab.
059: *
060: * @author Ritesh Adval
061: */
062: public class JTabbedPaneWithCloseIcons extends JTabbedPane implements
063: MouseListener {
064: public JTabbedPaneWithCloseIcons() {
065: super ();
066: addMouseListener(this );
067: }
068:
069: public void addTab(String title, Component component) {
070: this .addTab(title, component, null);
071: }
072:
073: public void addTab(String title, Component component, Icon extraIcon) {
074: if (component == null) {
075: component = new JPanel();
076: }
077: super .addTab(title, new CloseTabIcon(extraIcon), component);
078: }
079:
080: public void mouseClicked(MouseEvent e) {
081: int tabNumber = getUI().tabForCoordinate(this , e.getX(),
082: e.getY());
083: if (tabNumber < 0)
084: return;
085: Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber))
086: .getBounds();
087: if (rect.contains(e.getX(), e.getY())) {
088: //the tab is being closed
089: this .removeTabAt(tabNumber);
090: }
091: }
092:
093: // TODO: Implement close all.
094:
095: public void mouseEntered(MouseEvent e) {
096: }
097:
098: public void mouseExited(MouseEvent e) {
099: }
100:
101: public void mousePressed(MouseEvent e) {
102: }
103:
104: public void mouseReleased(MouseEvent e) {
105: }
106: }
107:
108: /**
109: * The class which generates the 'X' icon for the tabs. The constructor accepts an icon
110: * which is extra to the 'X' icon, so you can have tabs like in JBuilder. This value is
111: * null if no extra icon is required.
112: */
113:
114: class CloseTabIcon implements Icon {
115: private int x_pos;
116: private int y_pos;
117: private int width;
118: private int height;
119: private Icon fileIcon;
120:
121: public CloseTabIcon(Icon fileIcon) {
122: this .fileIcon = fileIcon;
123: width = 16;
124: height = 16;
125: }
126:
127: public void paintIcon(Component c, Graphics g, int x, int y) {
128: this .x_pos = x;
129: this .y_pos = y;
130:
131: Color col = g.getColor();
132:
133: g.setColor(Color.gray);
134: int y_p = y + 2;
135: g.drawLine(x + 1, y_p, x + 12, y_p);
136: g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);
137: g.drawLine(x, y_p + 1, x, y_p + 12);
138: g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);
139: g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);
140: g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);
141: g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);
142: g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);
143: g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);
144: g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);
145: g.setColor(col);
146: if (fileIcon != null) {
147: fileIcon.paintIcon(c, g, x + width, y_p);
148: }
149: }
150:
151: public int getIconWidth() {
152: return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
153: }
154:
155: public int getIconHeight() {
156: return height;
157: }
158:
159: public Rectangle getBounds() {
160: return new Rectangle(x_pos, y_pos, width, height);
161: }
162: }
|