KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > feature > WorkspaceFeatureModel


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