001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.cheatsheet.comp;
011:
012: import java.io.BufferedInputStream;
013: import java.io.ByteArrayInputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.PrintWriter;
017: import java.io.StringWriter;
018:
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.resources.IResource;
021: import org.eclipse.core.runtime.CoreException;
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.pde.core.IModelChangedEvent;
024: import org.eclipse.pde.internal.core.IWorkspaceModel;
025: import org.eclipse.pde.internal.core.PDECore;
026:
027: /**
028: * CompCSWorkspaceModel
029: *
030: */
031: public class CompCSWorkspaceModel extends CompCSModel implements
032: IWorkspaceModel {
033:
034: private IFile fFile;
035:
036: private boolean fDirty;
037:
038: private boolean fEditable;
039:
040: /**
041: *
042: */
043: private static final long serialVersionUID = 1L;
044:
045: /**
046: *
047: */
048: public CompCSWorkspaceModel(IFile file, boolean editable) {
049: fFile = file;
050: fEditable = editable;
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.pde.core.IEditableModel#save()
055: */
056: public void save() {
057: try {
058: String contents = getContents();
059: ByteArrayInputStream stream = new ByteArrayInputStream(
060: contents.getBytes("UTF8")); //$NON-NLS-1$
061: if (fFile.exists()) {
062: fFile.setContents(stream, false, false, null);
063: } else {
064: fFile.create(stream, false, null);
065: }
066: stream.close();
067: } catch (CoreException e) {
068: PDECore.logException(e);
069: } catch (IOException e) {
070: // Ignore
071: }
072: }
073:
074: /**
075: * @return
076: */
077: private String getContents() {
078: StringWriter swriter = new StringWriter();
079: PrintWriter writer = new PrintWriter(swriter);
080: setLoaded(true);
081: save(writer);
082: writer.flush();
083: try {
084: swriter.close();
085: } catch (IOException e) {
086: // Ignore
087: }
088: return swriter.toString();
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.pde.core.IEditable#isDirty()
093: */
094: public boolean isDirty() {
095: return fDirty;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
100: */
101: public void save(PrintWriter writer) {
102: if (isLoaded()) {
103: getCompCS().write("", writer); //$NON-NLS-1$
104: }
105: setDirty(false);
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
110: */
111: public void setDirty(boolean dirty) {
112: fDirty = dirty;
113: }
114:
115: /* (non-Javadoc)
116: * @see org.eclipse.pde.internal.core.AbstractModel#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
117: */
118: public void fireModelChanged(IModelChangedEvent event) {
119: setDirty(true);
120: super .fireModelChanged(event);
121: }
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel#isEditable()
125: */
126: public boolean isEditable() {
127: return fEditable;
128: }
129:
130: /**
131: * @return
132: */
133: public String getInstallLocation() {
134: return fFile.getLocation().toOSString();
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.pde.internal.core.AbstractModel#getUnderlyingResource()
139: */
140: public IResource getUnderlyingResource() {
141: return fFile;
142: }
143:
144: /* (non-Javadoc)
145: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel#isInSync()
146: */
147: public boolean isInSync() {
148: IPath path = fFile.getLocation();
149: if (path == null) {
150: return false;
151: }
152: return isInSync(path.toFile());
153: }
154:
155: /* (non-Javadoc)
156: * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel#load()
157: */
158: public void load() throws CoreException {
159: if (fFile.exists()) {
160: InputStream stream = null;
161: try {
162: stream = new BufferedInputStream(fFile
163: .getContents(true));
164: load(stream, false);
165: } catch (CoreException e) {
166: }
167: }
168: }
169:
170: /* (non-Javadoc)
171: * @see org.eclipse.pde.internal.core.IWorkspaceModel#reload()
172: */
173: public void reload() {
174: // Underlying file has to exist in order to reload the model
175: if (fFile.exists()) {
176: InputStream stream = null;
177: try {
178: // Get the file contents
179: stream = new BufferedInputStream(fFile
180: .getContents(true));
181: // Load the model using the last saved file contents
182: reload(stream, false);
183: // Remove the dirty (*) indicator from the editor window
184: setDirty(false);
185: } catch (CoreException e) {
186: // Ignore
187: }
188: }
189: }
190: }
|