001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.build;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.PrintWriter;
016: import java.io.StringWriter;
017:
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.resources.IResource;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.pde.core.IEditableModel;
022: import org.eclipse.pde.core.IModelChangedEvent;
023: import org.eclipse.pde.internal.core.PDECore;
024:
025: public class WorkspaceBuildModel extends BuildModel implements
026: IEditableModel {
027: private static final long serialVersionUID = 1L;
028: private IFile fUnderlyingResource;
029: private boolean fDirty;
030: private boolean fEditable = true;
031:
032: public WorkspaceBuildModel(IFile file) {
033: fUnderlyingResource = file;
034: }
035:
036: public void fireModelChanged(IModelChangedEvent event) {
037: setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
038: super .fireModelChanged(event);
039: }
040:
041: public String getContents() {
042: StringWriter swriter = new StringWriter();
043: PrintWriter writer = new PrintWriter(swriter);
044: save(writer);
045: writer.flush();
046: try {
047: swriter.close();
048: writer.close();
049: } catch (IOException e) {
050: }
051: return swriter.toString();
052: }
053:
054: public IResource getUnderlyingResource() {
055: return fUnderlyingResource;
056: }
057:
058: public boolean isDirty() {
059: return fDirty;
060: }
061:
062: public boolean isEditable() {
063: return fEditable;
064: }
065:
066: public void load() {
067: if (fUnderlyingResource.exists()) {
068: try {
069: InputStream stream = fUnderlyingResource
070: .getContents(true);
071: load(stream, false);
072: stream.close();
073: } catch (Exception e) {
074: PDECore.logException(e);
075: }
076: } else {
077: fBuild = new Build();
078: fBuild.setModel(this );
079: setLoaded(true);
080: }
081: }
082:
083: public boolean isInSync() {
084: return true;
085: }
086:
087: protected void updateTimeStamp() {
088: updateTimeStamp(fUnderlyingResource.getLocation().toFile());
089: }
090:
091: public void save() {
092: if (fUnderlyingResource == null)
093: return;
094: try {
095: String contents = getContents();
096: ByteArrayInputStream stream = new ByteArrayInputStream(
097: contents.getBytes("8859_1")); //$NON-NLS-1$
098: if (fUnderlyingResource.exists()) {
099: fUnderlyingResource.setContents(stream, false, false,
100: null);
101: } else {
102: fUnderlyingResource.create(stream, false, null);
103: }
104: stream.close();
105: } catch (CoreException e) {
106: PDECore.logException(e);
107: } catch (IOException e) {
108: }
109: }
110:
111: public void save(PrintWriter writer) {
112: getBuild().write("", writer); //$NON-NLS-1$
113: fDirty = false;
114: }
115:
116: public void setDirty(boolean dirty) {
117: fDirty = dirty;
118: }
119:
120: public void setEditable(boolean editable) {
121: fEditable = editable;
122: }
123:
124: /* (non-Javadoc)
125: * @see org.eclipse.pde.core.build.IBuildModel#getInstallLocation()
126: */
127: public String getInstallLocation() {
128: return fUnderlyingResource.getLocation().toOSString();
129: }
130: }
|