1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.ObjectInputStream ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.Comparator ; 30 import java.util.Iterator ; 31 import java.util.TreeSet ; 32 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.*; 35 import org.openide.nodes.Children; 36 import org.openide.nodes.CookieSet; 37 import org.openide.nodes.Node; 38 import org.openide.util.WeakListeners; 39 40 41 47 public final class PropertiesDataObject extends MultiDataObject implements CookieSet.Factory { 48 49 50 static final long serialVersionUID = 4795737295255253334L; 51 52 53 private transient BundleStructure bundleStructure; 54 55 56 private transient PropertiesOpen openSupport; 57 58 59 private final transient Object OPEN_SUPPORT_LOCK = new Object (); 60 61 63 private transient String pasteSuffix; 64 65 66 76 public PropertiesDataObject(final FileObject primaryFile, 77 final MultiFileLoader loader) 78 throws DataObjectExistsException { 79 super(primaryFile, loader); 80 initialize(); 82 } 83 84 85 86 private void initialize() { 87 bundleStructure = null; 88 Class <? extends Node.Cookie>[] arr = (Class <Node.Cookie>[]) new Class [2]; 89 arr[0] = PropertiesOpen.class; 90 arr[1] = PropertiesEditorSupport.class; 91 getCookieSet().add(arr, this); 92 } 93 94 95 @SuppressWarnings ("unchecked") 96 public <T extends Node.Cookie> T createCookie(Class <T> clazz) { 97 if(clazz.isAssignableFrom(PropertiesOpen.class)) { 98 return (T) getOpenSupport(); 99 } else if(clazz.isAssignableFrom(PropertiesEditorSupport.class)) { 100 return (T) ((PropertiesFileEntry)getPrimaryEntry()).getPropertiesEditor(); 101 } else { 102 return null; 103 } 104 } 105 106 CookieSet getCookieSet0() { 108 return getCookieSet(); 109 } 110 111 117 protected synchronized DataObject handleCopy(DataFolder df) throws IOException { 118 try { 119 pasteSuffix = createPasteSuffix(df); 120 121 return super.handleCopy(df); 122 } finally { 123 pasteSuffix = null; 124 } 125 } 126 127 133 protected FileObject handleMove(DataFolder df) throws IOException { 134 try { 135 pasteSuffix = createPasteSuffix(df); 136 137 return super.handleMove(df); 138 } finally { 139 pasteSuffix = null; 140 } 141 } 142 143 144 String getPasteSuffix() { 145 return pasteSuffix; 146 } 147 148 151 void removeSecondaryEntry2(Entry fe) { 152 removeSecondaryEntry (fe); 153 } 154 155 157 private String createPasteSuffix(DataFolder folder) { 158 String basicName = getPrimaryFile().getName(); 159 160 DataObject[] children = folder.getChildren(); 161 162 163 for(int i = 0; ; i++) { 165 String newName; 166 167 if (i == 0) { 168 newName = basicName; 169 } else { 170 newName = basicName + i; 171 } 172 boolean exist = false; 173 174 for(int j = 0; j < children.length; j++) { 175 if(children[j] instanceof PropertiesDataObject && newName.equals(children[j].getName())) { 176 exist = true; 177 break; 178 } 179 } 180 181 if(!exist) { 182 if (i == 0) { 183 return ""; } else { 185 return "" + i; } 187 } 188 } 189 } 190 191 192 public PropertiesOpen getOpenSupport() { 193 synchronized(OPEN_SUPPORT_LOCK) { 194 if(openSupport == null) { 195 openSupport = new PropertiesOpen(this); 196 } 197 198 return openSupport; 199 } 200 } 201 202 203 void updateModificationStatus() { 204 boolean modif = false; 205 if (((PresentableFileEntry)getPrimaryEntry()).isModified()) 206 modif = true; 207 else { 208 for (Iterator it = secondaryEntries().iterator(); it.hasNext(); ) { 209 if (((PresentableFileEntry)it.next()).isModified()) { 210 modif = true; 211 break; 212 } 213 } 214 } 215 216 super.setModified(modif); 217 } 218 219 229 protected Node createNodeDelegate () { 230 PropertiesChildren pc = new PropertiesChildren(); 231 232 DataNode dn = new PropertiesDataNode(this, pc); 234 return dn; 235 } 236 237 238 public BundleStructure getBundleStructure() { 239 if (bundleStructure == null) 240 bundleStructure = new BundleStructure(this); 241 return bundleStructure; 242 } 243 244 245 public static Comparator <String > getSecondaryFilesComparator() { 246 return new KeyComparator(); 247 } 248 249 251 void fireNameChange() { 252 firePropertyChange(PROP_NAME, null, null); 253 } 254 255 256 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 257 in.defaultReadObject(); 258 initialize(); 259 } 260 261 262 263 private class PropertiesChildren extends Children.Keys<String > { 264 265 266 private PropertyChangeListener propertyListener = null; 267 268 269 270 PropertiesChildren() { 271 super(); 272 } 273 274 275 276 protected void mySetKeys() { 277 TreeSet <String > newKeys = new TreeSet <String >(new Comparator <String >() { 278 public int compare(String o1, String o2) { 279 if (o1 == o2) { 280 return 0; 281 } 282 if (o1 == null) { 283 return -1; 284 } 285 if (o2 == null) { 286 return 1; 287 } 288 return o1.compareTo(o2); 289 } 290 }); 291 292 newKeys.add(getPrimaryEntry().getFile().getName()); 293 294 for (Entry entry : secondaryEntries()) { 295 newKeys.add(entry.getFile().getName()); 296 } 297 298 setKeys(newKeys); 299 } 300 301 303 protected void addNotify () { 304 mySetKeys(); 305 306 if(propertyListener == null) { 308 propertyListener = new PropertyChangeListener () { 309 public void propertyChange(PropertyChangeEvent evt) { 310 if(PROP_FILES.equals(evt.getPropertyName())) { 311 mySetKeys(); 312 } 313 } 314 }; 315 316 PropertiesDataObject.this.addPropertyChangeListener( 317 WeakListeners.propertyChange(propertyListener, PropertiesDataObject.this)); 318 } 319 } 320 321 325 protected void removeNotify () { 326 setKeys(new ArrayList <String >()); 327 } 328 329 330 protected Node[] createNodes(String entryName) { 331 if (entryName == null) { 332 return null; 333 } 334 335 PropertiesFileEntry entry = (PropertiesFileEntry)getPrimaryEntry(); 336 337 if(entryName.equals(entry.getFile().getName())) { 338 return new Node[] {entry.getNodeDelegate()}; 339 } 340 for(Iterator <Entry> it = secondaryEntries().iterator();it.hasNext();) { 341 entry = (PropertiesFileEntry)it.next(); 342 343 if (entryName.equals(entry.getFile().getName())) { 344 return new Node[] {entry.getNodeDelegate()}; 345 } 346 } 347 348 return null; 349 } 350 351 } 353 } 354 | Popular Tags |