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

« back to all changes in this revision

Viewing changes to TuxGuitar-converter/src/org/herac/tuxguitar/gui/tools/custom/converter/TGConverterDialog.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.custom.converter;
 
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.DisposeEvent;
 
9
import org.eclipse.swt.events.DisposeListener;
 
10
import org.eclipse.swt.events.SelectionAdapter;
 
11
import org.eclipse.swt.events.SelectionEvent;
 
12
import org.eclipse.swt.layout.GridData;
 
13
import org.eclipse.swt.layout.GridLayout;
 
14
import org.eclipse.swt.widgets.Button;
 
15
import org.eclipse.swt.widgets.Combo;
 
16
import org.eclipse.swt.widgets.Composite;
 
17
import org.eclipse.swt.widgets.DirectoryDialog;
 
18
import org.eclipse.swt.widgets.Group;
 
19
import org.eclipse.swt.widgets.Label;
 
20
import org.eclipse.swt.widgets.Shell;
 
21
import org.eclipse.swt.widgets.Text;
 
22
import org.herac.tuxguitar.gui.TuxGuitar;
 
23
import org.herac.tuxguitar.gui.system.icons.IconLoader;
 
24
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
 
25
import org.herac.tuxguitar.gui.util.DialogUtils;
 
26
import org.herac.tuxguitar.gui.util.MessageDialog;
 
27
import org.herac.tuxguitar.io.base.TGFileFormat;
 
28
import org.herac.tuxguitar.io.base.TGFileFormatManager;
 
29
import org.herac.tuxguitar.io.base.TGSongExporter;
 
30
 
 
31
public class TGConverterDialog implements LanguageLoader,IconLoader{
 
32
        
 
33
        private static final int SHELL_WIDTH = 500;
 
34
        
 
35
        protected List outputFormats;
 
36
        protected Shell dialog;
 
37
        protected Group group;
 
38
        protected Label outputFormatLabel;
 
39
        protected Label outputFolderLabel;
 
40
        protected Label inputFolderLabel;
 
41
        protected Button inputFolderChooser;
 
42
        protected Button outputFolderChooser;
 
43
        protected Button buttonOK;
 
44
        protected Button buttonCancel;
 
45
        
 
46
        public TGConverterDialog() {
 
47
                this.outputFormats = new ArrayList();
 
48
        }
 
49
        
 
50
        public void show() {
 
51
                this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(),SWT.DIALOG_TRIM);
 
52
                this.dialog.setLayout(new GridLayout());
 
53
                this.dialog.setMinimumSize(SHELL_WIDTH,SWT.DEFAULT);
 
54
                this.dialog.addDisposeListener(new DisposeListener() {
 
55
                        public void widgetDisposed(DisposeEvent e) {
 
56
                                TuxGuitar.instance().getIconManager().removeLoader( TGConverterDialog.this );
 
57
                                TuxGuitar.instance().getLanguageManager().removeLoader( TGConverterDialog.this );
 
58
                        }
 
59
                });
 
60
                
 
61
                // Settings
 
62
                this.group = new Group(this.dialog,SWT.SHADOW_ETCHED_IN);
 
63
                this.group.setLayout(new GridLayout());
 
64
                this.group.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
65
                
 
66
                Composite composite = new Composite(this.group,SWT.NONE);
 
67
                composite.setLayout(new GridLayout(3,false));
 
68
                composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
69
                
 
70
                this.outputFormatLabel = new Label(composite,SWT.LEFT);
 
71
                this.outputFormatLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false));
 
72
                
 
73
                final Combo outputFormat = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
 
74
                outputFormat.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,false,2,1));
 
75
                addFileFormats(outputFormat);
 
76
                
 
77
                this.inputFolderLabel = new Label(composite,SWT.LEFT);
 
78
                this.inputFolderLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false));
 
79
                
 
80
                final Text inputFolder = new Text(composite,SWT.BORDER);
 
81
                inputFolder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,true,false));
 
82
                
 
83
                this.inputFolderChooser = new Button(composite,SWT.PUSH);
 
