KickJava   Java API By Example, From Geeks To Geeks.

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


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.site;
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 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.pde.core.IEditableModel;
27 import org.eclipse.pde.core.IModelChangedEvent;
28 import org.eclipse.pde.internal.core.NLResourceHelper;
29 import org.eclipse.pde.internal.core.PDECore;
30
31 public class WorkspaceSiteModel
32     extends AbstractSiteModel
33     implements IEditableModel {
34     private static final long serialVersionUID = 1L;
35     private boolean fDirty;
36     private IFile fFile;
37     private boolean fEditable = true;
38
39     public WorkspaceSiteModel(IFile file) {
40         fFile = file;
41     }
42     public void fireModelChanged(IModelChangedEvent event) {
43         setDirty(event.getChangeType()!=IModelChangedEvent.WORLD_CHANGED);
44         super.fireModelChanged(event);
45     }
46
47     protected NLResourceHelper createNLResourceHelper() {
48         try {
49             IPath path = fFile.getLocation().removeLastSegments(1);
50             String JavaDoc installLocation = path.toOSString();
51             if (installLocation.startsWith("file:") == false) //$NON-NLS-1$
52
installLocation = "file:" + installLocation; //$NON-NLS-1$
53
URL JavaDoc url = new URL JavaDoc(installLocation + "/"); //$NON-NLS-1$
54
String JavaDoc name = "site"; //$NON-NLS-1$
55
NLResourceHelper helper =
56                 new NLResourceHelper(name, new URL JavaDoc[] { url });
57             return helper;
58         } catch (MalformedURLException JavaDoc e) {
59             return null;
60         }
61     }
62
63     public String JavaDoc getContents() {
64         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
65         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
66         setLoaded(true);
67         save(writer);
68         writer.flush();
69         try {
70             swriter.close();
71         } catch (IOException JavaDoc e) {
72         }
73         return swriter.toString();
74     }
75     public IFile getFile() {
76         return fFile;
77     }
78     public String JavaDoc getInstallLocation() {
79         return fFile.getParent().getLocation().toOSString();
80     }
81     public IResource getUnderlyingResource() {
82         return fFile;
83     }
84     public boolean isDirty() {
85         return fDirty;
86     }
87     public boolean isEditable() {
88         return fEditable;
89     }
90
91     public boolean isInSync() {
92         return isInSync(fFile.getLocation().toFile());
93     }
94
95     protected void updateTimeStamp() {
96         updateTimeStamp(fFile.getLocation().toFile());
97     }
98     public void load() {
99         if (fFile.exists()) {
100             InputStream JavaDoc stream = null;
101             try {
102                 stream = new BufferedInputStream JavaDoc(fFile.getContents(true));
103                 load(stream, false);
104             } catch (CoreException e) {
105             } finally {
106                 try {
107                     if (stream != null)
108                         stream.close();
109                 } catch (IOException JavaDoc e) {
110                 }
111             }
112         } else {
113             this.site = new Site();
114             site.model = this;
115             setLoaded(true);
116         }
117     }
118     public void save() {
119         try {
120             String JavaDoc contents = getContents();
121             ByteArrayInputStream JavaDoc stream =
122                 new ByteArrayInputStream JavaDoc(contents.getBytes("UTF8")); //$NON-NLS-1$
123
if (fFile.exists()) {
124                 fFile.setContents(stream, false, false, null);
125             } else {
126                 fFile.create(stream, false, null);
127             }
128             stream.close();
129         } catch (CoreException e) {
130             PDECore.logException(e);
131         } catch (IOException JavaDoc e) {
132         }
133     }
134     public void save(PrintWriter JavaDoc writer) {
135         if (isLoaded()) {
136             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
137
site.write("", writer); //$NON-NLS-1$
138
}
139         setDirty(false);
140     }
141     public void setDirty(boolean dirty) {
142         fDirty = dirty;
143     }
144
145     public void setEditable(boolean newEditable) {
146         fEditable = newEditable;
147     }
148 }
149
Popular Tags