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-2006 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:
042: package org.netbeans.swing.tabcontrol;
043:
044: import java.awt.Point;
045: import java.awt.event.ActionEvent;
046: import java.util.Arrays;
047: import java.util.Iterator;
048: import java.util.List;
049: import javax.swing.AbstractAction;
050: import javax.swing.AbstractButton;
051: import javax.swing.JComponent;
052: import javax.swing.SwingUtilities;
053: import org.netbeans.swing.popupswitcher.SwitcherTableItem;
054: import org.openide.windows.TopComponent;
055:
056: /**
057: * An action which, when invoked, displays a popup with an alphabetized table
058: * of all the tabs in a TabDisplayer.
059: *
060: * @author Tim Boudreau
061: */
062: public class TabListPopupAction extends AbstractAction {
063:
064: private TabDisplayer displayer;
065:
066: public TabListPopupAction(TabDisplayer displayer) {
067: this .displayer = displayer;
068: }
069:
070: public void actionPerformed(ActionEvent ae) {
071: if ("pressed".equals(ae.getActionCommand())) {
072: JComponent jc = (JComponent) ae.getSource();
073: Point p = new Point(jc.getWidth(), jc.getHeight());
074: SwingUtilities.convertPointToScreen(p, jc);
075: if (!ButtonPopupSwitcher.isShown()) {
076: SwitcherTableItem[] items = createSwitcherItems(displayer);
077: Arrays.sort(items);
078: ButtonPopupSwitcher.selectItem(jc, items, p.x, p.y);
079: }
080: //Other portion of issue 37487, looks funny if the
081: //button becomes pressed
082: if (jc instanceof AbstractButton) {
083: AbstractButton jb = (AbstractButton) jc;
084: jb.getModel().setPressed(false);
085: jb.getModel().setRollover(false);
086: jb.getModel().setArmed(false);
087: jb.repaint();
088: }
089: }
090: }
091:
092: private SwitcherTableItem[] createSwitcherItems(
093: TabDisplayer displayer) {
094: List tabs = displayer.getModel().getTabs();
095: SwitcherTableItem[] items = new SwitcherTableItem[tabs.size()];
096: int i = 0;
097: int selIdx = displayer.getSelectionModel().getSelectedIndex();
098: TabData selectedTab = selIdx >= 0 ? displayer.getModel()
099: .getTab(selIdx) : null;
100: for (Iterator it = tabs.iterator(); it.hasNext();) {
101: TabData tab = (TabData) it.next();
102: String name;
103: String htmlName;
104: if (tab.getComponent() instanceof TopComponent) {
105: TopComponent tabTC = (TopComponent) tab.getComponent();
106: name = tabTC.getDisplayName();
107: // #68291 fix - some hostile top components have null display name
108: if (name == null) {
109: name = tabTC.getName();
110: }
111: htmlName = tabTC.getHtmlDisplayName();
112: if (htmlName == null) {
113: htmlName = name;
114: }
115: } else {
116: name = htmlName = tab.getText();
117: }
118: items[i++] = new SwitcherTableItem(new ActivatableTab(tab),
119: name, htmlName, tab.getIcon(), tab == selectedTab);
120: }
121: return items;
122: }
123:
124: private class ActivatableTab implements
125: SwitcherTableItem.Activatable {
126: private TabData tab;
127:
128: private ActivatableTab(TabData tab) {
129: this .tab = tab;
130: }
131:
132: public void activate() {
133: if (tab != null) {
134: selectTab(tab);
135: }
136: }
137:
138: /**
139: * Maps tab selected in quicklist to tab index in displayer to select
140: * correct tab
141: */
142: private void selectTab(TabData tab) {
143: //Find corresponding index in displayer
144: List tabs = displayer.getModel().getTabs();
145: int ind = -1;
146: for (int i = 0; i < tabs.size(); i++) {
147: if (tab.equals(tabs.get(i))) {
148: ind = i;
149: break;
150: }
151: }
152: if (ind != -1) {
153: int old = displayer.getSelectionModel()
154: .getSelectedIndex();
155: displayer.getSelectionModel().setSelectedIndex(ind);
156: //#40665 fix start
157: if (displayer.getType() == TabbedContainer.TYPE_EDITOR
158: && ind >= 0 && ind == old) {
159: displayer.getUI().makeTabVisible(ind);
160: }
161: //#40665 fix end
162: }
163: }
164: }
165: }
|