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
10 *******************************************************************************/
12 package net.sourceforge.phpeclipse.overlaypages;
14 import java.io.IOException;
15 import java.io.OutputStream;
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;
24 * @author Berthold Daum
27 public class PropertyStore extends PreferenceStore {
29 private IResource resource;
30 private IPreferenceStore workbenchStore;
31 private String pageId;
32 private boolean inserting = false;
36 IPreferenceStore workbenchStore,
38 this.resource = resource;
39 this.workbenchStore = workbenchStore;
43 /*** Write modified values back to properties ***/
46 * @see org.eclipse.jface.preference.IPersistentPreferenceStore#save()
48 public void save() throws IOException {
53 * @see org.eclipse.jface.preference.PreferenceStore#save(java.io.OutputStream, java.lang.String)
55 public void save(OutputStream out, String header) throws IOException {
60 * Writes modified preferences into resource properties.
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];
67 setProperty(name, getString(name));
68 } catch (CoreException e) {
69 throw new IOException(Messages.getString("PropertyStore.Cannot_write_resource_property") + name); //$NON-NLS-1$
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
80 private void setProperty(String name, String value) throws CoreException {
81 resource.setPersistentProperty(new QualifiedName(pageId, name), value);
84 /*** Get default values (Delegate to workbench store) ***/
87 * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.lang.String)
89 public boolean getDefaultBoolean(String name) {
90 return workbenchStore.getDefaultBoolean(name);
94 * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang.String)
96 public double getDefaultDouble(String name) {
97 return workbenchStore.getDefaultDouble(name);
101 * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang.String)
103 public float getDefaultFloat(String name) {
104 return workbenchStore.getDefaultFloat(name);
108 * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang.String)
110 public int getDefaultInt(String name) {
111 return workbenchStore.getDefaultInt(name);
115 * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang.String)
117 public long getDefaultLong(String name) {
118 return workbenchStore.getDefaultLong(name);
122 * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang.String)
124 public String getDefaultString(String name) {
125 return workbenchStore.getDefaultString(name);
128 /*** Get property values ***/
131 * @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String)
133 public boolean getBoolean(String name) {
135 return super.getBoolean(name);
139 * @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String)
141 public double getDouble(String name) {
143 return super.getDouble(name);
147 * @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String)
149 public float getFloat(String name) {
151 return super.getFloat(name);
155 * @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String)
157 public int getInt(String name) {
159 return super.getInt(name);
163 * @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String)
165 public long getLong(String name) {
167 return super.getLong(name);
171 * @see org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String)
173 public String getString(String name) {
175 return super.getString(name);
181 private synchronized void insertValue(String name) {
184 if (super.contains(name))
189 prop = getProperty(name);
190 } catch (CoreException e) {
193 prop = workbenchStore.getString(name);
195 setValue(name, prop);
200 * Convenience method to fetch a property
201 * @param name - the preference name
202 * @return - the property value
203 * @throws CoreException
205 private String getProperty(String name) throws CoreException {
206 return resource.getPersistentProperty(new QualifiedName(pageId, name));
212 * @see org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String)
214 public boolean contains(String name) {
215 return workbenchStore.contains(name);
219 * @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.String)
221 public void setToDefault(String name) {
222 setValue(name, getDefaultString(name));
226 * @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String)
228 public boolean isDefault(String name) {
229 String defaultValue = getDefaultString(name);
230 if (defaultValue == null) return false;
231 return defaultValue.equals(getString(name));