1 11 package org.eclipse.pde.internal.core.build; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.PrintWriter ; 17 import java.io.StringWriter ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.pde.core.IEditableModel; 23 import org.eclipse.pde.core.IModelChangedEvent; 24 import org.eclipse.pde.internal.core.PDECore; 25 26 public class WorkspaceBuildModel extends BuildModel implements IEditableModel { 27 private static final long serialVersionUID = 1L; 28 private IFile fUnderlyingResource; 29 private boolean fDirty; 30 private boolean fEditable = true; 31 32 public WorkspaceBuildModel(IFile file) { 33 fUnderlyingResource = file; 34 } 35 36 public void fireModelChanged(IModelChangedEvent event) { 37 setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED); 38 super.fireModelChanged(event); 39 } 40 41 public String getContents() { 42 StringWriter swriter = new StringWriter (); 43 PrintWriter writer = new PrintWriter (swriter); 44 save(writer); 45 writer.flush(); 46 try { 47 swriter.close(); 48 writer.close(); 49 } catch (IOException e) { 50 } 51 return swriter.toString(); 52 } 53 54 public IResource getUnderlyingResource() { 55 return fUnderlyingResource; 56 } 57 58 public boolean isDirty() { 59 return fDirty; 60 } 61 62 public boolean isEditable() { 63 return fEditable; 64 } 65 66 public void load() { 67 if (fUnderlyingResource.exists()) { 68 try { 69 InputStream stream = fUnderlyingResource.getContents(true); 70 load(stream, false); 71 stream.close(); 72 } catch (Exception e) { 73 PDECore.logException(e); 74 } 75 } else { 76 fBuild = new Build(); 77 fBuild.setModel(this); 78 setLoaded(true); 79 } 80 } 81 82 public boolean isInSync() { 83 return true; 84 } 85 86 protected void updateTimeStamp() { 87 updateTimeStamp(fUnderlyingResource.getLocation().toFile()); 88 } 89 90 public void save() { 91 if (fUnderlyingResource == null) 92 return; 93 try { 94 String contents = getContents(); 95 ByteArrayInputStream stream = new ByteArrayInputStream (contents 96 .getBytes("8859_1")); if (fUnderlyingResource.exists()) { 98 fUnderlyingResource.setContents(stream, false, false, null); 99 } else { 100 fUnderlyingResource.create(stream, false, null); 101 } 102 stream.close(); 103 } catch (CoreException e) { 104 PDECore.logException(e); 105 } catch (IOException e) { 106 } 107 } 108 109 public void save(PrintWriter writer) { 110 getBuild().write("", writer); fDirty = false; 112 } 113 114 public void setDirty(boolean dirty) { 115 fDirty = dirty; 116 } 117 118 public void setEditable(boolean editable) { 119 fEditable = editable; 120 } 121 122 125 public String getInstallLocation() { 126 return fUnderlyingResource.getLocation().toOSString(); 127 } 128 } 129 | Popular Tags |