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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/keybindings/editor/KeyBindingEditor.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.editor;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Collections;
 
5
import java.util.Iterator;
 
6
import java.util.List;
 
7
 
 
8
import org.eclipse.swt.SWT;
 
9
import org.eclipse.swt.events.DisposeEvent;
 
10
import org.eclipse.swt.events.DisposeListener;
 
11
import org.eclipse.swt.events.MouseAdapter;
 
12
import org.eclipse.swt.events.MouseEvent;
 
13
import org.eclipse.swt.events.SelectionAdapter;
 
14
import org.eclipse.swt.events.SelectionEvent;
 
15
import org.eclipse.swt.layout.GridData;
 
16
import org.eclipse.swt.layout.GridLayout;
 
17
import org.eclipse.swt.widgets.Button;
 
18
import org.eclipse.swt.widgets.Composite;
 
19
import org.eclipse.swt.widgets.Shell;
 
20
import org.eclipse.swt.widgets.Table;
 
21
import org.eclipse.swt.widgets.TableColumn;
 
22
import org.eclipse.swt.widgets.TableItem;
 
23
import org.herac.tuxguitar.gui.TuxGuitar;
 
24
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingAction;
 
25
import org.herac.tuxguitar.gui.system.keybindings.KeyBinding;
 
26
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingActionDefaults;
 
27
import org.herac.tuxguitar.gui.util.DialogUtils;
 
