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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/tools/scale/ScaleManager.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
package org.herac.tuxguitar.gui.tools.scale;
 
2
 
 
3
import java.io.File;
 
4
import java.util.ArrayList;
 
5
import java.util.List;
 
6
 
 
7
import org.herac.tuxguitar.gui.TuxGuitar;
 
8
import org.herac.tuxguitar.gui.tools.scale.xml.ScaleReader;
 
9
import org.herac.tuxguitar.gui.util.TGMusicKeyUtils;
 
10
import org.herac.tuxguitar.gui.util.TGFileUtils;
 
11
import org.herac.tuxguitar.song.models.TGScale;
 
12
 
 
13
public class ScaleManager {
 
14
        private static final String[] KEY_NAMES = TGMusicKeyUtils.getSharpKeyNames(TGMusicKeyUtils.PREFIX_SCALE);
 
15
        
 
16
        private static final String KEY_SEPARATOR = ",";
 
17
        
 
18
        public static final int NONE_SELECTION = -1;
 
19
        
 
20
        private List scales;
 
21
        
 
22
        private TGScale scale;
 
23
        
 
24
        private int selectionIndex;
 
25
        
 
26
        private int selectionKey;
 
27
        
 
28
        public ScaleManager(){
 
29
                this.scales = new ArrayList();
 
30
                this.scale = TuxGuitar.instance().getSongManager().getFactory().newScale();
 
31
                this.selectionKey = 0;
 
32
                this.selectionIndex = NONE_SELECTION;
 
33
                this.loadScales();
 
34
        }
 
35
        
 
36
        public TGScale getScale() {
 
37
                return this.scale;
 
38
        }
 
39
        
 
40
        public void selectScale(int index,int key){
 
41
                if(index == NONE_SELECTION){
 
42
                        getScale().clear();
 
43
                }
 
44
                else if(index >= 0 && index < this.scales.size()){
 
45
                        getScale().clear();
 
46
                        ScaleInfo info = (ScaleInfo)this.scales.get(index);
 
47
                        String[] keys = info.getKeys().split(KEY_SEPARATOR);
 
48
                        for (int i = 0; i < keys.length; i ++){
 
49
                                int note = (Integer.parseInt(keys[i]) - 1);
 
50
                                if(note >= 0 && note < 12){
 
51
                                        getScale().setNote(note,true);
 
52
                                }
 
53
                        }
 
54
                        getScale().setKey(key);
 
55
                }
 
56
                this.selectionIndex = index;
 
57
                this.selectionKey = key;
 
58
        }
 
59
        
 
60
        public String[] getScaleNames(){
 
61
                String[] names = new String[this.scales.size()];
 
62
                for(int i = 0;i < this.scales.size();i ++){
 
63
                        ScaleInfo info = (ScaleInfo)this.scales.get(i);
 
64
                        names[i] = info.getName();
 
65
                }
 
66
                return names;
 
67
        }
 
68
        
 
69
        public String[] getKeyNames(){
 
70
                return KEY_NAMES;
 
71
        }
 
72
        
 
73
        public int getSelectionIndex() {
 
74
                return this.selectionIndex;
 
75
        }
 
76
        
 
77
        public int getSelectionKey() {
 
78
                return this.selectionKey;
 
79
        }
 
80
        
 
81
        private void loadScales(){
 
82
                try{
 
83
                        new ScaleReader().loadScales(this.scales,getScalesFileName());
 
84
                } catch (Throwable e) {
 
85
                        e.printStackTrace();
 
86
                } 
 
87
        }
 
88
        
 
89
        private String getScalesFileName(){
 
90
                return TGFileUtils.PATH_SCALES + File.separator + "scales.xml";
 
91
        }
 
92
}