1 19 20 package org.netbeans.modules.masterfs.filebasedfs.naming; 21 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import org.netbeans.modules.masterfs.providers.ProvidedExtensions; 26 import org.openide.util.Exceptions; 27 28 31 public class FileName implements FileNaming { 32 private String name; 33 private final FileNaming parent; 34 private Integer id; 35 36 protected FileName(final FileNaming parent, final File file) { 37 this.parent = parent; 38 this.name = parseName(file); 39 id = NamingFactory.createID(file); 40 } 41 42 private static String parseName(final File file) { 43 return (file.getParentFile() == null) ? file.getPath() : file.getName(); 44 } 45 46 public boolean rename(String name, ProvidedExtensions.IOHandler handler) { 47 boolean retVal = false; 48 final File f = getFile(); 49 50 if (f.exists()) { 51 File newFile = new File (f.getParentFile(), name); 52 if (handler != null) { 53 try { 54 handler.handle(); 55 retVal = true; 56 } catch (IOException ex) { 57 Exceptions.printStackTrace(ex); 58 } 59 } else { 60 retVal = f.renameTo(newFile); 61 } 62 if (retVal) { 63 this.name = name; 64 Integer iid = NamingFactory.createID(newFile); 65 if (!iid.equals(id)) { 66 id = iid; 67 } 68 } 69 } 70 FolderName.freeCaches(); 71 return retVal; 72 } 73 74 public final boolean rename(final String name) { 75 return rename(name, null); 76 } 77 78 public final boolean isRoot() { 79 return (getParent() == null); 80 } 81 82 83 public File getFile() { 84 final FileNaming parent = this.getParent(); 85 return (parent != null) ? new File (parent.getFile(), getName()) : new File (getName()); 86 } 87 88 89 public final String getName() { 90 return name; 91 } 92 93 public FileNaming getParent() { 94 return parent; 95 } 96 97 public final Integer getId() { 98 return getId(false); 99 } 100 101 public Integer getId(boolean recompute) { 102 if (recompute) { 103 id = NamingFactory.createID(getFile()); 104 } 105 return id; 106 } 107 108 public final boolean equals(final Object obj) { 109 return (obj instanceof FileNaming && obj.hashCode() == hashCode()); 110 } 111 112 113 public final String toString() { 114 return getFile().getAbsolutePath(); 115 } 116 117 public final int hashCode() { 118 return id.intValue(); 119 } 120 121 public boolean isFile() { 122 return true; 123 } 124 125 public boolean isDirectory() { 126 return !isFile(); 127 } 128 } 129 | Popular Tags |