KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > palette > ScrollPopupMenu


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.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 /** Hacked JPopupMenu(Plus) - displayed in JScrollPane if too long.
29  */

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     // scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
121
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                     // Issue 47181
128
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")) { // NOI18N
149
try {
150                 Class JavaDoc clazz = javax.swing.plaf.basic.BasicComboBoxUI JavaDoc.class;
151                 Field field = clazz.getDeclaredField("HIDE_POPUP_KEY"); // NOI18N
152
field.setAccessible(true);
153                 component.putClientProperty("doNotCancelPopup", field.get(null)); // NOI18N
154
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 JavaDoc ex) {
161                 ex.printStackTrace();
162             }
163         }
164     }
165 }
166
Popular Tags