KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > FormNode


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;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Window JavaDoc;
24 import java.awt.event.WindowAdapter JavaDoc;
25 import java.awt.event.WindowEvent JavaDoc;
26
27 import org.openide.nodes.*;
28 import org.openide.cookies.*;
29 import org.openide.actions.*;
30 import org.openide.util.actions.SystemAction;
31 import org.openide.loaders.DataObject;
32
33 /**
34  * A common superclass for nodes used in Form Editor.
35  *
36  * @author Tomas Pavek
37  */

38
39 public class FormNode extends AbstractNode implements FormCookie {
40
41     private FormModel formModel;
42
43     protected FormNode(Children children, FormModel formModel) {
44         super(children);
45         this.formModel = formModel;
46         getCookieSet().add(this);
47     }
48
49     // FormCookie implementation
50
public final FormModel getFormModel() {
51         return formModel;
52     }
53
54     // FormCookie implementation
55
public final Node getOriginalNode() {
56         return this;
57     }
58
59     public Node.Cookie getCookie(Class JavaDoc type) {
60         Node.Cookie cookie = super.getCookie(type);
61         if (cookie == null
62             && (DataObject.class.isAssignableFrom(type)
63                 || SaveCookie.class.isAssignableFrom(type)
64                 || CloseCookie.class.isAssignableFrom(type)
65                 || PrintCookie.class.isAssignableFrom(type)))
66         {
67             FormDataObject fdo = FormEditor.getFormDataObject(formModel);
68             if (fdo != null)
69                 cookie = fdo.getCookie(type);
70         }
71         return cookie;
72     }
73
74     // because delegating cookies to FormDataObject we have a bit complicated
75
// way of updating cookies on node - need fire a change on nodes explicitly
76
void updateCookies() {
77         super.fireCookieChange();
78     }
79
80     public javax.swing.Action JavaDoc[] getActions(boolean context) {
81         if (systemActions == null) // from AbstractNode
82
systemActions = new SystemAction[] {
83                 SystemAction.get(PropertiesAction.class)
84             };
85         return systemActions;
86     }
87
88     public Component JavaDoc getCustomizer() {
89         Component JavaDoc customizer = createCustomizer();
90         if (customizer instanceof Window JavaDoc) {
91             // register the customizer window (probably a dialog) to be closed
92
// automatically when the form is closed
93
FormEditor formEditor = FormEditor.getFormEditor(formModel);
94             if (formEditor != null) {
95                 Window JavaDoc customizerWindow = (Window JavaDoc) customizer;
96                 formEditor.registerFloatingWindow(customizerWindow);
97                 // attach a listener to unregister the window when it is closed
98
customizerWindow.addWindowListener(new WindowAdapter JavaDoc() {
99                     public void windowClosing(WindowEvent JavaDoc e) {
100                         if (e.getSource() instanceof Window JavaDoc) {
101                             Window JavaDoc window = (Window JavaDoc) e.getSource();
102                             FormEditor formEditor = FormEditor.getFormEditor(formModel);
103                             if (formEditor != null)
104                                 formEditor.unregisterFloatingWindow(window);
105                             window.removeWindowListener(this);
106                         }
107                     }
108                 });
109             }
110         }
111         return customizer;
112     }
113
114     // to be implemented in FormNode descendants (instead of getCustomizer)
115
protected Component JavaDoc createCustomizer() {
116         return null;
117     }
118     
119     /** Provides access for firing property changes */
120     public void firePropertyChangeHelper(String JavaDoc name,
121                                          Object JavaDoc oldValue, Object JavaDoc newValue) {
122         super.firePropertyChange(name, oldValue, newValue);
123     }
124
125     // ----------
126
// automatic children updates
127

128     void updateChildren() {
129         Children children = getChildren();
130         if (children instanceof FormNodeChildren)
131             ((FormNodeChildren)children).updateKeys();
132     }
133
134     // Special children class - to be implemented in FormNode descendants (if
135
// they know their set of children nodes and can update them).
136
protected abstract static class FormNodeChildren extends Children.Keys {
137         protected void updateKeys() {}
138     }
139
140     // ----------
141
// Persistence hacks - for the case the node is selected in some
142
// (standalone) properties window when IDE exits. We don't restore the
143
// original node after IDE restarts (would require to load the form), but
144
// provide a fake node which destroys itself immediately - closing the
145
// properties window. [Would be nice to find some better solution...]
146

147     public Node.Handle getHandle() {
148         return new Handle();
149     }
150
151     static class Handle implements Node.Handle {
152         static final long serialVersionUID = 1;
153         public Node getNode() throws java.io.IOException JavaDoc {
154             return new ClosingNode();
155         }
156     }
157
158     static class ClosingNode extends AbstractNode implements Runnable JavaDoc {
159         ClosingNode() {
160             super(Children.LEAF);
161         }
162         public String JavaDoc getName() {
163             java.awt.EventQueue.invokeLater(this);
164             return super.getName();
165         }
166         public Node.PropertySet[] getPropertySets() {
167             java.awt.EventQueue.invokeLater(this);
168             return super.getPropertySets();
169         }
170         public void run() {
171             this.fireNodeDestroyed();
172         }
173     }
174 }
175
Popular Tags