KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > build > WorkspaceBuildModel


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.build;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.pde.core.IEditableModel;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.internal.core.PDECore;
25
26 public class WorkspaceBuildModel extends BuildModel implements IEditableModel {
27     private static final long serialVersionUID = 1L;
28     private IFile fUnderlyingResource;
29     private boolean fDirty;
30     private boolean fEditable = true;
31
32     public WorkspaceBuildModel(IFile file) {
33         fUnderlyingResource = file;
34     }
35
36     public void fireModelChanged(IModelChangedEvent event) {
37         setDirty(event.getChangeType() != IModelChangedEvent.WORLD_CHANGED);
38         super.fireModelChanged(event);
39     }
40
41     public String JavaDoc getContents() {
42         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
43         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
44         save(writer);
45         writer.flush();
46         try {
47             swriter.close();
48             writer.close();
49         } catch (IOException JavaDoc e) {
50         }
51         return swriter.toString();
52     }
53
54     public IResource getUnderlyingResource() {
55         return fUnderlyingResource;
56     }
57
58     public boolean isDirty() {
59         return fDirty;
60     }
61
62     public boolean isEditable() {
63         return fEditable;
64     }
65
66     public void load() {
67         if (fUnderlyingResource.exists()) {
68             try {
69                 InputStream JavaDoc stream = fUnderlyingResource.getContents(true);
70                 load(stream, false);
71                 stream.close();
72             } catch (Exception JavaDoc e) {
73                 PDECore.logException(e);
74             }
75         } else {
76             fBuild = new Build();
77             fBuild.setModel(this);
78             setLoaded(true);
79         }
80     }
81
82     public boolean isInSync() {
83         return true;
84     }
85
86     protected void updateTimeStamp() {
87         updateTimeStamp(fUnderlyingResource.getLocation().toFile());
88     }
89
90     public void save() {
91         if (fUnderlyingResource == null)
92             return;
93         try {
94             String JavaDoc contents = getContents();
95             ByteArrayInputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(contents
96                     .getBytes("8859_1")); //$NON-NLS-1$
97
if (fUnderlyingResource.exists()) {
98                 fUnderlyingResource.setContents(stream, false, false, null);
99             } else {
100                 fUnderlyingResource.create(stream, false, null);
101             }
102             stream.close();
103         } catch (CoreException e) {
104             PDECore.logException(e);
105         } catch (IOException JavaDoc e) {
106         }
107     }
108
109     public void save(PrintWriter JavaDoc writer) {
110         getBuild().write("", writer); //$NON-NLS-1$
111
fDirty = false;
112     }
113
114     public void setDirty(boolean dirty) {
115         fDirty = dirty;
116     }
117
118     public void setEditable(boolean editable) {
119         fEditable = editable;
120     }
121     
122     /* (non-Javadoc)
123      * @see org.eclipse.pde.core.build.IBuildModel#getInstallLocation()
124      */

125     public String JavaDoc getInstallLocation() {
126         return fUnderlyingResource.getLocation().toOSString();
127     }
128 }
129
Popular Tags