KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.beans.*;
24 import java.util.*;
25
26 import org.openide.nodes.Node;
27
28 import org.netbeans.modules.form.layoutsupport.*;
29 import org.netbeans.modules.form.codestructure.*;
30 import org.netbeans.modules.form.FormProperty;
31
32 /**
33  * Support class for CardLayout. This support uses fictive layout constraints
34  * for holding the names of the cards. It also implements the "arranging
35  * features" - for the user to be able to choose the card in the form designer.
36  *
37  * @author Tran Duc Trung, Tomas Pavek
38  */

39
40 public class CardLayoutSupport extends AbstractLayoutSupport {
41
42     private CardConstraints currentCard;
43
44     /** Gets the supported layout manager class - CardLayout.
45      * @return the class supported by this delegate
46      */

47     public Class JavaDoc getSupportedClass() {
48         return CardLayout.class;
49     }
50
51     /** Adds new components to the layout. This is done just at the metadata
52      * level, no real components but their CodeExpression representations
53      * are added.
54      * @param compExpressions array of CodeExpression objects representing the
55      * components to be accepted
56      * @param constraints array of layout constraints of the components
57      * @param index position at which the components should be added (inserted);
58      * if -1, the components should be added at the end
59      */

60     public void addComponents(CodeExpression[] newCompExpressions,
61                               LayoutConstraints[] newConstraints,
62                               int index)
63     {
64         // same functionality as in AbstractLayoutSupport...
65
super.addComponents(newCompExpressions, newConstraints, index);
66
67         // ...just set the last added component as the active card
68
if (index < 0)
69             index = getComponentCount() - 1;
70         else
71             index += newCompExpressions.length - 1;
72
73         if (currentCard == null && index >= 0 && index < getComponentCount())
74             currentCard = (CardConstraints) getConstraints(index);
75     }
76
77     /** This method is called when a component is selected in Component
78      * Inspector.
79      * @param index position (index) of the selected component in container
80      */

81     public void selectComponent(int index) {
82         // set the active card according to index
83
LayoutConstraints constraints = getConstraints(index);
84         if (constraints instanceof CardConstraints)
85             currentCard = (CardConstraints) constraints;
86     }
87
88     /** In this method, the layout delegate has a chance to "arrange" real
89      * container instance additionally - some other way that cannot be
90      * done through layout properties and added components.
91      * @param container instance of a real container to be arranged
92      * @param containerDelegate effective container delegate of the container
93      * (for layout managers we always use container delegate instead of
94      * the container)
95      */

96     public void arrangeContainer(Container container,
97                                  Container containerDelegate)
98     {
99         LayoutManager lm = containerDelegate.getLayout();
100         if (!(lm instanceof CardLayout) || currentCard == null)
101             return;
102
103         // select the active card in real CardLayout
104
((CardLayout)lm).show(containerDelegate,
105                               (String JavaDoc)currentCard.getConstraintsObject());
106     }
107
108     /** This method should calculate position (index) for a component dragged
109      * over a container (or just for mouse cursor being moved over container,
110      * without any component).
111      * @param container instance of a real container over/in which the
112      * component is dragged
113      * @param containerDelegate effective container delegate of the container
114      * (for layout managers we always use container delegate instead of
115      * the container)
116      * @param component the real component being dragged; not needed here
117      * @param index position (index) of the component in its current container;
118      * not needed here
119      * @param posInCont position of mouse in the container delegate; not needed
120      * @param posInComp position of mouse in the dragged component; not needed
121      * @return index corresponding to the position of the component in the
122      * container; we just return the number of components here - as the
123      * drag&drop does not have much sense for CardLayout
124      */

125     public int getNewIndex(Container container,
126                            Container containerDelegate,
127                            Component component,
128                            int index,
129                            Point posInCont,
130                            Point posInComp)
131     {
132         if (!(containerDelegate.getLayout() instanceof CardLayout))
133             return -1;
134         return containerDelegate.getComponentCount();
135     }
136
137     public String JavaDoc getAssistantContext() {
138         return "cardLayout"; // NOI18N
139
}
140
141     /** This method paints a dragging feedback for a component dragged over
142      * a container (or just for mouse cursor being moved over container,
143      * without any component).
144      * @param container instance of a real container over/in which the
145      * component is dragged
146      * @param containerDelegate effective container delegate of the container
147      * (for layout managers we always use container delegate instead of
148      * the container)
149      * @param component the real component being dragged; not needed here
150      * @param newConstraints component layout constraints to be presented;
151      * not used for CardLayout
152      * @param newIndex component's index position to be presented; not needed
153      * @param g Graphics object for painting (with color and line style set)
154      * @return whether any feedback was painted (true in this case)
155      */

156     public boolean paintDragFeedback(Container container,
157                                      Container containerDelegate,
158                                      Component component,
159                                      LayoutConstraints newConstraints,
160                                      int newIndex,
161                                      Graphics g)
162     {
163         if (!(containerDelegate.getLayout() instanceof CardLayout))
164             return false;
165
166         Dimension sz = containerDelegate.getSize();
167         Insets insets = containerDelegate.getInsets();
168         sz.width -= insets.left + insets.right;
169         sz.height -= insets.top + insets.bottom;
170         
171         g.drawRect(0, 0, sz.width, sz.height);
172         return true;
173     }
174
175     // ---------
176

177     /** This method is called from readComponentCode method to read layout
178      * constraints of a component from code. It is just a simple String for
179      * CardLayout (the name of the card).
180      * @param constrExp CodeExpression object of the constraints (taken from
181      * add method in the code)
182      * @param constrCode CodeGroup to be filled with the relevant constraints
183      * initialization code; not needed here because String is just
184      * a single code expression
185      * @param compExp CodeExpression of the component for which the constraints
186      * are read (not needed here)
187      * @return LayoutConstraints based on information read form code
188      */

189     protected LayoutConstraints readConstraintsCode(CodeExpression constrExp,
190                                                     CodeGroup constrCode,
191                                                     CodeExpression compExp)
192     {
193         CardConstraints constr = new CardConstraints("card"); // NOI18N
194
FormCodeSupport.readPropertyExpression(constrExp,
195                                                constr.getProperties()[0],
196                                                false);
197         return constr;
198     }
199
200     /** Called from createComponentCode method, creates code for a component
201      * layout constraints (opposite to readConstraintsCode).
202      * @param constrCode CodeGroup to be filled with constraints code; not
203      * needed here String (used as the constraints object) is just
204      * a single code expression
205      * @param constr layout constraints metaobject representing the constraints
206      * @param compExp CodeExpression object representing the component; not
207      * needed here
208      * @return created CodeExpression representing the layout constraints
209      */

210     protected CodeExpression createConstraintsCode(CodeGroup constrCode,
211                                                    LayoutConstraints constr,
212                                                    CodeExpression compExp,
213                                                    int index)
214     {
215         if (!(constr instanceof CardConstraints))
216             return null; // should not happen
217

218         return getCodeStructure().createExpression(
219                    FormCodeSupport.createOrigin(constr.getProperties()[0]));
220     }
221
222     /** This method is called to get a default component layout constraints
223      * metaobject in case it is not provided (e.g. in addComponents method).
224      * @return the default LayoutConstraints object for the supported layout
225      */

226     protected LayoutConstraints createDefaultConstraints() {
227         return new CardConstraints("card"+(getComponentCount()+1)); // NOI18N
228
}
229
230     // ----------------
231

232     /** LayoutConstraints implementation holding name of a card in CardLayout.
233      */

234     public static class CardConstraints implements LayoutConstraints {
235         private String JavaDoc card;
236
237         private Node.Property[] properties;
238
239         public CardConstraints(String JavaDoc card) {
240             this.card = card;
241         }
242
243         public Node.Property[] getProperties() {
244             if (properties == null) {
245                 properties = new Node.Property[] {
246                     new FormProperty("CardConstraints cardName", // NOI18N
247
String JavaDoc.class,
248                                  getBundle().getString("PROP_cardName"), // NOI18N
249
getBundle().getString("HINT_cardName")) { // NOI18N
250

251                         public Object JavaDoc getTargetValue() {
252                             return card;
253                         }
254
255                         public void setTargetValue(Object JavaDoc value) {
256                             card = (String JavaDoc)value;
257                         }
258                         public void setPropertyContext(
259                             org.netbeans.modules.form.FormPropertyContext ctx)
260                         { // disabling this method due to limited persistence
261
} // capabilities (compatibility with previous versions)
262
}
263                 };
264                 properties[0].setValue("NOI18N", Boolean.TRUE); // NOI18N
265
}
266
267             return properties;
268         }
269
270         public Object JavaDoc getConstraintsObject() {
271             return card;
272         }
273
274         public LayoutConstraints cloneConstraints() {
275             return new CardConstraints(card);
276         }
277     }
278 }
279
Popular Tags