KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutsupport > LayoutNode


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;
21
22 import java.awt.*;
23 import java.util.*;
24 import java.beans.*;
25 import java.security.*;
26
27 import org.openide.ErrorManager;
28 import org.openide.nodes.*;
29 import org.openide.util.actions.SystemAction;
30
31 import org.netbeans.modules.form.*;
32 import org.netbeans.modules.form.actions.SelectLayoutAction;
33
34 /**
35  * @author Tomas Pavek
36  */

37
38 public class LayoutNode extends FormNode
39                         implements RADComponentCookie, FormPropertyCookie
40 {
41     private LayoutSupportManager layoutSupport;
42
43     public LayoutNode(RADVisualContainer cont) {
44         super(Children.LEAF, cont.getFormModel());
45         layoutSupport = cont.getLayoutSupport();
46         setName(layoutSupport.getDisplayName());
47         cont.setLayoutNodeReference(this);
48     }
49
50     // RADComponentCookie
51
public RADComponent getRADComponent() {
52         return layoutSupport.getMetaContainer();
53     }
54
55     // FormPropertyCookie
56
public FormProperty getProperty(String JavaDoc name) {
57         Node.Property prop = layoutSupport.getLayoutProperty(name);
58         return prop instanceof FormProperty ? (FormProperty) prop : null;
59     }
60
61     public void fireLayoutPropertiesChange() {
62         firePropertyChange(null, null, null);
63     }
64
65     public void fireLayoutPropertySetsChange() {
66         firePropertySetsChange(null, null);
67     }
68
69     public Image getIcon(int iconType) {
70         return layoutSupport.getIcon(iconType);
71     }
72
73     public Node.PropertySet[] getPropertySets() {
74         return layoutSupport.getPropertySets();
75     }
76
77     public javax.swing.Action JavaDoc[] getActions(boolean context) {
78         if (systemActions == null) { // from AbstractNode
79
ArrayList actions = new ArrayList(10);
80
81             if (!layoutSupport.getMetaContainer().isReadOnly()) {
82                 actions.add(SystemAction.get(SelectLayoutAction.class));
83                 actions.add(null);
84             }
85
86             javax.swing.Action JavaDoc[] superActions = super.getActions(context);
87             for (int i=0; i < superActions.length; i++)
88                 actions.add(superActions[i]);
89
90             systemActions = new SystemAction[actions.size()];
91             actions.toArray(systemActions);
92         }
93
94         return systemActions;
95     }
96
97     public boolean hasCustomizer() {
98         return !layoutSupport.getMetaContainer().isReadOnly()
99                && layoutSupport.getCustomizerClass() != null;
100     }
101
102     protected Component createCustomizer() {
103         Class JavaDoc customizerClass = layoutSupport.getCustomizerClass();
104         if (customizerClass == null)
105             return null;
106
107         Component supportCustomizer = layoutSupport.getSupportCustomizer();
108         if (supportCustomizer != null)
109             return supportCustomizer;
110
111         // create bean customizer for layout manager
112
Object JavaDoc customizerObject;
113         try {
114             customizerObject = customizerClass.newInstance();
115         }
116         catch (InstantiationException JavaDoc e) {
117             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
118             return null;
119         }
120         catch (IllegalAccessException JavaDoc e) {
121             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
122             return null;
123         }
124
125         if (customizerObject instanceof Component
126             && customizerObject instanceof Customizer)
127         {
128             Customizer customizer = (Customizer) customizerObject;
129             customizer.setObject(
130                 layoutSupport.getPrimaryContainerDelegate().getLayout());
131
132             customizer.addPropertyChangeListener(new PropertyChangeListener() {
133                 public void propertyChange(PropertyChangeEvent evt) {
134                     Node.Property[] properties;
135                     if (evt.getPropertyName() != null) {
136                         Node.Property changedProperty =
137                             layoutSupport.getLayoutProperty(evt.getPropertyName());
138                         if (changedProperty != null)
139                             properties = new Node.Property[] { changedProperty };
140                         else return; // non-existing property?
141
}
142                     else {
143                         properties = layoutSupport.getAllProperties();
144                         evt = null;
145                     }
146
147                     updatePropertiesFromCustomizer(properties, evt);
148                 }
149             });
150
151             return (Component) customizer;
152         }
153
154         return null;
155     }
156
157     private void updatePropertiesFromCustomizer(
158                      final Node.Property[] properties,
159                      final PropertyChangeEvent evt)
160     {
161         // just for sure we run this as privileged to avoid security problems,
162
// the property change can be fired from untrusted bean customizer code
163
AccessController.doPrivileged(new PrivilegedAction() {
164             public Object JavaDoc run() {
165                 try {
166                     PropertyChangeEvent ev = evt == null ? null :
167                          new PropertyChangeEvent(
168                                layoutSupport.getLayoutDelegate(),
169                                evt.getPropertyName(),
170                                evt.getOldValue(), evt.getNewValue());
171
172                     for (int i=0; i < properties.length; i++) {
173                         if (properties[i] instanceof FormProperty)
174                             ((FormProperty)properties[i]).reinstateProperty();
175                         // [there should be something for Node.Property too]
176

177                         if (ev != null)
178                             layoutSupport.containerLayoutChanged(ev);
179                     }
180
181                     if (ev == null) // anonymous property changed
182
layoutSupport.containerLayoutChanged(null);
183                         // [but this probably won't do anything...]
184
}
185                 catch (PropertyVetoException ex) {
186                     // the change is not accepted, but what can we do here?
187
// java.beans.Customizer has no veto capabilities
188
}
189                 catch (Exception JavaDoc ex) {
190                     ErrorManager.getDefault().notify(ex);
191                 }
192
193                 return null;
194             }
195         });
196     }
197
198 /* public HelpCtx getHelpCtx() {
199         Class layoutClass = layoutSupport.getLayoutClass();
200         String helpID = null;
201         if (layoutClass != null) {
202             if (layoutClass == BorderLayout.class)
203                 helpID = "gui.layouts.managers.border"; // NOI18N
204             else if (layoutClass == FlowLayout.class)
205                 helpID = "gui.layouts.managers.flow"; // NOI18N
206             else if (layoutClass == GridLayout.class)
207                 helpID = "gui.layouts.managers.grid"; // NOI18N
208             else if (layoutClass == GridBagLayout.class)
209                 helpID = "gui.layouts.managers.gridbag"; // NOI18N
210             else if (layoutClass == CardLayout.class)
211                 helpID = "gui.layouts.managers.card"; // NOI18N
212             else if (layoutClass == javax.swing.BoxLayout.class)
213                 helpID = "gui.layouts.managers.box"; // NOI18N
214             else if (layoutClass == org.netbeans.lib.awtextra.AbsoluteLayout.class)
215                 helpID = "gui.layouts.managers.absolute"; // NOI18N
216         }
217         if (helpID != null)
218             return new HelpCtx(helpID);
219         return super.getHelpCtx();
220     } */

221 }
222
Popular Tags