KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.netbeans.modules.form.fakepeer.FakePeerSupport;
27
28 /**
29  * Dedicated layout support class for ScrollPane.
30  *
31  * @author Tomas Pavek
32  */

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

39     public Class JavaDoc getSupportedClass() {
40         return ScrollPane.class;
41     }
42
43     /** This method should calculate position (index) for a component dragged
44      * over a container (or just for mouse cursor being moved over container,
45      * without any component).
46      * @param container instance of a real container over/in which the
47      * component is dragged
48      * @param containerDelegate effective container delegate of the container
49      * @param component the real component being dragged; not needed here
50      * @param index position (index) of the component in its current container;
51      * not needed here
52      * @param posInCont position of mouse in the container delegate; not needed
53      * @param posInComp position of mouse in the dragged component; not needed
54      * @return index corresponding to the position of the component in the
55      * container; we just return 0 here - as the drag&drop does not
56      * have much sense in JScrollPane
57      */

58     public int getNewIndex(Container container,
59                            Container containerDelegate,
60                            Component component,
61                            int index,
62                            Point posInCont,
63                            Point posInComp)
64     {
65         if (container.getComponentCount() > 1) // [or containerDelegate??]
66
return -1;
67         return 0;
68     }
69
70     public String JavaDoc getAssistantContext() {
71         return "scrollPaneLayout"; // NOI18N
72
}
73
74     /** This method paints a dragging feedback for a component dragged over
75      * a container (or just for mouse cursor being moved over container,
76      * without any component).
77      * @param container instance of a real container over/in which the
78      * component is dragged
79      * @param containerDelegate effective container delegate of the container
80      * @param component the real component being dragged; not needed here
81      * @param newConstraints component layout constraints to be presented;
82      * not used for ScrollPane
83      * @param newIndex component's index position to be presented; not needed
84      * @param g Graphics object for painting (with color and line style set)
85      * @return whether any feedback was painted (true in this case)
86      */

87     public boolean paintDragFeedback(Container container,
88                                      Container containerDelegate,
89                                      Component component,
90                                      LayoutConstraints newConstraints,
91                                      int newIndex,
92                                      Graphics g)
93     {
94         Dimension sz = container.getSize();
95         Insets insets = container.getInsets();
96         sz.width -= insets.left + insets.right;
97         sz.height -= insets.top + insets.bottom;
98         
99         g.drawRect(0, 0, sz.width, sz.height);
100         return true;
101     }
102
103     /** Adds real components to given container (according to layout
104      * constraints stored for the components).
105      * @param container instance of a real container to be added to
106      * @param containerDelegate effective container delegate of the container
107      * @param components components to be added
108      * @param index position at which to add the components to container
109      */

110     public void addComponentsToContainer(Container container,
111                                          Container containerDelegate,
112                                          Component[] components,
113                                          int index)
114     {
115         if (components.length == 0)
116             return;
117
118         if (container instanceof ScrollPane) {
119             // [do not allow adding a component when some already added??]
120
ScrollPane scroll = (ScrollPane) container;
121             Component removedComp = null;
122             if (scroll.getComponentCount() > 0)
123                 removedComp = scroll.getComponent(0);
124             try {
125                 scroll.add(components[0]);
126             } catch (NullPointerException JavaDoc npex) {
127                 // Issue 36629 - ScrollPane attempts to place
128
// components with a lightweight peer into a Panel
129
// This collides with our fake peers and can result
130
// in a NPE on JDK1.5. The correct peer for this
131
// Panel is set below.
132
}
133             if (System.getProperty("java.version").startsWith("1.5") // NOI18N
134
&& (scroll.getPeer() != null)) {
135                 Component comp = scroll.getComponent(0);
136                 comp.removeNotify();
137                 ensureFakePeerAttached(comp);
138                 comp.addNotify();
139                 scroll.validate();
140             }
141             // hack for AWT components - we must attach the fake peer again
142
// to the component that was removed by adding new component
143
ensureFakePeerAttached(removedComp);
144         }
145     }
146
147     /** Removes a real component from a real container.
148      * @param container instance of a real container
149      * @param containerDelegate effective container delegate of the container
150      * @param component component to be removed
151      * @return whether it was possible to remove the component (some containers
152      * may not support removing individual components reasonably)
153      */

154     public boolean removeComponentFromContainer(Container container,
155                                                 Container containerDelegate,
156                                                 Component component)
157     {
158         return false; // cannot remove component from JSplitPane
159
}
160
161     static private void ensureFakePeerAttached(Component comp) {
162         FakePeerSupport.attachFakePeer(comp);
163         if (comp instanceof Container)
164             FakePeerSupport.attachFakePeerRecursively((Container)comp);
165     }
166 }
167
Popular Tags