KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjTabbedPane


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.Graphics JavaDoc;
34 import java.awt.event.MouseAdapter JavaDoc;
35 import java.awt.event.MouseEvent JavaDoc;
36 import java.net.URL JavaDoc;
37
38 import javax.swing.Icon JavaDoc;
39 import javax.swing.ImageIcon JavaDoc;
40 import javax.swing.JTabbedPane JavaDoc;
41
42 public class DjTabbedPane extends JTabbedPane JavaDoc
43 {
44   private static final long serialVersionUID = 1L;
45   protected ImageIcon JavaDoc _closingIcon;
46
47   public DjTabbedPane()
48   {
49     this(getImageIcon("close.gif"));
50   }
51
52   public DjTabbedPane(ImageIcon JavaDoc closingIcon)
53   {
54     this._closingIcon = closingIcon;
55
56     addMouseListener(new ClosingListener());
57   }
58
59   public void addTab(String JavaDoc title, ImageIcon JavaDoc icon, Component JavaDoc component, String JavaDoc 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 JavaDoc title, ImageIcon JavaDoc icon, Component JavaDoc component)
66   {
67     addTab(title, icon, component, null);
68   }
69
70   public void addTab(String JavaDoc title, Component JavaDoc component)
71   {
72     addTab(title, null, component, null);
73   }
74
75   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
76   {
77     URL JavaDoc url = DjTabbedPane.class.getResource("images/" + fileName);
78     if (url != null)
79     {
80       return new ImageIcon JavaDoc(url);
81     }
82     else return null;
83   }
84
85   protected class ClosingListener extends MouseAdapter JavaDoc
86   {
87
88     public void mouseReleased(MouseEvent JavaDoc 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 JavaDoc 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 JavaDoc
114   {
115     private Icon JavaDoc _closeIcon;
116     private Icon JavaDoc _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 JavaDoc closeIcon, ImageIcon JavaDoc 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 JavaDoc c, Graphics JavaDoc 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