summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core.ui/src/org/genivi/commonapi/core/ui/preferences/PropertyStore.java
blob: e7d4d2d83bc3f1f003cc2cc777eee0002e23332f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2013 BMW Group Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de) This Source Code Form is
 * subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the
 * MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 */

package org.genivi.commonapi.core.ui.preferences;

import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceStore;

public class PropertyStore extends PreferenceStore {

    private IResource resource;
    private IPreferenceStore workbenchStore;
    private String pageId;
    private boolean inserting = false;

    public PropertyStore(IResource resource, IPreferenceStore workbenchStore, String pageId) throws IOException {

        this.resource = resource;
        this.workbenchStore = workbenchStore;
        this.pageId = pageId;
    }

    /*** Write modified values back to properties ***/

    /*
     * (non-Javadoc)
     * @see org.eclipse.jface.preference.IPersistentPreferenceStore#save()
     */
    public void save() throws IOException {

        writeProperties();    	
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.PreferenceStore#save(java.io.OutputStream,
     * java.lang.String)
     */
    public void save(OutputStream out, String header) throws IOException {
        writeProperties();
    }

    /**
     * Writes modified preferences into resource properties.
     */
    private void writeProperties() throws IOException {
        String[] preferences = super.preferenceNames();
        for (int i = 0; i < preferences.length; i++) {
            String name = preferences[i];
            try {
                setProperty(name, getString(name));
            } catch (CoreException e) {
                throw new IOException(Messages.getString("PropertyStore.Cannot_write_resource_property") + name); //$NON-NLS-1$
            }
        }
    }

    /**
     * Convenience method to set a property
     * @param name
     *            - the preference name
     * @param value
     *            - the property value or null to delete the property
     * @throws CoreException
     */
    private void setProperty(String name, String value) throws CoreException {
        resource.setPersistentProperty(new QualifiedName(pageId, name), value);
    }

    /*** Get default values (Delegate to workbench store) ***/

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.
     * lang.String)
     */
    public boolean getDefaultBoolean(String name) {
        return workbenchStore.getDefaultBoolean(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang
     * .String)
     */
    public double getDefaultDouble(String name) {
        return workbenchStore.getDefaultDouble(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang
     * .String)
     */
    public float getDefaultFloat(String name) {
        return workbenchStore.getDefaultFloat(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang
     * .String)
     */
    public int getDefaultInt(String name) {
        return workbenchStore.getDefaultInt(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang
     * .String)
     */
    public long getDefaultLong(String name) {
        return workbenchStore.getDefaultLong(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang
     * .String)
     */
    public String getDefaultString(String name) {
        return workbenchStore.getDefaultString(name);
    }

    /*** Get property values ***/

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String
     * )
     */
    public boolean getBoolean(String name) {
        insertValue(name);
        return super.getBoolean(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String)
     */
    public double getDouble(String name) {
        insertValue(name);
        return super.getDouble(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String)
     */
    public float getFloat(String name) {
        insertValue(name);
        return super.getFloat(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String)
     */
    public int getInt(String name) {
        insertValue(name);
        return super.getInt(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String)
     */
    public long getLong(String name) {
        insertValue(name);
        return super.getLong(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String)
     */
    public String getString(String name) {
        insertValue(name);
        return super.getString(name);
    }

    /**
     * @param name
     */
    private synchronized void insertValue(String name) {
        if (inserting)
            return;
        if (super.contains(name))
            return;
        inserting = true;
        String prop = null;
        try {
            prop = getProperty(name);
        } catch (CoreException e) {
        }
        if (prop == null)
            prop = workbenchStore.getString(name);
        if (prop != null)
            setValue(name, prop);
        inserting = false;
    }

    /**
     * Convenience method to fetch a property
     * @param name
     *            - the preference name
     * @return - the property value
     * @throws CoreException
     */
    private String getProperty(String name) throws CoreException {
        return resource.getPersistentProperty(new QualifiedName(pageId, name));
    }

    /*** Misc ***/

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String)
     */
    public boolean contains(String name) {
        return workbenchStore.contains(name);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.
     * String)
     */
    public void setToDefault(String name) {
        setValue(name, getDefaultString(name));
    }

    /*
     * (non-Javadoc)
     * @see
     * org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String)
     */
    public boolean isDefault(String name) {
        String defaultValue = getDefaultString(name);
        if (defaultValue == null)
            return false;
        return defaultValue.equals(getString(name));
    }

}