1 /*
2 * JScroll - the scrollable desktop pane for Java.
3 * Copyright (C) 2003 Tom Tessier
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 package org.jscroll.components;
22
23 import javax.swing.*;
24
25 import java.awt.*;
26 import java.awt.event.*;
27
28 import java.util.*;
29
30
31 /***
32 * Generic self-contained resizable toolbar class. When a button addition exceeds
33 * the width of the toolbar container, all buttons within the container are
34 * automatically resized to compensate, down to the minimum button width defined
35 * upon creation of the ResizableToolbar instance.
36 *
37 * @author <a href="mailto:tessier@gabinternet.com">Tom Tessier</a>
38 * @version 1.0 03-Mar-2001
39 */
40 public class ResizableToolBar extends JToolBar implements ComponentListener {
41 // ButtonGroups for toolbar buttons
42 private ButtonGroup buttonGroup;
43 private int minButtonWidth;
44 private int maxButtonWidth;
45
46 /***
47 * creates the ResizableToolbar object
48 *
49 * @param minButtonWidth the minimum button width allowed
50 * @param maxButtonWidth the maximum button width allowed
51 */
52 public ResizableToolBar(int minButtonWidth, int maxButtonWidth) {
53 buttonGroup = new ButtonGroup();
54 setFloatable(false);
55 this.minButtonWidth = minButtonWidth;
56 this.maxButtonWidth = maxButtonWidth;
57
58 addComponentListener(this);
59 }
60
61 /***
62 * adds a button to the ResizableToolbar
63 *
64 * @param button the button to add
65 */
66 public void add(AbstractButton button) {
67 buttonGroup.add(button);
68 super.add(button);
69 button.setSelected(true);
70 resizeButtons();
71 }
72
73 /***
74 * removes a button from the ResizableToolbar
75 *
76 * @param button the button to remove
77 */
78 public void remove(AbstractButton button) {
79 super.remove(button);
80 buttonGroup.remove(button);
81 resizeButtons();
82 repaint();
83 }
84
85 /***
86 * returns the ResizableToolbar elements
87 *
88 * @return an Enumeration of the ResizableToolbar elements
89 */
90 public Enumeration getElements() {
91 return buttonGroup.getElements();
92 }
93
94 /***
95 * returns the number of buttons stored within the ResizableToolbar
96 *
97 * @return the number of buttons
98 */
99 public int getButtonCount() {
100 // note: getButtonCount() will not work with JDK 1.2
101 return buttonGroup.getButtonCount();
102 }
103
104 /***
105 * resizes the buttons of the toolbar, depending upon the total number
106 * of components stored therein.
107 * Executes as an "invoked later" thread for a slight perceived
108 * performance boost.
109 */
110 private void resizeButtons() {
111 final float exactButtonWidth = getCurrentButtonWidth();
112
113 SwingUtilities.invokeLater(new Runnable() {
114 public void run() {
115 JToggleButton b = null;
116 Enumeration e = getElements();
117
118 float currentButtonXLocation = 0.0f;
119
120 // resize the buttons
121 while (e.hasMoreElements()) {
122 b = (JToggleButton) e.nextElement();
123
124 int buttonWidth = Math.round(currentButtonXLocation +
125 exactButtonWidth) -
126 Math.round(currentButtonXLocation);
127 assignWidth(b, buttonWidth);
128
129 currentButtonXLocation += exactButtonWidth;
130 }
131
132 revalidate();
133 }
134 });
135 }
136
137 /***
138 * returns the current button width, defined as the width of the ResizableToolbar
139 * divided by the number of buttons. The value returned ranges from
140 * minButtonWidth to maxButtonWidth (two variables defined upon creation
141 * of the ResizableToolbar instance).
142 *
143 * @return the current button width as a float.
144 */
145 private float getCurrentButtonWidth() {
146 int width = getWidth() - getInsets().left - getInsets().right;
147
148 // if width <= 0, means JToolbar hasn't been displayed yet, so use
149 // the maximum button width
150 float buttonWidth = ((width <= 0) ? maxButtonWidth : width);
151
152 int numButtons = getButtonCount();
153
154 // have at least one button? then divide the width by the # of buttons
155 // (ie: resultant buttonWidth = viewport width / # of buttons)
156 if (numButtons > 0) {
157 buttonWidth /= numButtons;
158 }
159
160 if (buttonWidth < minButtonWidth) {
161 buttonWidth = minButtonWidth;
162 } else if (buttonWidth > maxButtonWidth) {
163 buttonWidth = maxButtonWidth;
164 }
165
166 return buttonWidth;
167 }
168
169 /***
170 * assigns a new width to the specified button
171 *
172 * @param b the button whose width is to be adjusted
173 * @param buttonWidth the new width
174 */
175 private void assignWidth(JToggleButton b, int buttonWidth) {
176 b.setMinimumSize(new Dimension(buttonWidth - 2,
177 b.getPreferredSize().height));
178 b.setPreferredSize(new Dimension(buttonWidth,
179 b.getPreferredSize().height));
180
181 Dimension newSize = b.getPreferredSize();
182 b.setMaximumSize(newSize);
183 b.setSize(newSize);
184 }
185
186 /////
187 // respond to resize events...
188 /////
189
190 /***
191 * resize the buttons when the ResizableToolbar itself is resized
192 *
193 * @param e the ComponentEvent
194 */
195 public void componentResized(ComponentEvent e) {
196 resizeButtons();
197 }
198
199 /***
200 * interface placeholder
201 *
202 * @param e the ComponentEvent
203 */
204 public void componentShown(ComponentEvent e) {
205 }
206
207 /***
208 * interface placeholder
209 *
210 * @param e the ComponentEvent
211 */
212 public void componentMoved(ComponentEvent e) {
213 }
214
215 /***
216 * interface placeholder
217 *
218 * @param e the ComponentEvent
219 */
220 public void componentHidden(ComponentEvent e) {
221 }
222 }
This page was automatically generated by Maven