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.plugin;
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: import java.net.MalformedURLException;
019: import java.net.URL;
020:
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.core.runtime.IPath;
025: import org.eclipse.pde.core.IEditableModel;
026: import org.eclipse.pde.core.IModelChangedEvent;
027: import org.eclipse.pde.core.build.IBuildModel;
028: import org.eclipse.pde.internal.core.NLResourceHelper;
029: import org.eclipse.pde.internal.core.PDECore;
030: import org.eclipse.pde.internal.core.PDEManager;
031:
032: public abstract class WorkspacePluginModelBase extends
033: AbstractPluginModelBase implements IEditableModel {
034:
035: private IFile fUnderlyingResource;
036:
037: private boolean fDirty;
038:
039: private boolean fEditable = true;
040:
041: private IBuildModel fBuildModel;
042:
043: protected NLResourceHelper createNLResourceHelper() {
044: return new NLResourceHelper(
045: "plugin", PDEManager.getNLLookupLocations(this )); //$NON-NLS-1$
046: }
047:
048: public URL getNLLookupLocation() {
049: try {
050: return new URL("file:" + getInstallLocation() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
051: } catch (MalformedURLException e) {
052: return null;
053: }
054: }
055:
056: public WorkspacePluginModelBase(IFile file, boolean abbreviated) {
057: fUnderlyingResource = file;
058: fAbbreviated = abbreviated;
059: setEnabled(true);
060: }
061:
062: public void fireModelChanged(IModelChangedEvent event) {
063: fDirty = true;
064: super .fireModelChanged(event);
065: }
066:
067: public IBuildModel getBuildModel() {
068: return fBuildModel;
069: }
070:
071: public String getContents() {
072: StringWriter swriter = new StringWriter();
073: PrintWriter writer = new PrintWriter(swriter);
074: save(writer);
075: writer.flush();
076: try {
077: swriter.close();
078: } catch (IOException e) {
079: }
080: return swriter.toString();
081: }
082:
083: public IFile getFile() {
084: return fUnderlyingResource;
085: }
086:
087: public String getInstallLocation() {
088: IPath path = fUnderlyingResource.getLocation();
089: return path == null ? null : path.removeLastSegments(1)
090: .addTrailingSeparator().toOSString();
091: }
092:
093: public IResource getUnderlyingResource() {
094: return fUnderlyingResource;
095: }
096:
097: public boolean isInSync() {
098: if (fUnderlyingResource == null)
099: return true;
100: IPath path = fUnderlyingResource.getLocation();
101: if (path == null)
102: return false;
103: return super .isInSync(path.toFile());
104: }
105:
106: public boolean isDirty() {
107: return fDirty;
108: }
109:
110: public boolean isEditable() {
111: return fEditable;
112: }
113:
114: public void load() {
115: if (fUnderlyingResource == null)
116: return;
117: if (fUnderlyingResource.exists()) {
118: try {
119: InputStream stream = new BufferedInputStream(
120: fUnderlyingResource.getContents(true));
121: load(stream, false);
122: stream.close();
123: } catch (CoreException e) {
124: } catch (IOException e) {
125: PDECore.logException(e);
126: }
127: } else {
128: fPluginBase = createPluginBase();
129: setLoaded(true);
130: }
131: }
132:
133: protected void updateTimeStamp() {
134: updateTimeStamp(fUnderlyingResource.getLocation().toFile());
135: }
136:
137: public void save() {
138: if (fUnderlyingResource == null)
139: return;
140: try {
141: String contents = getContents();
142: ByteArrayInputStream stream = new ByteArrayInputStream(
143: contents.getBytes("UTF8")); //$NON-NLS-1$
144: if (fUnderlyingResource.exists()) {
145: fUnderlyingResource.setContents(stream, false, false,
146: null);
147: } else {
148: fUnderlyingResource.create(stream, false, null);
149: }
150: stream.close();
151: } catch (CoreException e) {
152: PDECore.logException(e);
153: } catch (IOException e) {
154: }
155: }
156:
157: public void save(PrintWriter writer) {
158: if (isLoaded()) {
159: fPluginBase.write("", writer); //$NON-NLS-1$
160: }
161: fDirty = false;
162: }
163:
164: public void setBuildModel(IBuildModel buildModel) {
165: fBuildModel = buildModel;
166: }
167:
168: public void setDirty(boolean dirty) {
169: fDirty = dirty;
170: }
171:
172: public void setEditable(boolean editable) {
173: fEditable = editable;
174: }
175:
176: }
|