1 19 20 package org.openide.loaders; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.beans.PropertyVetoException ; 26 import java.beans.VetoableChangeListener ; 27 import java.beans.VetoableChangeSupport ; 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.lang.ref.Reference ; 31 import java.lang.ref.WeakReference ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.WeakHashMap ; 37 import org.openide.cookies.EditCookie; 38 import org.openide.cookies.EditorCookie; 39 import org.openide.cookies.OpenCookie; 40 import org.openide.filesystems.FileStateInvalidException; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.util.NbBundle; 43 import org.openide.util.WeakSet; 44 import org.openide.windows.CloneableOpenSupport; 45 import org.openide.windows.CloneableTopComponent; 46 47 54 public abstract class OpenSupport extends CloneableOpenSupport { 55 56 protected MultiDataObject.Entry entry; 57 58 62 public OpenSupport (MultiDataObject.Entry entry) { 63 this (entry, new Env (entry.getDataObject ())); 64 } 65 66 72 protected OpenSupport (MultiDataObject.Entry entry, Env env) { 73 super (env); 74 this.entry = entry; 75 } 76 77 78 81 protected String messageOpening () { 82 DataObject obj = entry.getDataObject (); 83 84 return NbBundle.getMessage (OpenSupport.class , "CTL_ObjectOpen", obj.getName(), 86 obj.getPrimaryFile().toString() 87 ); 88 } 89 90 91 94 protected String messageOpened () { 95 return NbBundle.getMessage (OpenSupport.class, "CTL_ObjectOpened"); 96 } 97 98 99 101 final CloneableTopComponent.Ref allEditors () { 102 return allEditors; 103 } 104 105 107 public static class Env extends Object 108 implements CloneableOpenSupport.Env, java.io.Serializable , 109 PropertyChangeListener , VetoableChangeListener { 110 111 static final long serialVersionUID = -1934890789745432531L; 112 113 private DataObject obj; 114 115 117 private transient PropertyChangeSupport propSupp; 118 120 private transient VetoableChangeSupport vetoSupp; 121 122 125 private static final Map <FileSystem, Reference <FileSystemNameListener>> fsListenerMap = 126 new WeakHashMap <FileSystem, Reference <FileSystemNameListener>>(30); 127 128 private static final Object LOCK_SUPPORT = new Object (); 130 131 137 public Env (DataObject obj) { 138 this.obj = obj; 139 init(); 140 } 141 142 private void readObject (ObjectInputStream ois) 143 throws IOException , ClassNotFoundException { 144 ois.defaultReadObject(); 145 init(); 146 } 147 148 private void init() { 149 obj.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, obj)); 150 151 final FileSystem fs; 154 try { 155 fs = obj.getPrimaryFile().getFileSystem(); 156 } catch(FileStateInvalidException fsie) { 157 throw (IllegalStateException ) new IllegalStateException ("FileSystem is invalid for " + obj.getPrimaryFile() + "!").initCause(fsie); } 159 160 FileSystemNameListener fsListener; 161 boolean initListening = false; 162 synchronized(fsListenerMap) { 163 Reference <FileSystemNameListener> fsListenerRef = fsListenerMap.get(fs); 164 fsListener = fsListenerRef == null 165 ? null 166 : fsListenerRef.get(); 167 168 if(fsListener == null) { 169 fsListener = new FileSystemNameListener(); 171 fsListenerMap.put(fs, new WeakReference <FileSystemNameListener>(fsListener)); 172 initListening = true; 173 } 174 } 175 176 if(initListening) { 177 fs.addPropertyChangeListener(fsListener); 178 fs.addVetoableChangeListener(fsListener); 179 } 180 181 fsListener.add(this); 182 } 184 185 187 protected final DataObject getDataObject () { 188 return obj; 189 } 190 191 193 public void addPropertyChangeListener(PropertyChangeListener l) { 194 prop ().addPropertyChangeListener (l); 195 } 196 197 199 public void removePropertyChangeListener(PropertyChangeListener l) { 200 prop ().removePropertyChangeListener (l); 201 } 202 203 205 public void addVetoableChangeListener(VetoableChangeListener l) { 206 veto ().addVetoableChangeListener (l); 207 } 208 209 211 public void removeVetoableChangeListener(VetoableChangeListener l) { 212 veto ().removeVetoableChangeListener (l); 213 } 214 215 221 public boolean isValid () { 222 return getDataObject ().isValid (); 223 } 224 225 228 public boolean isModified() { 229 return getDataObject ().isModified (); 230 } 231 232 237 public void markModified() throws java.io.IOException { 238 getDataObject ().setModified (true); 239 } 240 241 244 public void unmarkModified() { 245 getDataObject ().setModified (false); 246 } 247 248 254 public CloneableOpenSupport findCloneableOpenSupport() { 255 OpenCookie oc = getDataObject().getCookie(OpenCookie.class); 256 if (oc != null && oc instanceof CloneableOpenSupport) { 257 return (CloneableOpenSupport) oc; 258 } 259 EditCookie edc = getDataObject().getCookie(EditCookie.class); 260 if (edc != null && edc instanceof CloneableOpenSupport) { 261 return (CloneableOpenSupport) edc; 262 } 263 EditorCookie ec = getDataObject().getCookie(EditorCookie.class); 264 if (ec != null && ec instanceof CloneableOpenSupport) { 265 return (CloneableOpenSupport) ec; 266 } 267 return null; 268 } 269 270 273 public void propertyChange(PropertyChangeEvent ev) { 274 if (DataObject.PROP_MODIFIED.equals (ev.getPropertyName())) { 275 if (getDataObject ().isModified ()) { 276 getDataObject ().addVetoableChangeListener(this); 277 } else { 278 getDataObject ().removeVetoableChangeListener(this); 279 } 280 } 281 282 firePropertyChange ( 283 ev.getPropertyName (), 284 ev.getOldValue (), 285 ev.getNewValue () 286 ); 287 } 288 289 291 public void vetoableChange(PropertyChangeEvent ev) throws PropertyVetoException { 292 fireVetoableChange ( 293 ev.getPropertyName (), 294 ev.getOldValue (), 295 ev.getNewValue () 296 ); 297 } 298 299 304 protected void firePropertyChange (String name, Object oldValue, Object newValue) { 305 prop ().firePropertyChange (name, oldValue, newValue); 306 } 307 308 313 protected void fireVetoableChange (String name, Object oldValue, Object newValue) 314 throws PropertyVetoException { 315 veto ().fireVetoableChange (name, oldValue, newValue); 316 } 317 318 320 private PropertyChangeSupport prop () { 321 synchronized (LOCK_SUPPORT) { 322 if (propSupp == null) { 323 propSupp = new PropertyChangeSupport (this); 324 } 325 return propSupp; 326 } 327 } 328 329 331 private VetoableChangeSupport veto () { 332 synchronized (LOCK_SUPPORT) { 333 if (vetoSupp == null) { 334 vetoSupp = new VetoableChangeSupport (this); 335 } 336 return vetoSupp; 337 } 338 } 339 } 340 341 342 343 private static final class FileSystemNameListener 344 implements PropertyChangeListener , VetoableChangeListener { 345 346 private final Set <Env> environments = new WeakSet<Env>(30); 347 348 public FileSystemNameListener() { 349 } 350 351 352 public void add(Env env) { 353 synchronized(environments) { 354 environments.add(env); 355 } 356 } 357 358 public void propertyChange(PropertyChangeEvent evt) { 359 if(FileSystem.PROP_SYSTEM_NAME.equals(evt.getPropertyName())) { 360 Set <Env> envs; 361 synchronized(environments) { 362 envs = new HashSet <Env>(environments); 363 } 364 365 for(Env env: envs){ 366 env.firePropertyChange(DataObject.PROP_VALID, 367 Boolean.TRUE, Boolean.FALSE); 368 } 369 } 370 } 371 372 public void vetoableChange(PropertyChangeEvent evt) 373 throws PropertyVetoException { 374 if(FileSystem.PROP_SYSTEM_NAME.equals(evt.getPropertyName())) { 375 Set <Env> envs; 376 synchronized(environments) { 377 envs = new HashSet <Env>(environments); 378 } 379 380 for(Env env: envs) { 381 env.fireVetoableChange(DataObject.PROP_VALID, 382 Boolean.TRUE, Boolean.FALSE); 383 } 384 } 385 } 386 } 388 390 private static final class Listener extends CloneableTopComponent.Ref { 391 392 static final long serialVersionUID = -1934890789745432531L; 393 394 private MultiDataObject.Entry entry; 395 396 Listener() {} 397 398 public Object readResolve () { 399 DataObject obj = entry.getDataObject (); 400 OpenSupport os = null; 401 OpenCookie oc = obj.getCookie(OpenCookie.class); 402 if (oc != null && oc instanceof OpenSupport) { 403 os = (OpenSupport) oc; 404 } else { 405 EditCookie edc = obj.getCookie(EditCookie.class); 406 if (edc != null && edc instanceof OpenSupport) { 407 os = (OpenSupport) edc; 408 } else { 409 EditorCookie ec = obj.getCookie(EditorCookie.class); 410 if (ec != null && ec instanceof OpenSupport) { 411 os = (OpenSupport) ec; 412 } 413 } 414 } 415 if (os == null) { 416 return this; 418 } 419 return os.allEditors (); 421 } 422 } 423 } 424 425 | Popular Tags |