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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/scale/MusicScaleManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.scale;
 
2
 
 
3
import java.io.File;
 
4
import java.io.IOException;
 
5
import java.util.ArrayList;
 
6
import java.util.Iterator;
 
7
import java.util.List;
 
8
 
 
9
import javax.xml.parsers.ParserConfigurationException;
 
10
 
 
11
import org.herac.tuxguitar.gui.scale.xml.ScaleReader;
 
12
import org.herac.tuxguitar.gui.util.TuxGuitarFileUtils;
 
13
import org.herac.tuxguitar.song.models.MusicScale;
 
14
import org.xml.sax.SAXException;
 
15
 
 
16
public class MusicScaleManager {
 
17
        private static final String[] KEY_NAMES = new String[]{"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
 
18
        
 
19
        private static final String KEY_SEPARATOR = ",";
 
20
        
 
21
        public static final int NONE_SELECTION = -1;
 
22
        
 
23
        private List scales;
 
24
        
 
25
        private MusicScale scale;
 
26
        
 
27
        private int selectionIndex;
 
28
        
 
29
        private int selectionKey;
 
30
        
 
31
        public MusicScaleManager(){
 
32
                this.scales = new ArrayList();
 
33
                this.scale = new MusicScale();          
 
34
                this.selectionKey = 0;
 
35
                this.selectionIndex = NONE_SELECTION;
 
36
                this.loadScales();
 
37
        }
 
38
                        
 
39
        public MusicScale getScale() {
 
40
                return scale;
 
41
        }               
 
42
 
 
43
        public void selectScale(int index,int key){
 
44
                if(index == NONE_SELECTION){
 
45
                        getScale().clear();
 
46
                }
 
47
                else if(index >= 0 && index < scales.size()){           
 
48
                        getScale().clear();
 
49
                        ScaleInfo info = (ScaleInfo)scales.get(index);
 
50
                        String[] keys = info.getKeys().split(KEY_SEPARATOR);                                            
 
51
                        for (int i = 0; i < keys.length; i ++){
 
52
                                int note = (Integer.parseInt(keys[i]) - 1);
 
53
                                if(note >= 0 && note < 12){
 
54
                                        getScale().setNote(note,true);
 
55
                                }
 
56
                        }                                       
 
57
                        getScale().setKey(key);
 
58
                }
 
59
                this.selectionIndex = index;
 
60
                this.selectionKey = key;
 
61
        }
 
62
        
 
63
        public String[] getScaleNames(){
 
64
                String[] names = new String[this.scales.size()];
 
65
                                
 
66
                Iterator it = this.scales.iterator();           
 
67
                for(int i = 0;i < scales.size();i ++){
 
68
                        ScaleInfo info = (ScaleInfo)scales.get(i);
 
69
                        names[i] = info.getName();
 
70
                }
 
71
                
 
72
                return names;
 
73
        }
 
74
 
 
75
        public String[] getKeyNames(){
 
76
                return KEY_NAMES;
 
77
        }
 
78
        
 
79
        public int getSelectionIndex() {
 
80
                return selectionIndex;
 
81
        }
 
82
 
 
83
        public int getSelectionKey() {
 
84
                return selectionKey;
 
85
        }
 
86
 
 
87
        private void loadScales(){
 
88
                try{
 
89
                        new ScaleReader().loadScales(scales,getScalesFileName());
 
90
                } catch (SAXException e) {
 
91
                        e.printStackTrace();
 
92
                } catch (ParserConfigurationException e) {
 
93
                        e.printStackTrace();
 
94
                } catch (IOException e) {
 
95
                        e.printStackTrace();
 
96
                } catch (Exception e) {
 
97
                        e.printStackTrace();
 
98
                } catch (Error e) {
 
99
                        e.printStackTrace();
 
100
                }
 
101
        }
 
102
        
 
103
    private String getScalesFileName(){
 
104
        return TuxGuitarFileUtils.SCALES_PREFIX + File.separator + "scales.xml";
 
105
    }   
 
106
 
 
107
 
 
108
}