KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > TabListPopupAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.tabcontrol;
21
22 import java.awt.Point JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.AbstractButton JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import org.netbeans.swing.popupswitcher.SwitcherTableItem;
32 import org.openide.windows.TopComponent;
33
34 /**
35  * An action which, when invoked, displays a popup with an alphabetized table
36  * of all the tabs in a TabDisplayer.
37  *
38  * @author Tim Boudreau
39  */

40 public class TabListPopupAction extends AbstractAction JavaDoc {
41     
42     private TabDisplayer displayer;
43     
44     public TabListPopupAction(TabDisplayer displayer) {
45         this.displayer = displayer;
46     }
47     
48     public void actionPerformed(ActionEvent JavaDoc ae) {
49         if ("pressed".equals(ae.getActionCommand())) {
50             JComponent JavaDoc jc = (JComponent JavaDoc) ae.getSource();
51             Point JavaDoc p = new Point JavaDoc(jc.getWidth(), jc.getHeight());
52             SwingUtilities.convertPointToScreen(p, jc);
53             if (!ButtonPopupSwitcher.isShown()) {
54                 SwitcherTableItem[] items = createSwitcherItems(displayer);
55                 Arrays.sort(items);
56                 ButtonPopupSwitcher.selectItem(jc, items, p.x, p.y);
57             }
58             //Other portion of issue 37487, looks funny if the
59
//button becomes pressed
60
if (jc instanceof AbstractButton JavaDoc) {
61                 AbstractButton JavaDoc jb = (AbstractButton JavaDoc) jc;
62                 jb.getModel().setPressed(false);
63                 jb.getModel().setRollover(false);
64                 jb.getModel().setArmed(false);
65                 jb.repaint();
66             }
67         }
68     }
69     
70     private SwitcherTableItem[] createSwitcherItems(TabDisplayer displayer) {
71         List JavaDoc tabs = displayer.getModel().getTabs();
72         SwitcherTableItem[] items = new SwitcherTableItem[tabs.size()];
73         int i = 0;
74         int selIdx = displayer.getSelectionModel().getSelectedIndex();
75         TabData selectedTab = selIdx >= 0 ? displayer.getModel().getTab(selIdx) : null;
76         for (Iterator JavaDoc it = tabs.iterator(); it.hasNext(); ) {
77             TabData tab = (TabData) it.next();
78             String JavaDoc name;
79             String JavaDoc htmlName;
80             if (tab.getComponent() instanceof TopComponent) {
81                 TopComponent tabTC = (TopComponent) tab.getComponent();
82                 name = tabTC.getDisplayName();
83                 // #68291 fix - some hostile top components have null display name
84
if (name == null) {
85                     name = tabTC.getName();
86                 }
87                 htmlName = tabTC.getHtmlDisplayName();
88                 if (htmlName == null) {
89                     htmlName = name;
90                 }
91             } else {
92                 name = htmlName = tab.getText();
93             }
94             items[i++] = new SwitcherTableItem(
95                     new ActivatableTab(tab),
96                     name,
97                     htmlName,
98                     tab.getIcon(),
99                     tab == selectedTab);
100         }
101         return items;
102     }
103     
104     private class ActivatableTab implements SwitcherTableItem.Activatable {
105         private TabData tab;
106         
107         private ActivatableTab(TabData tab) {
108             this.tab = tab;
109         }
110         
111         public void activate() {
112             if (tab != null) {
113                 selectTab(tab);
114             }
115         }
116         
117         /**
118          * Maps tab selected in quicklist to tab index in displayer to select
119          * correct tab
120          */

121         private void selectTab(TabData tab) {
122             //Find corresponding index in displayer
123
List JavaDoc tabs = displayer.getModel().getTabs();
124             int ind = -1;
125             for (int i = 0; i < tabs.size(); i++) {
126                 if (tab.equals(tabs.get(i))) {
127                     ind = i;
128                     break;
129                 }
130             }
131             if (ind != -1) {
132                 int old = displayer.getSelectionModel().getSelectedIndex();
133                 displayer.getSelectionModel().setSelectedIndex(ind);
134                 //#40665 fix start
135
if (displayer.getType() == TabbedContainer.TYPE_EDITOR
136                         && ind >= 0 && ind == old) {
137                     displayer.getUI().makeTabVisible(ind);
138                 }
139                 //#40665 fix end
140
}
141         }
142     }
143 }
144
Popular Tags