1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.jivalo.editor;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InvalidClassException;
24 import java.io.InvalidObjectException;
25 import java.util.Properties;
26
27 import javax.swing.JFileChooser;
28
29 /**
30 * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
31 *
32 */
33 public final class PropertiesUtils
34 {
35
36 private static final PropertiesUtils INSTANCE = new PropertiesUtils();
37
38 private static final String HOME_DIR = ".settings";
39
40 private static final String PROPERTIES_FILE_NAME =
41 "jivalo.editor.properties";
42
43 private static final String PROPERTIES_FILE;
44
45 static {
46
47 String userDir = System.getProperty("user.dir");
48
49 String propertiesDir;
50
51 if (userDir.endsWith(File.separator)) {
52 propertiesDir = userDir+HOME_DIR+File.separator;
53 } else {
54 propertiesDir = userDir+File.separator+HOME_DIR+File.separator;
55 }
56
57 File dir = new File(propertiesDir);
58
59 if ( !dir.exists() )
60 {
61 dir.mkdirs();
62 }
63
64 PROPERTIES_FILE = propertiesDir+PROPERTIES_FILE_NAME;
65
66 }
67
68 private JIvaloEditorProperties defaultProperties;
69
70 private JIvaloEditorProperties currentProperties;
71
72 private JFileChooser fileChooser;
73
74 private PropertiesUtils()
75 {
76 super();
77 }
78
79 public static PropertiesUtils getInstance() {
80 return INSTANCE;
81 }
82
83 public JFileChooser getFileChooser() {
84
85 if ( this.fileChooser == null )
86 {
87 this.fileChooser = new JFileChooser();
88 }
89 return this.fileChooser;
90 }
91
92 public JIvaloEditorProperties getEditorProperties()
93 throws InvalidClassException, InvalidObjectException
94 {
95
96 if ( this.currentProperties == null )
97 {
98 this.currentProperties = loadEditorProperties();
99 }
100
101 return this.currentProperties;
102 }
103
104 private JIvaloEditorProperties loadEditorProperties()
105 throws InvalidClassException, InvalidObjectException
106 {
107
108 File file = new File(PROPERTIES_FILE);
109
110 FileInputStream fis = null;
111
112 Properties properties = new Properties();
113
114 try
115 {
116 fis = new FileInputStream(file);
117
118 properties.load( fis );
119 } catch ( IOException e )
120 {
121 return restoreEditorProperties();
122 } finally
123 {
124 if ( fis != null )
125 {
126 try
127 {
128 fis.close();
129 } catch ( IOException e1 )
130 {
131
132 e1.printStackTrace();
133 }
134 }
135 }
136
137 JIvaloEditorProperties props = new JIvaloEditorProperties( properties );
138
139 if ( props.isUpgradePropertiesFile() )
140 {
141 storeEditorProperties( props );
142 }
143
144 return props;
145 }
146
147 private JIvaloEditorProperties restoreEditorProperties()
148 throws InvalidClassException, InvalidObjectException
149 {
150 JIvaloEditorProperties defaultProps = getDefaultEditorProperties();
151
152 storeEditorProperties(defaultProps);
153
154 return defaultProps;
155 }
156
157 private void storeEditorProperties( JIvaloEditorProperties props )
158 {
159
160 File file = new File( PROPERTIES_FILE );
161
162
163
164 FileOutputStream fos = null;
165
166 try
167 {
168
169 fos = new FileOutputStream(file);
170
171 props.asProperties().store( fos, props.getPropertiesFileComment() );
172
173 } catch ( FileNotFoundException e )
174 {
175
176 e.printStackTrace();
177 } catch ( IOException e )
178 {
179
180 e.printStackTrace();
181 } finally
182 {
183 if ( fos != null )
184 {
185 try
186 {
187 fos.close();
188 } catch ( IOException e1 )
189 {
190
191 e1.printStackTrace();
192 }
193 }
194 }
195
196 }
197
198 private JIvaloEditorProperties getDefaultEditorProperties()
199 throws InvalidClassException, InvalidObjectException
200 {
201
202 if ( this.defaultProperties == null )
203 {
204 this.defaultProperties = JIvaloEditorProperties.defaultProperties();
205 }
206
207 return this.defaultProperties;
208 }
209
210 }