1 19 20 package org.netbeans.modules.properties; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.OutputStream ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 import org.openide.filesystems.FileLock; 32 import org.openide.filesystems.FileObject; 33 import org.openide.loaders.MultiDataObject; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.CookieSet; 36 import org.openide.nodes.Node; 37 import org.openide.NotifyDescriptor; 38 import org.openide.DialogDisplayer; 39 import org.openide.util.HelpCtx; 40 import org.openide.util.NbBundle; 41 42 49 public class PropertiesFileEntry extends PresentableFileEntry 50 implements CookieSet.Factory { 51 52 53 private String basicName; 54 55 56 private transient StructHandler propStruct; 57 58 59 private transient PropertiesEditorSupport editorSupport; 60 61 62 static final long serialVersionUID = -3882240297814143015L; 63 64 65 71 PropertiesFileEntry(MultiDataObject obj, FileObject file) { 72 super(obj, file); 73 FileObject fo = getDataObject().getPrimaryFile(); 74 if (fo == null) 75 basicName = getFile().getName(); 77 else 78 basicName = fo.getName(); 79 80 getCookieSet().add(PropertiesEditorSupport.class, this); 81 } 82 83 84 88 public FileObject copy(FileObject folder, String suffix) throws IOException { 89 String pasteSuffix = ((PropertiesDataObject)getDataObject()).getPasteSuffix(); 90 91 if(pasteSuffix == null) 92 return super.copy(folder, suffix); 93 94 FileObject fileObject = getFile(); 95 96 String basicName = getDataObject().getPrimaryFile().getName(); 97 String newName = basicName + pasteSuffix + Util.getLocaleSuffix(this); 98 99 return fileObject.copy(folder, newName, fileObject.getExt()); 100 } 101 102 103 public void delete() throws IOException { 104 getHandler().stopParsing(); 105 106 try { 107 super.delete(); 108 } finally { 109 getHandler().allowParsing(); 111 } 112 } 113 114 118 public FileObject move(FileObject folder, String suffix) throws IOException { 119 String pasteSuffix = ((PropertiesDataObject)getDataObject()).getPasteSuffix(); 120 121 if(pasteSuffix == null) 122 return super.move(folder, suffix); 123 124 FileObject fileObject = getFile(); 125 FileLock lock = takeLock (); 126 127 try { 128 String basicName = getDataObject().getPrimaryFile().getName(); 129 String newName = basicName + pasteSuffix + Util.getLocaleSuffix(this); 130 131 return fileObject.move (lock, folder, newName, fileObject.getExt()); 132 } finally { 133 lock.releaseLock (); 134 } 135 } 136 137 138 @SuppressWarnings ("unchecked") 139 public <T extends Node.Cookie> T createCookie(Class <T> clazz) { 140 if (clazz.isAssignableFrom(PropertiesEditorSupport.class)) { 141 return (T) getPropertiesEditor(); 142 } else { 143 return null; 144 } 145 } 146 147 148 protected Node createNodeDelegate() { 149 return new PropertiesLocaleNode(this); 150 } 151 152 153 public Children getChildren() { 154 return new PropKeysChildren(); 155 } 156 157 159 public StructHandler getHandler() { 160 if (propStruct == null) { 161 propStruct = new StructHandler(this); 162 } 163 return propStruct; 164 } 165 166 167 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 168 in.defaultReadObject(); 169 } 170 171 173 protected PropertiesEditorSupport getPropertiesEditor() { 174 getDataObject().getCookie(PropertiesOpen.class); 177 178 if(editorSupport == null) { 179 synchronized(this) { 180 if(editorSupport == null) 181 editorSupport = new PropertiesEditorSupport(this); 182 } 183 } 184 185 return editorSupport; 186 } 187 188 194 public FileObject rename (String name) throws IOException { 195 196 if (!getFile().getName().startsWith(basicName)) 197 throw new IllegalStateException ("Resource Bundles: error in Properties loader/rename."); 199 FileObject fo = super.rename(name + getFile().getName().substring(basicName.length())); 200 basicName = name; 201 return fo; 202 } 203 204 210 public FileObject renameEntry (String name) throws IOException { 211 212 if (!getFile().getName().startsWith(basicName)) 213 throw new IllegalStateException ("Resource Bundles: error in Properties loader / rename"); 215 if (basicName.equals(getFile().getName())) { 216 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 218 NbBundle.getBundle(PropertiesDataLoader.class).getString("MSG_AttemptToRenamePrimaryFile"), 219 NotifyDescriptor.ERROR_MESSAGE); 220 DialogDisplayer.getDefault().notify(msg); 221 return getFile(); 222 } 223 224 FileObject fo = super.rename(name); 225 226 ((PropertiesDataObject)getDataObject()).getBundleStructure().notifyOneFileChanged(getHandler()); 228 229 return fo; 230 } 231 232 public FileObject createFromTemplate (FileObject folder, String name) throws IOException { 233 if (!getFile().getName().startsWith(basicName)) 234 throw new IllegalStateException ("Resource Bundles: error in Properties createFromTemplate"); 236 String suffix = getFile ().getName ().substring (basicName.length ()); 237 String nuename = name + suffix; 238 String ext = getFile ().getExt (); 239 FileObject existing = folder.getFileObject (nuename, ext); 240 if (existing == null) { 241 return super.createFromTemplate (folder, nuename); 242 } else { 243 { byte[] originalData; 251 byte[] buf = new byte[4096]; 252 int count; 253 FileLock lock = existing.lock (); 254 try { 255 InputStream is = existing.getInputStream (); 256 try { 257 ByteArrayOutputStream baos = new ByteArrayOutputStream ((int) existing.getSize ()); 258 try { 259 while ((count = is.read (buf)) != -1) { 260 baos.write (buf, 0, count); 261 } 262 } finally { 263 originalData = baos.toByteArray (); 264 baos.close (); 265 } 266 } finally { 267 is.close (); 268 } 269 existing.delete (lock); 270 } finally { 271 lock.releaseLock (); 272 } 273 FileObject nue = folder.createData (nuename, ext); 274 lock = nue.lock (); 275 try { 276 OutputStream os = nue.getOutputStream (lock); 277 try { 278 os.write (originalData); 279 InputStream is = getFile ().getInputStream (); 280 try { 281 while ((count = is.read (buf)) != -1) { 282 os.write (buf, 0, count); 283 } 284 } finally { 285 is.close (); 286 } 287 } finally { 288 os.close (); 289 } 290 } finally { 291 lock.releaseLock (); 292 } 293 return nue; 297 } 298 } 299 } 300 301 304 public boolean isDeleteAllowed() { 305 return (getFile ().canWrite ()) && (!basicName.equals(getFile().getName())); 307 } 308 309 312 public boolean isCopyAllowed() { 313 return true; 314 } 315 318 320 public boolean isMoveAllowed() { 321 return (getFile().canWrite()) && (getDataObject().getPrimaryEntry() != this); 322 } 323 324 327 public boolean isRenameAllowed () { 328 return getFile ().canWrite (); 329 } 330 331 334 public HelpCtx getHelpCtx() { 335 return new HelpCtx(Util.HELP_ID_CREATING); 336 } 337 338 339 341 private class PropKeysChildren extends Children.Keys<String > { 342 343 344 private PropertyBundleListener bundleListener = null; 345 346 347 348 PropKeysChildren() { 349 super(); 350 } 351 352 353 355 private void mySetKeys() { 356 Set <String > keys = new TreeSet <String >(new KeyComparator()); 358 PropertiesStructure propStructure = getHandler().getStructure(); 359 if (propStructure != null) { 360 for (Iterator <Element.ItemElem> iterator = propStructure.allItems(); iterator.hasNext(); ) { 361 Element.ItemElem item = iterator.next(); 362 if (item != null && item.getKey() != null) { 363 keys.add(item.getKey()); 364 } 365 } 366 } 367 368 setKeys(keys); 369 } 370 371 374 protected void addNotify () { 375 mySetKeys(); 376 377 bundleListener = new PropertyBundleListener () { 378 public void bundleChanged(PropertyBundleEvent evt) { 379 int changeType = evt.getChangeType(); 380 381 if(changeType == PropertyBundleEvent.CHANGE_STRUCT 382 || changeType == PropertyBundleEvent.CHANGE_ALL) { 383 mySetKeys(); 384 } else if(changeType == PropertyBundleEvent.CHANGE_FILE 385 && evt.getEntryName().equals(getFile().getName())) { 386 387 mySetKeys(); 389 } 390 } 391 }; 393 bundleStructure().addPropertyBundleListener(bundleListener); 394 } 395 396 400 protected void removeNotify () { 401 bundleStructure().removePropertyBundleListener(bundleListener); 402 setKeys(new ArrayList <String >()); 403 } 404 405 406 protected Node[] createNodes (String itemKey) { 407 return new Node[] { new KeyNode(getHandler().getStructure(), itemKey) }; 408 } 409 410 411 private BundleStructure bundleStructure() { 412 return ((PropertiesDataObject)PropertiesFileEntry.this.getDataObject()).getBundleStructure(); 413 } 414 } 416 } 417 | Popular Tags |