1 19 20 package org.netbeans.core.registry; 21 22 import org.netbeans.core.startup.layers.SessionManager; 23 import org.netbeans.spi.registry.ResettableContext; 24 import org.netbeans.spi.registry.SpiUtils; 25 import org.netbeans.api.registry.ContextException; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileSystem; 28 import org.openide.filesystems.Repository; 29 import org.openide.ErrorManager; 30 31 import java.io.IOException ; 32 33 public class ResettableContextImpl extends ContextImpl implements ResettableContext { 34 35 private ContextBindings resettableContextBindings; 36 37 private FileSystem defaults; 38 private FileSystem customizations; 39 40 public ResettableContextImpl () { 41 this (Repository.getDefault().getDefaultFileSystem().getRoot()); 42 } 43 44 public ResettableContextImpl (FileObject folder) { 45 this (folder, null, SessionManager.getDefault().getLayer(SessionManager.LAYER_INSTALL), SessionManager.getDefault().getLayer(SessionManager.LAYER_SESSION)); 46 } 47 48 public ResettableContextImpl(FileObject folder, ContextImpl rootContext, FileSystem defaults, FileSystem customizations) { 49 super(folder, rootContext); 50 this.defaults = defaults; 51 this.customizations = customizations; 52 } 53 54 public synchronized ContextBindings getContextBindings() { 55 if (resettableContextBindings == null) { 56 resettableContextBindings = ContextBindings.createResettableContextBindings(getFolder(), this, defaults, customizations); 57 } 58 return resettableContextBindings; 59 } 60 61 public ContextImpl getCtx(FileObject fo) { 62 ContextImpl ctx = getRootContextImpl().getContextCache().retrieveContextFromCache(fo); 65 if (ctx == null) { 66 ctx = new ResettableContextImpl(fo, getRootContextImpl(), defaults, customizations); 67 } 68 return ctx; 69 } 70 71 72 public boolean hasDefault(String bindingName) { 73 if (bindingName != null) { 74 return getContextBindings().hasDefault(bindingName); 75 } else { 76 FileSystem fs = defaults; 77 FileObject defaults = fs.findResource(getFolder ().getPath()); 78 return (defaults != null); 79 } 80 } 81 82 83 public boolean isModified(String bindingName) { 84 if (!(this instanceof ResettableContext)) { 85 return false; 88 } 89 if (bindingName != null) { 90 if (getContextBindings().isModified(bindingName)) { 91 return true; 92 } 93 return false; 94 } else { 95 FileSystem fs = customizations; 96 FileObject custs = fs.findResource(getFolder ().getPath()); 97 return (custs != null); 98 } 99 } 100 101 102 public void revert(String bindingName) throws ContextException { 103 if (!isModified(bindingName)) { 104 return; 105 } 106 107 if (bindingName != null) { 108 if (getContextBindings().isModified(bindingName)) { 109 getContextBindings().revert(bindingName); 110 return; 111 } 112 } else { 113 FileSystem fs = customizations; 114 FileObject custs = fs.findResource(getFolder ().getPath()); 115 if (custs != null) { 116 deleteFolderContent(custs); 117 } 118 } 119 } 120 121 private void deleteFolderContent(FileObject fo) throws ContextException { 122 try { 123 FileObject f = fo.getParent(); 136 String name = fo.getNameExt(); 137 fo.delete(); 138 f.createFolder(name); 139 } catch (IOException ex) { 140 ContextException ce = SpiUtils.createContextException(this, "Error on underlaying filesystem occured during the revert."); 141 ErrorManager.getDefault().annotate(ce, ex); 142 throw ce; 143 } 144 } 145 146 } 147 | Popular Tags |