001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
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 (at your option) 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,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: TabDropDownList.java,v 1.18 2005/12/04 13:46:05 jesper Exp $
023: package net.infonode.tabbedpanel.internal;
024:
025: import net.infonode.gui.PopupList;
026: import net.infonode.gui.PopupListListener;
027: import net.infonode.gui.TextIconListCellRenderer;
028: import net.infonode.tabbedpanel.*;
029:
030: import javax.swing.*;
031: import javax.swing.event.ListSelectionEvent;
032: import javax.swing.event.ListSelectionListener;
033:
034: /**
035: * @author Bjorn Lind
036: * @version $Revision: 1.18 $ $Date: 2005/12/04 13:46:05 $
037: * @since ITP 1.1.0
038: */
039: public class TabDropDownList extends PopupList {
040: private TabbedPanel tabbedPanel;
041: private TextIconListCellRenderer cellRenderer;
042:
043: private TabListener tabListener = new TabAdapter() {
044: public void tabAdded(TabEvent event) {
045: if (event.getTab().getTabbedPanel().getTabCount() == 2)
046: setVisible(true);
047: }
048:
049: public void tabRemoved(TabRemovedEvent event) {
050: if (event.getTabbedPanel().getTabCount() == 1)
051: setVisible(false);
052: }
053: };
054:
055: public TabDropDownList(final TabbedPanel tabbedPanel,
056: AbstractButton button) {
057: super (button);
058: this .tabbedPanel = tabbedPanel;
059:
060: addPopupListListener(new PopupListListener() {
061: public void willBecomeVisible(PopupList l) {
062: int numTabs = tabbedPanel.getTabCount();
063: Tab[] tabs = new Tab[numTabs];
064: for (int i = 0; i < numTabs; i++) {
065: tabs[i] = tabbedPanel.getTabAt(i);
066: }
067: cellRenderer.calculateMaximumIconWidth(tabs);
068: getList().setListData(tabs);
069: getList().setSelectedValue(
070: tabbedPanel.getSelectedTab(), true);
071: }
072: });
073:
074: addListSelectionListener(new ListSelectionListener() {
075: public void valueChanged(ListSelectionEvent e) {
076: if (!e.getValueIsAdjusting())
077: tabbedPanel.setSelectedTab((Tab) getList()
078: .getSelectedValue());
079: }
080: });
081:
082: if (tabbedPanel.getProperties()
083: .getTabDropDownListVisiblePolicy() == TabDropDownListVisiblePolicy.MORE_THAN_ONE_TAB) {
084: tabbedPanel.addTabListener(tabListener);
085: setVisible(tabbedPanel.getTabCount() > 1);
086: }
087:
088: cellRenderer = new TextIconListCellRenderer(getList()
089: .getCellRenderer());
090: getList().setCellRenderer(cellRenderer);
091: setOpaque(false);
092: }
093:
094: public void dispose() {
095: tabbedPanel.removeTabListener(tabListener);
096: }
097:
098: public void updateUI() {
099: super .updateUI();
100:
101: if (cellRenderer != null) {
102: ListCellRenderer renderer = (ListCellRenderer) UIManager
103: .get("List.cellRenderer");
104: if (renderer == null)
105: renderer = new DefaultListCellRenderer();
106: cellRenderer.setRenderer(renderer);
107: }
108: }
109: }
|