1 11 12 package org.eclipse.pde.internal.core.cheatsheet.comp; 13 14 import java.io.BufferedInputStream ; 15 import java.io.ByteArrayInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.pde.core.IModelChangedEvent; 26 import org.eclipse.pde.internal.core.IWorkspaceModel; 27 import org.eclipse.pde.internal.core.PDECore; 28 29 33 public class CompCSWorkspaceModel extends CompCSModel implements IWorkspaceModel { 34 35 private IFile fFile; 36 37 private boolean fDirty; 38 39 private boolean fEditable; 40 41 44 private static final long serialVersionUID = 1L; 45 46 49 public CompCSWorkspaceModel(IFile file, boolean editable) { 50 fFile = file; 51 fEditable = editable; 52 } 53 54 57 public void save() { 58 try { 59 String contents = getContents(); 60 ByteArrayInputStream stream = 61 new ByteArrayInputStream (contents.getBytes("UTF8")); if (fFile.exists()) { 63 fFile.setContents(stream, false, false, null); 64 } else { 65 fFile.create(stream, false, null); 66 } 67 stream.close(); 68 } catch (CoreException e) { 69 PDECore.logException(e); 70 } catch (IOException e) { 71 } 73 } 74 75 78 private String getContents() { 79 StringWriter swriter = new StringWriter (); 80 PrintWriter writer = new PrintWriter (swriter); 81 setLoaded(true); 82 save(writer); 83 writer.flush(); 84 try { 85 swriter.close(); 86 } catch (IOException e) { 87 } 89 return swriter.toString(); 90 } 91 92 95 public boolean isDirty() { 96 return fDirty; 97 } 98 99 102 public void save(PrintWriter writer) { 103 if (isLoaded()) { 104 getCompCS().write("", writer); } 106 setDirty(false); 107 } 108 109 112 public void setDirty(boolean dirty) { 113 fDirty = dirty; 114 } 115 116 119 public void fireModelChanged(IModelChangedEvent event) { 120 setDirty(true); 121 super.fireModelChanged(event); 122 } 123 124 127 public boolean isEditable() { 128 return fEditable; 129 } 130 131 134 public String getInstallLocation() { 135 return fFile.getLocation().toOSString(); 136 } 137 138 141 public IResource getUnderlyingResource() { 142 return fFile; 143 } 144 145 148 public boolean isInSync() { 149 IPath path = fFile.getLocation(); 150 if (path == null) { 151 return false; 152 } 153 return isInSync(path.toFile()); 154 } 155 156 159 public void load() throws CoreException { 160 if (fFile.exists()) { 161 InputStream stream = null; 162 try { 163 stream = new BufferedInputStream (fFile.getContents(true)); 164 load(stream, false); 165 } catch (CoreException e) { 166 } 167 } 168 } 169 170 171 174 public void reload() { 175 if (fFile.exists()) { 177 InputStream stream = null; 178 try { 179 stream = new BufferedInputStream (fFile.getContents(true)); 181 reload(stream, false); 183 setDirty(false); 185 } catch (CoreException e) { 186 } 188 } 189 } 190 } 191 | Popular Tags |