1 19 package org.openide.windows; 20 21 import org.openide.awt.StatusDisplayer; 22 import org.openide.util.*; 23 24 import java.beans.*; 25 26 27 34 public abstract class CloneableOpenSupport extends Object { 35 private static java.awt.Container container; 36 37 38 protected Env env; 39 40 46 protected CloneableTopComponent.Ref allEditors; 47 48 51 public CloneableOpenSupport(Env env) { 52 this.env = env; 53 54 Listener l = new Listener(env); 55 this.allEditors = l; 56 57 env.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(l, env)); 59 60 env.addVetoableChangeListener(org.openide.util.WeakListeners.vetoableChange(l, env)); 62 } 63 64 71 public void open() { 72 Mutex.EVENT.writeAccess( 74 new Runnable () { 75 public void run() { 76 CloneableTopComponent editor = openCloneableTopComponent(); 77 editor.requestActive(); 78 } 79 } 80 ); 81 } 82 83 87 public void view() { 88 open(); 89 } 90 91 95 public void edit() { 96 open(); 97 } 98 99 103 public boolean close() { 104 return close(true); 105 } 106 107 111 protected boolean close(final boolean ask) { 112 if (allEditors.isEmpty()) { 113 return true; 114 } 115 116 Boolean ret = Mutex.EVENT.writeAccess( 119 new Mutex.Action<Boolean >() { 120 public Boolean run() { 121 synchronized (getLock()) { 123 if (ask && !canClose()) { 125 return Boolean.FALSE; 126 } 127 128 java.util.Enumeration en = allEditors.getComponents(); 129 130 while (en.hasMoreElements()) { 131 TopComponent c = (TopComponent) en.nextElement(); 132 133 if (!c.close()) { 134 return Boolean.FALSE; 135 } 136 } 137 } 138 139 return Boolean.TRUE; 140 } 141 } 142 ); 143 144 return ret.booleanValue(); 145 } 146 147 153 protected boolean canClose() { 154 return true; 155 } 156 157 158 protected final CloneableTopComponent openCloneableTopComponent() { 159 synchronized (getLock()) { 161 CloneableTopComponent ret = allEditors.getArbitraryComponent(); 162 163 if (ret != null) { 164 ret.open(); 165 166 return ret; 167 } else { 168 String msg = messageOpening(); 170 171 if (msg != null) { 172 StatusDisplayer.getDefault().setStatusText(msg); 173 } 174 175 CloneableTopComponent editor = createCloneableTopComponent(); 176 editor.setReference(allEditors); 177 editor.open(); 178 179 msg = messageOpened(); 180 181 if (msg == null) { 182 msg = ""; } 184 185 StatusDisplayer.getDefault().setStatusText(msg); 186 187 return editor; 188 } 189 } 190 } 191 192 193 private Object getLock() { 194 if (container == null) { 195 container = new java.awt.Container (); 196 } 197 198 return container.getTreeLock(); 199 } 200 201 204 protected abstract CloneableTopComponent createCloneableTopComponent(); 205 206 209 protected abstract String messageOpening(); 210 211 214 protected abstract String messageOpened(); 215 216 219 public static interface Env extends java.io.Serializable { 220 223 public static final String PROP_VALID = "valid"; 225 228 public static final String PROP_MODIFIED = "modified"; 230 232 public void addPropertyChangeListener(PropertyChangeListener l); 233 234 236 public void removePropertyChangeListener(PropertyChangeListener l); 237 238 240 public void addVetoableChangeListener(VetoableChangeListener l); 241 242 244 public void removeVetoableChangeListener(VetoableChangeListener l); 245 246 252 public boolean isValid(); 253 254 257 public boolean isModified(); 258 259 264 public void markModified() throws java.io.IOException ; 265 266 269 public void unmarkModified(); 270 271 274 public CloneableOpenSupport findCloneableOpenSupport(); 275 } 276 277 280 private static final class Listener extends CloneableTopComponent.Ref implements PropertyChangeListener, 281 VetoableChangeListener, Runnable { 282 283 static final long serialVersionUID = -1934890789745432531L; 284 285 286 private Env env; 287 288 290 public Listener(Env env) { 291 this.env = env; 292 } 293 294 297 private CloneableOpenSupport support() { 298 return env.findCloneableOpenSupport(); 299 } 300 301 public void propertyChange(PropertyChangeEvent ev) { 302 if (Env.PROP_VALID.equals(ev.getPropertyName())) { 303 if (Boolean.FALSE.equals(ev.getOldValue())) { 305 return; 306 } 307 308 Mutex.EVENT.readAccess(this); 309 } 310 } 311 312 314 public void run() { 315 CloneableOpenSupport os = support(); 317 318 if (os != null) { 319 env.unmarkModified(); 322 323 os.close(false); 324 } 325 } 326 327 332 public void vetoableChange(PropertyChangeEvent ev) 333 throws PropertyVetoException { 334 if (Env.PROP_VALID.equals(ev.getPropertyName())) { 335 if (Boolean.FALSE.equals(ev.getOldValue())) { 337 return; 338 } 339 340 if (env.isModified()) { 341 CloneableOpenSupport os = support(); 343 344 if ((os != null) && !os.canClose()) { 345 throw new PropertyVetoException( 347 "", ev ); 350 } 351 } 352 } 353 } 354 355 359 public Object readResolve() { 360 CloneableOpenSupport os = support(); 361 362 if (os == null) { 363 return this; 365 } 366 367 return os.allEditors; 369 } 370 } 371 } 372 | Popular Tags |