1 19 20 package org.netbeans.modules.form.palette; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.lang.reflect.*; 25 import org.openide.awt.*; 26 27 28 30 public class ScrollPopupMenu extends JPopupMenu { 31 32 JWindow popWin; 33 JScrollPane scrollPane; 34 int posX, posY; 35 int maxHeight; 36 37 ScrollPopupMenu(int maxH) { 38 maxHeight = maxH; 39 } 40 41 public void setVisible(boolean visible) { 42 if (visible == isVisible()) 43 return; 44 45 if (visible) { 46 if (getInvoker() != null && !(getInvoker() instanceof JMenu)) { 47 if (getSubElements().length > 0) { 48 MenuElement me[] = new MenuElement[2]; 49 me[0] = (MenuElement)this; 50 me[1] = getSubElements()[0]; 51 MenuSelectionManager.defaultManager().setSelectedPath(me); 52 } 53 else { 54 MenuElement me[] = new MenuElement[1]; 55 me[0] = (MenuElement)this; 56 MenuSelectionManager.defaultManager().setSelectedPath(me); 57 } 58 } 59 60 firePopupMenuWillBecomeVisible(); 61 62 Component comp = getInvoker(); 63 while (comp.getParent() != null) 64 comp = comp.getParent(); 65 66 popWin = comp instanceof Window ? 67 new JWindow((Window)comp) : 68 new JWindow(new JFrame()); 69 popWin.setLocation(posX, posY); 70 71 pack(); 72 popWin.setVisible(true); 73 } 74 else { 75 getSelectionModel().clearSelection(); 76 if (popWin != null) { 77 firePopupMenuWillBecomeInvisible(); 78 popWin.dispose(); 79 popWin = null; 80 scrollPane = null; 81 } 82 } 83 } 84 85 public boolean isVisible() { 86 return popWin != null ? popWin.isShowing() : false; 87 } 88 89 public void setLocation(int x, int y) { 90 if (popWin != null && popWin.isShowing()) 91 popWin.setLocation(x, y); 92 else { 93 posX = x; 94 posY = y; 95 } 96 } 97 98 public void pack() { 99 if (popWin == null) 100 return; 101 102 Dimension prefSize = getPreferredSize(); 103 if (maxHeight == 0 || prefSize.height <= maxHeight) { 104 if (scrollPane != null) { 105 popWin.getContentPane().remove(scrollPane); 106 scrollPane = null; 107 } 108 popWin.getContentPane().setLayout(null); 109 popWin.getContentPane().add(this); 110 setBounds(0,0, prefSize.width, prefSize.height); 111 popWin.setSize(prefSize.width, prefSize.height); 112 } 113 else { 114 if (scrollPane == null) { 115 JPanel view = new JPanel(new BorderLayout()); 116 view.add(this, BorderLayout.CENTER); 117 118 scrollPane = new JScrollPane(view); 119 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 120 JScrollBar bar = scrollPane.getVerticalScrollBar(); 122 if (bar != null) { 123 Dimension d = bar.getPreferredSize(); 124 d.width = 12; 125 bar.setPreferredSize(d); 126 bar.setUnitIncrement(21); 127 doNotCancelPopupHack(bar); 129 } 130 131 popWin.getContentPane().add(scrollPane, BorderLayout.CENTER); 132 } 133 popWin.pack(); 134 popWin.setSize(popWin.getSize().width+12, maxHeight); 135 requestFocus(); 136 } 137 } 138 139 public JScrollPane getScrollPane() { 140 return scrollPane; 141 } 142 143 public JScrollBar getScrollBar() { 144 return scrollPane != null ? scrollPane.getVerticalScrollBar() : null; 145 } 146 147 public static void doNotCancelPopupHack(JComponent component) { 148 if (System.getProperty("java.version").startsWith("1.5")) { try { 150 Class clazz = javax.swing.plaf.basic.BasicComboBoxUI .class; 151 Field field = clazz.getDeclaredField("HIDE_POPUP_KEY"); field.setAccessible(true); 153 component.putClientProperty("doNotCancelPopup", field.get(null)); for (int i=0; i<component.getComponentCount(); i++) { 155 Component comp = component.getComponent(i); 156 if (comp instanceof JComponent) { 157 doNotCancelPopupHack((JComponent)comp); 158 } 159 } 160 } catch (Exception ex) { 161 ex.printStackTrace(); 162 } 163 } 164 } 165 } 166 | Popular Tags |