KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > options > ContextSystemOption


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 package org.openide.options;
20
21 import org.openide.util.io.NbMarshalledObject;
22
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.beancontext.BeanContext JavaDoc;
25 import java.beans.beancontext.BeanContextChild JavaDoc;
26 import java.beans.beancontext.BeanContextProxy JavaDoc;
27 import java.beans.beancontext.BeanContextSupport JavaDoc;
28
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInput JavaDoc;
31 import java.io.ObjectOutput JavaDoc;
32
33 import java.util.Arrays JavaDoc;
34 import java.util.Comparator JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38
39 /** Provides a group of system options with this as the parent.
40 * You must still implement {@link SystemOption#displayName}, at least.
41 * The suboptions are automatically saved as a group.
42 *
43 * @author Ales Novak
44 */

45 public abstract class ContextSystemOption extends SystemOption implements BeanContextProxy JavaDoc {
46     /** beanContext property's key. */
47     private static Object JavaDoc ctxt = new Object JavaDoc();
48     private static final long serialVersionUID = -781528552645947127L;
49
50     /** Reference to the bean context describing the structure of this option tree.
51      * @deprecated To obtain bean context use {@link #getBeanContextProxy}.
52      */

53     protected BeanContext JavaDoc beanContext;
54
55     /** Default constructor. */
56     public ContextSystemOption() {
57         // backward compatability
58
beanContext = getBeanContext();
59     }
60
61     /** Add a new option to the set.
62     * @param so the option to add
63     */

64     public final void addOption(SystemOption so) {
65         getBeanContext().add(so);
66     }
67
68     /** Remove an option from the set.
69     * @param so the option to remove
70     */

71     public final void removeOption(SystemOption so) {
72         getBeanContext().remove(so);
73     }
74
75     /** Get all options in the set.
76     * @return the options
77     */

78     public final SystemOption[] getOptions() {
79         // [WARNING] call to getBeanContext().toArray() can return either SystemOptions
80
// or something of another type (I detected BeanContextSupport)
81
// It requires deep investigation ...
82
int i;
83
84         // [WARNING] call to getBeanContext().toArray() can return either SystemOptions
85
// or something of another type (I detected BeanContextSupport)
86
// It requires deep investigation ...
87
int j;
88         SystemOption[] options;
89
90         Object JavaDoc[] objs = getBeanContext().toArray();
91
92         // filter out everything not SystemOption
93
for (i = 0, j = 0; i < objs.length; i++) {
94             if (objs[i] instanceof SystemOption) {
95                 if (i > j) {
96                     objs[j] = objs[i];
97                 }
98
99                 j++;
100             }
101         }
102
103         options = new SystemOption[j];
104         System.arraycopy(objs, 0, options, 0, j);
105
106         return options;
107     }
108
109     /* Method from interface BeanContextProxy.
110     * @return a BeanContext - tree of options
111     */

112     public final BeanContextChild JavaDoc getBeanContextProxy() {
113         return getBeanContext();
114     }
115
116     private BeanContext JavaDoc getBeanContext() {
117         return (BeanContext JavaDoc) getProperty(ctxt);
118     }
119
120     protected void initialize() {
121         super.initialize();
122         this.putProperty(ctxt, new OptionBeanContext(this));
123     }
124
125     /* Writes the beanContext variable to an ObjectOutput instance.
126     * @param out
127     */

128     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
129         super.writeExternal(out);
130
131         Object JavaDoc[] objects = getBeanContext().toArray();
132         Arrays.sort(objects, new ClassComparator());
133
134         for (int i = 0; i < objects.length; i++) {
135             out.writeObject(new NbMarshalledObject(objects[i]));
136         }
137
138         out.writeObject(null);
139     }
140
141     /* Reads the beanContext variable from an ObjectInpuit instance.
142     * @param in
143     */

144     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
145         super.readExternal(in);
146
147         Object JavaDoc obj = in.readObject();
148
149         if (obj instanceof BeanContext JavaDoc) {
150             // old version of serialization
151
// XXX does this really work??
152
beanContext = (BeanContext JavaDoc) obj;
153         } else {
154             // new version with safe serialization
155
BeanContext JavaDoc c = getBeanContext();
156
157             while (obj != null) {
158                 NbMarshalledObject m = (NbMarshalledObject) obj;
159
160                 // #18626 fix: deserialization of the context option should survive
161
// deserialization of its children. They can belong to disabled
162
// or removed modules.
163
try {
164                     c.add(m.get());
165                 } catch (Exception JavaDoc e) {
166                     Logger.getLogger(ContextSystemOption.class.getName()).log(Level.WARNING, null, e);
167                 } catch (LinkageError JavaDoc e) {
168                     Logger.getLogger(ContextSystemOption.class.getName()).log(Level.WARNING, null, e);
169                 }
170
171                 // read next
172
obj = in.readObject();
173             }
174         }
175     }
176
177     /** Comparator of class names of objects. It is used in
178      * <code>writeExternal</code>.
179      */

180     private static class ClassComparator implements Comparator JavaDoc {
181         ClassComparator() {
182         }
183
184         /** It Compares name of classes of two objects */
185         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
186             return o1.getClass().getName().compareTo(o2.getClass().getName());
187         }
188     }
189
190     /** A hierarchy of SystemOptions.
191     * Allows add/remove SystemOption beans only.
192     * @warning many methods throws UnsupportedOperationException like BeanContextSupport does.
193     */

194     private static class OptionBeanContext extends BeanContextSupport JavaDoc implements PropertyChangeListener JavaDoc {
195         private static final long serialVersionUID = 3532434266136225440L;
196         private ContextSystemOption parent = null;
197
198         public OptionBeanContext(ContextSystemOption p) {
199             parent = p;
200         }
201
202         /** Overridden from base class.
203         * @exception IllegalArgumentException if not targetChild instanceof SystemOption
204         */

205         public boolean add(Object JavaDoc targetChild) {
206             if (!(targetChild instanceof SystemOption)) {
207                 throw new IllegalArgumentException JavaDoc("Not a SystemOption: " + targetChild); // NOI18N
208
}
209
210             boolean b = super.add(targetChild);
211
212             if (b) {
213                 ((SystemOption) targetChild).addPropertyChangeListener(this);
214             }
215
216             return b;
217         }
218
219         public boolean remove(Object JavaDoc targetChild) {
220             if (!(targetChild instanceof SystemOption)) {
221                 throw new IllegalArgumentException JavaDoc("Not a SystemOption: " + targetChild); // NOI18N
222
}
223
224             boolean b = super.remove(targetChild);
225
226             if (b) {
227                 ((SystemOption) targetChild).removePropertyChangeListener(this);
228             }
229
230             return b;
231         }
232
233         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
234             if (parent != null) {
235                 parent.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
236             }
237         }
238     }
239 }
240
Popular Tags