1 19 20 package org.openide.loaders; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.IOException ; 24 import java.lang.ref.Reference ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.text.MessageFormat ; 28 import java.util.ArrayList ; 29 import java.util.EventObject ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 import javax.swing.Action ; 39 import org.openide.actions.CopyAction; 40 import org.openide.actions.CutAction; 41 import org.openide.actions.DeleteAction; 42 import org.openide.actions.PasteAction; 43 import org.openide.actions.PropertiesAction; 44 import org.openide.actions.ToolsAction; 45 import org.openide.filesystems.FileEvent; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileStateInvalidException; 48 import org.openide.filesystems.Repository; 49 import org.openide.filesystems.URLMapper; 50 import org.openide.nodes.Children; 51 import org.openide.nodes.Node; 52 import org.openide.nodes.Node.Property; 53 import org.openide.nodes.Node.PropertySet; 54 import org.openide.nodes.PropertySupport; 55 import org.openide.nodes.Sheet; 56 import org.openide.util.HelpCtx; 57 import org.openide.util.actions.SystemAction; 58 59 63 final class BrokenDataShadow extends MultiDataObject { 64 65 private URL url; 66 67 72 public BrokenDataShadow ( 73 FileObject fo, MultiFileLoader loader 74 ) throws DataObjectExistsException { 75 super (fo, loader); 76 77 try { 78 url = DataShadow.readURL(fo); 79 } catch (IOException ex) { 80 try { 81 url = new URL ("file",null,"/UNKNOWN"); } catch (MalformedURLException ex2) { 83 Logger.getLogger(BrokenDataShadow.class.getName()).log(Level.WARNING, null, ex2); 84 } 85 } 86 enqueueBrokenDataShadow(this); 87 } 88 89 90 private static Map <String , Set <Reference <BrokenDataShadow>>> allDataShadows; 91 92 private static final long serialVersionUID = -3046981691235483810L; 93 94 95 static synchronized Map <String , Set <Reference <BrokenDataShadow>>> getDataShadowsSet() { 96 if (allDataShadows == null) { 97 allDataShadows = new HashMap <String , Set <Reference <BrokenDataShadow>>>(); 98 } 99 return allDataShadows; 100 } 101 102 private static synchronized void enqueueBrokenDataShadow(BrokenDataShadow ds) { 103 Map <String , Set <Reference <BrokenDataShadow>>> m = getDataShadowsSet (); 104 105 String prim = ds.getUrl().toExternalForm(); 106 Reference <BrokenDataShadow> ref = new DataShadow.DSWeakReference<BrokenDataShadow>(ds); 107 Set <Reference <BrokenDataShadow>> s = m.get (prim); 108 if (s == null) { 109 s = java.util.Collections.<Reference <BrokenDataShadow>>singleton (ref); 110 getDataShadowsSet ().put (prim, s); 111 } else { 112 if (! (s instanceof HashSet )) { 113 s = new HashSet <Reference <BrokenDataShadow>> (s); 114 getDataShadowsSet ().put (prim, s); 115 } 116 s.add (ref); 117 } 118 } 119 120 121 private static synchronized List <BrokenDataShadow> getAllDataShadows() { 122 if (allDataShadows == null || allDataShadows.isEmpty()) { 123 return null; 124 } 125 126 List <BrokenDataShadow> ret = new ArrayList <BrokenDataShadow>(allDataShadows.size()); 127 Iterator <Set <Reference <BrokenDataShadow>>> it = allDataShadows.values ().iterator(); 128 while (it.hasNext()) { 129 Set <Reference <BrokenDataShadow>> ref = it.next(); 130 Iterator <Reference <BrokenDataShadow>> refs = ref.iterator (); 131 while (refs.hasNext ()) { 132 Reference <BrokenDataShadow> r = refs.next (); 133 BrokenDataShadow shadow = r.get(); 134 if (shadow != null) { 135 ret.add(shadow); 136 } 137 } 138 } 139 140 return ret; 141 } 142 143 146 static void checkValidity(EventObject ev) { 147 DataObject src = null; 148 if (ev instanceof OperationEvent) { 149 src = ((OperationEvent)ev).getObject(); 150 } 151 152 FileObject file; 153 if (src != null) { 154 file = src.getPrimaryFile (); 155 } else { 156 if (ev instanceof FileEvent) { 157 file = ((FileEvent)ev).getFile(); 158 } else { 159 return; 160 } 161 } 162 163 try { 165 if (!file.getFileSystem().equals( 166 Repository.getDefault().getDefaultFileSystem())) { 167 return; 168 } 169 } catch (FileStateInvalidException e) { 170 DataObject.LOG.log(Level.WARNING, e.toString(), e); 172 return; 173 } 174 175 synchronized (BrokenDataShadow.class) { 176 if (allDataShadows == null || allDataShadows.isEmpty ()) return; 177 } 178 179 String key; 180 try { 181 key = file.getURL().toExternalForm(); 182 } catch (FileStateInvalidException ex) { 183 return; 185 } 186 187 Set shadows = null; 188 synchronized (BrokenDataShadow.class) { 189 if (allDataShadows == null || allDataShadows.isEmpty ()) return; 190 191 if (src != null) { 192 shadows = (Set )allDataShadows.get(key); 193 if (shadows == null) { 194 return; 197 } 198 } 199 } 200 201 202 List all = getAllDataShadows(); 203 if (all == null) { 204 return; 205 } 206 207 int size = all.size(); 208 for (int i = 0; i < size; i++) { 209 Object obj = all.get(i); 210 ((BrokenDataShadow) obj).refresh(); 211 } 212 } 213 214 217 private BrokenDataShadow (FileObject fo) throws DataObjectExistsException { 218 this (fo, (MultiFileLoader)DataLoaderPool.getShadowLoader ()); 219 } 220 221 224 public boolean isDeleteAllowed() { 225 return getPrimaryFile().canWrite(); 226 } 227 228 229 public void refresh() { 230 try { 231 if (URLMapper.findFileObject(getUrl()) != null) { 232 233 this.setValid(false); 234 } 235 } catch (PropertyVetoException e) { 236 e.printStackTrace(); 237 } 238 } 239 240 243 public boolean isCopyAllowed() { 244 return true; 245 } 246 247 250 public boolean isMoveAllowed() { 251 return getPrimaryFile().canWrite(); 252 } 253 254 257 public boolean isRenameAllowed () { 258 return getPrimaryFile().canWrite(); 259 } 260 261 264 public HelpCtx getHelpCtx() { 265 return HelpCtx.DEFAULT_HELP; 266 } 267 268 270 protected Node createNodeDelegate () { 271 return new BrokenShadowNode (this); 272 } 273 274 URL getUrl() { 275 return url; 276 } 277 278 279 private static final class BrokenShadowNode extends DataNode { 280 281 282 private static MessageFormat format; 283 284 285 private Sheet sheet; 286 287 private static final String ICON_NAME = "org/openide/loaders/brokenShadow.gif"; 289 292 public BrokenShadowNode (BrokenDataShadow par) { 293 super (par,Children.LEAF); 294 setIconBaseWithExtension(ICON_NAME); 295 } 296 297 301 public String getDisplayName () { 302 if (format == null) { 303 format = new MessageFormat (DataObject.getString ("FMT_brokenShadowName")); 304 } 305 return format.format (createArguments ()); 306 } 307 308 public Action [] getActions(boolean context) { 309 return new Action [] { 310 SystemAction.get (CutAction.class), 311 SystemAction.get (CopyAction.class), 312 SystemAction.get (PasteAction.class), 313 null, 314 SystemAction.get (DeleteAction.class), 315 null, 316 SystemAction.get (ToolsAction.class), 317 SystemAction.get (PropertiesAction.class) 318 }; 319 } 320 321 324 public PropertySet[] getPropertySets () { 325 if (sheet == null) { 326 sheet = cloneSheet (); 327 } 328 return sheet.toArray (); 329 } 330 331 333 private Sheet cloneSheet () { 334 PropertySet[] sets = super.getPropertySets (); 335 336 Sheet s = new Sheet (); 337 for (int i = 0; i < sets.length; i++) { 338 Sheet.Set ss = new Sheet.Set (); 339 ss.put (sets[i].getProperties ()); 340 ss.setName (sets[i].getName ()); 341 ss.setDisplayName (sets[i].getDisplayName ()); 342 ss.setShortDescription (sets[i].getShortDescription ()); 343 344 modifySheetSet (ss); 346 347 s.put (ss); 348 } 349 350 return s; 351 } 352 353 356 private void modifySheetSet (Sheet.Set ss) { 357 Property p = ss.remove (DataObject.PROP_NAME); 358 if (p != null) { 359 p = new PropertySupport.Name (this); 360 ss.put (p); 361 362 p = new Name (); 363 ss.put (p); 364 } 365 } 366 367 368 private Object [] createArguments () { 369 return new Object [] { 370 getDataObject().getName () 371 }; 372 } 373 374 376 private final class Name extends PropertySupport.ReadWrite<String > { 377 378 public Name () { 379 super ( 380 "BrokenLink", String .class, 382 DataObject.getString ("PROP_brokenShadowOriginalName"), 383 DataObject.getString ("HINT_brokenShadowOriginalName") 384 ); 385 } 386 387 388 public String getValue () { 389 BrokenDataShadow bds = (BrokenDataShadow)getDataObject(); 390 return bds.getUrl().toExternalForm(); 391 } 392 393 394 public void setValue (String newLink) { 395 BrokenDataShadow bds = (BrokenDataShadow)getDataObject(); 396 try { 397 URL u = new URL (newLink); 398 DataShadow.writeOriginal(bds.getPrimaryFile(), u); 399 bds.url = u; 400 } catch (IOException ex) { 401 throw (IllegalArgumentException ) new IllegalArgumentException (ex.toString()).initCause(ex); 402 } 403 bds.refresh (); 404 } 405 } 406 407 } 408 } 409 | Popular Tags |