1 19 package org.openide.options; 20 21 import org.openide.util.io.NbMarshalledObject; 22 23 import java.beans.PropertyChangeListener ; 24 import java.beans.beancontext.BeanContext ; 25 import java.beans.beancontext.BeanContextChild ; 26 import java.beans.beancontext.BeanContextProxy ; 27 import java.beans.beancontext.BeanContextSupport ; 28 29 import java.io.IOException ; 30 import java.io.ObjectInput ; 31 import java.io.ObjectOutput ; 32 33 import java.util.Arrays ; 34 import java.util.Comparator ; 35 import java.util.logging.Level ; 36 import java.util.logging.Logger ; 37 38 39 45 public abstract class ContextSystemOption extends SystemOption implements BeanContextProxy { 46 47 private static Object ctxt = new Object (); 48 private static final long serialVersionUID = -781528552645947127L; 49 50 53 protected BeanContext beanContext; 54 55 56 public ContextSystemOption() { 57 beanContext = getBeanContext(); 59 } 60 61 64 public final void addOption(SystemOption so) { 65 getBeanContext().add(so); 66 } 67 68 71 public final void removeOption(SystemOption so) { 72 getBeanContext().remove(so); 73 } 74 75 78 public final SystemOption[] getOptions() { 79 int i; 83 84 int j; 88 SystemOption[] options; 89 90 Object [] objs = getBeanContext().toArray(); 91 92 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 112 public final BeanContextChild getBeanContextProxy() { 113 return getBeanContext(); 114 } 115 116 private BeanContext getBeanContext() { 117 return (BeanContext ) getProperty(ctxt); 118 } 119 120 protected void initialize() { 121 super.initialize(); 122 this.putProperty(ctxt, new OptionBeanContext(this)); 123 } 124 125 128 public void writeExternal(ObjectOutput out) throws IOException { 129 super.writeExternal(out); 130 131 Object [] 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 144 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 145 super.readExternal(in); 146 147 Object obj = in.readObject(); 148 149 if (obj instanceof BeanContext ) { 150 beanContext = (BeanContext ) obj; 153 } else { 154 BeanContext c = getBeanContext(); 156 157 while (obj != null) { 158 NbMarshalledObject m = (NbMarshalledObject) obj; 159 160 try { 164 c.add(m.get()); 165 } catch (Exception e) { 166 Logger.getLogger(ContextSystemOption.class.getName()).log(Level.WARNING, null, e); 167 } catch (LinkageError e) { 168 Logger.getLogger(ContextSystemOption.class.getName()).log(Level.WARNING, null, e); 169 } 170 171 obj = in.readObject(); 173 } 174 } 175 } 176 177 180 private static class ClassComparator implements Comparator { 181 ClassComparator() { 182 } 183 184 185 public int compare(Object o1, Object o2) { 186 return o1.getClass().getName().compareTo(o2.getClass().getName()); 187 } 188 } 189 190 194 private static class OptionBeanContext extends BeanContextSupport implements PropertyChangeListener { 195 private static final long serialVersionUID = 3532434266136225440L; 196 private ContextSystemOption parent = null; 197 198 public OptionBeanContext(ContextSystemOption p) { 199 parent = p; 200 } 201 202 205 public boolean add(Object targetChild) { 206 if (!(targetChild instanceof SystemOption)) { 207 throw new IllegalArgumentException ("Not a SystemOption: " + targetChild); } 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 targetChild) { 220 if (!(targetChild instanceof SystemOption)) { 221 throw new IllegalArgumentException ("Not a SystemOption: " + targetChild); } 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 evt) { 234 if (parent != null) { 235 parent.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue()); 236 } 237 } 238 } 239 } 240 | Popular Tags |