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.ByteArrayInputStream;
013: import java.io.IOException;
014: import java.io.PrintWriter;
015: import java.io.StringWriter;
016: import java.net.MalformedURLException;
017: import java.net.URL;
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.IEditableModel;
024: import org.eclipse.pde.core.IModelChangedEvent;
025: import org.eclipse.pde.internal.core.NLResourceHelper;
026: import org.eclipse.pde.internal.core.PDECore;
027: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
028: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelProvider;
029:
030: public class WorkspaceExtensionsModel extends AbstractExtensionsModel
031: implements IEditableModel, IBundlePluginModelProvider {
032: private static final long serialVersionUID = 1L;
033: private IFile fUnderlyingResource;
034: private boolean fDirty;
035: private boolean fEditable = true;
036: private transient IBundlePluginModelBase fBundleModel;
037:
038: protected NLResourceHelper createNLResourceHelper() {
039: return new NLResourceHelper("plugin", getNLLookupLocations()); //$NON-NLS-1$
040: }
041:
042: public URL getNLLookupLocation() {
043: try {
044: return new URL("file:" + getInstallLocation() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
045: } catch (MalformedURLException e) {
046: return null;
047: }
048: }
049:
050: public WorkspaceExtensionsModel(IFile file) {
051: fUnderlyingResource = file;
052: }
053:
054: public void fireModelChanged(IModelChangedEvent event) {
055: fDirty = true;
056: super .fireModelChanged(event);
057: }
058:
059: public String getContents() {
060: StringWriter swriter = new StringWriter();
061: PrintWriter writer = new PrintWriter(swriter);
062: save(writer);
063: writer.flush();
064: try {
065: swriter.close();
066: } catch (IOException e) {
067: }
068: return swriter.toString();
069: }
070:
071: public String getInstallLocation() {
072: return fUnderlyingResource.getLocation().removeLastSegments(1)
073: .addTrailingSeparator().toOSString();
074: }
075:
076: public IResource getUnderlyingResource() {
077: return fUnderlyingResource;
078: }
079:
080: public boolean isInSync() {
081: if (fUnderlyingResource == null)
082: return true;
083: IPath path = fUnderlyingResource.getLocation();
084: if (path == null)
085: return false;
086: return super .isInSync(path.toFile());
087: }
088:
089: public boolean isDirty() {
090: return fDirty;
091: }
092:
093: public boolean isEditable() {
094: return fEditable;
095: }
096:
097: public void load() {
098: if (fUnderlyingResource == null)
099: return;
100: getExtensions(true);
101: }
102:
103: protected void updateTimeStamp() {
104: updateTimeStamp(fUnderlyingResource.getLocation().toFile());
105: }
106:
107: public void save() {
108: if (fUnderlyingResource == null)
109: return;
110: try {
111: String contents = getContents();
112: ByteArrayInputStream stream = new ByteArrayInputStream(
113: contents.getBytes("UTF8")); //$NON-NLS-1$
114: if (fUnderlyingResource.exists()) {
115: fUnderlyingResource.setContents(stream, false, false,
116: null);
117: } else {
118: fUnderlyingResource.create(stream, false, null);
119: }
120: stream.close();
121: } catch (CoreException e) {
122: PDECore.logException(e);
123: } catch (IOException e) {
124: }
125: }
126:
127: public void save(PrintWriter writer) {
128: if (isLoaded()) {
129: fExtensions.write("", writer); //$NON-NLS-1$
130: }
131: fDirty = false;
132: }
133:
134: public void setDirty(boolean dirty) {
135: fDirty = dirty;
136: }
137:
138: public void setEditable(boolean editable) {
139: fEditable = editable;
140: }
141:
142: /* (non-Javadoc)
143: * @see org.eclipse.pde.internal.core.plugin.AbstractExtensionsModel#createExtensions()
144: */
145: protected Extensions createExtensions() {
146: Extensions extensions = super .createExtensions();
147: extensions.setIsFragment(fUnderlyingResource.getName().equals(
148: "fragment.xml")); //$NON-NLS-1$
149: return extensions;
150: }
151:
152: /* (non-Javadoc)
153: * @see java.lang.Object#toString()
154: */
155: public String toString() {
156: return fUnderlyingResource.getName();
157: }
158:
159: public void setBundleModel(IBundlePluginModelBase model) {
160: fBundleModel = model;
161: }
162:
163: public IBundlePluginModelBase getBundlePluginModel() {
164: return fBundleModel;
165: }
166:
167: }
|