KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > WorkspaceExtensionsModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.plugin;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.pde.core.IEditableModel;
27 import org.eclipse.pde.core.IModelChangedEvent;
28 import org.eclipse.pde.internal.core.NLResourceHelper;
29 import org.eclipse.pde.internal.core.PDECore;
30 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
31 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelProvider;
32
33 public class WorkspaceExtensionsModel
34     extends AbstractExtensionsModel
35     implements IEditableModel, IBundlePluginModelProvider {
36     private static final long serialVersionUID = 1L;
37     private IFile fUnderlyingResource;
38     private boolean fDirty;
39     private boolean fEditable = true;
40     private transient IBundlePluginModelBase fBundleModel;
41
42
43     protected NLResourceHelper createNLResourceHelper() {
44         return new NLResourceHelper("plugin", getNLLookupLocations()); //$NON-NLS-1$
45
}
46     
47     public URL JavaDoc getNLLookupLocation() {
48         try {
49             return new URL JavaDoc("file:" + getInstallLocation() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
50
} catch (MalformedURLException JavaDoc e) {
51             return null;
52         }
53     }
54
55     public WorkspaceExtensionsModel(IFile file) {
56         fUnderlyingResource = file;
57     }
58     
59     public void fireModelChanged(IModelChangedEvent event) {
60         fDirty = true;
61         super.fireModelChanged(event);
62     }
63
64     public String JavaDoc getContents() {
65         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
66         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
67         save(writer);
68         writer.flush();
69         try {
70             swriter.close();
71         } catch (IOException JavaDoc e) {
72         }
73         return swriter.toString();
74     }
75     
76     public String JavaDoc getInstallLocation() {
77         return fUnderlyingResource.getLocation().removeLastSegments(1).addTrailingSeparator().toOSString();
78     }
79     
80     public IResource getUnderlyingResource() {
81         return fUnderlyingResource;
82     }
83
84     public boolean isInSync() {
85         if (fUnderlyingResource == null)
86             return true;
87         IPath path = fUnderlyingResource.getLocation();
88         if (path == null)
89             return false;
90         return super.isInSync(path.toFile());
91     }
92
93     public boolean isDirty() {
94         return fDirty;
95     }
96     public boolean isEditable() {
97         return fEditable;
98     }
99
100     public void load() {
101         if (fUnderlyingResource == null)
102             return;
103         if (fUnderlyingResource.exists()) {
104             try {
105                 InputStream JavaDoc stream = new BufferedInputStream JavaDoc(fUnderlyingResource.getContents(true));
106                 load(stream, false);
107                 stream.close();
108             } catch (Exception JavaDoc e) {
109                 PDECore.logException(e);
110             }
111         } else {
112             getExtensions(true);
113             setLoaded(true);
114         }
115     }
116     
117     protected void updateTimeStamp() {
118         updateTimeStamp(fUnderlyingResource.getLocation().toFile());
119     }
120
121     public void save() {
122         if (fUnderlyingResource == null)
123             return;
124         try {
125             String JavaDoc contents = getContents();
126             ByteArrayInputStream JavaDoc stream =
127                 new ByteArrayInputStream JavaDoc(contents.getBytes("UTF8")); //$NON-NLS-1$
128
if (fUnderlyingResource.exists()) {
129                 fUnderlyingResource.setContents(stream, false, false, null);
130             } else {
131                 fUnderlyingResource.create(stream, false, null);
132             }
133             stream.close();
134         } catch (CoreException e) {
135             PDECore.logException(e);
136         } catch (IOException JavaDoc e) {
137         }
138     }
139     public void save(PrintWriter JavaDoc writer) {
140         if (isLoaded()) {
141             fExtensions.write("", writer); //$NON-NLS-1$
142
}
143         fDirty = false;
144     }
145     public void setDirty(boolean dirty) {
146         fDirty = dirty;
147     }
148     public void setEditable(boolean editable) {
149         fEditable = editable;
150     }
151     /* (non-Javadoc)
152      * @see org.eclipse.pde.internal.core.plugin.AbstractExtensionsModel#createExtensions()
153      */

154     protected Extensions createExtensions() {
155         Extensions extensions = super.createExtensions();
156         extensions.setIsFragment(fUnderlyingResource.getName().equals("fragment.xml")); //$NON-NLS-1$
157
return extensions;
158     }
159     
160     /* (non-Javadoc)
161      * @see java.lang.Object#toString()
162      */

163     public String JavaDoc toString() {
164         return fUnderlyingResource.getName();
165     }
166     
167     public void setBundleModel(IBundlePluginModelBase model) {
168         fBundleModel = model;
169     }
170     
171     public IBundlePluginModelBase getBundlePluginModel() {
172         return fBundleModel;
173     }
174     
175     
176 }
177
Popular Tags