View Javadoc
1   /******************************************************************************
2    * ButtonTabComponent.java - Be the tab in the chat system, maybe have an X.
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright(c) 2011 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.1  2011/06/22 06:00:33  dick
10   * Be the tab in the chat system, maybe have an X.
11   *
12   */
13  
14  /* 
15   * This program is free software: you can redistribute it and/or modify
16   * it under the terms of the GNU General Public License as published by
17   * the Free Software Foundation, either version 3 of the License, or
18   * (at your option) any later version.
19   *
20   * This program is distributed in the hope that it will be useful,
21   * but WITHOUT ANY WARRANTY; without even the implied warranty of
22   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   * GNU General Public License for more details.
24   *
25   * You should have received a copy of the GNU General Public License
26   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27   *
28   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
29   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
30   * 
31   */
32  
33  /*
34   * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions
38   * are met:
39   *
40   *   - Redistributions of source code must retain the above copyright
41   *     notice, this list of conditions and the following disclaimer.
42   *
43   *   - Redistributions in binary form must reproduce the above copyright
44   *     notice, this list of conditions and the following disclaimer in the
45   *     documentation and/or other materials provided with the distribution.
46   *
47   *   - Neither the name of Oracle or the names of its
48   *     contributors may be used to endorse or promote products derived
49   *     from this software without specific prior written permission.
50   *
51   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
52   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
53   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
55   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
58   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
59   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62   */ 
63  
64  package com.buckosoft.fibs.BuckoFIBS.gui.chatWindow;
65  
66  import java.awt.BasicStroke;
67  import java.awt.Color;
68  import java.awt.Component;
69  import java.awt.Dimension;
70  import java.awt.FlowLayout;
71  import java.awt.Graphics;
72  import java.awt.Graphics2D;
73  import java.awt.event.ActionEvent;
74  import java.awt.event.ActionListener;
75  import java.awt.event.MouseAdapter;
76  import java.awt.event.MouseEvent;
77  import java.awt.event.MouseListener;
78  
79  import javax.swing.AbstractButton;
80  import javax.swing.BorderFactory;
81  import javax.swing.JButton;
82  import javax.swing.JLabel;
83  import javax.swing.JPanel;
84  import javax.swing.plaf.basic.BasicButtonUI;
85  
86  /** Component to be used as tabComponent;
87   * Contains a JLabel to show the text and a JButton to close the tab it belongs to. 
88   * @author Sun/Oracle
89   * @since 2011/06/21
90   * @version $Revision$ <br> $Date$
91   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/chatWindow/ButtonTabComponent.java">cvs ButtonTabComponent.java</a>
92   */
93  public class ButtonTabComponent extends JPanel {
94  	private static final long serialVersionUID = 1L;
95  	private final ChatTabs pane;
96  
97  	public ButtonTabComponent(final ChatTabs pane) {
98  		//unset default FlowLayout' gaps
99  		super(new FlowLayout(FlowLayout.LEFT, 0, 0));
100 		if (pane == null) {
101 			throw new NullPointerException("TabbedPane is null");
102 		}
103 		this.pane = pane;
104 		setOpaque(false);
105 
106 		//make JLabel read titles from JTabbedPane
107 		JLabel label = new JLabel() {
108 			private static final long serialVersionUID = 1L;
109 
110 			public String getText() {
111 				int i = pane.indexOfTabComponent(ButtonTabComponent.this);
112 				if (i != -1) {
113 					return pane.getTitleAt(i);
114 				}
115 				return null;
116 			}
117 		};
118 
119 		add(label);
120 		//add more space between the label and the button
121 		label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
122 		//tab button
123 		JButton button = new TabButton();
124 		add(button);
125 		//add more space to the top of the component
126 		setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
127 	}
128 	
129 	private class TabButton extends JButton implements ActionListener {
130 		private static final long serialVersionUID = 1L;
131 
132 		public TabButton() {
133 			int size = 17;
134 			setPreferredSize(new Dimension(size, size));
135 			setToolTipText("close this tab");
136 			//Make the button looks the same for all Laf's
137 			setUI(new BasicButtonUI());
138 			//Make it transparent
139 			setContentAreaFilled(false);
140 			//No need to be focusable
141 			setFocusable(false);
142 			setBorder(BorderFactory.createEtchedBorder());
143 			setBorderPainted(false);
144 			//Making nice rollover effect
145 			//we use the same listener for all buttons
146 			addMouseListener(buttonMouseListener);
147 			setRolloverEnabled(true);
148 			//Close the proper tab by clicking the button
149 			addActionListener(this);
150 		}
151 
152 		public void actionPerformed(ActionEvent e) {
153 			int i = pane.indexOfTabComponent(ButtonTabComponent.this);
154 			if (i != -1) {
155 				pane.remove(i);
156 			}
157 		}
158 
159 		//we don't want to update UI for this button
160 		public void updateUI() {
161 		}
162 
163 		//paint the cross
164 		protected void paintComponent(Graphics g) {
165 			super.paintComponent(g);
166 			Graphics2D g2 = (Graphics2D) g.create();
167 			//shift the image for pressed buttons
168 			if (getModel().isPressed()) {
169 				g2.translate(1, 1);
170 			}
171 			g2.setStroke(new BasicStroke(2));
172 			g2.setColor(Color.BLACK);
173 			if (getModel().isRollover()) {
174 				g2.setColor(Color.MAGENTA);
175 			}
176 			int delta = 6;
177 			g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
178 			g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
179 			g2.dispose();
180 		}
181 	}
182 
183 	private final static MouseListener buttonMouseListener = new MouseAdapter() {
184 		public void mouseEntered(MouseEvent e) {
185 			Component component = e.getComponent();
186 			if (component instanceof AbstractButton) {
187 				AbstractButton button = (AbstractButton) component;
188 				button.setBorderPainted(true);
189 			}
190 		}
191 
192 		public void mouseExited(MouseEvent e) {
193 			Component component = e.getComponent();
194 			if (component instanceof AbstractButton) {
195 				AbstractButton button = (AbstractButton) component;
196 				button.setBorderPainted(false);
197 			}
198 		}
199 	};
200 }
201 
202