~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/layoutsupport/delegates/JToolBarSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.form.layoutsupport.delegates;
 
43
 
 
44
import java.awt.*;
 
45
import javax.swing.*;
 
46
 
 
47
import org.netbeans.modules.form.layoutsupport.*;
 
48
 
 
49
/**
 
50
 * Dedicated layout support class for JToolBar.
 
51
 *
 
52
 * @author Tomas Pavek
 
53
 */
 
54
 
 
55
public class JToolBarSupport extends AbstractLayoutSupport {
 
56
 
 
57
    /** Gets the supported layout manager class - JToolBar.
 
58
     * @return the class supported by this delegate
 
59
     */
 
60
    public Class getSupportedClass() {
 
61
        return JToolBar.class;
 
62
    }
 
63
 
 
64
    @Override
 
65
    public void addComponentsToContainer(Container container,
 
66
                                         Container containerDelegate,
 
67
                                         Component[] components,
 
68
                                         int index) {
 
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);
 
75
        }
 
76
        super.addComponentsToContainer(container, containerDelegate, components, index);
 
77
    }
 
78
 
 
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;
 
87
     *        not needed here
 
88
     * @param posInCont position of mouse in the container delegate
 
89
     * @param posInComp position of mouse in the dragged component;
 
90
     *        not needed here
 
91
     * @return index corresponding to the position of the component in the
 
92
     *         container
 
93
     */
 
94
    @Override
 
95
    public int getNewIndex(Container container,
 
96
                           Container containerDelegate,
 
97
                           Component component,
 
98
                           int index,
 
99
                           Point posInCont,
 
100
                           Point posInComp)
 
101
    {
 
102
        if (!(container instanceof JToolBar))
 
103
            return -1;
 
104
 
 
105
        int orientation = ((JToolBar)container).getOrientation();
 
106
        
 
107
        assistantParams = 0;
 
108
        Component[] components = container.getComponents();
 
109
        for (int i = 0; i < components.length; i++) {
 
110
            if (component == components[i]) {
 
111
                assistantParams--;
 
112
                continue;
 
113
            }
 
114
            Rectangle b = components[i].getBounds();
 
115
            if (orientation == SwingConstants.HORIZONTAL) {
 
116
                if (posInCont.x < b.x + b.width / 2) {
 
117
                    assistantParams += i;
 
118
                    return i;
 
119
                }
 
120
            }
 
121
            else {
 
122
                if (posInCont.y < b.y + b.height / 2) {   
 
123
                    assistantParams += i;
 
124
                    return i;
 
125
                }
 
126
            }
 
127
        }
 
128
 
 
129
        assistantParams += components.length;
 
130
        return components.length;
 
131
    }
 
132
 
 
133
    private int assistantParams;
 
134
    @Override
 
135
    public String getAssistantContext() {
 
136
        return "toolbarLayout"; // NOI18N
 
137
    }
 
138
 
 
139
    @Override
 
140
    public Object[] getAssistantParams() {
 
141
        return new Object[] {Integer.valueOf(assistantParams+1)};
 
142
    }
 
143
 
 
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
 
151
     *        the container)
 
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)
 
158
     */
 
159
    @Override
 
160
    public boolean paintDragFeedback(Container container, 
 
161
                                     Container containerDelegate,
 
162
                                     Component component,
 
163
                                     LayoutConstraints newConstraints,
 
164
                                     int newIndex,
 
165
                                     Graphics g)
 
166
    {
 
167
        if (!(container instanceof JToolBar))
 
168
            return false;
 
169
 
 
170
        int orientation = ((JToolBar)container).getOrientation();
 
171
        Component[] components = container.getComponents();
 
172
        Rectangle rect;
 
173
 
 
174
        if ((newIndex >= 0) && (newIndex < components.length) && (component == components[newIndex])) {
 
175
            newIndex++;
 
176
        }
 
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,
 
183
                                 30, 20) :
 
184
                   new Rectangle(ins.left + (container.getWidth() - ins.left
 
185
                                 - ins.right - 30) / 2,
 
186
                                 ins.top,
 
187
                                 30, 20);
 
188
        }
 
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);
 
195
        }
 
196
        else {
 
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);
 
201
        }
 
202
 
 
203
        g.drawRect(rect.x, rect.y, rect.width, rect.height);
 
204
 
 
205
        return true;
 
206
    }
 
207
}