1
package org.herac.tuxguitar.gui.system.keybindings.editor;
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Iterator;
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;
29
public class KeyBindingEditor {
31
private static final int ACTION_WIDTH = 400;
32
private static final int SHORTCUT_WIDTH = 100;
34
protected Shell dialog;
35
protected Table table;
38
public KeyBindingEditor(){
39
this.items = new ArrayList();
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());
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));
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();
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);
68
TableColumn actionColumn = new TableColumn(this.table, SWT.LEFT);
69
actionColumn.setText(TuxGuitar.getProperty("key-bindings-editor-action-column"));
71
TableColumn shortcutColumn = new TableColumn(this.table, SWT.LEFT);
72
shortcutColumn.setText(TuxGuitar.getProperty("key-bindings-editor-shortcut-column"));
74
loadAvailableActionKeyBindings();
75
loadEnableActionKeyBindings(TuxGuitar.instance().getkeyBindingManager().getKeyBindingActions());
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));
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());
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();
100
this.dialog.addDisposeListener(new DisposeListener() {
101
public void widgetDisposed(DisposeEvent e) {
106
this.table.setLayoutData(new GridData( (adjustWidth(actionColumn,ACTION_WIDTH) + adjustWidth(shortcutColumn,SHORTCUT_WIDTH)) ,250) );
108
this.dialog.setDefaultButton( close );
110
DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK);
113
protected int adjustWidth(TableColumn column, int defaultWidth){
115
int width = column.getWidth();
116
if( width < defaultWidth ){
117
width = defaultWidth;
118
column.setWidth( width );
123
protected GridData getButtonData(){
124
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
125
data.minimumWidth = 80;
126
data.minimumHeight = 25;
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});
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);
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();
166
itemData.setKeyBinding(keyBinding);
167
loadTableItemLabel(item);
172
protected void removeKeyBindingAction(KeyBinding kb){
174
Iterator it = this.items.iterator();
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);
188
protected TableItem getSelectedItem(){
189
TableItem item = null;
190
int itemSelected = this.table.getSelectionIndex();
191
if(itemSelected >= 0){
192
item = this.table.getItem(itemSelected);
197
public boolean exists(KeyBinding kb){
198
Iterator it = this.items.iterator();
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())){
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);
223
TuxGuitar.instance().getkeyBindingManager().reset(list);
224
TuxGuitar.instance().getkeyBindingManager().saveKeyBindings();
227
public Shell getDialog(){
231
public boolean isDisposed(){
232
return (this.dialog == null || this.dialog.isDisposed() );