84
                this.inputFolderChooser.addSelectionListener(new SelectionAdapter() {
 
85
                        public void widgetSelected(SelectionEvent e) {
 
86
                                DirectoryDialog directoryDialog = new DirectoryDialog(TGConverterDialog.this.dialog);
 
87
                                String selection = directoryDialog.open();
 
88
                                if(selection != null){
 
89
                                        inputFolder.setText(selection);
 
90
                                }
 
91
                        }
 
92
                });
 
93
                
 
94
                this.outputFolderLabel = new Label(composite,SWT.LEFT);
 
95
                this.outputFolderLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,false,false));
 
96
                
 
97
                final Text outputFolder = new Text(composite,SWT.BORDER);
 
98
                outputFolder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,true,false));
 
99
                
 
100
                this.outputFolderChooser = new Button(composite,SWT.PUSH);
 
101
                this.outputFolderChooser.addSelectionListener(new SelectionAdapter() {
 
102
                        public void widgetSelected(SelectionEvent e) {
 
103
                                DirectoryDialog directoryDialog = new DirectoryDialog(TGConverterDialog.this.dialog);
 
104
                                String selection = directoryDialog.open();
 
105
                                if(selection != null){
 
106
                                        outputFolder.setText(selection);
 
107
                                }
 
108
                        }
 
109
                });
 
110
                
 
111
                //------------------BUTTONS--------------------------
 
112
                Composite buttons = new Composite(this.dialog, SWT.NONE);
 
113
                buttons.setLayout(new GridLayout(2,false));
 
114
                buttons.setLayoutData(new GridData(SWT.RIGHT,SWT.BOTTOM,true,false));
 
115
                
 
116
                this.buttonOK = new Button(buttons, SWT.PUSH);
 
117
                this.buttonOK.setLayoutData(getGridData(80,25));
 
118
                this.buttonOK.addSelectionListener(new SelectionAdapter() {
 
119
                        public void widgetSelected(SelectionEvent arg0) {
 
120
                                String inputFolderValue = inputFolder.getText();
 
121
                                String outputFolderValue = outputFolder.getText();
 
122
                                String outputFormatValue = getFileFormat( outputFormat.getSelectionIndex() );
 
123
                                
 
124
                                if(inputFolderValue == null || inputFolderValue.trim().length() == 0){
 
125
                                        MessageDialog.errorMessage(TGConverterDialog.this.dialog,TuxGuitar.getProperty("batch.converter.input.folder.invalid"));
 
126
                                }
 
127
                                else if(outputFolderValue == null || outputFolderValue.trim().length() == 0){
 
128
                                        MessageDialog.errorMessage(TGConverterDialog.this.dialog,TuxGuitar.getProperty("batch.converter.output.folder.invalid"));
 
129
                                }
 
130
                                else if(outputFormatValue == null || outputFormatValue.trim().length() == 0){
 
131
                                        MessageDialog.errorMessage(TGConverterDialog.this.dialog,TuxGuitar.getProperty("batch.converter.output.format.invalid"));
 
132
                                }
 
133
                                else{
 
134
                                        TGConverterProcess process = new TGConverterProcess();
 
135
                                        process.start(inputFolderValue.trim(), outputFolderValue.trim(), outputFormatValue.trim());
 
136
                                        TGConverterDialog.this.dialog.dispose();
 
137
                                }
 
138
                        }
 
139
                });
 
140
                
 
141
                this.buttonCancel = new Button(buttons, SWT.PUSH);
 
142
                this.buttonCancel.setLayoutData(getGridData(80,25));
 
143
                this.buttonCancel.addSelectionListener(new SelectionAdapter() {
 
144
                        public void widgetSelected(SelectionEvent arg0) {
 
145
                                TGConverterDialog.this.dialog.dispose();
 
146
                        }
 
147
                });
 
148
                
 
149
                this.dialog.setDefaultButton( this.buttonOK );
 
150
                
 
151
                this.loadIcons(false);
 
152
                this.loadProperties(false);
 
153
                
 
154
                TuxGuitar.instance().getIconManager().addLoader( this );
 
