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

« back to all changes in this revision

Viewing changes to TuxGuitar-fluidsynth/src/org/herac/tuxguitar/player/impl/midiport/fluidsynth/MidiPortSettings.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.player.impl.midiport.fluidsynth;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Iterator;
 
5
import java.util.List;
 
6
 
 
7
import org.eclipse.swt.SWT;
 
8
import org.eclipse.swt.events.SelectionAdapter;
 
9
import org.eclipse.swt.events.SelectionEvent;
 
10
import org.eclipse.swt.layout.GridData;
 
11
import org.eclipse.swt.layout.GridLayout;
 
12
import org.eclipse.swt.widgets.Button;
 
13
import org.eclipse.swt.widgets.Composite;
 
14
import org.eclipse.swt.widgets.FileDialog;
 
15
import org.eclipse.swt.widgets.Shell;
 
16
import org.eclipse.swt.widgets.Table;
 
17
import org.eclipse.swt.widgets.TableColumn;
 
18
import org.eclipse.swt.widgets.TableItem;
 
19
import org.herac.tuxguitar.gui.TuxGuitar;
 
20
import org.herac.tuxguitar.gui.system.config.TGConfigManager;
 
21
import org.herac.tuxguitar.gui.system.plugins.TGPluginConfigManager;
 
22
import org.herac.tuxguitar.gui.util.DialogUtils;
 
