~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/system/config/ConfigManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on 09-ene-2006
3
 
 *
4
 
 * TODO To change the template for this generated file go to
5
 
 * Window - Preferences - Java - Code Style - Code Templates
6
 
 */
7
 
package org.herac.tuxguitar.gui.system.config;
8
 
 
9
 
import java.io.File;
10
 
import java.io.FileInputStream;
11
 
import java.io.FileNotFoundException;
12
 
import java.io.FileOutputStream;
13
 
import java.io.IOException;
14
 
import java.io.InputStream;
15
 
import java.util.Iterator;
16
 
import java.util.Map;
17
 
import java.util.Properties;
18
 
 
19
 
import org.eclipse.swt.graphics.FontData;
20
 
import org.eclipse.swt.graphics.RGB;
21
 
import org.herac.tuxguitar.gui.util.TuxGuitarFileUtils;
22
 
 
23
 
/**
24
 
 * @author julian
25
 
 * 
26
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
27
 
 */
28
 
public class ConfigManager {
29
 
    private Properties properties;  
30
 
    
31
 
    public ConfigManager() {
32
 
        this.properties = new Properties(new ConfigDefaults().getProperties());
33
 
    }
34
 
 
35
 
    public void init(){         
36
 
        this.load();
37
 
    }
38
 
    
39
 
    private String getProperty(String key) {      
40
 
        return this.properties.getProperty(key);    
41
 
    }    
42
 
 
43
 
    public String getStringConfigValue(String key,String defaultValue) {
44
 
        String property = getProperty(key); 
45
 
        return (property == null)?defaultValue:property.trim();
46
 
    }    
47
 
    
48
 
    public String getStringConfigValue(String key) {
49
 
        return this.getStringConfigValue(key,null);
50
 
    }
51
 
    
52
 
    public int getIntConfigValue(String key,int defaultValue) {
53
 
        String value = getProperty(key);        
54
 
        return (value == null)?defaultValue:Integer.parseInt(value.trim());
55
 
    }      
56
 
    
57
 
    public int getIntConfigValue(String key) {
58
 
        return this.getIntConfigValue(key,0);
59
 
    }   
60
 
    
61
 
    public boolean getBooleanConfigValue(String key,boolean defaultValue) {
62
 
        String value = getProperty(key);
63
 
        return (value == null)?defaultValue:Boolean.valueOf(value.trim()).booleanValue();
64
 
    }     
65
 
    
66
 
    public boolean getBooleanConfigValue(String key) {
67
 
        return this.getBooleanConfigValue(key,false);
68
 
    }   
69
 
 
70
 
    public FontData getFontDataConfigValue(String key){
71
 
        String value = getProperty(key);
72
 
        if(value != null){
73
 
                String[] values = value.trim().split(",");
74
 
                if(values != null && values.length == 3){
75
 
                        try{
76
 
                                String name = values[0].trim();
77
 
                                int size = Integer.parseInt(values[1].trim());
78
 
                                int style = Integer.parseInt(values[2].trim());
79
 
        
80
 
                                return new FontData(name,size,style);
81
 
                        }catch(NumberFormatException e){}               
82
 
                }
83
 
        }
84
 
        return null;
85
 
    }
86
 
    
87
 
    public RGB getRGBConfigValue(String key){
88
 
        String value = getProperty(key);
89
 
        if(value != null){
90
 
                String[] values = value.trim().split(",");
91
 
                if(values != null && values.length == 3){
92
 
                        try{
93
 
                                int red = Integer.parseInt(values[0].trim());
94
 
                                int green = Integer.parseInt(values[1].trim());
95
 
                                int blue = Integer.parseInt(values[2].trim());
96
 
        
97
 
                                return new RGB(red,green,blue);
98
 
                        }catch(NumberFormatException e){}               
99
 
                }
100
 
        }
101
 
        return null;
102
 
    }    
103
 
    
104
 
    public void setProperty(String key,String value){
105
 
        this.properties.setProperty(key,value);
106
 
    }
107
 
    
108
 
    public void setProperty(String key,int value){
109
 
        setProperty(key,Integer.toString(value));
110
 
    }
111
 
    
112
 
    public void setProperty(String key,boolean value){
113
 
        setProperty(key,Boolean.toString(value));
114
 
    }
115
 
    
116
 
    public void setDefaults(){
117
 
        Properties defaults = new ConfigDefaults().getProperties();
118
 
        Iterator it = defaults.entrySet().iterator();
119
 
        while(it.hasNext()){
120
 
                Map.Entry property = (Map.Entry)it.next();              
121
 
                setProperty((String)property.getKey(),(String)property.getValue());
122
 
        }       
123
 
        this.save();
124
 
    }
125
 
    
126
 
    public void removeProperty(String key){
127
 
        this.properties.remove(key);        
128
 
    }
129
 
    
130
 
    public void clear(){
131
 
        this.properties.clear();
132
 
    }
133
 
    
134
 
    public void load() {       
135
 
        try {                    
136
 
                if(new File(getFileName()).exists()){           
137
 
                        InputStream inputStream = new FileInputStream(getFileName());                        
138
 
                        this.properties.clear();
139
 
                        this.properties.load(inputStream);
140
 
                }else{
141
 
                        this.save();
142
 
                }
143
 
        } catch (Exception e) {
144
 
            e.printStackTrace();
145
 
        }
146
 
    }
147
 
    
148
 
    public void save(){
149
 
        try {
150
 
            properties.store(new FileOutputStream(getFileName()),"System Configuration");
151
 
        } catch (FileNotFoundException e1) {
152
 
            e1.printStackTrace();
153
 
        } catch (IOException e1) {
154
 
            e1.printStackTrace();
155
 
        }
156
 
    }
157
 
    
158
 
    private String getFileName(){
159
 
        return TuxGuitarFileUtils.USER_CONFIG_PREFIX + File.separator + "config.properties";
160
 
    }
161
 
 
162
 
}
 
 
b'\\ No newline at end of file'