KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.swing.*;
26 import java.lang.reflect.Method JavaDoc;
27
28 import org.openide.nodes.Node;
29
30 import org.netbeans.modules.form.layoutsupport.*;
31 import org.netbeans.modules.form.codestructure.*;
32 import org.netbeans.modules.form.*;
33
34 /**
35  * Dedicated layout support class for JTabbedPane.
36  *
37  * @author Tomas Pavek
38  */

39
40 public class JTabbedPaneSupport extends AbstractLayoutSupport {
41
42     private int selectedTab = -1;
43
44     private static Method JavaDoc addTabMethod1;
45     private static Method JavaDoc addTabMethod2;
46     private static Method JavaDoc addTabMethod3;
47
48     /** Gets the supported layout manager class - JTabbedPane.
49      * @return the class supported by this delegate
50      */

51     public Class JavaDoc getSupportedClass() {
52         return JTabbedPane.class;
53     }
54
55     /** Removes one component from the layout (at metadata level).
56      * The code structures describing the layout is updated immediately.
57      * @param index index of the component in the layout
58      */

59     public void removeComponent(int index) {
60         super.removeComponent(index);
61         if (selectedTab >= getComponentCount())
62             selectedTab = getComponentCount() - 1;
63     }
64
65     /** This method is called when user clicks on the container in form
66      * designer. For JTabbedPane, we it switch the selected TAB.
67      * @param p Point of click in the container
68      * @param real instance of the container when the click occurred
69      * @param containerDelegate effective container delegate of the container
70      */

71     public void processMouseClick(Point p,
72                                   Container container,
73                                   Container containerDelegate)
74     {
75         if (!(container instanceof JTabbedPane))
76             return;
77
78         JTabbedPane tabbedPane = (JTabbedPane)container;
79         int n = tabbedPane.getTabCount();
80         for (int i=0; i < n; i++) {
81             if (tabbedPane.getBoundsAt(i).contains(p)) {
82                 selectedTab = i;
83                 tabbedPane.setSelectedIndex(i);
84                 break;
85             }
86         }
87     }
88
89     /** This method is called when a component is selected in Component
90      * Inspector.
91      * @param index position (index) of the selected component in container
92      */

93     public void selectComponent(int index) {
94         selectedTab = index; // remember as selected tab
95
}
96
97     /** In this method, the layout delegate has a chance to "arrange" real
98      * container instance additionally - some other way that cannot be
99      * done through layout properties and added components.
100      * @param container instance of a real container to be arranged
101      * @param containerDelegate effective container delegate of the container
102      */

103     public void arrangeContainer(Container container,
104                                  Container containerDelegate)
105     {
106         if (!(container instanceof JTabbedPane))
107             return;
108
109         JTabbedPane tabbedPane = (JTabbedPane) container;
110         if (selectedTab >= 0) {
111             if (tabbedPane.getTabCount() > selectedTab) {
112                 // select the tab
113
tabbedPane.setSelectedIndex(selectedTab);
114
115                 // workaround for JTabbedPane bug 4190719
116
Component comp = tabbedPane.getSelectedComponent();
117                 if (comp != null)
118                     comp.setVisible(true);
119                 tabbedPane.repaint();
120             }
121         }
122         else if (tabbedPane.getTabCount() > 0) {
123             // workaround for JTabbedPane bug 4190719
124
tabbedPane.getComponentAt(0).setVisible(true);
125         }
126     }
127
128     /** This method should calculate position (index) for a component dragged
129      * over a container (or just for mouse cursor being moved over container,
130      * without any component).
131      * @param container instance of a real container over/in which the
132      * component is dragged
133      * @param containerDelegate effective container delegate of the container
134      * @param component the real component being dragged; not needed here
135      * @param index position (index) of the component in its current container;
136      * not needed here
137      * @param posInCont position of mouse in the container delegate; not needed
138      * @param posInComp position of mouse in the dragged component; not needed
139      * @return index corresponding to the position of the component in the
140      * container
141      */

142     public int getNewIndex(Container container,
143                            Container containerDelegate,
144                            Component component,
145                            int index,
146                            Point posInCont,
147                            Point posInComp)
148     {
149         if (!(container instanceof JTabbedPane))
150             return -1;
151         return ((JTabbedPane)container).getTabCount();
152     }
153
154     public String JavaDoc getAssistantContext() {
155         return "tabbedPaneLayout"; // NOI18N
156
}
157
158     /** This method paints a dragging feedback for a component dragged over
159      * a container (or just for mouse cursor being moved over container,
160      * without any component).
161      * @param container instance of a real container over/in which the
162      * component is dragged
163      * @param containerDelegate effective container delegate of the container
164      * @param component the real component being dragged; not needed here
165      * @param newConstraints component layout constraints to be presented;
166      * not used for JTabbedPane
167      * @param newIndex component's index position to be presented; not needed
168      * @param g Graphics object for painting (with color and line style set)
169      * @return whether any feedback was painted (true in this case)
170      */

171     public boolean paintDragFeedback(Container container,
172                                      Container containerDelegate,
173                                      Component component,
174                                      LayoutConstraints newConstraints,
175                                      int newIndex,
176                                      Graphics g)
177     {
178         if (!(container instanceof JTabbedPane))
179             return false;
180
181         JTabbedPane tabbedPane = (JTabbedPane) container;
182         if ((tabbedPane.getTabCount() == 0) || (component == tabbedPane.getComponentAt(0))) {
183             Dimension sz = container.getSize();
184             Insets insets = container.getInsets();
185             sz.width -= insets.left + insets.right;
186             sz.height -= insets.top + insets.bottom;
187             g.drawRect(0, 0, sz.width, sz.height);
188         }
189         else {
190             Rectangle rect = tabbedPane.getComponentAt(0).getBounds();
191             g.drawRect(rect.x, rect.y, rect.width, rect.height);
192         }
193         return true;
194     }
195     
196     /** Adds real components to given container (according to layout
197      * constraints stored for the components).
198      * @param container instance of a real container to be added to
199      * @param containerDelegate effective container delegate of the container
200      * @param components components to be added
201      * @param index position at which to add the components to container
202      */

203     public void addComponentsToContainer(Container container,
204                                          Container containerDelegate,
205                                          Component[] components,
206                                          int index)
207     {
208         if (!(container instanceof JTabbedPane))
209             return;
210
211         for (int i=0; i < components.length; i++) {
212             LayoutConstraints constraints = getConstraints(i + index);
213             if (constraints instanceof TabConstraints) {
214                 JTabbedPane tabbedPane = (JTabbedPane) container;
215                 try {
216                     Object JavaDoc title =
217                         ((FormProperty)constraints.getProperties()[0])
218                             .getRealValue();
219                     Object JavaDoc icon =
220                         ((FormProperty)constraints.getProperties()[1])
221                             .getRealValue();
222                     Object JavaDoc tooltip =
223                         ((FormProperty)constraints.getProperties()[2])
224                             .getRealValue();
225
226                     tabbedPane.addTab(
227                         title instanceof String JavaDoc ? (String JavaDoc) title : null,
228                         icon instanceof Icon ? (Icon) icon : null,
229                         components[i],
230                         tooltip instanceof String JavaDoc ? (String JavaDoc) tooltip : null);
231                 }
232                 catch (Exception JavaDoc ex) {
233                     org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
234                 }
235             }
236         }
237     }
238
239     // ---------
240

241     /** This method is used for scanning code structures and recognizing
242      * components added to containers and their constraints. It's called from
243      * initialize method. When a relevant code statement is found, then the
244      * CodeExpression of component is get and added to component, and also the
245      * layout constraints information is read.
246      * @param statement CodeStatement to be tested if it contains relevant code
247      * @param componentCode CodeGroup to be filled with all component code
248      * @return CodeExpression representing found component; null if the
249      * statement is not relevant
250      */

251     protected CodeExpression readComponentCode(CodeStatement statement,
252                                                CodeGroup componentCode)
253     {
254         CodeExpression compExp;
255         int[] constrPropsIndices;
256         CodeExpression[] params = statement.getStatementParameters();
257
258         Object JavaDoc connectingObject = statement.getMetaObject();
259         if (getAddTabMethod1().equals(connectingObject)) {
260             compExp = params[2];
261             constrPropsIndices = new int[] { 0, 1, -1, 2 }; // tab, icon, tooltip
262
}
263         else if (getAddTabMethod2().equals(connectingObject)) {
264             compExp = params[2];
265             constrPropsIndices = new int[] { 0, 1, -1 }; // tab, icon
266
}
267         else if (getAddTabMethod3().equals(connectingObject)) {
268             compExp = params[1];
269             constrPropsIndices = new int[] { 0, -1 }; // tab
270
}
271         else return null;
272
273         TabConstraints constr = new TabConstraints("tab"); // NOI18N
274
Node.Property[] props = constr.getProperties();
275         for (int i=0; i < params.length; i++) {
276             if (params[i] != compExp)
277                 FormCodeSupport.readPropertyExpression(
278                                     params[i],
279                                     props[constrPropsIndices[i]],
280                                     false);
281         }
282         getConstraintsList().add(constr);
283
284         componentCode.addStatement(statement);
285
286         return compExp;
287     }
288
289     /** Creates code for a component added to the layout (opposite to
290      * readComponentCode method).
291      * @param componentCode CodeGroup to be filled with complete component code
292      * (code for initializing the layout constraints and adding the
293      * component to the layout)
294      * @param compExp CodeExpression object representing component
295      * @param index position of the component in the layout
296      */

297     protected void createComponentCode(CodeGroup componentCode,
298                                        CodeExpression componentExpression,
299                                        int index)
300     {
301         LayoutConstraints constr = getConstraints(index);
302         if (!(constr instanceof TabConstraints))
303             return; // should not happen
304

305         ((TabConstraints)constr).createComponentCode(
306                            componentCode,
307                            getLayoutContext().getContainerCodeExpression(),
308                            componentExpression);
309     }
310
311     /** This method is called to get a default component layout constraints
312      * metaobject in case it is not provided (e.g. in addComponents method).
313      * @return the default LayoutConstraints object for the supported layout;
314      * null if no component constraints are used
315      */

316     protected LayoutConstraints createDefaultConstraints() {
317         return new TabConstraints("tab"+(getComponentCount())); // NOI18N
318
}
319
320     // ----------
321

322     // tab, icon, component, tooltip
323
private static Method JavaDoc getAddTabMethod1() {
324         if (addTabMethod1 == null) {
325             try {
326                 addTabMethod1 = JTabbedPane.class.getMethod(
327                                 "addTab", // NOI18N
328
new Class JavaDoc[] { String JavaDoc.class, Icon.class,
329                                       Component.class, String JavaDoc.class });
330             }
331             catch (NoSuchMethodException JavaDoc ex) { // should not happen
332
ex.printStackTrace();
333             }
334         }
335         return addTabMethod1;
336     }
337
338     // tab, icon, component
339
private static Method JavaDoc getAddTabMethod2() {
340         if (addTabMethod2 == null) {
341             try {
342                 addTabMethod2 = JTabbedPane.class.getMethod(
343                                 "addTab", // NOI18N
344
new Class JavaDoc[] { String JavaDoc.class, Icon.class,
345                                               Component.class });
346             }
347             catch (NoSuchMethodException JavaDoc ex) { // should not happen
348
ex.printStackTrace();
349             }
350         }
351         return addTabMethod2;
352     }
353
354     // tab, component
355
private static Method JavaDoc getAddTabMethod3() {
356         if (addTabMethod3 == null) {
357             try {
358                 addTabMethod3 = JTabbedPane.class.getMethod(
359                                 "addTab", // NOI18N
360
new Class JavaDoc[] { String JavaDoc.class, Component.class });
361             }
362             catch (NoSuchMethodException JavaDoc ex) { // should not happen
363
ex.printStackTrace();
364             }
365         }
366         return addTabMethod3;
367     }
368
369     // ----------
370

371     /** LayoutConstraints implementation for managing JTabbedPane tab
372      * parameters.
373      */

374     public static class TabConstraints implements LayoutConstraints {
375         private String JavaDoc title;
376         private Icon icon;
377         private String JavaDoc toolTip;
378
379         private FormProperty[] properties;
380
381         private CodeExpression containerExpression;
382         private CodeExpression componentExpression;
383         private CodeGroup componentCode;
384         private CodeExpression[] propertyExpressions;
385
386         public TabConstraints(String JavaDoc title) {
387             this.title = title;
388         }
389
390         public TabConstraints(String JavaDoc title, Icon icon, String JavaDoc toolTip) {
391             this.title = title;
392             this.icon = icon;
393             this.toolTip = toolTip;
394         }
395
396         public String JavaDoc getTitle() {
397             return title;
398         }
399
400         public Icon getIcon() {
401             return icon;
402         }
403
404         public String JavaDoc getToolTip() {
405             return toolTip;
406         }
407
408         // -----------
409

410         public Node.Property[] getProperties() {
411             if (properties == null) {
412                 properties = new FormProperty[] {
413                     new FormProperty("TabConstraints.tabTitle", // NOI18N
414
String JavaDoc.class,
415                                  getBundle().getString("PROP_tabTitle"), // NOI18N
416
getBundle().getString("HINT_tabTitle")) { // NOI18N
417

418                         public Object JavaDoc getTargetValue() {
419                             return title;
420                         }
421
422                         public void setTargetValue(Object JavaDoc value) {
423                             title = (String JavaDoc)value;
424                         }
425
426                         protected Object JavaDoc getRealValue(Object JavaDoc value) {
427                             Object JavaDoc realValue = super.getRealValue(value);
428                             if (realValue == FormDesignValue.IGNORED_VALUE)
429                                 realValue = ((FormDesignValue)value).getDescription();
430                             return realValue;
431                         }
432
433                         protected void propertyValueChanged(Object JavaDoc old, Object JavaDoc current) {
434                             if (isChangeFiring())
435                                 updateCode();
436                             super.propertyValueChanged(old, current);
437                         }
438                     },
439
440                     new FormProperty("TabConstraints tabIcon", // NOI18N
441
Icon.class,
442                                  getBundle().getString("PROP_tabIcon"), // NOI18N
443
getBundle().getString("HINT_tabIcon")) { // NOI18N
444

445                         public Object JavaDoc getTargetValue() {
446                             return icon;
447                         }
448
449                         public void setTargetValue(Object JavaDoc value) {
450                             icon = (Icon)value;
451                         }
452
453                         public boolean supportsDefaultValue() {
454                             return true;
455                         }
456
457                         public Object JavaDoc getDefaultValue() {
458                             return null;
459                         }
460
461                         protected void propertyValueChanged(Object JavaDoc old, Object JavaDoc current) {
462                             if (isChangeFiring())
463                                 updateCode();
464                             super.propertyValueChanged(old, current);
465                         }
466                     },
467
468                     new FormProperty("TabConstraints tabToolTip", // NOI18N
469
String JavaDoc.class,
470                                  getBundle().getString("PROP_tabToolTip"), // NOI18N
471
getBundle().getString("HINT_tabToolTip")) { // NOI18N
472

473                         public Object JavaDoc getTargetValue() {
474                             return toolTip;
475                         }
476
477                         public void setTargetValue(Object JavaDoc value) {
478                             toolTip = (String JavaDoc)value;
479                         }
480
481                         protected Object JavaDoc getRealValue(Object JavaDoc value) {
482                             Object JavaDoc realValue = super.getRealValue(value);
483                             if (realValue == FormDesignValue.IGNORED_VALUE)
484                                 realValue = ((FormDesignValue)value).getDescription();
485                             return realValue;
486                         }
487
488                         public boolean supportsDefaultValue() {
489                             return true;
490                         }
491
492                         public Object JavaDoc getDefaultValue() {
493                             return null;
494                         }
495
496                         protected void propertyValueChanged(Object JavaDoc old, Object JavaDoc current) {
497                             if (isChangeFiring())
498                                 updateCode();
499                             super.propertyValueChanged(old, current);
500                         }
501                     }
502                 };
503
504                 properties[0].setChanged(true);
505             }
506
507             return properties;
508         }
509
510         public Object JavaDoc getConstraintsObject() {
511             return title;
512         }
513
514         public LayoutConstraints cloneConstraints() {
515             LayoutConstraints constr = new TabConstraints(title);
516             org.netbeans.modules.form.FormUtils.copyProperties(
517                 getProperties(),
518                 constr.getProperties(),
519                 FormUtils.CHANGED_ONLY | FormUtils.DISABLE_CHANGE_FIRING);
520             return constr;
521         }
522
523         // --------
524

525         private void createComponentCode(CodeGroup compCode,
526                                          CodeExpression contExp,
527                                          CodeExpression compExp)
528         {
529             this.componentCode = compCode;
530             this.containerExpression = contExp;
531             this.componentExpression = compExp;
532             this.propertyExpressions = null;
533             updateCode();
534         }
535
536         private void updateCode() {
537             if (componentCode == null)
538                 return;
539
540             CodeStructure.removeStatements(
541                 componentCode.getStatementsIterator());
542             componentCode.removeAll();
543
544             getProperties();
545
546             Method JavaDoc addTabMethod;
547             CodeExpression[] params;
548
549             if (properties[2].isChanged()) {
550                 addTabMethod = getAddTabMethod1();
551                 params = new CodeExpression[] { getPropertyExpression(0), // tab
552
getPropertyExpression(1), // icon
553
componentExpression,
554                                                 getPropertyExpression(2) }; // tooltip
555
}
556             else if (properties[1].isChanged()) {
557                 addTabMethod = getAddTabMethod2();
558                 params = new CodeExpression[] { getPropertyExpression(0), // tab
559
getPropertyExpression(1), // icon
560
componentExpression };
561             }
562             else { // tab
563
addTabMethod = getAddTabMethod3();
564                 params = new CodeExpression[] { getPropertyExpression(0), // tab
565
componentExpression };
566             }
567
568             CodeStatement addTabStatement = CodeStructure.createStatement(
569                                                           containerExpression,
570                                                           addTabMethod,
571                                                           params);
572             componentCode.addStatement(addTabStatement);
573         }
574
575         private CodeExpression getPropertyExpression(int index) {
576             if (propertyExpressions == null) {
577                 propertyExpressions = new CodeExpression[properties.length];
578                 for (int i=0; i < properties.length; i++) {
579                     propertyExpressions[i] =
580                         componentExpression.getCodeStructure().createExpression(
581                             FormCodeSupport.createOrigin(properties[i]));
582                 }
583             }
584             return propertyExpressions[index];
585         }
586     }
587 }
588
Popular Tags