KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > simple > SimpleCSWorkspaceModel


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.simple;
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  * SimpleCSWorkspaceModel
31  *
32  */

33 public class SimpleCSWorkspaceModel extends SimpleCSModel implements
34         IWorkspaceModel {
35
36     /**
37      *
38      */

39     private static final long serialVersionUID = 1L;
40
41     private IFile fFile;
42
43     private boolean fDirty;
44
45     private boolean fEditable;
46     
47     /**
48      *
49      */

50     public SimpleCSWorkspaceModel(IFile file, boolean editable) {
51         fFile = file;
52         fEditable = editable;
53     }
54
55     /* (non-Javadoc)
56      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSModel#load()
57      */

58     public void load() throws CoreException {
59         if (fFile.exists()) {
60             InputStream JavaDoc stream = null;
61             try {
62                 stream = new BufferedInputStream JavaDoc(fFile.getContents(true));
63                 load(stream, false);
64             } catch (CoreException e) {
65             }
66         }
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSModel#isInSync()
71      */

72     public boolean isInSync() {
73         IPath path = fFile.getLocation();
74         if (path == null) {
75             return false;
76         }
77         return isInSync(path.toFile());
78     }
79     
80     /* (non-Javadoc)
81      * @see org.eclipse.pde.internal.core.AbstractModel#getUnderlyingResource()
82      */

83     public IResource getUnderlyingResource() {
84         return fFile;
85     }
86     
87     /**
88      * @return
89      */

90     public String JavaDoc getInstallLocation() {
91         return fFile.getLocation().toOSString();
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.pde.core.IEditableModel#save()
96      */

97     public void save() {
98         try {
99             String JavaDoc contents = getContents();
100             ByteArrayInputStream JavaDoc stream =
101                 new ByteArrayInputStream JavaDoc(contents.getBytes("UTF8")); //$NON-NLS-1$
102
if (fFile.exists()) {
103                 fFile.setContents(stream, false, false, null);
104             } else {
105                 fFile.create(stream, false, null);
106             }
107             stream.close();
108         } catch (CoreException e) {
109             PDECore.logException(e);
110         } catch (IOException JavaDoc e) {
111         }
112     }
113
114     /**
115      * @return
116      */

117     private String JavaDoc getContents() {
118         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
119         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
120         setLoaded(true);
121         save(writer);
122         writer.flush();
123         try {
124             swriter.close();
125         } catch (IOException JavaDoc e) {
126         }
127         return swriter.toString();
128     }
129     
130     /* (non-Javadoc)
131      * @see org.eclipse.pde.core.IEditable#isDirty()
132      */

133     public boolean isDirty() {
134         return fDirty;
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
139      */

140     public void save(PrintWriter JavaDoc writer) {
141         if (isLoaded()) {
142             getSimpleCS().write("", writer); //$NON-NLS-1$
143
}
144         setDirty(false);
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
149      */

150     public void setDirty(boolean dirty) {
151         fDirty = dirty;
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.pde.internal.core.AbstractModel#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
156      */

157     public void fireModelChanged(IModelChangedEvent event) {
158         setDirty(true);
159         super.fireModelChanged(event);
160     }
161     
162     /* (non-Javadoc)
163      * @see org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSModel#isEditable()
164      */

165     public boolean isEditable() {
166         return fEditable;
167     }
168
169     /* (non-Javadoc)
170      * @see org.eclipse.pde.internal.core.IWorkspaceModel#reload()
171      */

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