155
                TuxGuitar.instance().getLanguageManager().addLoader( this );
 
156
                
 
157
                DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK);
 
158
        }
 
159
        
 
160
        public boolean isDisposed(){
 
161
                return (this.dialog == null || this.dialog.isDisposed());
 
162
        }
 
163
        
 
164
        private GridData getGridData(int minimumWidth, int minimumHeight){
 
165
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
 
166
                data.minimumWidth = minimumWidth;
 
167
                data.minimumHeight = minimumHeight;
 
168
                return data;
 
169
        }
 
170
        
 
171
        private void addFileFormats(Combo combo){
 
172
                this.outputFormats.clear();
 
173
                
 
174
                Iterator outputStreams = TGFileFormatManager.instance().getOutputFormats().iterator();
 
175
                while (outputStreams.hasNext()) {
 
176
                        TGFileFormat format = (TGFileFormat)outputStreams.next();
 
177
                        addFileFormats(combo, format);
 
178
                }
 
179
                
 
180
                Iterator exporters = TGFileFormatManager.instance().getExporters();
 
181
                while (exporters.hasNext()) {
 
182
                        TGSongExporter exporter = (TGSongExporter)exporters.next();
 
183
                        addFileFormats(combo, exporter.getFileFormat());
 
184
                }
 
185
                if(this.outputFormats.size() > 0 ){
 
186
                        combo.select( 0 );
 
187
                }
 
188
        }
 
189
        
 
190
        private void addFileFormats(Combo combo, TGFileFormat format){
 
191
                if(format.getSupportedFormats() != null){
 
192
                        String[] extensions = format.getSupportedFormats().split(TGFileFormat.EXTENSION_SEPARATOR);
 
193
                        if(extensions != null && extensions.length > 0){
 
194
                                for(int i = 0; i < extensions.length; i ++){
 
195
                                        int dotIndex = extensions[i].indexOf(".");
 
196
                                        if(dotIndex >= 0){
 
197
                                                combo.add( format.getName() + " (" + extensions[i] + ")");
 
198
                                                this.outputFormats.add(extensions[i].substring( dotIndex ));
 
199
                                        }
 
200
                                }
 
201
                        }
 
202
                }
 
203
        }
 
204
        
 
205
        protected String getFileFormat(int index){
 
206
                if(index >= 0 && index < this.outputFormats.size()){
 
207
                        return (String)this.outputFormats.get(index);
 
208
                }
 
209
                return null;
 
210
        }
 
211
        
 
212
        public void loadProperties(){
 
213
                this.loadProperties(true);
 
214
        }
 
215
        
 
216
        public void loadProperties(boolean layout){
 
217
                if(!isDisposed()){
 
218
                        this.dialog.setText(TuxGuitar.getProperty("batch.converter"));
 
219
                        this.group.setText(TuxGuitar.getProperty("batch.converter.settings"));
 
220
                        this.inputFolderLabel.setText(TuxGuitar.getProperty("batch.converter.input.folder"));
 
221
                        this.outputFolderLabel.setText(TuxGuitar.getProperty("batch.converter.output.folder"));
 
222
                        this.outputFormatLabel.setText(TuxGuitar.getProperty("batch.converter.output.format"));
 
223
                        this.buttonOK.setText(TuxGuitar.getProperty("ok"));
 
224
                        this.buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
225
                        if(layout){
 
226
                                this.dialog.layout(true, true);
 
227
                        }
 
228
                }
 
229
        }
 
230
        
 
231
        public void loadIcons() {
 
232
                this.loadIcons(true);
 
233
        }
 
234
        
 
235
        public void loadIcons(boolean layout){
 
236
                if(!isDisposed()){
 
237
                        this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
 
238
                        this.inputFolderChooser.setImage(TuxGuitar.instance().getIconManager().getFileOpen());
 
239
                        this.outputFolderChooser.setImage(TuxGuitar.instance().getIconManager().getFileOpen());
 
240
                        if(layout){
 
241
                                this.dialog.layout(true, true);
 
242
                        }
 
243
                }
 
244
        }
 
245
}