KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > PrimitiveBinding


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.core.registry;
21
22
23 import org.openide.filesystems.FileObject;
24 import org.openide.filesystems.FileSystem;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.awt.*;
32 import java.net.URL JavaDoc;
33
34 final class PrimitiveBinding extends ObjectBinding {
35     public static final String JavaDoc PRIMITIVE_BINDING_PREFIX = "BINDING:";
36
37     private FileObject folder;
38     private String JavaDoc bindingName;
39
40     public String JavaDoc getBindingName() {
41         return bindingName;
42     }
43
44     public Object JavaDoc createInstance() throws IOException JavaDoc {
45         Object JavaDoc o = folder.getAttribute(PRIMITIVE_BINDING_PREFIX + bindingName);
46         if (o != null) {
47             o = convertToObject(o);
48         }
49         return o;
50     }
51
52     // MultiFilesystem stores attributes of folders which do not exist
53
// on writable layer yet on root file object of the writable filesystem
54
// in the format of "folder\folder\attribute". Check that here:
55
static boolean isCustomizedAndAttachedToRoot (FileSystem customFS, FileObject folder, String JavaDoc bindingName) {
56         String JavaDoc name = folder.getPath().replace('/', '\\');
57         return (customFS.getRoot().getAttribute(name+"\\"+PRIMITIVE_BINDING_PREFIX+bindingName) != null) ? true : false;
58     }
59
60     // MultiFilesystem stores attributes of folders which do not exist
61
// on writable layer yet on root file object of the writable filesystem
62
// in the format of "folder\folder\attribute". Check that here:
63
static void deleteIfCustomizedAndAttachedToRoot (FileSystem customFS, FileObject folder, String JavaDoc bindingName) throws IOException JavaDoc {
64         String JavaDoc name = folder.getPath().replace('/', '\\');
65         FileObject root = customFS.getRoot();
66         if (root.getAttribute(name+"\\"+PRIMITIVE_BINDING_PREFIX+bindingName) != null) {
67             root.setAttribute(name+"\\"+PRIMITIVE_BINDING_PREFIX+bindingName, null);
68         }
69     }
70     
71     static ObjectBinding get(FileObject folder, String JavaDoc bindingName) {
72         return isValid(folder, bindingName) ? new PrimitiveBinding(folder, bindingName) : null;
73     }
74
75     static Collection JavaDoc getAll(FileObject folder) {
76         Set JavaDoc retVal = new HashSet JavaDoc();
77         Enumeration JavaDoc en = folder.getAttributes();
78         while (en.hasMoreElements()) {
79             String JavaDoc attr = (String JavaDoc) en.nextElement();
80             if (attr.startsWith(PRIMITIVE_BINDING_PREFIX)) {
81                 ObjectBinding ob = PrimitiveBinding.get(folder, attr.substring(PRIMITIVE_BINDING_PREFIX.length()));
82                 if (ob != null) retVal.add(ob);
83             }
84         }
85         return retVal;
86     }
87
88
89     /**
90      * may return null
91      */

92     static ObjectBinding create(FileObject folder, String JavaDoc bindingName, Object JavaDoc object) {
93         if (!isPrimitive(object)) return null;
94
95         object = convertToPrimitive(object);
96         try {
97             folder.setAttribute(PRIMITIVE_BINDING_PREFIX + bindingName, object);
98         } catch (IOException JavaDoc e) {
99             // then this method returns null;
100
}
101
102         return isValid(folder, bindingName) ? new PrimitiveBinding(folder, bindingName) : null;
103     }
104
105     private PrimitiveBinding(FileObject folder, String JavaDoc bindingName) {
106         super(folder);
107         this.folder = folder;
108         this.bindingName = bindingName;
109     }
110
111     public void delete() throws IOException JavaDoc {
112         if (isEnabled()) {
113             // delete primitive binding attributes
114
deleteAllAttributes();
115             // delete primitive binding
116
folder.setAttribute(PRIMITIVE_BINDING_PREFIX + bindingName, null);
117         }
118     }
119
120     private void deleteAllAttributes() throws IOException JavaDoc {
121         Enumeration JavaDoc en = folder.getAttributes();
122         while (en.hasMoreElements()) {
123             String JavaDoc attrName = (String JavaDoc) en.nextElement();
124             if (attrName.startsWith(ContextImpl.PRIMITIVE_BINDING_ATTR_PREFIX + bindingName + "/")) {
125                 folder.setAttribute(attrName, null);
126             }
127         }
128     }
129
130     public boolean isEnabled() {
131         return isValid(folder, bindingName);
132     }
133
134     public ObjectBinding write(Object JavaDoc object) throws IOException JavaDoc {
135         return super.write(object);
136     }
137
138     private static boolean isValid(FileObject folder, String JavaDoc bindingName) {
139         return (folder.getAttribute(PRIMITIVE_BINDING_PREFIX + bindingName) != null);
140     }
141
142     private static Object JavaDoc convertToPrimitive(Object JavaDoc o) {
143         if (!((o instanceof Font) || (o instanceof Color))) {
144             // all other simple or primitive data types (including URL)
145
// are handled directly by FS attribute types
146
return o;
147         }
148
149         String JavaDoc value;
150         if (o instanceof Font) {
151             Font f = (Font) o;
152             String JavaDoc strStyle;
153
154             if (f.isBold()) {
155                 strStyle = f.isItalic() ? "bolditalic" : "bold";
156             } else {
157                 strStyle = f.isItalic() ? "italic" : "plain";
158             }
159             value = "Font[" + f.getFamily() + "-" + strStyle + "-" + f.getSize() + "]";
160         } else {
161             value = "Color[" + Integer.toString(((Color) o).getRGB()) + "]";
162         }
163         return value;
164     }
165
166     static boolean isPrimitive(Object JavaDoc o) {
167         if (o instanceof String JavaDoc ||
168                 o instanceof Integer JavaDoc ||
169                 o instanceof Long JavaDoc ||
170                 o instanceof Boolean JavaDoc ||
171                 o instanceof Float JavaDoc ||
172                 o instanceof Double JavaDoc ||
173                 o instanceof Font ||
174                 o instanceof URL JavaDoc ||
175                 o instanceof Color) {
176             return true;
177         } else {
178             return false;
179         }
180     }
181
182     private static Object JavaDoc convertToObject(Object JavaDoc o) {
183         if ((!(o instanceof String JavaDoc)) || (!(((String JavaDoc) o).endsWith("]")))) {
184             return o;
185         }
186         try {
187             String JavaDoc string = (String JavaDoc) o;
188             String JavaDoc val = string.substring(0, string.length() - 1);
189             if (val.startsWith("Font[")) {
190                 val = val.substring("Font[".length());
191                 return Font.decode(val);
192             } else if (val.startsWith("Color[")) {
193                 val = val.substring("Color[".length());
194                 return Color.decode(val);
195             }
196         } catch (Exception JavaDoc e) {
197             // ignore it
198
}
199         return o;
200     }
201
202 }
203
Popular Tags