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.Component; |
45 |
import java.awt.Dimension; |
46 |
import java.awt.Insets; |
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 javax.swing.BorderFactory; |
52 |
import javax.swing.JButton; |
53 |
import javax.swing.JComponent; |
54 |
import javax.swing.JPopupMenu; |
55 |
import javax.swing.JToolBar; |
56 |
import org.openide.util.ImageUtilities; |
57 |
|
58 |
/** |
59 |
* ToolbarWithOverflow provides a component which is useful for displaying commonly used |
60 |
* actions. It adds an overflow button when the toolbar becomes too small to show all the |
61 |
* available actions. |
62 |
* |
63 |
* @author Th. Oikonomou |
64 |
* @since 7.50 |
65 |
*/ |
66 |
public class ToolbarWithOverflow extends JToolBar { |
67 |
|
68 |
private JButton overflowButton; |
69 |
private JPopupMenu popup; |
70 |
private JToolBar overflowToolbar; |
71 |
private int visibleButtons; |
72 |
private final String toolbarArrowHorizontal = "org/openide/awt/resources/toolbar_arrow_horizontal.png"; //NOI18N |
73 |
private final String toolbarArrowVertical = "org/openide/awt/resources/toolbar_arrow_vertical.png"; //NOI18N |
74 |
private final String PROP_PREF_ICON_SIZE = "PreferredIconSize"; //NOI18N |
75 |
private final String PROP_DRAGGER = "_toolbar_dragger_"; //NOI18N |
76 |
|
77 |
/** |
78 |
* Creates a new tool bar; orientation defaults to |
79 |
* <code>HORIZONTAL</code>. |
80 |
*/ |
81 |
public ToolbarWithOverflow() { |
82 |
this(HORIZONTAL); |
83 |
} |
84 |
|
85 |
/** |
86 |
* Creates a new tool bar with the specified |
87 |
* <code>orientation</code>. The |
88 |
* <code>orientation</code> must be either |
89 |
* <code>HORIZONTAL</code> or |
90 |
* <code>VERTICAL</code>. |
91 |
* |
92 |
* @param orientation the orientation desired |
93 |
*/ |
94 |
public ToolbarWithOverflow(int orientation) { |
95 |
this(null, orientation); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Creates a new tool bar with the specified |
100 |
* <code>name</code>. The name is used as the title of the undocked tool |
101 |
* bar. The default orientation is |
102 |
* <code>HORIZONTAL</code>. |
103 |
* |
104 |
* @param name the name of the tool bar |
105 |
*/ |
106 |
public ToolbarWithOverflow(String name) { |
107 |
this(name, HORIZONTAL); |
108 |
} |
109 |
|
110 |
/** |
111 |
* Creates a new tool bar with a specified |
112 |
* <code>name</code> and |
113 |
* <code>orientation</code>. All other constructors call this constructor. |
114 |
* If |
115 |
* <code>orientation</code> is an invalid value, an exception will be |
116 |
* thrown. |
117 |
* |
118 |
* @param name the name of the tool bar |
119 |
* @param orientation the initial orientation -- it must be * either <code>HORIZONTAL</code> or <code>VERTICAL</code> |
120 |
* @exception IllegalArgumentException if orientation is neither |
121 |
* <code>HORIZONTAL</code> nor <code>VERTICAL</code> |
122 |
*/ |
123 |
public ToolbarWithOverflow(String name, int orientation) { |
124 |
super(name, orientation); |
125 |
setupOverflowButton(); |
126 |
popup = new JPopupMenu(); |
127 |
popup.setBorderPainted(false); |
128 |
popup.setBorder(BorderFactory.createEmptyBorder()); |
129 |
overflowToolbar = new JToolBar("overflowToolbar", orientation == HORIZONTAL ? VERTICAL : HORIZONTAL); |
130 |
overflowToolbar.setFloatable(false); |
131 |
overflowToolbar.setBorderPainted(false); |
132 |
overflowToolbar.setBorder(BorderFactory.createEmptyBorder()); |
133 |
addComponentListener(new ComponentAdapter() { |
134 |
@Override |
135 |
public void componentResized(ComponentEvent e) { |
136 |
maybeAddOverflow(); |
137 |
} |
138 |
}); |
139 |
} |
140 |
|
141 |
@Override |
142 |
public final Dimension getPreferredSize() { |
143 |
Component[] comps = getAllComponents(); |
144 |
int width = 0; |
145 |
int height = 0; |
146 |
for (int i = 0; i < comps.length; i++) { |
147 |
Component comp = comps[i]; |
148 |
width += getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; |
149 |
height = getOrientation() == HORIZONTAL ? comp.getPreferredSize().height : comp.getPreferredSize().width; |
150 |
} |
151 |
if(overflowToolbar.getComponentCount() > 0) { |
152 |
width += getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; |
153 |
} |
154 |
Dimension dim = getOrientation() == HORIZONTAL ? new Dimension(width, height) : new Dimension(height, width); |
155 |
return dim; |
156 |
} |
157 |
|
158 |
@Override |
159 |
public void setOrientation(int o) { |
160 |
super.setOrientation(o); |
161 |
setupOverflowButton(); |
162 |
} |
163 |
|
164 |
@Override |
165 |
public void removeAll() { |
166 |
super.removeAll(); |
167 |
overflowToolbar.removeAll(); |
168 |
} |
169 |
|
170 |
@Override |
171 |
public void validate() { |
172 |
computeVisibleButtons(); |
173 |
if (visibleButtons == -1) { |
174 |
handleOverflowRemoval(); |
175 |
} else { |
176 |
handleOverflowAddittion(); |
177 |
} |
178 |
super.validate(); |
179 |
} |
180 |
|
181 |
private void setupOverflowButton() { |
182 |
overflowButton = new JButton(ImageUtilities.loadImageIcon(getOrientation() == HORIZONTAL ? toolbarArrowVertical : toolbarArrowHorizontal, false)); |
183 |
overflowButton.addMouseListener(new MouseAdapter() { |
184 |
@Override |
185 |
public void mouseClicked(MouseEvent e) { |
186 |
int x = getOrientation() == HORIZONTAL ? 0 : overflowButton.getWidth(); |
187 |
int y = getOrientation() == HORIZONTAL ? overflowButton.getHeight() : 0; |
188 |
popup.show(overflowButton, x, y); |
189 |
} |
190 |
}); |
191 |
} |
192 |
|
193 |
/** |
194 |
* Determines if an overflow button should be added to or removed from the toolbar. |
195 |
*/ |
196 |
private void maybeAddOverflow() { |
197 |
validate(); |
198 |
repaint(); |
199 |
} |
200 |
|
201 |
private void computeVisibleButtons() { |
202 |
if (isShowing()) { |
203 |
int w = getOrientation() == HORIZONTAL ? overflowButton.getIcon().getIconWidth() + 4 : getWidth() - getInsets().left - getInsets().right; |
204 |
int h = getOrientation() == HORIZONTAL ? getHeight() - getInsets().top - getInsets().bottom : overflowButton.getIcon().getIconHeight() + 4; |
205 |
overflowButton.setMaximumSize(new Dimension(w, h)); |
206 |
overflowButton.setMinimumSize(new Dimension(w, h)); |
207 |
overflowButton.setPreferredSize(new Dimension(w, h)); |
208 |
} |
209 |
handleIconResize(); |
210 |
Component[] comps = getAllComponents(); |
211 |
int sizeSoFar = 0; |
212 |
int maxSize = getOrientation() == HORIZONTAL ? getWidth() : getHeight(); |
213 |
int overflowButtonSize = getOrientation() == HORIZONTAL ? overflowButton.getPreferredSize().width : overflowButton.getPreferredSize().height; |
214 |
for (int i = 0; i < comps.length; i++) { |
215 |
Component comp = comps[i]; |
216 |
sizeSoFar += getOrientation() == HORIZONTAL ? comp.getPreferredSize().width : comp.getPreferredSize().height; |
217 |
if (sizeSoFar > maxSize) { |
218 |
visibleButtons = i; |
219 |
break; |
220 |
} else { |
221 |
if (sizeSoFar + overflowButtonSize > maxSize && i + 1 != comps.length) { |
222 |
visibleButtons = i; |
223 |
break; |
224 |
} else { |
225 |
visibleButtons = i + 1; |
226 |
} |
227 |
} |
228 |
} |
229 |
if(visibleButtons == 0 && comps.length > 0) { |
230 |
if (comps[0] instanceof JComponent) { |
231 |
if (Boolean.TRUE.equals(((JComponent) comps[0]).getClientProperty(PROP_DRAGGER))) { |
232 |
visibleButtons = 1; |
233 |
} |
234 |
} |
235 |
} |
236 |
if (visibleButtons == comps.length) { |
237 |
visibleButtons = -1; |
238 |
} |
239 |
} |
240 |
|
241 |
private void handleOverflowAddittion() { |
242 |
Component[] comps = getAllComponents(); |
243 |
if (visibleButtons <= comps.length) { |
244 |
removeAll(); |
245 |
overflowToolbar.setOrientation(getOrientation() == HORIZONTAL ? VERTICAL : HORIZONTAL); |
246 |
popup.removeAll(); |
247 |
|
248 |
for (int i = 0; i < visibleButtons; i++) { |
249 |
add(comps[i]); |
250 |
} |
251 |
for (int i = visibleButtons; i < comps.length; i++) { |
252 |
overflowToolbar.add(comps[i]); |
253 |
} |
254 |
popup.add(overflowToolbar); |
255 |
add(overflowButton); |
256 |
} |
257 |
} |
258 |
|
259 |
private void handleOverflowRemoval() { |
260 |
if (overflowToolbar.getComponents().length > 0) { |
261 |
remove(overflowButton); |
262 |
handleIconResize(); |
263 |
for (Component comp : overflowToolbar.getComponents()) { |
264 |
add(comp); |
265 |
} |
266 |
overflowToolbar.removeAll(); |
267 |
popup.removeAll(); |
268 |
} |
269 |
} |
270 |
|
271 |
private void handleIconResize() { |
272 |
for (Component comp : overflowToolbar.getComponents()) { |
273 |
boolean smallToolbarIcons = getClientProperty(PROP_PREF_ICON_SIZE) == null; |
274 |
if (smallToolbarIcons) { |
275 |
((JComponent) comp).putClientProperty(PROP_PREF_ICON_SIZE, null); |
276 |
} else { |
277 |
((JComponent) comp).putClientProperty(PROP_PREF_ICON_SIZE, Integer.valueOf(24)); |
278 |
} |
279 |
} |
280 |
} |
281 |
|
282 |
private Component[] getAllComponents() { |
283 |
Component[] toolbarComps; |
284 |
Component[] overflowComps = overflowToolbar.getComponents(); |
285 |
if (overflowComps.length == 0) { |
286 |
toolbarComps = getComponents(); |
287 |
} else { |
288 |
if (getComponentCount() > 0) { |
289 |
toolbarComps = new Component[getComponents().length - 1]; |
290 |
System.arraycopy(getComponents(), 0, toolbarComps, 0, toolbarComps.length); |
291 |
} else { |
292 |
toolbarComps = new Component[0]; |
293 |
} |
294 |
} |
295 |
Component[] comps = new Component[toolbarComps.length + overflowComps.length]; |
296 |
System.arraycopy(toolbarComps, 0, comps, 0, toolbarComps.length); |
297 |
System.arraycopy(overflowComps, 0, comps, toolbarComps.length, overflowComps.length); |
298 |
return comps; |
299 |
} |
300 |
} |