3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import javax.swing.JOptionPane; |
6 |
+ |
import javax.swing.JFileChooser; |
7 |
|
import javax.swing.SwingUtilities; |
8 |
+ |
import javax.swing.filechooser.FileFilter; |
9 |
|
import java.awt.Frame; |
10 |
|
import java.util.Properties; |
11 |
|
import java.io.*; |
66 |
|
* @param configFile the path to the file that this configuration should load |
67 |
|
*/ |
68 |
|
private Configuration(String configFile) { |
69 |
+ |
_configFile = new File(configFile); |
70 |
+ |
_defaultConfigFile = _configFile; |
71 |
|
try { |
72 |
< |
_properties = readFileConfiguration(configFile); |
72 |
> |
_properties = readFileConfiguration(_configFile); |
73 |
|
} catch (FileNotFoundException e) { |
74 |
< |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + configFile, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
74 |
> |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
75 |
|
System.exit(1); |
76 |
|
} catch (IOException e) { |
77 |
< |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + e.getMessage(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
77 |
> |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
78 |
|
System.exit(1); |
79 |
|
} |
80 |
|
} |
127 |
|
public void GUIReconfiguration() { |
128 |
|
ConfigurationDialog conf = new ConfigurationDialog(); |
129 |
|
} |
130 |
+ |
|
131 |
+ |
public void saveNewConfiguration() { |
132 |
+ |
fc.setCurrentDirectory(_configFile.getParentFile()); |
133 |
+ |
|
134 |
+ |
int returnVal = fc.showSaveDialog(Conient.getFrame()); |
135 |
+ |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
136 |
+ |
_configFile = fc.getSelectedFile(); |
137 |
+ |
_usingSpecificConfig = true; |
138 |
+ |
saveConfiguration(); |
139 |
+ |
} |
140 |
+ |
} |
141 |
+ |
|
142 |
+ |
public void saveConfiguration() { |
143 |
+ |
saveFileConfiguration(_configFile); |
144 |
+ |
} |
145 |
+ |
|
146 |
+ |
public void saveDefaultConfiguration() { |
147 |
+ |
saveFileConfiguration(_defaultConfigFile); |
148 |
+ |
} |
149 |
+ |
|
150 |
+ |
public void loadConfiguration() { |
151 |
+ |
fc.setCurrentDirectory(_configFile.getParentFile()); |
152 |
+ |
int returnVal = fc.showOpenDialog(Conient.getFrame()); |
153 |
+ |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
154 |
+ |
_configFile = fc.getSelectedFile(); |
155 |
+ |
_usingSpecificConfig = true; |
156 |
+ |
try { |
157 |
+ |
_properties = readFileConfiguration(_configFile); |
158 |
+ |
JOptionPane.showMessageDialog(null, "Configuration sucessfully read in - " + _configFile.getName(), "Configuration Loaded", JOptionPane.INFORMATION_MESSAGE); |
159 |
+ |
} catch (FileNotFoundException e) { |
160 |
+ |
JOptionPane.showMessageDialog(null, "Configuration file not found - " + _configFile.getName(), "Configuration Error", JOptionPane.ERROR_MESSAGE); |
161 |
+ |
} catch (IOException e) { |
162 |
+ |
JOptionPane.showMessageDialog(null, "Unable to read configuration file - " + _configFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
163 |
+ |
} |
164 |
+ |
} |
165 |
+ |
} |
166 |
+ |
|
167 |
|
//---PRIVATE METHODS--- |
168 |
|
|
169 |
|
/** |
173 |
|
* @param configFile the path of the file to be read |
174 |
|
* @return the parsed properties |
175 |
|
*/ |
176 |
< |
private Properties readFileConfiguration(String configFile) throws FileNotFoundException, IOException { |
136 |
< |
File file = new File(configFile); |
176 |
> |
private Properties readFileConfiguration(File inputFile) throws FileNotFoundException, IOException { |
177 |
|
Properties configHolder = new Properties(); |
178 |
< |
configHolder.load(new FileInputStream(file)); |
178 |
> |
configHolder.load(new FileInputStream(inputFile)); |
179 |
|
return configHolder; |
180 |
|
} |
181 |
+ |
|
182 |
+ |
private void saveFileConfiguration(File outputFile) { |
183 |
+ |
try { |
184 |
+ |
_properties.store(new FileOutputStream(outputFile), "!!!IN TIME A NICE DESCRIPTIVE HEADER WILL BE HERE!!!"); |
185 |
+ |
JOptionPane.showMessageDialog(null, "Configuration written out - " + _configFile.getName(), "Configuration Saved", JOptionPane.INFORMATION_MESSAGE); |
186 |
+ |
} catch (IOException e) { |
187 |
+ |
JOptionPane.showMessageDialog(null, "Unable to write default configuration file - " + outputFile.getName() + "\nReason: " + e, "Configuration Error", JOptionPane.ERROR_MESSAGE); |
188 |
+ |
} |
189 |
+ |
} |
190 |
|
|
191 |
|
//---ACCESSOR/MUTATOR METHODS--- |
192 |
|
|
200 |
|
public String getProperty(String key) { |
201 |
|
return _properties.getProperty(key); |
202 |
|
} |
203 |
+ |
|
204 |
+ |
public void setProperty(String key, String value) { |
205 |
+ |
_properties.setProperty(key, value); |
206 |
+ |
} |
207 |
+ |
|
208 |
+ |
public boolean getUsingSpecificConfig() { |
209 |
+ |
return _usingSpecificConfig; |
210 |
+ |
} |
211 |
|
|
212 |
|
//---ATTRIBUTES--- |
213 |
|
|
215 |
|
* The current configuration in use |
216 |
|
*/ |
217 |
|
private Properties _properties = null; |
218 |
< |
|
218 |
> |
|
219 |
> |
private boolean _usingSpecificConfig = false; |
220 |
> |
|
221 |
> |
private File _configFile; |
222 |
> |
|
223 |
> |
private File _defaultConfigFile; |
224 |
> |
|
225 |
> |
final FileFilter filter = new SuffixFileFilter("conf", "Conient Configuration Files"); |
226 |
> |
final JFileChooser fc = new JFileChooser(); |
227 |
> |
{ |
228 |
> |
fc.setFileSelectionMode(JFileChooser.FILES_ONLY); |
229 |
> |
fc.setMultiSelectionEnabled(false); |
230 |
> |
fc.setFileFilter(new FileFilter() { |
231 |
> |
public boolean accept(File f) { |
232 |
> |
return (f != null) && (f.isDirectory() || filter.accept(f)); |
233 |
> |
} |
234 |
> |
public String getDescription() { |
235 |
> |
return "Folders and Conient Configuration Files"; |
236 |
> |
} |
237 |
> |
}); |
238 |
> |
fc.addChoosableFileFilter(filter); |
239 |
> |
} |
240 |
> |
|
241 |
|
//---STATIC ATTRIBUTES--- |
242 |
|
|
243 |
|
/** |
244 |
|
* The reference to the singleton instance of this class |
245 |
|
*/ |
246 |
|
private static Configuration _instance = null; |
247 |
+ |
//---INNER CLASSES--- |
248 |
+ |
|
249 |
+ |
private class SuffixFileFilter extends FileFilter { |
250 |
+ |
public SuffixFileFilter(String suffix, String description) { |
251 |
+ |
if (suffix.charAt(0) != '.') { |
252 |
+ |
_suffix = '.' + suffix; |
253 |
+ |
} else { |
254 |
+ |
_suffix = suffix; |
255 |
+ |
} |
256 |
+ |
_description = description; |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
public boolean accept(File f) { |
260 |
+ |
boolean ok = false; |
261 |
+ |
if (f != null) { |
262 |
+ |
final String name = f.getName(); |
263 |
+ |
if (name != null) { |
264 |
+ |
final int nameLength = name.length(); |
265 |
+ |
int dotIndex = name.lastIndexOf("."); |
266 |
+ |
ok = (dotIndex >= 1) && |
267 |
+ |
getSuffix().equalsIgnoreCase(name.substring(dotIndex)); |
268 |
+ |
} |
269 |
+ |
} |
270 |
+ |
return ok; |
271 |
+ |
} |
272 |
+ |
|
273 |
+ |
public String getDescription() { |
274 |
+ |
return _description; |
275 |
+ |
} |
276 |
+ |
|
277 |
+ |
public String getSuffix() { |
278 |
+ |
return _suffix; |
279 |
+ |
} |
280 |
+ |
|
281 |
+ |
private final String _suffix, _description; |
282 |
+ |
} |
283 |
+ |
|
284 |
|
} |