1 11 package org.eclipse.pde.internal.core.plugin; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.pde.core.IEditableModel; 27 import org.eclipse.pde.core.IModelChangedEvent; 28 import org.eclipse.pde.internal.core.NLResourceHelper; 29 import org.eclipse.pde.internal.core.PDECore; 30 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 31 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelProvider; 32 33 public class WorkspaceExtensionsModel 34 extends AbstractExtensionsModel 35 implements IEditableModel, IBundlePluginModelProvider { 36 private static final long serialVersionUID = 1L; 37 private IFile fUnderlyingResource; 38 private boolean fDirty; 39 private boolean fEditable = true; 40 private transient IBundlePluginModelBase fBundleModel; 41 42 43 protected NLResourceHelper createNLResourceHelper() { 44 return new NLResourceHelper("plugin", getNLLookupLocations()); } 46 47 public URL getNLLookupLocation() { 48 try { 49 return new URL ("file:" + getInstallLocation() + "/"); } catch (MalformedURLException e) { 51 return null; 52 } 53 } 54 55 public WorkspaceExtensionsModel(IFile file) { 56 fUnderlyingResource = file; 57 } 58 59 public void fireModelChanged(IModelChangedEvent event) { 60 fDirty = true; 61 super.fireModelChanged(event); 62 } 63 64 public String getContents() { 65 StringWriter swriter = new StringWriter (); 66 PrintWriter writer = new PrintWriter (swriter); 67 save(writer); 68 writer.flush(); 69 try { 70 swriter.close(); 71 } catch (IOException e) { 72 } 73 return swriter.toString(); 74 } 75 76 public String getInstallLocation() { 77 return fUnderlyingResource.getLocation().removeLastSegments(1).addTrailingSeparator().toOSString(); 78 } 79 80 public IResource getUnderlyingResource() { 81 return fUnderlyingResource; 82 } 83 84 public boolean isInSync() { 85 if (fUnderlyingResource == null) 86 return true; 87 IPath path = fUnderlyingResource.getLocation(); 88 if (path == null) 89 return false; 90 return super.isInSync(path.toFile()); 91 } 92 93 public boolean isDirty() { 94 return fDirty; 95 } 96 public boolean isEditable() { 97 return fEditable; 98 } 99 100 public void load() { 101 if (fUnderlyingResource == null) 102 return; 103 if (fUnderlyingResource.exists()) { 104 try { 105 InputStream stream = new BufferedInputStream (fUnderlyingResource.getContents(true)); 106 load(stream, false); 107 stream.close(); 108 } catch (Exception e) { 109 PDECore.logException(e); 110 } 111 } else { 112 getExtensions(true); 113 setLoaded(true); 114 } 115 } 116 117 protected void updateTimeStamp() { 118 updateTimeStamp(fUnderlyingResource.getLocation().toFile()); 119 } 120 121 public void save() { 122 if (fUnderlyingResource == null) 123 return; 124 try { 125 String contents = getContents(); 126 ByteArrayInputStream stream = 127 new ByteArrayInputStream (contents.getBytes("UTF8")); if (fUnderlyingResource.exists()) { 129 fUnderlyingResource.setContents(stream, false, false, null); 130 } else { 131 fUnderlyingResource.create(stream, false, null); 132 } 133 stream.close(); 134 } catch (CoreException e) { 135 PDECore.logException(e); 136 } catch (IOException e) { 137 } 138 } 139 public void save(PrintWriter writer) { 140 if (isLoaded()) { 141 fExtensions.write("", writer); } 143 fDirty = false; 144 } 145 public void setDirty(boolean dirty) { 146 fDirty = dirty; 147 } 148 public void setEditable(boolean editable) { 149 fEditable = editable; 150 } 151 154 protected Extensions createExtensions() { 155 Extensions extensions = super.createExtensions(); 156 extensions.setIsFragment(fUnderlyingResource.getName().equals("fragment.xml")); return extensions; 158 } 159 160 163 public String toString() { 164 return fUnderlyingResource.getName(); 165 } 166 167 public void setBundleModel(IBundlePluginModelBase model) { 168 fBundleModel = model; 169 } 170 171 public IBundlePluginModelBase getBundlePluginModel() { 172 return fBundleModel; 173 } 174 175 176 } 177 | Popular Tags |