KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutsupport > delegates > JToolBarSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.layoutsupport.delegates;
21
22 import java.awt.*;
23 import javax.swing.*;
24
25 import org.netbeans.modules.form.layoutsupport.*;
26
27 /**
28  * Dedicated layout support class for JToolBar.
29  *
30  * @author Tomas Pavek
31  */

32
33 public class JToolBarSupport extends AbstractLayoutSupport {
34
35     /** Gets the supported layout manager class - JToolBar.
36      * @return the class supported by this delegate
37      */

38     public Class JavaDoc getSupportedClass() {
39         return JToolBar.class;
40     }
41
42     public void addComponentsToContainer(Container container,
43                                          Container containerDelegate,
44                                          Component[] components,
45                                          int index) {
46         // Issue 63955 and JDK bug 4294758
47
LayoutManager lm = containerDelegate.getLayout();
48         // Cannot use instanceof BoxLayout because JToolBar
49
// uses DefaultToolBarLayout wrapper around BoxLayout
50
if (lm instanceof LayoutManager2) {
51             ((LayoutManager2)lm).invalidateLayout(containerDelegate);
52         }
53         super.addComponentsToContainer(container, containerDelegate, components, index);
54     }
55
56     /** This method calculates position (index) for a component dragged
57      * over a container (or just for mouse cursor being moved over container,
58      * without any component).
59      * @param container instance of a real container over/in which the
60      * component is dragged
61      * @param containerDelegate effective container delegate of the container
62      * @param component the real component being dragged; not needed here
63      * @param index position (index) of the component in its current container;
64      * not needed here
65      * @param posInCont position of mouse in the container delegate
66      * @param posInComp position of mouse in the dragged component;
67      * not needed here
68      * @return index corresponding to the position of the component in the
69      * container
70      */

71     public int getNewIndex(Container container,
72                            Container containerDelegate,
73                            Component component,
74                            int index,
75                            Point posInCont,
76                            Point posInComp)
77     {
78         if (!(container instanceof JToolBar))
79             return -1;
80
81         int orientation = ((JToolBar)container).getOrientation();
82         
83         assistantParams = 0;
84         Component[] components = container.getComponents();
85         for (int i = 0; i < components.length; i++) {
86             if (component == components[i]) {
87                 assistantParams--;
88                 continue;
89             }
90             Rectangle b = components[i].getBounds();
91             if (orientation == SwingConstants.HORIZONTAL) {
92                 if (posInCont.x < b.x + b.width / 2) {
93                     assistantParams += i;
94                     return i;
95                 }
96             }
97             else {
98                 if (posInCont.y < b.y + b.height / 2) {
99                     assistantParams += i;
100                     return i;
101                 }
102             }
103         }
104
105         assistantParams += components.length;
106         return components.length;
107     }
108
109     private int assistantParams;
110     public String JavaDoc getAssistantContext() {
111         return "toolbarLayout"; // NOI18N
112
}
113
114     public Object JavaDoc[] getAssistantParams() {
115         return new Object JavaDoc[] {Integer.valueOf(assistantParams+1)};
116     }
117
118     /** This method paints a dragging feedback for a component dragged over
119      * a container (or just for mouse cursor being moved over container,
120      * without any component).
121      * @param container instance of a real container over/in which the
122      * component is dragged
123      * @param containerDelegate effective container delegate of the container
124      * (for layout managers we always use container delegate instead of
125      * the container)
126      * @param component the real component being dragged, not needed here
127      * @param newConstraints component layout constraints to be presented;
128      * not used for JToolBar
129      * @param newIndex component's index position to be presented
130      * @param g Graphics object for painting (with color and line style set)
131      * @return whether any feedback was painted (true in this case)
132      */

133     public boolean paintDragFeedback(Container container,
134                                      Container containerDelegate,
135                                      Component component,
136                                      LayoutConstraints newConstraints,
137                                      int newIndex,
138                                      Graphics g)
139     {
140         if (!(container instanceof JToolBar))
141             return false;
142
143         int orientation = ((JToolBar)container).getOrientation();
144         Component[] components = container.getComponents();
145         Rectangle rect;
146
147         if ((newIndex >= 0) && (newIndex < components.length) && (component == components[newIndex])) {
148             newIndex++;
149         }
150         if ((components.length == 0) || ((components.length == 1) && (components[0] == component))) {
151             Insets ins = container.getInsets();
152             rect = orientation == SwingConstants.HORIZONTAL ?
153                    new Rectangle(ins.left,
154                                  ins.top + (container.getHeight() - ins.top
155                                             - ins.bottom - 20) / 2,
156                                  30, 20) :
157                    new Rectangle(ins.left + (container.getWidth() - ins.left
158                                  - ins.right - 30) / 2,
159                                  ins.top,
160                                  30, 20);
161         }
162         else if (newIndex < 0 || newIndex >= components.length) {
163             int index = (components[components.length-1] == component) ? components.length-2 : components.length-1;
164             Rectangle b = components[index].getBounds();
165             rect = orientation == SwingConstants.HORIZONTAL ?
166                    new Rectangle(b.x + b.width - 10, b.y, 20, b.height) :
167                    new Rectangle(b.x, b.y + b.height - 10, b.width, 20);
168         }
169         else {
170             Rectangle b = components[newIndex].getBounds();
171             rect = orientation == SwingConstants.HORIZONTAL ?
172                    new Rectangle(b.x - 10, b.y, 20, b.height) :
173                    new Rectangle(b.x, b.y - 10, b.width, 20);
174         }
175
176         g.drawRect(rect.x, rect.y, rect.width, rect.height);
177
178         return true;
179     }
180 }
181
Popular Tags