KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Method JavaDoc;
25
26 import org.netbeans.modules.form.layoutsupport.*;
27 import org.netbeans.modules.form.codestructure.*;
28
29 /**
30  * Dedicated layout support class for JScrollPane.
31  *
32  * @author Tomas Pavek
33  */

34
35 public class JScrollPaneSupport extends AbstractLayoutSupport {
36
37     private static Method JavaDoc setViewportViewMethod;
38
39     /** Gets the supported layout manager class - JScrollPane.
40      * @return the class supported by this delegate
41      */

42     public Class JavaDoc getSupportedClass() {
43         return JScrollPane.class;
44     }
45
46     /** For dedicated supports: check whether given default container instance
47      * is empty. In case of JScrollPane it means to check for empty viewport.
48      * @param cont default instance of Container
49      * @return true if the container can be used as default (empty) instance
50      * with this layout support
51      */

52     public boolean checkEmptyContainer(Container cont) {
53         return cont instanceof JScrollPane
54                && ((JScrollPane)cont).getViewport().getView() == null;
55     }
56
57     /** This method should calculate position (index) for a component dragged
58      * over a container (or just for mouse cursor being moved over container,
59      * without any component).
60      * @param container instance of a real container over/in which the
61      * component is dragged
62      * @param containerDelegate effective container delegate of the container
63      * @param component the real component being dragged; not needed here
64      * @param index position (index) of the component in its current container;
65      * not needed here
66      * @param posInCont position of mouse in the container; not needed
67      * @param posInComp position of mouse in the dragged component; not needed
68      * @return index corresponding to the position of the component in the
69      * container; we just return 0 here - as the drag&drop does not
70      * have much sense in JScrollPane
71      */

72     public int getNewIndex(Container container,
73                            Container containerDelegate,
74                            Component component,
75                            int index,
76                            Point posInCont,
77                            Point posInComp)
78     {
79         assistantParams = (container instanceof JScrollPane
80             && ((JScrollPane)container).getViewport().getView() == null);
81         return assistantParams ? 0 : -1;
82     }
83
84     private boolean assistantParams;
85     public String JavaDoc getAssistantContext() {
86         return assistantParams ? "jscrollPaneLayout" : null; // NOI18N
87
}
88
89     /** This method paints a dragging feedback for a component dragged over
90      * a container (or just for mouse cursor being moved over container,
91      * without any component).
92      * @param container instance of a real container over/in which the
93      * component is dragged
94      * @param containerDelegate effective container delegate of the container
95      * @param component the real component being dragged; not needed here
96      * @param newConstraints component layout constraints to be presented;
97      * not used for JScrollPane
98      * @param newIndex component's index position to be presented; not needed
99      * @param g Graphics object for painting (with color and line style set)
100      * @return whether any feedback was painted (true in this case)
101      */

102     public boolean paintDragFeedback(Container container,
103                                      Container containerDelegate,
104                                      Component component,
105                                      LayoutConstraints newConstraints,
106                                      int newIndex,
107                                      Graphics g)
108     {
109         if (container instanceof JScrollPane
110             && ((JScrollPane)container).getViewport().getView() == null)
111         { // empty JScrollPane - it makes sense to add something to it
112
Dimension sz = container.getSize();
113             Insets insets = container.getInsets();
114             sz.width -= insets.left + insets.right;
115             sz.height -= insets.top + insets.bottom;
116
117             g.drawRect(0, 0, sz.width, sz.height);
118             return true;
119         }
120         return false;
121     }
122
123     /** Adds real components to given container (according to layout
124      * constraints stored for the components).
125      * @param container instance of a real container to be added to
126      * @param containerDelegate effective container delegate of the container
127      * @param components components to be added
128      * @param index position at which to add the components to container
129      */

130     public void addComponentsToContainer(Container container,
131                                          Container containerDelegate,
132                                          Component[] components,
133                                          int index)
134     {
135         if (components.length == 0)
136             return;
137
138         if (container instanceof JScrollPane)
139             ((JScrollPane)container).setViewportView(components[0]);
140     }
141
142     /** Removes a real component from a real container.
143      * @param container instance of a real container
144      * @param containerDelegate effective container delegate of the container
145      * @param component component to be removed
146      * @return whether it was possible to remove the component (some containers
147      * may not support removing individual components reasonably)
148      */

149     public boolean removeComponentFromContainer(Container container,
150                                                 Container containerDelegate,
151                                                 Component component)
152     {
153         return false; // cannot remove component from JScrollPane
154
}
155
156     /** Removes all components from given real container.
157      * @param container instance of a real container to be cleared
158      * @param containerDelegate effective container delegate of the container
159      * @return whether it was possible to clear the container (some containers
160      * may not support this)
161      */

162     public boolean clearContainer(Container container,
163                                   Container containerDelegate)
164     {
165         if (container instanceof JScrollPane) {
166             JScrollPane scrollPane = (JScrollPane) container;
167             Component comp = scrollPane.getViewport().getView();
168             if (comp != null) {
169                 comp.removeNotify();
170                 comp.setBounds(0, 0, 0, 0);
171             }
172             scrollPane.setViewportView(null);
173             return true;
174         }
175         else return super.clearContainer(container, containerDelegate);
176     }
177
178     // ------------
179

180     /** This methods returns the code expression to be used for container on
181      * which the layout is set and to which components are added. This can be
182      * either container, or container delegate expression. In fact, it is
183      * container delegate in most cases, but not in case of JScrollPane which
184      * has its viewport as the container delegate, but we work with the
185      * JScrollPane (whole container).
186      * @return code expression representing the effective container
187      */

188     protected CodeExpression getActiveContainerCodeExpression() {
189         return getLayoutContext().getContainerCodeExpression();
190     }
191
192     /** This method is used for scanning code structures and recognizing
193      * components added to containers and their constraints. It's called from
194      * initialize method. When a relevant code statement is found, then the
195      * CodeExpression of component is get and added to component, and also the
196      * layout constraints information is read (using separate
197      * readConstraintsCode method).
198      * @param statement CodeStatement to be tested if it contains relevant code
199      * @param componentCode CodeGroup to be filled with all component code
200      * @return CodeExpression representing found component; null if the
201      * statement is not relevant
202      */

203     protected CodeExpression readComponentCode(CodeStatement statement,
204                                                CodeGroup componentCode)
205     {
206         if (getSetViewportViewMethod().equals(statement.getMetaObject())
207             || getSimpleAddMethod().equals(statement.getMetaObject()))
208         {
209             componentCode.addStatement(statement);
210             getConstraintsList().add(null); // no constraints
211
return statement.getStatementParameters()[0];
212         }
213
214         return null;
215     }
216
217     /** Creates code for a component added to the layout (opposite to
218      * readComponentCode method).
219      * @param componentCode CodeGroup to be filled with complete component code
220      * (code for initializing the layout constraints and adding the
221      * component to the layout)
222      * @param componentExpression CodeExpression object representing component
223      * @param index position of the component in the layout
224      */

225     protected void createComponentCode(CodeGroup componentCode,
226                                        CodeExpression componentExpression,
227                                        int index)
228     {
229         CodeStatement addStatement = CodeStructure.createStatement(
230                          getLayoutContext().getContainerCodeExpression(),
231                          getSetViewportViewMethod(),
232                          new CodeExpression[] { componentExpression });
233         componentCode.addStatement(addStatement);
234     }
235
236     private static Method JavaDoc getSetViewportViewMethod() {
237         if (setViewportViewMethod == null) {
238             try {
239                 setViewportViewMethod = JScrollPane.class.getMethod(
240                                             "setViewportView", // NOI18N
241
new Class JavaDoc[] { Component.class });
242             }
243             catch (NoSuchMethodException JavaDoc ex) { // should not happen
244
ex.printStackTrace();
245             }
246         }
247         return setViewportViewMethod;
248     }
249 }
250
Popular Tags