KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > target > WorkspaceTargetModel


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 package org.eclipse.pde.internal.core.target;
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
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.pde.core.IModelChangedEvent;
25 import org.eclipse.pde.internal.core.IWorkspaceModel;
26 import org.eclipse.pde.internal.core.PDECore;
27
28
29 public class WorkspaceTargetModel extends TargetModel implements IWorkspaceModel {
30
31     private static final long serialVersionUID = 1L;
32     
33     private IFile fFile;
34
35     private boolean fDirty;
36
37     private boolean fEditable;
38
39     public WorkspaceTargetModel(IFile file, boolean editable) {
40         fFile = file;
41         fEditable = editable;
42     }
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.pde.internal.core.product.ProductModel#load()
46      */

47     public void load() throws CoreException {
48         if (fFile.exists()) {
49             InputStream JavaDoc stream = null;
50             try {
51                 stream = new BufferedInputStream JavaDoc(fFile.getContents(true));
52                 load(stream, false);
53             } catch (CoreException e) {
54             }
55         }
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.pde.internal.core.product.ProductModel#isInSync()
60      */

61     public boolean isInSync() {
62         IPath path = fFile.getLocation();
63         return path == null ? false : isInSync(path.toFile());
64     }
65     
66     /* (non-Javadoc)
67      * @see org.eclipse.pde.internal.core.AbstractModel#getUnderlyingResource()
68      */

69     public IResource getUnderlyingResource() {
70         return fFile;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.eclipse.pde.internal.core.product.ProductModel#getInstallLocation()
75      */

76     public String JavaDoc getInstallLocation() {
77         return fFile.getLocation().toOSString();
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.pde.core.IEditableModel#save()
82      */

83     public void save() {
84         try {
85             String JavaDoc contents = getContents();
86             ByteArrayInputStream JavaDoc stream =
87                 new ByteArrayInputStream JavaDoc(contents.getBytes("UTF8")); //$NON-NLS-1$
88
if (fFile.exists()) {
89                 fFile.setContents(stream, false, false, null);
90             } else {
91                 fFile.create(stream, false, null);
92             }
93             stream.close();
94         } catch (CoreException e) {
95             PDECore.logException(e);
96         } catch (IOException JavaDoc e) {
97         }
98     }
99     
100     public String JavaDoc getContents() {
101         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
102         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
103         setLoaded(true);
104         save(writer);
105         writer.flush();
106         try {
107             swriter.close();
108         } catch (IOException JavaDoc e) {
109         }
110         return swriter.toString();
111     }
112
113
114     /* (non-Javadoc)
115      * @see org.eclipse.pde.core.IEditable#isDirty()
116      */

117     public boolean isDirty() {
118         return fDirty;
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
123      */

124     public void save(PrintWriter JavaDoc writer) {
125         if (isLoaded()) {
126             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
127
writer.println("<?pde version=\"3.2\"?>"); //$NON-NLS-1$
128
writer.println();
129             getTarget().write("", writer); //$NON-NLS-1$
130
}
131         setDirty(false);
132     }
133
134     /* (non-Javadoc)
135      * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
136      */

137     public void setDirty(boolean dirty) {
138         fDirty = dirty;
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.pde.internal.core.AbstractModel#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
143      */

144     public void fireModelChanged(IModelChangedEvent event) {
145         setDirty(true);
146         super.fireModelChanged(event);
147     }
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.pde.internal.core.product.ProductModel#isEditable()
151      */

152     public boolean isEditable() {
153         return fEditable;
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.pde.internal.core.IWorkspaceModel#reload()
158      */

159     public void reload() {
160         // Underlying file has to exist in order to reload the model
161
if (fFile.exists()) {
162             InputStream JavaDoc stream = null;
163             try {
164                 // Get the file contents
165
stream = new BufferedInputStream JavaDoc(fFile.getContents(true));
166                 // Load the model using the last saved file contents
167
reload(stream, false);
168                 // Remove the dirty (*) indicator from the editor window
169
setDirty(false);
170             } catch (CoreException e) {
171                 // Ignore
172
}
173         }
174     }
175 }
176
Popular Tags