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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/system/keybindings/KeyBindingManager.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.system.keybindings;
2
 
 
3
 
import java.io.File;
4
 
import java.util.HashMap;
5
 
import java.util.Map;
6
 
import java.util.Set;
7
 
 
8
 
import org.eclipse.swt.events.KeyEvent;
9
 
import org.eclipse.swt.events.KeyListener;
10
 
import org.eclipse.swt.widgets.Composite;
11
 
import org.herac.tuxguitar.gui.actions.Action;
12
 
import org.herac.tuxguitar.gui.system.keybindings.xml.KeyBindingReader;
13
 
import org.herac.tuxguitar.gui.system.keybindings.xml.KeyBindingWriter;
14
 
import org.herac.tuxguitar.gui.util.TuxGuitarFileUtils;
15
 
 
16
 
public class KeyBindingManager {
17
 
        private static KeyBindingManager instance;
18
 
        private Map keyBindings;
19
 
        private KeyBindingListener kbl;
20
 
        
21
 
        public KeyBindingManager(){ 
22
 
                this.keyBindings = new HashMap();
23
 
        }
24
 
        
25
 
        public void init(){
26
 
                Map userKeyBindings = KeyBindingReader.getKeyBindings(getUserFileName());
27
 
                if(userKeyBindings != null){
28
 
                        this.keyBindings.putAll(userKeyBindings);       
29
 
                }else{
30
 
                        userKeyBindings = KeyBindingDefaults.getDefaultKeyBindings();
31
 
                        this.keyBindings.putAll(userKeyBindings);
32
 
                        this.saveKeyBindings();
33
 
                }
34
 
                this.kbl = new KeyBindingListener();
35
 
        }
36
 
 
37
 
    private String getUserFileName(){
38
 
        return TuxGuitarFileUtils.USER_CONFIG_PREFIX + File.separator + "keybindings.xml";
39
 
    }
40
 
    
41
 
    public Action getActionForKeyBinding(KeyBinding kb){
42
 
        return (Action)keyBindings.get(kb);
43
 
    }
44
 
    
45
 
    public void removeAction(KeyBinding kb){
46
 
        keyBindings.remove(kb);
47
 
    }
48
 
 
49
 
    public void reset(Map keyBindings){
50
 
        this.keyBindings.clear();
51
 
        this.keyBindings.putAll(keyBindings);
52
 
    }
53
 
    
54
 
    public Set getKeyBindings(){
55
 
        return keyBindings.keySet();
56
 
    }
57
 
    
58
 
    public void saveKeyBindings(){
59
 
        KeyBindingWriter.setBindings(getKeyBindings(),getUserFileName());
60
 
    }
61
 
    
62
 
    public void appendListenersTo(Composite c){
63
 
        c.addKeyListener(kbl);
64
 
    }
65
 
    
66
 
    class KeyBindingListener implements KeyListener {
67
 
 
68
 
                public void keyPressed(KeyEvent evt) {
69
 
                        KeyBinding kb = new KeyBinding();
70
 
                        kb.setKey(evt.keyCode);
71
 
                        kb.setMask(evt.stateMask);
72
 
                        
73
 
                        Action a = (Action)keyBindings.get(kb);                 
74
 
                        if (a != null){
75
 
                                a.process(evt);                         
76
 
                        }
77
 
                }
78
 
 
79
 
                public void keyReleased(KeyEvent evt) {
80
 
                        
81
 
                }
82
 
        
83
 
    }
84
 
}