KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > comp > CompCSWorkspaceModel


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.pde.internal.core.cheatsheet.comp;
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringWriter JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.pde.core.IModelChangedEvent;
26 import org.eclipse.pde.internal.core.IWorkspaceModel;
27 import org.eclipse.pde.internal.core.PDECore;
28
29 /**
30  * CompCSWorkspaceModel
31  *
32  */

33 public class CompCSWorkspaceModel extends CompCSModel implements IWorkspaceModel {
34
35     private IFile fFile;
36
37     private boolean fDirty;
38
39     private boolean fEditable;
40     
41     /**
42      *
43      */

44     private static final long serialVersionUID = 1L;
45
46     /**
47      *
48      */

49     public CompCSWorkspaceModel(IFile file, boolean editable) {
50         fFile = file;
51         fEditable = editable;
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.pde.core.IEditableModel#save()
56      */

57     public void save() {
58         try {
59             String JavaDoc contents = getContents();
60             ByteArrayInputStream JavaDoc stream =
61                 new ByteArrayInputStream JavaDoc(contents.getBytes("UTF8")); //$NON-NLS-1$
62
if (fFile.exists()) {
63                 fFile.setContents(stream, false, false, null);
64             } else {
65                 fFile.create(stream, false, null);
66             }
67             stream.close();
68         } catch (CoreException e) {
69             PDECore.logException(e);
70         } catch (IOException JavaDoc e) {
71             // Ignore
72
}
73     }
74
75     /**
76      * @return
77      */

78     private String JavaDoc getContents() {
79         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
80         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
81         setLoaded(true);
82         save(writer);
83         writer.flush();
84         try {
85             swriter.close();
86         } catch (IOException JavaDoc e) {
87             // Ignore
88
}
89         return swriter.toString();
90     }
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.pde.core.IEditable#isDirty()
94      */

95     public boolean isDirty() {
96         return fDirty;
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
101      */

102     public void save(PrintWriter JavaDoc writer) {
103         if (isLoaded()) {
104             getCompCS().write("", writer); //$NON-NLS-1$
105
}
106         setDirty(false);
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
111      */

112     public void setDirty(boolean dirty) {
113         fDirty = dirty;
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.pde.internal.core.AbstractModel#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
118      */

119     public void fireModelChanged(IModelChangedEvent event) {
120         setDirty(true);
121         super.fireModelChanged(event);
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel#isEditable()
126      */

127     public boolean isEditable() {
128         return fEditable;
129     }
130     
131     /**
132      * @return
133      */

134     public String JavaDoc getInstallLocation() {
135         return fFile.getLocation().toOSString();
136     }
137     
138     /* (non-Javadoc)
139      * @see org.eclipse.pde.internal.core.AbstractModel#getUnderlyingResource()
140      */

141     public IResource getUnderlyingResource() {
142         return fFile;
143     }
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel#isInSync()
147      */

148     public boolean isInSync() {
149         IPath path = fFile.getLocation();
150         if (path == null) {
151             return false;
152         }
153         return isInSync(path.toFile());
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel#load()
158      */

159     public void load() throws CoreException {
160         if (fFile.exists()) {
161             InputStream JavaDoc stream = null;
162             try {
163                 stream = new BufferedInputStream JavaDoc(fFile.getContents(true));
164                 load(stream, false);
165             } catch (CoreException e) {
166             }
167         }
168     }
169     
170
171     /* (non-Javadoc)
172      * @see org.eclipse.pde.internal.core.IWorkspaceModel#reload()
173      */

174     public void reload() {
175         // Underlying file has to exist in order to reload the model
176
if (fFile.exists()) {
177             InputStream JavaDoc stream = null;
178             try {
179                 // Get the file contents
180
stream = new BufferedInputStream JavaDoc(fFile.getContents(true));
181                 // Load the model using the last saved file contents
182
reload(stream, false);
183                 // Remove the dirty (*) indicator from the editor window
184
setDirty(false);
185             } catch (CoreException e) {
186                 // Ignore
187
}
188         }
189     }
190 }
191
Popular Tags