Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd39e16cd4926b07679f5005750ea13c09a3af9a (plain) (blame)
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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.internal.util.om.pref;

import org.eclipse.net4j.internal.util.bundle.AbstractBundle;
import org.eclipse.net4j.internal.util.event.Notifier;
import org.eclipse.net4j.util.io.IORunnable;
import org.eclipse.net4j.util.io.IORuntimeException;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.om.pref.OMPreference;
import org.eclipse.net4j.util.om.pref.OMPreferences;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Eike Stepper
 */
public class Preferences extends Notifier implements OMPreferences
{
  public static final boolean DEFAULT_BOOLEAN = false;

  public static final int DEFAULT_INTEGER = 0;

  public static final long DEFAULT_LONG = 0L;

  public static final float DEFAULT_FLOAT = 0.0f;

  public static final double DEFAULT_DOUBLE = 0.0d;

  public static final String DEFAULT_STRING = "";

  private AbstractBundle bundle;

  private Map<String, Preference> prefs = new HashMap();

  private boolean loaded;

  private boolean dirty;

  public Preferences(AbstractBundle bundle)
  {
    this.bundle = bundle;
  }

  public AbstractBundle getBundle()
  {
    return bundle;
  }

  public synchronized void load()
  {
    if (!loaded)
    {
      loaded = true;
      File file = getFile();
      if (file.exists())
      {
        final Properties properties = new Properties();
        IOUtil.input(file, new IORunnable<FileInputStream>()
        {
          public void run(FileInputStream io) throws IOException
          {
            properties.load(io);
          }
        });

        for (Preference preference : prefs.values())
        {
          String name = preference.getName();
          String value = properties.getProperty(name);
          if (value != null)
          {
            preference.init(value);
          }
        }
      }
    }
  }

  public synchronized void save()
  {
    if (dirty)
    {
      final Properties properties = new Properties();
      for (Preference preference : prefs.values())
      {
        if (preference.isSet())
        {
          String name = preference.getName();
          String value = preference.getString();
          properties.put(name, value);
        }
      }

      File file = getFile();
      if (properties.isEmpty())
      {
        if (file.exists())
        {
          file.delete();
        }
      }
      else
      {
        IOUtil.output(file, new IORunnable<FileOutputStream>()
        {
          public void run(FileOutputStream io) throws IOException
          {
            properties.store(io, "Preferences of " + bundle.getBundleID());
          }
        });
      }

      dirty = false;
    }
  }

  public boolean isDirty()
  {
    return dirty;
  }

  public OMPreference<Boolean> init(String name, boolean defaultValue)
  {
    return new BooleanPreference(this, name, defaultValue);
  }

  public OMPreference<Integer> init(String name, int defaultValue)
  {
    return new IntegerPreference(this, name, defaultValue);
  }

  public OMPreference<Long> init(String name, long defaultValue)
  {
    return new LongPreference(this, name, defaultValue);
  }

  public OMPreference<Float> init(String name, float defaultValue)
  {
    return new FloatPreference(this, name, defaultValue);
  }

  public OMPreference<Double> init(String name, double defaultValue)
  {
    return new DoublePreference(this, name, defaultValue);
  }

  public OMPreference<String> init(String name, String defaultValue)
  {
    return new StringPreference(this, name, defaultValue);
  }

  public OMPreference<Boolean> initBoolean(String name)
  {
    return new BooleanPreference(this, name, DEFAULT_BOOLEAN);
  }

  public OMPreference<Integer> initInteger(String name)
  {
    return new IntegerPreference(this, name, DEFAULT_INTEGER);
  }

  public OMPreference<Long> initLong(String name)
  {
    return new LongPreference(this, name, DEFAULT_LONG);
  }

  public OMPreference<Float> initFloat(String name)
  {
    return new FloatPreference(this, name, DEFAULT_FLOAT);
  }

  public OMPreference<Double> initDouble(String name)
  {
    return new DoublePreference(this, name, DEFAULT_DOUBLE);
  }

  public OMPreference<String> initString(String name)
  {
    return new StringPreference(this, name, DEFAULT_STRING);
  }

  public OMPreference<Boolean> getBoolean(String name)
  {
    return null;
  }

  public OMPreference<Integer> getInteger(String name)
  {
    return null;
  }

  public OMPreference<Long> getLong(String name)
  {
    return null;
  }

  public OMPreference<Float> getFloat(String name)
  {
    return null;
  }

  public OMPreference<Double> getDouble(String name)
  {
    return null;
  }

  public OMPreference<String> getString(String name)
  {
    return null;
  }

  public <T> void fireChangeEvent(Preference<T> preference, T oldValue, T newValue)
  {
    fireEvent(new PreferencesChangeEvent<T>(preference, oldValue, newValue));
  }

  private File getFile()
  {
    File file = new File(bundle.getStateLocation(), ".prefs");
    if (file.exists() && !file.isFile())
    {
      throw new IORuntimeException("Not a file: " + file.getAbsolutePath());
    }

    return file;
  }
}

Back to the top