Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.awt; |
43 |
|
44 |
import java.awt.Color; |
45 |
import java.awt.Component; |
46 |
import java.awt.Dimension; |
47 |
import java.awt.event.ComponentAdapter; |
48 |
import java.awt.event.ComponentEvent; |
49 |
import java.awt.event.MouseAdapter; |
50 |
import java.awt.event.MouseEvent; |
51 |
import java.awt.event.MouseMotionListener; |
52 |
import javax.swing.BorderFactory; |
53 |
import javax.swing.JButton; |
54 |
import javax.swing.JComponent; |
55 |
import javax.swing.JPopupMenu; |
56 |
import javax.swing.JToolBar; |
57 |
import org.openide.util.ImageUtilities; |
58 |
|
59 |
/** |
60 |
* ToolbarWithOverflow provides a component which is useful for displaying commonly used |
61 |
* actions. It adds an overflow button when the toolbar becomes too small to show all the |
62 |
* available actions. |
63 |
* |
64 |
* @author Th. Oikonomou |
65 |
* @since 7.50 |
66 |
*/ |
67 |
public class ToolbarWithOverflow extends JToolBar { |
68 |
|
69 |
private JButton overflowButton; |
70 |
private JPopupMenu popup; |
71 |
private JToolBar overflowToolbar; |
72 |
private int visibleButtons; |
73 |
private boolean displayOverflowOnHover = true; |
74 |
private final String toolbarArrowHorizontal = "org/openide/awt/resources/toolbar_arrow_horizontal.png"; //NOI18N |
75 |
private final String toolbarArrowVertical = "org/openide/awt/resources/toolbar_arrow_vertical.png"; //NOI18N |
76 |
private final String PROP_PREF_ICON_SIZE = "PreferredIconSize"; //NOI18N |
77 |
private final String PROP_DRAGGER = "_toolbar_dragger_"; //NOI18N |
78 |
|
79 |
/** |
80 |
* Creates a new tool bar; orientation defaults to |
81 |
* <code>HORIZONTAL</code>. |
82 |
*/ |
83 |
public ToolbarWithOverflow() { |
84 |
this(HORIZONTAL); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Creates a new tool bar with the specified |
89 |
* <code>orientation</code>. The |
90 |
* <code>orientation</code> must be either |
91 |
* <code>HORIZONTAL</code> or |
92 |
* <code>VERTICAL</code>. |
93 |
* |
94 |
* @param orientation the orientation desired |
95 |
*/ |
96 |
public ToolbarWithOverflow(int orientation) { |
97 |
this(null, orientation); |
98 |
} |
99 |
|
100 |
/** |
101 |
* Creates a new tool bar with the specified |
102 |
* <code>name</code>. The name is used as the title of the undocked tool |
103 |
* bar. The default orientation is |
104 |
* <code>HORIZONTAL</code>. |
105 |
* |
106 |
* @param name the name of the tool bar |
107 |
*/ |
108 |
public ToolbarWithOverflow(String name) { |
109 |
this(name, HORIZONTAL); |
110 |
} |
111 |
|
112 |
/** |
113 |
* Creates a new tool bar with a specified |
114 |
* <code>name</code> and |
115 |
* <code>orientation</code>. All other constructors call this constructor. |
116 |
* If |
117 |
* <code>orientation</code> is an invalid value, an exception will be |
118 |
* thrown. |
119 |
* |
120 |
* @param name the name of the tool bar |
121 |
* @param orientation the initial orientation -- it must be * either <code>HORIZONTAL</code> or <code>VERTICAL</code> |
122 |
* @exception IllegalArgumentException if orientation is neither |
123 |
* <code>HORIZONTAL</code> nor <code>VERTICAL</code> |
124 |
*/ |
125 |
public ToolbarWithOverflow(String name, int orientation) { |
126 |
super(name, orientation); |
127 |
setupOverflowButton(); |
128 |
popup = new JPopupMenu(); |
129 |
popup.setBorderPainted(false); |
130 |
popup.setBorder(BorderFactory.createEmptyBorder()); |
131 |
overflowToolbar = new JToolBar("overflowToolbar", orientation == HORIZONTAL ? VERTICAL : HORIZONTAL); |
132 |
overflowToolbar.setFloatable(false); |
133 |
overflowToolbar.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1)); |
134 |
addComponentListener(new ComponentAdapter() { |
135 |
@Override |
136 |
public void componentResized(ComponentEvent e) { |
137 |
maybeAddOverflow(); |
138 |
} |
139 |
}); |
140 |
overflowToolbar.addMouseListener(new MouseAdapter() { |
141 |
|
142 |
@Override |
143 |
public void mouseExited(MouseEvent e) { |
144 |
if (popup.isShowing()) { |
145 |
int minX = popup.getLocationOnScreen().x; |
146 |
int maxX = popup.getLocationOnScreen().x + popup.getWidth(); |
147 |
int minY = popup.getLocationOnScreen().y; |
148 |
int maxY = popup.getLocationOnScreen().y + popup.getHeight(); |
149 |
if (e.getXOnScreen() < minX || e.getXOnScreen() >= maxX || e.getYOnScreen() < minY || e.getYOnScreen() >= maxY) { |
150 |
popup.setVisible(false); |
151 |
} |
152 |
} |
153 |
} |
154 |
}); |
155 |
} |
156 |
|
157 |
/** |
158 |
* Returns whether the overflow should be displayed on hover or not. The |
159 |
* default value is <code>true</code>. |
160 |
* |
161 |
* @return <code>true</code> if overflow is displayed on hover; <code>false</code> otherwise |
162 |
*/ |
163 |
public boolean isDisplayOverflowOnHover() { |
164 |
return displayOverflowOnHover; |
165 |
} |
166 |
|
167 |
/** |
168 |
* Sets whether the overflow should be displayed on hover or not. The |
169 |
* default value is <code>true</code>. |
170 |
* |
171 |
* @param displayOverflowOnHover if <code>true</code>, the overflow will be displayed on hover; |
172 |
* <code>false</code> otherwise |
173 |
*/ |
174 |
public void setDisplayOverflowOnHover(boolean displayOverflowOnHover) { |
175 |
this.displayOverflowOnHover = displayOverflowOnHover; |
176 |
setupOverflowButton(); |
177 |
} |
178 |
|
179 |
@Override |
180 |
public final Dimension getPreferredSize() { |
181 |
Component[] comps = getAllComponents(); |
182 |
int width = 0; |
183 |
int height = 0; |
184 |
for (int i = 0; i < comps.length; i++) { |
185 |
Component comp = comps[i]; |
186 |
width += getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; |
187 |
height = getOrientation() == HORIZONTAL ? comp.getPreferredSize().height : comp.getPreferredSize().width; |
188 |
} |
189 |
if(overflowToolbar.getComponentCount() > 0) { |
190 |
width += getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; |
191 |
} |
192 |
Dimension dim = getOrientation() == HORIZONTAL ? new Dimension(width, height) : new Dimension(height, width); |
193 |
return dim; |
194 |
} |
195 |
|
196 |
@Override |
197 |
public void setOrientation(int o) { |
198 |
super.setOrientation(o); |
199 |
setupOverflowButton(); |
200 |
} |
201 |
|
202 |
@Override |
203 |
public void removeAll() { |
204 |
super.removeAll(); |
205 |
overflowToolbar.removeAll(); |
206 |
} |
207 |
|
208 |
@Override |
209 |
public void validate() { |
210 |
computeVisibleButtons(); |
211 |
if (visibleButtons == -1) { |
212 |
handleOverflowRemoval(); |
213 |
} else { |
214 |
handleOverflowAddittion(); |
215 |
} |
216 |
super.validate(); |
217 |
} |
218 |
|
219 |
private void setupOverflowButton() { |
220 |
overflowButton = new JButton(ImageUtilities.loadImageIcon(getOrientation() == HORIZONTAL ? toolbarArrowVertical : toolbarArrowHorizontal, false)); |
221 |
overflowButton.addMouseListener(new MouseAdapter() { |
222 |
@Override |
223 |
public void mouseClicked(MouseEvent e) { |
224 |
displayOverflow(); |
225 |
} |
226 |
|
227 |
@Override |
228 |
public void mouseEntered(MouseEvent e) { |
229 |
if(displayOverflowOnHover) { |
230 |
displayOverflow(); |
231 |
} |
232 |
} |
233 |
|
234 |
@Override |
235 |
public void mouseExited(MouseEvent e) { |
236 |
if (popup.isShowing()) { |
237 |
int minX = overflowButton.getLocationOnScreen().x; |
238 |
int maxX = getOrientation() == HORIZONTAL ? minX + overflowButton.getWidth() : popup.getLocationOnScreen().x + popup.getWidth(); |
239 |
int minY = overflowButton.getLocationOnScreen().y; |
240 |
int maxY = getOrientation() == HORIZONTAL ? popup.getLocationOnScreen().y + popup.getHeight() : minY + overflowButton.getHeight(); |
241 |
if(e.getXOnScreen() < minX || e.getXOnScreen() >= maxX || e.getYOnScreen() < minY || e.getYOnScreen() >= maxY) { |
242 |
popup.setVisible(false); |
243 |
} |
244 |
} |
245 |
} |
246 |
}); |
247 |
} |
248 |
|
249 |
private void displayOverflow() { |
250 |
int x = getOrientation() == HORIZONTAL ? 0 : overflowButton.getWidth(); |
251 |
int y = getOrientation() == HORIZONTAL ? overflowButton.getHeight() : 0; |
252 |
popup.show(overflowButton, x, y); |
253 |
} |
254 |
|
255 |
/** |
256 |
* Determines if an overflow button should be added to or removed from the toolbar. |
257 |
*/ |
258 |
private void maybeAddOverflow() { |
259 |
validate(); |
260 |
repaint(); |
261 |
} |
262 |
|
263 |
private void computeVisibleButtons() { |
264 |
if (isShowing()) { |
265 |
int w = getOrientation() == HORIZONTAL ? overflowButton.getIcon().getIconWidth() + 4 : getWidth() - getInsets().left - getInsets().right; |
266 |
int h = getOrientation() == HORIZONTAL ? getHeight() - getInsets().top - getInsets().bottom : overflowButton.getIcon().getIconHeight() + 4; |
267 |
overflowButton.setMaximumSize(new Dimension(w, h)); |
268 |
overflowButton.setMinimumSize(new Dimension(w, h)); |
269 |
overflowButton.setPreferredSize(new Dimension(w, h)); |
270 |
} |
271 |
handleIconResize(); |
272 |
Component[] comps = getAllComponents(); |
273 |
int sizeSoFar = 0; |
274 |
int maxSize = getOrientation() == HORIZONTAL ? getWidth() : getHeight(); |
275 |
int overflowButtonSize = getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; |
276 |
for (int i = 0; i < comps.length; i++) { |
277 |
Component comp = comps[i]; |
278 |
sizeSoFar += getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; |
279 |
if (sizeSoFar > maxSize) { |
280 |
visibleButtons = i; |
281 |
break; |
282 |
} else { |
283 |
if (sizeSoFar + overflowButtonSize > maxSize && i + 1 != comps.length) { |
284 |
visibleButtons = i; |
285 |
break; |
286 |
} else { |
287 |
visibleButtons = i + 1; |
288 |
} |
289 |
} |
290 |
} |
291 |
if(visibleButtons == 0 && comps.length > 0) { |
292 |
if (comps[0] instanceof JComponent) { |
293 |
if (Boolean.TRUE.equals(((JComponent) comps[0]).getClientProperty(PROP_DRAGGER))) { |
294 |
visibleButtons = 1; |
295 |
} |
296 |
} |
297 |
} |
298 |
if (visibleButtons == comps.length) { |
299 |
visibleButtons = -1; |
300 |
} |
301 |
} |
302 |
|
303 |
private void handleOverflowAddittion() { |
304 |
Component[] comps = getAllComponents(); |
305 |
if (visibleButtons <= comps.length) { |
306 |
removeAll(); |
307 |
overflowToolbar.setOrientation(getOrientation() == HORIZONTAL ? VERTICAL : HORIZONTAL); |
308 |
popup.removeAll(); |
309 |
|
310 |
for (int i = 0; i < visibleButtons; i++) { |
311 |
add(comps[i]); |
312 |
} |
313 |
for (int i = visibleButtons; i < comps.length; i++) { |
314 |
overflowToolbar.add(comps[i]); |
315 |
} |
316 |
popup.add(overflowToolbar); |
317 |
add(overflowButton); |
318 |
} |
319 |
} |
320 |
|
321 |
private void handleOverflowRemoval() { |
322 |
if (overflowToolbar.getComponents().length > 0) { |
323 |
remove(overflowButton); |
324 |
handleIconResize(); |
325 |
for (Component comp : overflowToolbar.getComponents()) { |
326 |
add(comp); |
327 |
} |
328 |
overflowToolbar.removeAll(); |
329 |
popup.removeAll(); |
330 |
} |
331 |
} |
332 |
|
333 |
private void handleIconResize() { |
334 |
for (Component comp : overflowToolbar.getComponents()) { |
335 |
boolean smallToolbarIcons = getClientProperty(PROP_PREF_ICON_SIZE) == null; |
336 |
if (smallToolbarIcons) { |
337 |
((JComponent) comp).putClientProperty(PROP_PREF_ICON_SIZE, null); |
338 |
} else { |
339 |
((JComponent) comp).putClientProperty(PROP_PREF_ICON_SIZE, Integer.valueOf(24)); |
340 |
} |
341 |
} |
342 |
} |
343 |
|
344 |
private Component[] getAllComponents() { |
345 |
Component[] toolbarComps; |
346 |
Component[] overflowComps = overflowToolbar.getComponents(); |
347 |
if (overflowComps.length == 0) { |
348 |
toolbarComps = getComponents(); |
349 |
} else { |
350 |
if (getComponentCount() > 0) { |
351 |
toolbarComps = new Component[getComponents().length - 1]; |
352 |
System.arraycopy(getComponents(), 0, toolbarComps, 0, toolbarComps.length); |
353 |
} else { |
354 |
toolbarComps = new Component[0]; |
355 |
} |
356 |
} |
357 |
Component[] comps = new Component[toolbarComps.length + overflowComps.length]; |
358 |
System.arraycopy(toolbarComps, 0, comps, 0, toolbarComps.length); |
359 |
System.arraycopy(overflowComps, 0, comps, toolbarComps.length, overflowComps.length); |
360 |
return comps; |
361 |
} |
362 |
} |