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.core.build.IBuildModel; 29 import org.eclipse.pde.internal.core.NLResourceHelper; 30 import org.eclipse.pde.internal.core.PDECore; 31 import org.eclipse.pde.internal.core.PDEManager; 32 33 public abstract class WorkspacePluginModelBase extends AbstractPluginModelBase 34 implements IEditableModel { 35 36 private IFile fUnderlyingResource; 37 38 private boolean fDirty; 39 40 private boolean fEditable = true; 41 42 private IBuildModel fBuildModel; 43 44 protected NLResourceHelper createNLResourceHelper() { 45 return new NLResourceHelper("plugin" , PDEManager.getNLLookupLocations(this)); } 47 48 public URL getNLLookupLocation() { 49 try { 50 return new URL ("file:" + getInstallLocation() + "/"); } catch (MalformedURLException e) { 52 return null; 53 } 54 } 55 56 public WorkspacePluginModelBase(IFile file, boolean abbreviated) { 57 fUnderlyingResource = file; 58 fAbbreviated = abbreviated; 59 setEnabled(true); 60 } 61 62 public void fireModelChanged(IModelChangedEvent event) { 63 fDirty = true; 64 super.fireModelChanged(event); 65 } 66 67 public IBuildModel getBuildModel() { 68 return fBuildModel; 69 } 70 71 public String getContents() { 72 StringWriter swriter = new StringWriter (); 73 PrintWriter writer = new PrintWriter (swriter); 74 save(writer); 75 writer.flush(); 76 try { 77 swriter.close(); 78 } catch (IOException e) { 79 } 80 return swriter.toString(); 81 } 82 83 public IFile getFile() { 84 return fUnderlyingResource; 85 } 86 87 public String getInstallLocation() { 88 IPath path = fUnderlyingResource.getLocation(); 89 return path == null ? null : path.removeLastSegments(1).addTrailingSeparator().toOSString(); 90 } 91 92 public IResource getUnderlyingResource() { 93 return fUnderlyingResource; 94 } 95 96 public boolean isInSync() { 97 if (fUnderlyingResource == null) 98 return true; 99 IPath path = fUnderlyingResource.getLocation(); 100 if (path == null) 101 return false; 102 return super.isInSync(path.toFile()); 103 } 104 105 public boolean isDirty() { 106 return fDirty; 107 } 108 109 public boolean isEditable() { 110 return fEditable; 111 } 112 113 public void load() { 114 if (fUnderlyingResource == null) 115 return; 116 if (fUnderlyingResource.exists()) { 117 try { 118 InputStream stream = new BufferedInputStream (fUnderlyingResource.getContents(true)); 119 load(stream, false); 120 stream.close(); 121 } catch (CoreException e) { 122 } catch (IOException e) { 123 PDECore.logException(e); 124 } 125 } else { 126 fPluginBase = createPluginBase(); 127 setLoaded(true); 128 } 129 } 130 131 protected void updateTimeStamp() { 132 updateTimeStamp(fUnderlyingResource.getLocation().toFile()); 133 } 134 135 public void save() { 136 if (fUnderlyingResource == null) 137 return; 138 try { 139 String contents = getContents(); 140 ByteArrayInputStream stream = new ByteArrayInputStream (contents 141 .getBytes("UTF8")); if (fUnderlyingResource.exists()) { 143 fUnderlyingResource.setContents(stream, false, false, null); 144 } else { 145 fUnderlyingResource.create(stream, false, null); 146 } 147 stream.close(); 148 } catch (CoreException e) { 149 PDECore.logException(e); 150 } catch (IOException e) { 151 } 152 } 153 154 public void save(PrintWriter writer) { 155 if (isLoaded()) { 156 fPluginBase.write("", writer); } 158 fDirty = false; 159 } 160 161 public void setBuildModel(IBuildModel buildModel) { 162 fBuildModel = buildModel; 163 } 164 165 public void setDirty(boolean dirty) { 166 fDirty = dirty; 167 } 168 169 public void setEditable(boolean editable) { 170 fEditable = editable; 171 } 172 173 } 174 | Popular Tags |