28
 
 
29
public class KeyBindingEditor {
 
30
        
 
31
        private static final int ACTION_WIDTH = 400;
 
32
        private static final int SHORTCUT_WIDTH = 100;
 
33
        
 
34
        protected Shell dialog;
 
35
        protected Table table;
 
36
        protected List items;
 
37
        
 
38
        public KeyBindingEditor(){
 
39
                this.items = new ArrayList();
 
40
        }
 
41
        
 
42
        public void show(Shell parent){
 
43
                this.dialog = DialogUtils.newDialog(parent,SWT.DIALOG_TRIM |SWT.APPLICATION_MODAL);
 
44
                this.dialog.setText(TuxGuitar.getProperty("key-bindings-editor"));
 
45
                this.dialog.setLayout(new GridLayout());
 
46
                
 
47
                Composite composite = new Composite(this.dialog,SWT.NONE);
 
48
                composite.setLayout(new GridLayout());
 
49
                composite.setLayoutData(new GridData(SWT.FILL,SWT.NONE,true,true));
 
50
                
 
51
                this.table = new Table(composite, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
 
52
                this.table.setLayoutData(new GridData((ACTION_WIDTH + SHORTCUT_WIDTH) ,250));
 
53
                this.table.setHeaderVisible(true);
 
54
                this.table.addMouseListener(new MouseAdapter() {
 
55
                        public void mouseDoubleClick(MouseEvent e) {
 
56
                                TableItem item = getSelectedItem();
 
57
                                if(item != null){
 
58
                                        KeyBindingAction itemData = (KeyBindingAction)item.getData();
 
59
                                        KeyBindingSelector selector = new KeyBindingSelector(KeyBindingEditor.this,itemData);
 
60
                                        KeyBinding kb = selector.select(KeyBindingEditor.this.dialog.getShell());
 
61
                                        removeKeyBindingAction(kb);
 
62
                                        itemData.setKeyBinding(kb);
 
63
                                        loadTableItemLabel(item);
 
64
                                }
 
65
                        }
 
66
                });
 
67
                
 
68
                TableColumn actionColumn = new TableColumn(this.table, SWT.LEFT);
 
69
                actionColumn.setText(TuxGuitar.getProperty("key-bindings-editor-action-column"));
 
70
                
 
71
                TableColumn shortcutColumn = new TableColumn(this.table, SWT.LEFT);
 
72
                shortcutColumn.setText(TuxGuitar.getProperty("key-bindings-editor-shortcut-column"));
 
73
                
 
74
                loadAvailableActionKeyBindings();
 
75
                loadEnableActionKeyBindings(TuxGuitar.instance().getkeyBindingManager().getKeyBindingActions());
 
76
                
 
77
                //------------------BUTTONS--------------------------
 
78
                Composite buttons = new Composite(this.dialog, SWT.NONE);
 
79
                buttons.setLayout(new GridLayout(2,false));
 
80
                buttons.setLayoutData(new GridData(SWT.RIGHT,SWT.FILL,true,true));
 
81
                
 
82
                Button defaults = new Button(buttons,SWT.PUSH);
 
83
                defaults.setText(TuxGuitar.getProperty("defaults"));
 
84
                defaults.setLayoutData(getButtonData());
 
85
                defaults.addSelectionListener(new SelectionAdapter() {
 
86
                        public void widgetSelected(SelectionEvent e) {
 
87
                                loadEnableActionKeyBindings(KeyBindingActionDefaults.getDefaultKeyBindings());
 
88
                        }
 
89
                });
 
90
                
 
91
                Button close = new Button(buttons,SWT.PUSH);
 
92
                close.setText(TuxGuitar.getProperty("close"));
 
93
                close.setLayoutData(getButtonData());
 
94
                close.addSelectionListener(new SelectionAdapter() {
 
95
                        public void widgetSelected(SelectionEvent e) {
 
96
                                KeyBindingEditor.this.dialog.dispose();
 
97
                        }
 
98
                });
 
99
                
 
100
                this.dialog.addDisposeListener(new DisposeListener() {
 
101
                        public void widgetDisposed(DisposeEvent e) {
 
102
                                save();
 
103
                        }
 
104
                });
 
105
                
 
106
                this.table.setLayoutData(new GridData( (adjustWidth(actionColumn,ACTION_WIDTH) + adjustWidth(shortcutColumn,SHORTCUT_WIDTH)) ,250) );
 
107
                
 
108
                this.dialog.setDefaultButton( close );
 
109
                
 
110
                DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK);
 
111
        }
 
112
        
 
113
        protected int adjustWidth(TableColumn column, int defaultWidth){
 
114
                column.pack();
 
115
                int width = column.getWidth();
 
116
                if( width < defaultWidth ){
 
117
                        width = defaultWidth;
 
118
                        column.setWidth( width );
 
119
                }
 
120
                return width;
 
121
        }
 
122
        
 
123
        protected GridData getButtonData(){
 
124
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
 
125
                data.minimumWidth = 80;
 
126
                data.minimumHeight = 25;
 
127
                return data;
 
128
        }
 
129
        
 
130
        protected void loadTableItemLabel(TableItem item){
 
131
                if(item.getData() instanceof KeyBindingAction){
 
132
                        KeyBindingAction actionkeyBinding = (KeyBindingAction)item.getData();
 
133
                        String action = actionkeyBinding.getAction();
 
134
                        String shortcut = (actionkeyBinding.getKeyBinding() != null)?actionkeyBinding.getKeyBinding().toString():"";
 
135
                        item.setText(new String[] { TuxGuitar.getProperty(action),shortcut});
 
136
                }
 
137
        }
 
138
        
 
139
        protected void loadAvailableActionKeyBindings(){
 
140
                List list = TuxGuitar.instance().getActionManager().getAvailableKeyBindingActions();
 
141
                Collections.sort(list); 
 
142
                Iterator it = list.iterator();
 
143
                while (it.hasNext()) {
 
144
                        String action = (String) it.next();
 
145
                        TableItem item = new TableItem(this.table, SWT.NONE);
 
146
                        item.setData(new KeyBindingAction(action,null));
 
147
                        this.items.add(item);
 
148
                }
 
149
        }
 
150
        
 
151
        protected void loadEnableActionKeyBindings(List list){
 
152
                Iterator items = this.items.iterator();
 
153
                while (items.hasNext()) {
 
154
                        TableItem item = (TableItem) items.next();
 
155
                        if(item.getData() instanceof KeyBindingAction){
 
156
                                KeyBindingAction itemData = (KeyBindingAction)item.getData();
 
157
                                KeyBinding keyBinding = null;
 
158
                                Iterator it = list.iterator();
 
159
                                while (it.hasNext()) {
 
160
                                        KeyBindingAction keyBindingAction = (KeyBindingAction) it.next();
 
161
                                        if(keyBindingAction.getAction().equals(itemData.getAction())){
 
162
                                                keyBinding =  (KeyBinding) keyBindingAction.getKeyBinding().clone();
 
163
                                                break;
 
164
                                        }
 
165
                                }
 
166
                                itemData.setKeyBinding(keyBinding);
 
167
                                loadTableItemLabel(item);
 
168
                        }
 
169
                }
 
170
        }
 
171
        
 
172
        protected void removeKeyBindingAction(KeyBinding kb){
 
173
                if(kb != null){
 
174
                        Iterator it = this.items.iterator();
 
175
                        while(it.hasNext()){
 
176
                                TableItem item = (TableItem) it.next();
 
177
                                if(item.getData() instanceof KeyBindingAction){
 
178
                                        KeyBindingAction itemData = (KeyBindingAction)item.getData();
 
179
                                        if(kb.isSameAs(itemData.getKeyBinding())){
 
180
                                                itemData.setKeyBinding(null);
 
181
                                                loadTableItemLabel(item);
 
182
                                        }
 
183
                                }
 
184
                        }
 
185
                }
 
186
        }
 
187
        
 
188
        protected TableItem getSelectedItem(){
 
189
                TableItem item = null;
 
190
                int itemSelected = this.table.getSelectionIndex();
 
191
                if(itemSelected >= 0){
 
192
                        item = this.table.getItem(itemSelected);
 
193
                }
 
194
                return item;
 
195
        }
 
196
        
 
197
        public boolean exists(KeyBinding kb){
 
198
                Iterator it = this.items.iterator();
 
199
                while(it.hasNext()){
 
200
                        TableItem item = (TableItem) it.next();
 
201
                        if(item.getData() instanceof KeyBindingAction){
 
202
                                KeyBindingAction itemData = (KeyBindingAction)item.getData();
 
203
                                if(itemData.getKeyBinding() != null && kb.isSameAs(itemData.getKeyBinding())){
 
204
                                        return true;
 
205
                                }
 
206
                        }
 
207
                }
 
208
                return false;
 
209
        }
 
210
        
 
211
        protected void save(){
 
212
                List list = new ArrayList();
 
213
                Iterator it = this.items.iterator();
 
214
                while (it.hasNext()) {
 
215
                        TableItem item = (TableItem) it.next();
 
216
                        if(item.getData() instanceof KeyBindingAction){
 
217
                                KeyBindingAction keyBindingAction = (KeyBindingAction)item.getData();
 
218
                                if(keyBindingAction.getAction() != null && keyBindingAction.getKeyBinding() != null){
 
219
                                        list.add(keyBindingAction);
 
220
                                }
 
221
                        }
 
222
                }
 
223
                TuxGuitar.instance().getkeyBindingManager().reset(list);
 
224
                TuxGuitar.instance().getkeyBindingManager().saveKeyBindings();
 
225
        }
 
226
        
 
227
        public Shell getDialog(){
 
228
                return this.dialog;
 
229
        }
 
230
        
 
231
        public boolean isDisposed(){
 
232
                return (this.dialog == null || this.dialog.isDisposed() );
 
233
        }
 
234
}