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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/config/TGConfigManager.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
 
 
22
/**
 
23
 * @author julian
 
24
 * 
 
25
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
26
 */
 
27
public abstract class TGConfigManager {
 
28
        
 
29
        private Properties properties;
 
30
        
 
31
        public TGConfigManager() {
 
32
                super();
 
33
        }
 
34
        
 
35
        public void init(){
 
36
                this.properties = new Properties(getDefaults());
 
37
                this.load();
 
38
        }
 
39
        
 
40
        private String getProperty(String key) {
 
41
                return this.properties.getProperty(key);
 
42
        }
 
43
        
 
44
        public String getStringConfigValue(String key,String defaultValue) {
 
45
                try{
 
46
                        String property = getProperty(key);
 
47
                        return (property == null)?defaultValue:property.trim();
 
48
                }catch(Throwable throwable){
 
49
                        throwable.printStackTrace();
 
50
                }
 
51
                return defaultValue;
 
52
        }
 
53
        
 
54
        public String getStringConfigValue(String key) {
 
55
                return this.getStringConfigValue(key,null);
 
56
        }
 
57
        
 
58
        public int getIntConfigValue(String key,int defaultValue) {
 
59
                try{
 
60
                        String value = getProperty(key);
 
61
                        return (value == null)?defaultValue:Integer.parseInt(value.trim());
 
62
                }catch(Throwable throwable){
 
63
                        throwable.printStackTrace();
 
64
                }
 
65
                return defaultValue;
 
66
        }
 
67
        
 
68
        public int getIntConfigValue(String key) {
 
69
                return this.getIntConfigValue(key,0);
 
70
        }
 
71
        
 
72
        public boolean getBooleanConfigValue(String key,boolean defaultValue) {
 
73
                try{
 
74
                        String value = getProperty(key);
 
75
                        return (value == null)?defaultValue:Boolean.valueOf(value.trim()).booleanValue();
 
76
                }catch(Throwable throwable){
 
77
                        throwable.printStackTrace();
 
78
                }
 
79
                return defaultValue;
 
80
        }
 
81
        
 
82
        public boolean getBooleanConfigValue(String key) {
 
83
                return this.getBooleanConfigValue(key,false);
 
84
        }
 
85
        
 
86
        public FontData getFontDataConfigValue(String key){
 
87
                try{
 
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
                                                String name = values[0].trim();
 
94
                                                int size = Integer.parseInt(values[1].trim());
 
95
                                                int style = Integer.parseInt(values[2].trim());
 
96
                                                return new FontData( (name == null ? "" : name),size,style);
 
97
                                        }catch(NumberFormatException e){
 
98
                                                e.printStackTrace();
 
99
                                        }
 
100
                                }
 
101
                        }
 
102
                }catch(Throwable throwable){
 
103
                        throwable.printStackTrace();
 
104
                }
 
105
                return new FontData();
 
106
        }
 
107
        
 
108
        public RGB getRGBConfigValue(String key){
 
109
                try{
 
110
                        String value = getProperty(key);
 
111
                        if(value != null){
 
112
                                String[] values = value.trim().split(",");
 
113
                                if(values != null && values.length == 3){
 
114
                                        try{
 
115
                                                int red = Integer.parseInt(values[0].trim());
 
116
                                                int green = Integer.parseInt(values[1].trim());
 
117
                                                int blue = Integer.parseInt(values[2].trim());
 
118
                                                
 
119
                                                return new RGB(red,green,blue);
 
120
                                        }catch(NumberFormatException e){
 
121
                                                e.printStackTrace();
 
122
                                        }
 
123
                                }
 
124
                        }
 
125
                }catch(Throwable throwable){
 
126
                        throwable.printStackTrace();
 
127
                }
 
128
                return null;
 
129
        }
 
130
        
 
131
        public void setProperty(String key,String value){
 
132
                this.properties.setProperty(key, (value != null ? value : new String()) );
 
133
        }
 
134
        
 
135
        public void setProperty(String key,int value){
 
136
                this.setProperty(key,Integer.toString(value));
 
137
        }
 
138
        
 
139
        public void setProperty(String key,boolean value){
 
140
                this.setProperty(key,Boolean.toString(value));
 
141
        }
 
142
        
 
143
        public void setProperty(String key,RGB rgb){
 
144
                this.setProperty(key,(rgb.red + "," + rgb.green + "," + rgb.blue));
 
145
        }
 
146
        
 
147
        public void setProperty(String key,FontData fd){
 
148
                this.setProperty(key,(fd.getName() + "," + fd.getHeight() + "," + fd.getStyle()));
 
149
        }
 
150
        
 
151
        public void setDefaults(){
 
152
                Properties defaults = new TGConfigDefaults().getProperties();
 
153
                Iterator it = defaults.entrySet().iterator();
 
154
                while(it.hasNext()){
 
155
                        Map.Entry property = (Map.Entry)it.next();
 
156
                        setProperty((String)property.getKey(),(String)property.getValue());
 
157
                }
 
158
                this.save();
 
159
        }
 
160
        
 
161
        public void removeProperty(String key){
 
162
                this.properties.remove(key);
 
163
        }
 
164
        
 
165
        public void clear(){
 
166
                this.properties.clear();
 
167
        }
 
168
        
 
169
        public void load() {
 
170
                try {
 
171
                        if(new File(getFileName()).exists()){
 
172
                                InputStream inputStream = new FileInputStream(getFileName());
 
173
                                this.properties.clear();
 
174
                                this.properties.load(inputStream);
 
175
                        }else{
 
176
                                this.save();
 
177
                        }
 
178
                } catch (Exception e) {
 
179
                        e.printStackTrace();
 
180
                }
 
181
        }
 
182
        
 
183
        public void save(){
 
184
                try {
 
185
                        File file = new File(getFileName());
 
186
                        if(!file.exists()){
 
187
                                File folder = file.getParentFile();
 
188
                                if(folder != null && !folder.exists()){
 
189
                                        folder.mkdirs();
 
190
                                }
 
191
                        }
 
192
                        this.properties.store(new FileOutputStream(file),getName());
 
193
                } catch (FileNotFoundException e1) {
 
194
                        e1.printStackTrace();
 
195
                } catch (IOException e1) {
 
196
                        e1.printStackTrace();
 
197
                }
 
198
        }
 
199
        
 
200
        public abstract String getName();
 
201
        
 
202
        public abstract String getFileName();
 
203
        
 
204
        public abstract Properties getDefaults();
 
205
        
 
206
}
 
 
b'\\ No newline at end of file'