KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.build.IBuildModel;
29 import org.eclipse.pde.internal.core.NLResourceHelper;
30 import org.eclipse.pde.internal.core.PDECore;
31 import org.eclipse.pde.internal.core.PDEManager;
32
33 public abstract class WorkspacePluginModelBase extends AbstractPluginModelBase
34         implements IEditableModel {
35     
36     private IFile fUnderlyingResource;
37
38     private boolean fDirty;
39
40     private boolean fEditable = true;
41
42     private IBuildModel fBuildModel;
43
44     protected NLResourceHelper createNLResourceHelper() {
45         return new NLResourceHelper("plugin" , PDEManager.getNLLookupLocations(this)); //$NON-NLS-1$
46
}
47
48     public URL JavaDoc getNLLookupLocation() {
49         try {
50             return new URL JavaDoc("file:" + getInstallLocation() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
51
} catch (MalformedURLException JavaDoc e) {
52             return null;
53         }
54     }
55
56     public WorkspacePluginModelBase(IFile file, boolean abbreviated) {
57         fUnderlyingResource = file;
58         fAbbreviated = abbreviated;
59         setEnabled(true);
60     }
61
62     public void fireModelChanged(IModelChangedEvent event) {
63         fDirty = true;
64         super.fireModelChanged(event);
65     }
66
67     public IBuildModel getBuildModel() {
68         return fBuildModel;
69     }
70     
71     public String JavaDoc getContents() {
72         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
73         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
74         save(writer);
75         writer.flush();
76         try {
77             swriter.close();
78         } catch (IOException JavaDoc e) {
79         }
80         return swriter.toString();
81     }
82
83     public IFile getFile() {
84         return fUnderlyingResource;
85     }
86
87     public String JavaDoc getInstallLocation() {
88         IPath path = fUnderlyingResource.getLocation();
89         return path == null ? null : path.removeLastSegments(1).addTrailingSeparator().toOSString();
90     }
91
92     public IResource getUnderlyingResource() {
93         return fUnderlyingResource;
94     }
95
96     public boolean isInSync() {
97         if (fUnderlyingResource == null)
98             return true;
99         IPath path = fUnderlyingResource.getLocation();
100         if (path == null)
101             return false;
102         return super.isInSync(path.toFile());
103     }
104
105     public boolean isDirty() {
106         return fDirty;
107     }
108
109     public boolean isEditable() {
110         return fEditable;
111     }
112
113     public void load() {
114         if (fUnderlyingResource == null)
115             return;
116         if (fUnderlyingResource.exists()) {
117             try {
118                 InputStream JavaDoc stream = new BufferedInputStream JavaDoc(fUnderlyingResource.getContents(true));
119                 load(stream, false);
120                 stream.close();
121             } catch (CoreException e) {
122             } catch (IOException JavaDoc e) {
123                 PDECore.logException(e);
124             }
125         } else {
126             fPluginBase = createPluginBase();
127             setLoaded(true);
128         }
129     }
130
131     protected void updateTimeStamp() {
132         updateTimeStamp(fUnderlyingResource.getLocation().toFile());
133     }
134
135     public void save() {
136         if (fUnderlyingResource == null)
137             return;
138         try {
139             String JavaDoc contents = getContents();
140             ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(contents
141                     .getBytes("UTF8")); //$NON-NLS-1$
142
if (fUnderlyingResource.exists()) {
143                 fUnderlyingResource.setContents(stream, false, false, null);
144             } else {
145                 fUnderlyingResource.create(stream, false, null);
146             }
147             stream.close();
148         } catch (CoreException e) {
149             PDECore.logException(e);
150         } catch (IOException JavaDoc e) {
151         }
152     }
153
154     public void save(PrintWriter JavaDoc writer) {
155         if (isLoaded()) {
156             fPluginBase.write("", writer); //$NON-NLS-1$
157
}
158         fDirty = false;
159     }
160
161     public void setBuildModel(IBuildModel buildModel) {
162         fBuildModel = buildModel;
163     }
164
165     public void setDirty(boolean dirty) {
166         fDirty = dirty;
167     }
168
169     public void setEditable(boolean editable) {
170         fEditable = editable;
171     }
172
173 }
174
Popular Tags