1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.player;
22 
23 import java.io.IOException;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.PropertyResourceBundle;
28 import java.util.StringTokenizer;
29 
30 import net.sourceforge.atunes.utils.Logger;
31 
32 /**
33  * Reads equalizer presets
34  *
35  * @author alex
36  *
37  */
38 public class EqualizerPresetsReader {
39 
40     private String presetsFile;
41 
42     /**
43      * @param presetsFile
44      */
setPresetsFile(final String presetsFile)45     public void setPresetsFile(final String presetsFile) {
46 	this.presetsFile = presetsFile;
47     }
48 
49     /**
50      * Returns presets loaded from properties file. Keys are transformed to be
51      * shown on GUI
52      *
53      * @return the presets from bundle
54      */
getPresetsFromBundle()55     public Map<String, Integer[]> getPresetsFromBundle() {
56 	Map<String, Integer[]> result = new HashMap<String, Integer[]>();
57 
58 	try {
59 	    PropertyResourceBundle presetsBundle = new PropertyResourceBundle(
60 		    EqualizerPresetsReader.class
61 			    .getResourceAsStream(presetsFile));
62 	    Enumeration<String> keys = presetsBundle.getKeys();
63 
64 	    while (keys.hasMoreElements()) {
65 		String key = keys.nextElement();
66 		String preset = presetsBundle.getString(key);
67 
68 		// Transform key
69 		key = key.replace('.', ' ');
70 
71 		// Parse preset
72 		StringTokenizer st = new StringTokenizer(preset, ",");
73 		Integer[] presetsArray = new Integer[10];
74 		int i = 0;
75 		while (st.hasMoreTokens()) {
76 		    String token = st.nextToken();
77 		    presetsArray[i++] = Integer.parseInt(token);
78 		}
79 
80 		result.put(key, presetsArray);
81 	    }
82 	} catch (IOException ioe) {
83 	    Logger.error(ioe);
84 	} catch (NumberFormatException nfe) {
85 	    Logger.error(nfe);
86 	}
87 	return result;
88     }
89 
90 }
91