1 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 ; 27 import java.util.Enumeration ; 28 import java.util.Collection ; 29 import java.util.Set ; 30 import java.util.HashSet ; 31 import java.awt.*; 32 import java.net.URL ; 33 34 final class PrimitiveBinding extends ObjectBinding { 35 public static final String PRIMITIVE_BINDING_PREFIX = "BINDING:"; 36 37 private FileObject folder; 38 private String bindingName; 39 40 public String getBindingName() { 41 return bindingName; 42 } 43 44 public Object createInstance() throws IOException { 45 Object o = folder.getAttribute(PRIMITIVE_BINDING_PREFIX + bindingName); 46 if (o != null) { 47 o = convertToObject(o); 48 } 49 return o; 50 } 51 52 static boolean isCustomizedAndAttachedToRoot (FileSystem customFS, FileObject folder, String bindingName) { 56 String name = folder.getPath().replace('/', '\\'); 57 return (customFS.getRoot().getAttribute(name+"\\"+PRIMITIVE_BINDING_PREFIX+bindingName) != null) ? true : false; 58 } 59 60 static void deleteIfCustomizedAndAttachedToRoot (FileSystem customFS, FileObject folder, String bindingName) throws IOException { 64 String 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 bindingName) { 72 return isValid(folder, bindingName) ? new PrimitiveBinding(folder, bindingName) : null; 73 } 74 75 static Collection getAll(FileObject folder) { 76 Set retVal = new HashSet (); 77 Enumeration en = folder.getAttributes(); 78 while (en.hasMoreElements()) { 79 String attr = (String ) 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 92 static ObjectBinding create(FileObject folder, String bindingName, Object 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 e) { 99 } 101 102 return isValid(folder, bindingName) ? new PrimitiveBinding(folder, bindingName) : null; 103 } 104 105 private PrimitiveBinding(FileObject folder, String bindingName) { 106 super(folder); 107 this.folder = folder; 108 this.bindingName = bindingName; 109 } 110 111 public void delete() throws IOException { 112 if (isEnabled()) { 113 deleteAllAttributes(); 115 folder.setAttribute(PRIMITIVE_BINDING_PREFIX + bindingName, null); 117 } 118 } 119 120 private void deleteAllAttributes() throws IOException { 121 Enumeration en = folder.getAttributes(); 122 while (en.hasMoreElements()) { 123 String attrName = (String ) 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 object) throws IOException { 135 return super.write(object); 136 } 137 138 private static boolean isValid(FileObject folder, String bindingName) { 139 return (folder.getAttribute(PRIMITIVE_BINDING_PREFIX + bindingName) != null); 140 } 141 142 private static Object convertToPrimitive(Object o) { 143 if (!((o instanceof Font) || (o instanceof Color))) { 144 return o; 147 } 148 149 String value; 150 if (o instanceof Font) { 151 Font f = (Font) o; 152 String 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 o) { 167 if (o instanceof String || 168 o instanceof Integer || 169 o instanceof Long || 170 o instanceof Boolean || 171 o instanceof Float || 172 o instanceof Double || 173 o instanceof Font || 174 o instanceof URL || 175 o instanceof Color) { 176 return true; 177 } else { 178 return false; 179 } 180 } 181 182 private static Object convertToObject(Object o) { 183 if ((!(o instanceof String )) || (!(((String ) o).endsWith("]")))) { 184 return o; 185 } 186 try { 187 String string = (String ) o; 188 String 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 e) { 197 } 199 return o; 200 } 201 202 } 203 | Popular Tags |