OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Rev 65 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.preferences;
15
 
16
import org.openconcerto.utils.FileUtils;
17
 
18
import java.io.BufferedInputStream;
19
import java.io.BufferedOutputStream;
20
import java.io.File;
21
import java.io.FileInputStream;
22
import java.io.FileOutputStream;
23
import java.io.IOException;
24
import java.util.Properties;
25
 
26
import javax.swing.JFrame;
27
import javax.swing.JOptionPane;
28
 
29
public abstract class AbstractProps {
30
 
31
    protected final Properties props = new Properties();
32
 
33
    protected AbstractProps() {
34
        load();
35
    }
36
 
37
    public Properties getProps() {
38
        return this.props;
39
    }
40
 
41
    protected abstract String getPropsFileName();
42
 
43
    public final boolean contains(String name) {
44
        return this.props.containsKey(name + getPropertySuffix());
45
    }
46
 
47
    public String getPropertySuffix() {
48
        return "";
49
    }
50
 
51
    /**
52
     * Return the property for the passed key.
53
     *
54
     * @param name the key.
55
     * @return the matching string, never <code>null</code>.
56
     * @see #getDefaultStringValue()
57
     */
58
    public String getStringProperty(String name) {
59
        final String property = getProperty(name);
60
        if (property == null)
61
            return getDefaultStringValue();
62
        else
63
            return property;
64
    }
65
 
66
    /**
67
     * Return the property for the passed key.
68
     *
69
     * @param name the key.
70
     * @return the matching property value or <code>null</code>.
71
     */
72
    public final String getProperty(String name) {
73
        final String key = name + getPropertySuffix();
74
        final String property = this.props.getProperty(key);
75
        return property;
76
    }
77
 
19 ilm 78
    /**
79
     * Return Boolean.TRUE only if the property is defined and equals to "true". If the property is
80
     * not defined or not equals to "true", return Boolean.FALSE
81
     * */
17 ilm 82
    public final Boolean getBooleanValue(String name) {
19 ilm 83
        final String property = this.getProperty(name);
84
        if (property == null) {
85
            return Boolean.FALSE;
86
        }
87
        return Boolean.valueOf(property);
17 ilm 88
    }
89
 
19 ilm 90
    public final boolean getBooleanValue(String name, boolean defaultValue) {
91
        final String property = this.getProperty(name);
92
        if (property == null) {
93
            return defaultValue;
94
        }
95
        return Boolean.valueOf(property);
96
    }
97
 
17 ilm 98
    public String getDefaultStringValue() {
99
        return "";
100
    }
101
 
20 ilm 102
    public final int getIntProperty(String name) {
103
        return getIntProperty(name, getDefautIntValue());
104
    }
105
 
106
    public final int getIntProperty(String name, int defaultVal) {
17 ilm 107
        final String property = this.getProperty(name);
20 ilm 108
        return property == null ? defaultVal : Integer.valueOf(property).intValue();
17 ilm 109
    }
110
 
111
    protected int getDefautIntValue() {
112
        return -1;
113
    }
114
 
115
    /**
116
     * Set or remove a property.
117
     *
118
     * @param key the key.
119
     * @param value the value, <code>null</code> meaning remove.
120
     */
121
    public void setProperty(String key, String value) {
122
        final String fullKey = key + getPropertySuffix();
123
        if (value == null)
124
            this.props.remove(fullKey);
125
        else
126
            this.props.setProperty(fullKey, value);
127
    }
128
 
129
    public void load() {
130
        final File file = new File(getPropsFileName());
25 ilm 131
        System.out.println("Loading properties from " + file.getAbsolutePath());
132
        if (!file.exists()) {
133
            System.out.println("Warning: " + file.getAbsolutePath() + " does not exist");
17 ilm 134
            return;
25 ilm 135
        }
17 ilm 136
        BufferedInputStream bufferedInputStream = null;
137
        try {
138
            final FileInputStream fileInputStream = new FileInputStream(file);
139
            bufferedInputStream = new BufferedInputStream(fileInputStream);
140
            this.props.load(bufferedInputStream);
141
        } catch (IOException e) {
142
            JOptionPane.showMessageDialog(new JFrame(), "Impossible de lire le fichier de configuration:\n" + file.getAbsolutePath());
143
            e.printStackTrace();
144
        } finally {
145
            if (bufferedInputStream != null) {
146
                try {
147
                    bufferedInputStream.close();
148
                } catch (IOException e) {
149
                    e.printStackTrace();
150
                }
151
            }
152
        }
153
    }
154
 
155
    public void store() {
156
        BufferedOutputStream bufferedOutputStream = null;
157
        final File file = new File(getPropsFileName());
158
        try {
159
            FileUtils.mkdir_p(file.getParentFile());
160
            final FileOutputStream fileOutputStream = new FileOutputStream(file);
161
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
162
            this.props.store(bufferedOutputStream, "");
163
        } catch (IOException e) {
164
            JOptionPane.showMessageDialog(new JFrame(), "Impossible d'enregistrer le fichier de configuration:\n" + file.getAbsolutePath());
165
            e.printStackTrace();
166
        } finally {
167
            if (bufferedOutputStream != null) {
168
                try {
169
                    bufferedOutputStream.close();
170
                } catch (IOException e) {
171
                    e.printStackTrace();
172
                }
173
            }
174
        }
175
    }
176
}