23
 
 
24
public class MidiPortSettings {
 
25
        
 
26
        private static final int TABLE_WIDTH = 350;
 
27
        private static final int TABLE_HEIGHT = 200;
 
28
        
 
29
        public MidiPortSettings(){
 
30
                super();
 
31
        }
 
32
        
 
33
        public static TGConfigManager getConfig(){
 
34
                TGConfigManager config = new TGPluginConfigManager("tuxguitar-fluidsynth");
 
35
                config.init();
 
36
                return config;
 
37
        }
 
38
        
 
39
        public List getSoundfonts(){
 
40
                List ports = new ArrayList();
 
41
                TGConfigManager config = getConfig();
 
42
                
 
43
                int count = config.getIntConfigValue("soundfont.count");
 
44
                for(int i = 0; i < count;i ++){
 
45
                        String path = config.getStringConfigValue("soundfont.path" + i);
 
46
                        if(path != null && path.length() > 0 ){
 
47
                                ports.add( path );
 
48
                        }
 
49
                }
 
50
                return ports;
 
51
        }
 
52
        
 
53
        public void setSoundfonts(List soundfonts){
 
54
                TGConfigManager config = getConfig();
 
55
                config.setProperty("soundfont.count", soundfonts.size() );
 
56
                for( int i = 0 ; i < soundfonts.size() ; i ++ ){
 
57
                        String path = (String)soundfonts.get( i );
 
58
                        config.setProperty("soundfont.path" + i, path );
 
59
                }
 
60
                config.save();
 
61
        }
 
62
        
 
63
        public void configure(Shell parent) {
 
64
                final Shell dialog = DialogUtils.newDialog(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
 
65
                dialog.setText(TuxGuitar.getProperty("fluidsynth.settings"));
 
66
                dialog.setLayout(new GridLayout(2,false));
 
67
                dialog.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
68
                // ----------------------------------------------------------------------
 
69
                Composite compositeTable = new Composite(dialog, SWT.NONE);
 
70
                compositeTable.setLayout(new GridLayout());
 
71
                compositeTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
72
                
 
73
                final Table table = new Table(compositeTable, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
 
74
                table.setLayoutData(new GridData(TABLE_WIDTH,TABLE_HEIGHT));
 
75
                table.setHeaderVisible(true);
 
76
                
 
77
                TableColumn column = new TableColumn(table, SWT.NONE);
 
78
                column.setWidth(TABLE_WIDTH);
 
79
                column.setText(TuxGuitar.getProperty("fluidsynth.settings.soundfont-list"));
 
80
                
 
81
                // ------------------BUTTONS--------------------------
 
82
                Composite compositeButtons = new Composite(dialog, SWT.NONE);
 
83
                compositeButtons.setLayout(new GridLayout());
 
84
                compositeButtons.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
85
                
 
86
                Button buttonAdd = new Button(compositeButtons, SWT.PUSH);
 
87
                buttonAdd.setLayoutData(getButtonData(SWT.FILL,SWT.TOP, true,false));
 
88
                buttonAdd.setText(TuxGuitar.getProperty("add"));
 
89
                buttonAdd.addSelectionListener(new SelectionAdapter() {
 
90
                        public void widgetSelected(SelectionEvent e) {
 
91
                                addMidiPort(table);
 
92
                        }
 
93
                });
 
94
                
 
95
                Button buttonDelete = new Button(compositeButtons, SWT.PUSH);
 
96
                buttonDelete.setText(TuxGuitar.getProperty("remove"));
 
97
                buttonDelete.setLayoutData(getButtonData(SWT.FILL,SWT.TOP, true,false));
 
98
                buttonDelete.addSelectionListener(new SelectionAdapter() {
 
99
                        public void widgetSelected(SelectionEvent arg0) {
 
100
                                removeMidiPort(table);
 
101
                        }
 
102
                });
 
103
                
 
104
                final Button buttonOK = new Button(compositeButtons, SWT.PUSH);
 
105
                buttonOK.setText(TuxGuitar.getProperty("ok"));
 
106
                buttonOK.setLayoutData(getButtonData(SWT.FILL,SWT.BOTTOM,true, true));
 
107
                buttonOK.addSelectionListener(new SelectionAdapter() {
 
108
                        public void widgetSelected(SelectionEvent arg0) {
 
109
                                updateMidiPorts( table );
 
110
                                dialog.dispose();
 
111
                        }
 
112
                });
 
113
                
 
114
                Button buttonCancel = new Button(compositeButtons, SWT.PUSH);
 
115
                buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
116
                buttonCancel.setLayoutData(getButtonData(SWT.FILL,SWT.BOTTOM,true, false));
 
117
                buttonCancel.addSelectionListener(new SelectionAdapter() {
 
118
                        public void widgetSelected(SelectionEvent arg0) {
 
119
                                dialog.dispose();
 
120
                        }
 
121
                });
 
122
                
 
123
                this.addMidiPorts(table);
 
124
                
 
125
                dialog.setDefaultButton( buttonOK );
 
126
                
 
127
                DialogUtils.openDialog(dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
 
128
        }
 
129
        
 
130
        protected GridData getButtonData(int hAlignment,int vAlignment,boolean grabExcessHSpace,boolean grabExcessVSpace){
 
131
                GridData data = new GridData(hAlignment,vAlignment,grabExcessHSpace,grabExcessVSpace);
 
132
                data.minimumWidth = 80;
 
133
                data.minimumHeight = 25;
 
134
                return data;
 
135
        }
 
136
        
 
137
        protected void addMidiPorts(Table table){
 
138
                Iterator it = getSoundfonts().iterator();
 
139
                while(it.hasNext()){
 
140
                        String path = (String)it.next();
 
141
                        this.addMidiPort(table, path );
 
142
                }
 
143
        }
 
144
        
 
145
        protected void updateMidiPorts(Table table){
 
146
                List ports = new ArrayList();
 
147
                int count = table.getItemCount();
 
148
                for( int i = 0 ; i < count; i ++ ){
 
149
                        TableItem item = table.getItem( i );
 
150
                        if( item.getData() instanceof String ){
 
151
                                ports.add( item.getData() );
 
152
                        }
 
153
                }
 
154
                this.setSoundfonts(ports);
 
155
        }
 
156
        
 
157
        protected void addMidiPort(Table table, String path){
 
158
                TableItem item = new TableItem(table, SWT.NONE);
 
159
                item.setText( path );
 
160
                item.setData( path );
 
161
        }
 
162
        
 
163
        protected void removeMidiPort(Table table){
 
164
                int index = table.getSelectionIndex();
 
165
                if(index >= 0 && index < table.getItemCount()){
 
166
                        table.remove( index );
 
167
                }
 
168
        }
 
169
        
 
170
        public void addMidiPort(final Table table) {
 
171
                FileDialog chooser = new FileDialog(table.getShell());
 
172
                String path = chooser.open();
 
173
                if(path != null && path.length() > 0){
 
174
                        addMidiPort(table, path);
 
175
                }
 
176
        }
 
177
}