2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.form.layoutsupport.delegates;
47
import org.netbeans.modules.form.layoutsupport.*;
50
* Dedicated layout support class for JToolBar.
55
public class JToolBarSupport extends AbstractLayoutSupport {
57
/** Gets the supported layout manager class - JToolBar.
58
* @return the class supported by this delegate
60
public Class getSupportedClass() {
61
return JToolBar.class;
65
public void addComponentsToContainer(Container container,
66
Container containerDelegate,
67
Component[] components,
69
// Issue 63955 and JDK bug 4294758
70
LayoutManager lm = containerDelegate.getLayout();
71
// Cannot use instanceof BoxLayout because JToolBar
72
// uses DefaultToolBarLayout wrapper around BoxLayout
73
if (lm instanceof LayoutManager2) {
74
((LayoutManager2)lm).invalidateLayout(containerDelegate);
76
super.addComponentsToContainer(container, containerDelegate, components, index);
79
/** This method calculates position (index) for a component dragged
80
* over a container (or just for mouse cursor being moved over container,
81
* without any component).
82
* @param container instance of a real container over/in which the
83
* component is dragged
84
* @param containerDelegate effective container delegate of the container
85
* @param component the real component being dragged; not needed here
86
* @param index position (index) of the component in its current container;
88
* @param posInCont position of mouse in the container delegate
89
* @param posInComp position of mouse in the dragged component;
91
* @return index corresponding to the position of the component in the
95
public int getNewIndex(Container container,
96
Container containerDelegate,
102
if (!(container instanceof JToolBar))
105
int orientation = ((JToolBar)container).getOrientation();
108
Component[] components = container.getComponents();
109
for (int i = 0; i < components.length; i++) {
110
if (component == components[i]) {
114
Rectangle b = components[i].getBounds();
115
if (orientation == SwingConstants.HORIZONTAL) {
116
if (posInCont.x < b.x + b.width / 2) {
117
assistantParams += i;
122
if (posInCont.y < b.y + b.height / 2) {
123
assistantParams += i;
129
assistantParams += components.length;
130
return components.length;
133
private int assistantParams;
135
public String getAssistantContext() {
136
return "toolbarLayout"; // NOI18N
140
public Object[] getAssistantParams() {
141
return new Object[] {Integer.valueOf(assistantParams+1)};
144
/** This method paints a dragging feedback for a component dragged over
145
* a container (or just for mouse cursor being moved over container,
146
* without any component).
147
* @param container instance of a real container over/in which the
148
* component is dragged
149
* @param containerDelegate effective container delegate of the container
150
* (for layout managers we always use container delegate instead of
152
* @param component the real component being dragged, not needed here
153
* @param newConstraints component layout constraints to be presented;
154
* not used for JToolBar
155
* @param newIndex component's index position to be presented
156
* @param g Graphics object for painting (with color and line style set)
157
* @return whether any feedback was painted (true in this case)
160
public boolean paintDragFeedback(Container container,
161
Container containerDelegate,
163
LayoutConstraints newConstraints,
167
if (!(container instanceof JToolBar))
170
int orientation = ((JToolBar)container).getOrientation();
171
Component[] components = container.getComponents();
174
if ((newIndex >= 0) && (newIndex < components.length) && (component == components[newIndex])) {
177
if ((components.length == 0) || ((components.length == 1) && (components[0] == component))) {
178
Insets ins = container.getInsets();
179
rect = orientation == SwingConstants.HORIZONTAL ?
180
new Rectangle(ins.left,
181
ins.top + (container.getHeight() - ins.top
182
- ins.bottom - 20) / 2,
184
new Rectangle(ins.left + (container.getWidth() - ins.left
185
- ins.right - 30) / 2,
189
else if (newIndex < 0 || newIndex >= components.length) {
190
int index = (components[components.length-1] == component) ? components.length-2 : components.length-1;
191
Rectangle b = components[index].getBounds();
192
rect = orientation == SwingConstants.HORIZONTAL ?
193
new Rectangle(b.x + b.width - 10, b.y, 20, b.height) :
194
new Rectangle(b.x, b.y + b.height - 10, b.width, 20);
197
Rectangle b = components[newIndex].getBounds();
198
rect = orientation == SwingConstants.HORIZONTAL ?
199
new Rectangle(b.x - 10, b.y, 20, b.height) :
200
new Rectangle(b.x, b.y - 10, b.width, 20);
203
g.drawRect(rect.x, rect.y, rect.width, rect.height);