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