KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > site > WorkspaceSiteBuildModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.site;
12
13 import java.io.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.pde.core.*;
18 import org.eclipse.pde.internal.core.*;
19
20 public class WorkspaceSiteBuildModel
21     extends AbstractSiteBuildModel
22     implements IEditableModel {
23     private boolean dirty;
24     private IFile file;
25     private boolean editable = true;
26
27     public WorkspaceSiteBuildModel() {
28     }
29     public WorkspaceSiteBuildModel(IFile file) {
30         setFile(file);
31     }
32     public void fireModelChanged(IModelChangedEvent event) {
33         dirty = true;
34         super.fireModelChanged(event);
35     }
36
37     public String JavaDoc getContents() {
38         StringWriter swriter = new StringWriter();
39         PrintWriter writer = new PrintWriter(swriter);
40         loaded = true;
41         save(writer);
42         writer.flush();
43         try {
44             swriter.close();
45         } catch (IOException e) {
46         }
47         return swriter.toString();
48     }
49
50     public IFile getFile() {
51         return file;
52     }
53
54     public String JavaDoc getInstallLocation() {
55         return file.getParent().getLocation().toOSString();
56     }
57     public IResource getUnderlyingResource() {
58         return file;
59     }
60     public boolean isDirty() {
61         return dirty;
62     }
63     public boolean isEditable() {
64         return editable;
65     }
66
67     public boolean isInSync() {
68         return isInSync(file.getLocation().toFile());
69     }
70
71     protected void updateTimeStamp() {
72         updateTimeStamp(file.getLocation().toFile());
73     }
74
75     public void load() {
76         if (file == null)
77             return;
78         if (file.exists()) {
79             boolean outOfSync = false;
80             InputStream stream = null;
81             try {
82                 stream = file.getContents(false);
83             } catch (CoreException e) {
84                 outOfSync = true;
85                 try {
86                     stream = file.getContents(true);
87                 } catch (CoreException ex) {
88                     return;
89                 }
90             }
91             try {
92                 load(stream, outOfSync);
93                 stream.close();
94             } catch (CoreException e) {
95             } catch (IOException e) {
96                 PDECore.logException(e);
97             }
98         } else {
99             this.siteBuild = new SiteBuild();
100             siteBuild.model = this;
101             loaded = true;
102         }
103     }
104     public void save() {
105         if (file == null)
106             return;
107         try {
108             String JavaDoc contents = getContents();
109             ByteArrayInputStream stream =
110                 new ByteArrayInputStream(contents.getBytes("UTF8")); //$NON-NLS-1$
111
if (file.exists()) {
112                 file.setContents(stream, false, false, null);
113             } else {
114                 createFolder(file, null);
115                 file.create(stream, false, null);
116             }
117             stream.close();
118         } catch (CoreException e) {
119             PDECore.logException(e);
120         } catch (IOException e) {
121         }
122     }
123     public void save(PrintWriter writer) {
124         if (isLoaded()) {
125             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
126
siteBuild.write("", writer); //$NON-NLS-1$
127
}
128         dirty = false;
129     }
130     private void createFolder(IFile file, IProgressMonitor monitor) throws CoreException {
131         if (file.exists())
132             return;
133         IProject project = file.getProject();
134         IPath path = file.getProjectRelativePath();
135         for (int i = path.segmentCount()-1; i>0; i--){
136             IFolder folder = project.getFolder(path.removeLastSegments(i));
137             if (!folder.exists())
138                 folder.create(true, true, monitor);
139         }
140     }
141     public void setDirty(boolean dirty) {
142         this.dirty = dirty;
143     }
144     public void setEditable(boolean newEditable) {
145         editable = newEditable;
146     }
147     public void setFile(IFile newFile) {
148         file = newFile;
149     }
150 }
151
Popular Tags