001: /*******************************************************************************
002: * Copyright (c) 2005, 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.product;
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: public class WorkspaceProductModel extends ProductModel implements
028: IWorkspaceModel {
029:
030: private static final long serialVersionUID = 1L;
031:
032: private IFile fFile;
033:
034: private boolean fDirty;
035:
036: private boolean fEditable;
037:
038: public WorkspaceProductModel(IFile file, boolean editable) {
039: fFile = file;
040: fEditable = editable;
041: }
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.pde.internal.core.product.ProductModel#load()
045: */
046: public void load() throws CoreException {
047: if (fFile.exists()) {
048: InputStream stream = null;
049: try {
050: stream = new BufferedInputStream(fFile
051: .getContents(true));
052: load(stream, false);
053: } catch (CoreException e) {
054: }
055: }
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.pde.internal.core.product.ProductModel#isInSync()
060: */
061: public boolean isInSync() {
062: IPath path = fFile.getLocation();
063: return path == null ? false : isInSync(path.toFile());
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.pde.internal.core.AbstractModel#getUnderlyingResource()
068: */
069: public IResource getUnderlyingResource() {
070: return fFile;
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.pde.internal.core.product.ProductModel#getInstallLocation()
075: */
076: public String getInstallLocation() {
077: return fFile.getLocation().toOSString();
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.pde.core.IEditableModel#save()
082: */
083: public void save() {
084: try {
085: String contents = getContents();
086: ByteArrayInputStream stream = new ByteArrayInputStream(
087: contents.getBytes("UTF8")); //$NON-NLS-1$
088: if (fFile.exists()) {
089: fFile.setContents(stream, false, false, null);
090: } else {
091: fFile.create(stream, false, null);
092: }
093: stream.close();
094: } catch (CoreException e) {
095: PDECore.logException(e);
096: } catch (IOException e) {
097: }
098: }
099:
100: public String getContents() {
101: StringWriter swriter = new StringWriter();
102: PrintWriter writer = new PrintWriter(swriter);
103: setLoaded(true);
104: save(writer);
105: writer.flush();
106: try {
107: swriter.close();
108: } catch (IOException e) {
109: }
110: return swriter.toString();
111: }
112:
113: /* (non-Javadoc)
114: * @see org.eclipse.pde.core.IEditable#isDirty()
115: */
116: public boolean isDirty() {
117: return fDirty;
118: }
119:
120: /* (non-Javadoc)
121: * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
122: */
123: public void save(PrintWriter writer) {
124: if (isLoaded()) {
125: writer
126: .println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
127: writer.println("<?pde version=\"3.1\"?>"); //$NON-NLS-1$
128: writer.println();
129: getProduct().write("", writer); //$NON-NLS-1$
130: }
131: setDirty(false);
132: }
133:
134: /* (non-Javadoc)
135: * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
136: */
137: public void setDirty(boolean dirty) {
138: fDirty = dirty;
139: }
140:
141: /* (non-Javadoc)
142: * @see org.eclipse.pde.internal.core.AbstractModel#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
143: */
144: public void fireModelChanged(IModelChangedEvent event) {
145: setDirty(true);
146: super .fireModelChanged(event);
147: }
148:
149: /* (non-Javadoc)
150: * @see org.eclipse.pde.internal.core.product.ProductModel#isEditable()
151: */
152: public boolean isEditable() {
153: return fEditable;
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.pde.internal.core.IWorkspaceModel#reload()
158: */
159: public void reload() {
160: // Underlying file has to exist in order to reload the model
161: if (fFile.exists()) {
162: InputStream stream = null;
163: try {
164: // Get the file contents
165: stream = new BufferedInputStream(fFile
166: .getContents(true));
167: // Load the model using the last saved file contents
168: reload(stream, false);
169: // Remove the dirty (*) indicator from the editor window
170: setDirty(false);
171: } catch (CoreException e) {
172: // Ignore
173: }
174: }
175: }
176:
177: }
|