replaced a lot of deprecated code; if someone runs into a commit conflict afterwards...
[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         private IPreferenceStore workbenchStore;
31         private String pageId;
32         private boolean inserting = false;
33
34         public PropertyStore(
35                 IResource resource,
36                 IPreferenceStore workbenchStore,
37                 String pageId) {
38                 this.resource = resource;
39                 this.workbenchStore = workbenchStore;
40                 this.pageId = pageId;
41         }
42
43         /*** Write modified values back to properties ***/
44
45         /* (non-Javadoc)
46          * @see org.eclipse.jface.preference.IPersistentPreferenceStore#save()
47          */
48         public void save() throws IOException {
49                 writeProperties();
50         }
51
52         /* (non-Javadoc)
53          * @see org.eclipse.jface.preference.PreferenceStore#save(java.io.OutputStream, java.lang.String)
54          */
55         public void save(OutputStream out, String header) throws IOException {
56                 writeProperties();
57         }
58
59         /**
60          * Writes modified preferences into resource properties.
61          */
62         private void writeProperties() throws IOException {
63                 String[] preferences = super.preferenceNames();
64                 for (int i = 0; i < preferences.length; i++) {
65                         String name = preferences[i];
66                         try {
67                                 setProperty(name, getString(name));
68                         } catch (CoreException e) {
69                                 throw new IOException(Messages.getString("PropertyStore.Cannot_write_resource_property") + name); //$NON-NLS-1$
70                         }
71                 }
72         }
73
74         /**
75          * Convenience method to set a property
76          * @param name - the preference name
77          * @param value - the property value or null to delete the property
78          * @throws CoreException
79          */
80         private void setProperty(String name, String value) throws CoreException {
81                 resource.setPersistentProperty(new QualifiedName(pageId, name), value);
82         }
83
84         /*** Get default values (Delegate to workbench store) ***/
85
86         /* (non-Javadoc)
87          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.lang.String)
88          */
89         public boolean getDefaultBoolean(String name) {
90                 return workbenchStore.getDefaultBoolean(name);
91         }
92
93         /* (non-Javadoc)
94          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang.String)
95          */
96         public double getDefaultDouble(String name) {
97                 return workbenchStore.getDefaultDouble(name);
98         }
99
100         /* (non-Javadoc)
101          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang.String)
102          */
103         public float getDefaultFloat(String name) {
104                 return workbenchStore.getDefaultFloat(name);
105         }
106
107         /* (non-Javadoc)
108          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang.String)
109          */
110         public int getDefaultInt(String name) {
111                 return workbenchStore.getDefaultInt(name);
112         }
113
114         /* (non-Javadoc)
115          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang.String)
116          */
117         public long getDefaultLong(String name) {
118                 return workbenchStore.getDefaultLong(name);
119         }
120
121         /* (non-Javadoc)
122          * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang.String)
123          */
124         public String getDefaultString(String name) {
125                 return workbenchStore.getDefaultString(name);
126         }
127
128         /*** Get property values ***/
129
130         /* (non-Javadoc)
131          * @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String)
132          */
133         public boolean getBoolean(String name) {
134                 insertValue(name);
135                 return super.getBoolean(name);
136         }
137
138         /* (non-Javadoc)
139          * @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String)
140          */
141         public double getDouble(String name) {
142                 insertValue(name);
143                 return super.getDouble(name);
144         }
145
146         /* (non-Javadoc)
147          * @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String)
148          */
149         public float getFloat(String name) {
150                 insertValue(name);
151                 return super.getFloat(name);
152         }
153
154         /* (non-Javadoc)
155          * @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String)
156          */
157         public int getInt(String name) {
158                 insertValue(name);
159                 return super.getInt(name);
160         }
161
162         /* (non-Javadoc)
163          * @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String)
164          */
165         public long getLong(String name) {
166                 insertValue(name);
167                 return super.getLong(name);
168         }
169
170         /* (non-Javadoc)
171          * @see org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String)
172          */
173         public String getString(String name) {
174                 insertValue(name);
175                 return super.getString(name);
176         }
177
178         /**
179          * @param name
180          */
181         private synchronized void insertValue(String name) {
182                 if (inserting)
183                         return;
184                 if (super.contains(name))
185                         return;
186                 inserting = true;
187                 String prop = null;
188                 try {
189                         prop = getProperty(name);
190                 } catch (CoreException e) {
191                 }
192                 if (prop == null)
193                         prop = workbenchStore.getString(name);
194                 if (prop != null)
195                         setValue(name, prop);
196                 inserting = false;
197         }
198
199         /**
200          * Convenience method to fetch a property
201          * @param name - the preference name
202          * @return - the property value
203          * @throws CoreException
204          */
205         private String getProperty(String name) throws CoreException {
206                 return resource.getPersistentProperty(new QualifiedName(pageId, name));
207         }
208
209         /*** Misc ***/
210
211         /* (non-Javadoc)
212          * @see org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String)
213          */
214         public boolean contains(String name) {
215                 return workbenchStore.contains(name);
216         }
217
218         /* (non-Javadoc)
219          * @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.String)
220          */
221         public void setToDefault(String name) {
222                         setValue(name, getDefaultString(name));
223         }
224
225         /* (non-Javadoc)
226          * @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String)
227          */
228         public boolean isDefault(String name) {
229                 String defaultValue = getDefaultString(name);
230                 if (defaultValue == null) return false;
231                 return defaultValue.equals(getString(name));
232         }
233
234 }