1 19 20 package org.netbeans.swing.tabcontrol; 21 22 import java.awt.Point ; 23 import java.awt.event.ActionEvent ; 24 import java.util.Arrays ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.AbstractButton ; 29 import javax.swing.JComponent ; 30 import javax.swing.SwingUtilities ; 31 import org.netbeans.swing.popupswitcher.SwitcherTableItem; 32 import org.openide.windows.TopComponent; 33 34 40 public class TabListPopupAction extends AbstractAction { 41 42 private TabDisplayer displayer; 43 44 public TabListPopupAction(TabDisplayer displayer) { 45 this.displayer = displayer; 46 } 47 48 public void actionPerformed(ActionEvent ae) { 49 if ("pressed".equals(ae.getActionCommand())) { 50 JComponent jc = (JComponent ) ae.getSource(); 51 Point p = new Point (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 if (jc instanceof AbstractButton ) { 61 AbstractButton jb = (AbstractButton ) 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 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 it = tabs.iterator(); it.hasNext(); ) { 77 TabData tab = (TabData) it.next(); 78 String name; 79 String htmlName; 80 if (tab.getComponent() instanceof TopComponent) { 81 TopComponent tabTC = (TopComponent) tab.getComponent(); 82 name = tabTC.getDisplayName(); 83 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 121 private void selectTab(TabData tab) { 122 List 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 if (displayer.getType() == TabbedContainer.TYPE_EDITOR 136 && ind >= 0 && ind == old) { 137 displayer.getUI().makeTabVisible(ind); 138 } 139 } 141 } 142 } 143 } 144 | Popular Tags |