1 /*
2 * Copyright (c) 1998-2004 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.experiment.gui.views.helpers;
20
21 import java.awt.Color;
22
23 import javax.swing.BorderFactory;
24 import javax.swing.JComboBox;
25 import javax.swing.JScrollPane;
26 import javax.swing.ScrollPaneConstants;
27 import javax.swing.plaf.basic.BasicComboBoxUI;
28 import javax.swing.plaf.basic.BasicComboPopup;
29 import javax.swing.plaf.basic.ComboPopup;
30
31 /**
32 * @author Bjarte Svaeren
33 */
34 public class ScrollableComboBox extends JComboBox {
35
36 /**
37 *
38 */
39 public ScrollableComboBox()
40 {
41 super();
42 setUI(new ScrollableComboUI());
43 setBorder(BorderFactory.createLineBorder(Color.BLACK));
44 }
45
46
47 private class ScrollableComboUI extends BasicComboBoxUI
48 {
49 /* (non-Javadoc)
50 * @see javax.swing.plaf.basic.BasicComboBoxUI#createPopup()
51 */
52 protected ComboPopup createPopup()
53 {
54 BasicComboPopup popup = new ScrollableComboPopup(comboBox);
55 popup.getAccessibleContext().setAccessibleParent(comboBox);
56 return popup;
57 }
58 }
59
60
61 private class ScrollableComboPopup extends BasicComboPopup
62 {
63 private boolean scrollbarAdded;
64
65 public ScrollableComboPopup(JComboBox combo) {
66 super(combo);
67 scrollbarAdded = false;
68 }
69
70
71 /* (non-Javadoc)
72 * @see javax.swing.plaf.basic.BasicComboPopup#createScroller()
73 */
74 protected JScrollPane createScroller() {
75 JScrollPane scrollPane = new JScrollPane(list,
76 ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
77 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
78 return scrollPane;
79 }
80
81
82 /* (non-Javadoc)
83 * @see javax.swing.plaf.basic.BasicComboPopup#getPopupHeightForRowCount(int)
84 */
85 // protected int getPopupHeightForRowCount(int maxRowCount) {
86 // // Set the cached value of the minimum row count
87 // int minRowCount = Math.min( maxRowCount, comboBox.getItemCount() );
88 // int height = 0;
89 // ListCellRenderer renderer = list.getCellRenderer();
90 // Object value = null;
91 //
92 // for ( int i = 0; i < minRowCount; ++i ) {
93 // value = list.getModel().getElementAt( i );
94 // Component c = renderer.getListCellRendererComponent( list, value, i, false, false );
95 // height += c.getPreferredSize().height;
96 // }
97 //
98 // return height == 0 ? 100 : height;
99 //
100 //
101 // int height = super.getPopupHeightForRowCount(maxRowCount);
102 // height += scroller.getHeight();
103 // int scrollbarHeight = scroller.getHeight();
104 // // Add the height of the scrollbar if it's not been added
105 // if(!scrollbarAdded && scrollbarHeight > 0) {
106 // height += scrollbarHeight;
107 // scrollbarAdded = true;
108 // }
109 //
110 // if(comboBox.getItemCount() > maxRowCount)
111 // scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
112 //
113 // return height;
114 // }
115 }
116 }