001: /*
002: * CloseTabbedPane.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Component;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import javax.swing.Icon;
028: import javax.swing.JMenu;
029: import javax.swing.JMenuItem;
030: import javax.swing.JPopupMenu;
031: import javax.swing.JTabbedPane;
032:
033: import org.underworldlabs.swing.plaf.CloseTabContentPanel;
034: import org.underworldlabs.swing.plaf.CloseTabbedPaneUI;
035: import org.underworldlabs.swing.plaf.TabMenuItem;
036:
037: /* ----------------------------------------------------------
038: * CVS NOTE: Changes to the CVS repository prior to the
039: * release of version 3.0.0beta1 has meant a
040: * resetting of CVS revision numbers.
041: * ----------------------------------------------------------
042: */
043:
044: /**
045: *
046: * @author Takis Diakoumis
047: * @version $Revision: 1.4 $
048: * @date $Date: 2006/05/14 06:56:07 $
049: */
050: public class CloseTabbedPane extends JTabbedPane {
051:
052: /** whether the default popup is enabled */
053: private boolean tabPopupEnabled;
054:
055: /** the popup menu */
056: private TabPopupMenu popup;
057:
058: public CloseTabbedPane() {
059: this (TOP, SCROLL_TAB_LAYOUT);
060: }
061:
062: public CloseTabbedPane(int tabPlacement) {
063: this (tabPlacement, SCROLL_TAB_LAYOUT);
064: }
065:
066: public CloseTabbedPane(int tabPlacement, int tabLayoutPolicy) {
067: super (tabPlacement, tabLayoutPolicy);
068: }
069:
070: public void addTab(String title, Component component) {
071: addTab(title, null, component, null);
072: }
073:
074: public void addTab(String title, Icon icon, Component component) {
075: addTab(title, icon, component, null);
076: }
077:
078: public void insertTab(String title, Icon icon, Component component,
079: String tip, int index) {
080: // make sure the pane is visible - may have been empty
081: if (!isVisible()) {
082: setVisible(true);
083: }
084:
085: CloseTabContentPanel _component = new CloseTabContentPanel(
086: tabPlacement, component);
087: super .insertTab(title, icon, _component, tip, index);
088:
089: if (tabPopupEnabled) {
090: TabMenuItem menuItem = addAssociatedMenu(title, icon,
091: _component);
092: _component.setTabMenuItem(menuItem);
093: }
094: }
095:
096: public void addTab(String title, Icon icon, Component component,
097: String tip) {
098:
099: // make sure the pane is visible - may have been empty
100: if (!isVisible()) {
101: setVisible(true);
102: }
103:
104: CloseTabContentPanel _component = new CloseTabContentPanel(
105: tabPlacement, component);
106: super .addTab(title, icon, _component, tip);
107:
108: if (tabPopupEnabled) {
109: TabMenuItem menuItem = addAssociatedMenu(title, icon,
110: _component);
111: _component.setTabMenuItem(menuItem);
112: }
113: }
114:
115: /*
116: public void setSelectedComponent(Component component) {
117: for(int i = 0; i < getTabCount(); i++) {
118: Component c = super.getComponentAt(i);
119: if (c instanceof CloseTabContentPanel) {
120: CloseTabContentPanel panel = (CloseTabContentPanel)c;
121: if (panel.getDisplayComponent() == component) {
122: super.setSelectedComponent(c);
123: }
124: }
125: }
126: }
127:
128: public int indexOfComponent(Component component) {
129: for(int i = 0; i < getTabCount(); i++) {
130: Component c = super.getComponentAt(i);
131: if (c instanceof CloseTabContentPanel) {
132: CloseTabContentPanel panel = (CloseTabContentPanel)c;
133: if (panel.getDisplayComponent() == component) {
134: return i;
135: }
136: }
137: }
138: return super.indexOfComponent(component);
139: }
140:
141: public Component getComponentAt(int index) {
142: Component c = super.getComponentAt(index);
143: if (c instanceof CloseTabContentPanel) {
144: CloseTabContentPanel panel = (CloseTabContentPanel)c;
145: return panel.getDisplayComponent();
146: }
147: return c;
148: }
149:
150: public Component getSelectedComponent() {
151: Component c = super.getSelectedComponent();
152: if (c instanceof CloseTabContentPanel) {
153: CloseTabContentPanel panel = (CloseTabContentPanel)c;
154: return panel.getDisplayComponent();
155: }
156: return c;
157: }
158: */
159: protected TabMenuItem addAssociatedMenu(String title, Icon icon,
160: Component component) {
161: TabMenuItem menuItem = new TabMenuItem(title, icon, component);
162: popup.addTabSelectionMenuItem(menuItem);
163: return menuItem;
164: }
165:
166: public void removeAll() {
167: popup.removeAllTabSelectionMenuItems();
168: super .removeAll();
169: setVisible(false);
170: }
171:
172: public void remove(int index) {
173: CloseTabContentPanel component = (CloseTabContentPanel) getComponentAt(index);
174: if (tabPopupEnabled) {
175: popup
176: .removeTabSelectionMenuItem(component
177: .getTabMenuItem());
178: }
179: super .remove(index);
180: if (getTabCount() == 0) {
181: setVisible(false);
182: }
183: }
184:
185: public void updateUI() {
186: setUI(new CloseTabbedPaneUI());
187: }
188:
189: public boolean isTabPopupEnabled() {
190: return tabPopupEnabled;
191: }
192:
193: public void setTabPopupEnabled(boolean tabPopupEnabled) {
194: this .tabPopupEnabled = tabPopupEnabled;
195: if (tabPopupEnabled && popup == null) {
196: popup = new TabPopupMenu(this );
197: }
198: }
199:
200: public void showPopup(int index, int x, int y) {
201: popup.setHoverTabIndex(index);
202: popup.show(this , x, y);
203: }
204:
205: private class TabPopupMenu extends JPopupMenu implements
206: ActionListener {
207:
208: private JMenu openTabs;
209: private JMenuItem close;
210: private JMenuItem closeAll;
211: private JMenuItem closeOther;
212:
213: private JTabbedPane tabPane;
214:
215: private int hoverTabIndex;
216:
217: public TabPopupMenu(JTabbedPane tabPane) {
218: this .tabPane = tabPane;
219:
220: close = new JMenuItem("Close");
221: closeAll = new JMenuItem("Close All");
222: closeOther = new JMenuItem("Close Others");
223:
224: close.addActionListener(this );
225: closeAll.addActionListener(this );
226: closeOther.addActionListener(this );
227:
228: add(close);
229: add(closeAll);
230: add(closeOther);
231:
232: hoverTabIndex = -1;
233: }
234:
235: public void addTabSelectionMenuItem(TabMenuItem menuItem) {
236: if (openTabs == null) {
237: addSeparator();
238: openTabs = new JMenu("Select");
239: add(openTabs);
240: }
241: menuItem.addActionListener(this );
242: openTabs.add(menuItem);
243: }
244:
245: public void removeAllTabSelectionMenuItems() {
246: if (openTabs == null) {
247: return;
248: }
249: openTabs.removeAll();
250: }
251:
252: public void removeTabSelectionMenuItem(TabMenuItem menuItem) {
253: if (openTabs == null) {
254: return;
255: }
256: openTabs.remove(menuItem);
257: }
258:
259: public void actionPerformed(ActionEvent e) {
260:
261: if (hoverTabIndex == -1) {
262: return;
263: }
264:
265: Object source = e.getSource();
266: if (source == close) {
267: tabPane.remove(hoverTabIndex);
268: } else if (source == closeAll) {
269: tabPane.removeAll();
270: } else if (source == closeOther) {
271: int count = 0;
272: int tabCount = tabPane.getTabCount();
273: Component[] tabs = new Component[tabCount - 1];
274: for (int i = 0; i < tabCount; i++) {
275: if (i != hoverTabIndex) {
276: tabs[count++] = tabPane.getComponentAt(i);
277: }
278: }
279: for (int i = 0; i < tabs.length; i++) {
280: tabPane.remove(tabs[i]);
281: }
282: } else if (source instanceof TabMenuItem) {
283: TabMenuItem item = (TabMenuItem) source;
284: tabPane.setSelectedComponent(item.getTabComponent());
285: }
286:
287: }
288:
289: public int getHoverTabIndex() {
290: return hoverTabIndex;
291: }
292:
293: public void setHoverTabIndex(int hoverTabIndex) {
294: this .hoverTabIndex = hoverTabIndex;
295: }
296:
297: } // class TabPopupMenu
298:
299: }
|