Adding in new phpeclipse-projectSet_1.2.x.psf for svn export.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / overlaypages / PropertyStore.java
1 /*******************************************************************************
2  * Copyright (c) 2003 Berthold Daum.
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  *     Berthold Daum
10  *******************************************************************************/
11
12 package net.sourceforge.phpeclipse.ui.overlaypages;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.QualifiedName;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.preference.PreferenceStore;
22
23 /**
24  * @author Berthold Daum
25  * 
26  */
27 public class PropertyStore extends PreferenceStore {
28
29         private IResource resource;
30
31         private IPreferenceStore workbenchStore;
32
33         private String pageId;
34
35         private boolean inserting = false;
36
37         public PropertyStore(IResource resource, IPreferenceStore workbenchStore,
38                         String pageId) {
39                 this.resource = resource;
40                 this.workbenchStore = workbenchStore;
41                 this.pageId = pageId;
42         }
43
44         /** * Write modified values back to properties ** */
45
46         /*
47          * (non-Javadoc)
48          * 
49          * @see org.eclipse.jface.preference.IPersistentPreferenceStore#save()
50          */
51         public void save() throws IOException {
52                 writeProperties();
53         }
54
55         /*
56          * (non-Javadoc)
57          * 
58          * @see org.eclipse.jface.preference.PreferenceStore#save(java.io.OutputStream,
59          *      java.lang.String)
60          */
61         public void save(OutputStream out, String header) throws IOException {
62                 writeProperties();
63         }
64
65         /**
66          * Writes modified preferences into resource properties.
67          */
68         private void writeProperties() throws IOException {
69                 String[] preferences = super.preferenceNames();
70                 for (int i = 0; i < preferences.length; i++) {
71                         String name = preferences[i];
72                         try {
73                                 setProperty(name, getString(name));
74                         } catch (CoreException e) {
75                                 throw new IOException(
76                                                 Messages
77                                                                 .getString("PropertyStore.Cannot_write_resource_property") + name); //$NON-NLS-1$
78                         }
79                 }
80         }
81
82         /**
83          * Convenience method to set a property
84          * 
85          * @param name -
86          *            the preference name
87          * @param value -
88          *            the property value or null to delete the property
89          * @throws CoreException
90          */
91         private void setProperty(String name, String value) throws CoreException {
92                 resource.setPersistentProperty(new QualifiedName(pageId, name), value);
93         }
94
95         /** * Get default values (Delegate to workbench store) ** */
96
97         /*
98          * (non-Javadoc)
99          * 
100          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.lang.String)
101          */
102         public boolean getDefaultBoolean(String name) {
103                 return workbenchStore.getDefaultBoolean(name);
104         }
105
106         /*
107          * (non-Javadoc)
108          * 
109          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang.String)
110          */
111         public double getDefaultDouble(String name) {
112                 return workbenchStore.getDefaultDouble(name);
113         }
114
115         /*
116          * (non-Javadoc)
117          * 
118          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang.String)
119          */
120         public float getDefaultFloat(String name) {
121                 return workbenchStore.getDefaultFloat(name);
122         }
123
124         /*
125          * (non-Javadoc)
126          * 
127          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang.String)
128          */
129         public int getDefaultInt(String name) {
130                 return workbenchStore.getDefaultInt(name);
131         }
132
133         /*
134          * (non-Javadoc)
135          * 
136          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang.String)
137          */
138         public long getDefaultLong(String name) {
139                 return workbenchStore.getDefaultLong(name);
140         }
141
142         /*
143          * (non-Javadoc)
144          * 
145          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang.String)
146          */
147         public String getDefaultString(String name) {
148                 return workbenchStore.getDefaultString(name);
149         }
150
151         /** * Get property values ** */
152
153         /*
154          * (non-Javadoc)
155          * 
156          * @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String)
157          */
158         public boolean getBoolean(String name) {
159                 insertValue(name);
160                 return super.getBoolean(name);
161         }
162
163         /*
164          * (non-Javadoc)
165          * 
166          * @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String)
167          */
168         public double getDouble(String name) {
169                 insertValue(name);
170                 return super.getDouble(name);
171         }
172
173         /*
174          * (non-Javadoc)
175          * 
176          * @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String)
177          */
178         public float getFloat(String name) {
179                 insertValue(name);
180                 return super.getFloat(name);
181         }
182
183         /*
184          * (non-Javadoc)
185          * 
186          * @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String)
187          */
188         public int getInt(String name) {
189                 insertValue(name);
190                 return super.getInt(name);
191         }
192
193         /*
194          * (non-Javadoc)
195          * 
196          * @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String)
197          */
198         public long getLong(String name) {
199                 insertValue(name);
200                 return super.getLong(name);
201         }
202
203         /*
204          * (non-Javadoc)
205          * 
206          * @see org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String)
207          */
208         public String getString(String name) {
209                 insertValue(name);
210                 return super.getString(name);
211         }
212
213         /**
214          * @param name
215          */
216         private synchronized void insertValue(String name) {
217                 if (inserting)
218                         return;
219                 if (super.contains(name))
220                         return;
221                 inserting = true;
222                 String prop = null;
223                 try {
224                         prop = getProperty(name);
225                 } catch (CoreException e) {
226                 }
227                 if (prop == null)
228                         prop = workbenchStore.getString(name);
229                 if (prop != null)
230                         setValue(name, prop);
231                 inserting = false;
232         }
233
234         /**
235          * Convenience method to fetch a property
236          * 
237          * @param name -
238          *            the preference name
239          * @return - the property value
240          * @throws CoreException
241          */
242         private String getProperty(String name) throws CoreException {
243                 return resource.getPersistentProperty(new QualifiedName(pageId, name));
244         }
245
246         /** * Misc ** */
247
248         /*
249          * (non-Javadoc)
250          * 
251          * @see org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String)
252          */
253         public boolean contains(String name) {
254                 return workbenchStore.contains(name);
255         }
256
257         /*
258          * (non-Javadoc)
259          * 
260          * @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.String)
261          */
262         public void setToDefault(String name) {
263                 setValue(name, getDefaultString(name));
264         }
265
266         /*
267          * (non-Javadoc)
268          * 
269          * @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String)
270          */
271         public boolean isDefault(String name) {
272                 String defaultValue = getDefaultString(name);
273                 if (defaultValue == null)
274                         return false;
275                 return defaultValue.equals(getString(name));
276         }
277
278 }