1 11 12 package org.eclipse.pde.internal.core.cheatsheet.simple; 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 SimpleCSWorkspaceModel extends SimpleCSModel implements 34 IWorkspaceModel { 35 36 39 private static final long serialVersionUID = 1L; 40 41 private IFile fFile; 42 43 private boolean fDirty; 44 45 private boolean fEditable; 46 47 50 public SimpleCSWorkspaceModel(IFile file, boolean editable) { 51 fFile = file; 52 fEditable = editable; 53 } 54 55 58 public void load() throws CoreException { 59 if (fFile.exists()) { 60 InputStream stream = null; 61 try { 62 stream = new BufferedInputStream (fFile.getContents(true)); 63 load(stream, false); 64 } catch (CoreException e) { 65 } 66 } 67 } 68 69 72 public boolean isInSync() { 73 IPath path = fFile.getLocation(); 74 if (path == null) { 75 return false; 76 } 77 return isInSync(path.toFile()); 78 } 79 80 83 public IResource getUnderlyingResource() { 84 return fFile; 85 } 86 87 90 public String getInstallLocation() { 91 return fFile.getLocation().toOSString(); 92 } 93 94 97 public void save() { 98 try { 99 String contents = getContents(); 100 ByteArrayInputStream stream = 101 new ByteArrayInputStream (contents.getBytes("UTF8")); if (fFile.exists()) { 103 fFile.setContents(stream, false, false, null); 104 } else { 105 fFile.create(stream, false, null); 106 } 107 stream.close(); 108 } catch (CoreException e) { 109 PDECore.logException(e); 110 } catch (IOException e) { 111 } 112 } 113 114 117 private String getContents() { 118 StringWriter swriter = new StringWriter (); 119 PrintWriter writer = new PrintWriter (swriter); 120 setLoaded(true); 121 save(writer); 122 writer.flush(); 123 try { 124 swriter.close(); 125 } catch (IOException e) { 126 } 127 return swriter.toString(); 128 } 129 130 133 public boolean isDirty() { 134 return fDirty; 135 } 136 137 140 public void save(PrintWriter writer) { 141 if (isLoaded()) { 142 getSimpleCS().write("", writer); } 144 setDirty(false); 145 } 146 147 150 public void setDirty(boolean dirty) { 151 fDirty = dirty; 152 } 153 154 157 public void fireModelChanged(IModelChangedEvent event) { 158 setDirty(true); 159 super.fireModelChanged(event); 160 } 161 162 165 public boolean isEditable() { 166 return fEditable; 167 } 168 169 172 public void reload() { 173 if (fFile.exists()) { 175 InputStream stream = null; 176 try { 177 stream = new BufferedInputStream (fFile.getContents(true)); 179 reload(stream, false); 181 setDirty(false); 183 } catch (CoreException e) { 184 } 186 } 187 } 188 189 } 190 | Popular Tags |