~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/TGConverterProcess.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 org.eclipse.swt.SWT;
 
4
import org.eclipse.swt.custom.StyleRange;
 
5
import org.eclipse.swt.custom.StyledText;
 
6
import org.eclipse.swt.events.DisposeEvent;
 
7
import org.eclipse.swt.events.DisposeListener;
 
8
import org.eclipse.swt.events.SelectionAdapter;
 
9
import org.eclipse.swt.events.SelectionEvent;
 
10
import org.eclipse.swt.events.ShellAdapter;
 
11
import org.eclipse.swt.events.ShellEvent;
 
12
import org.eclipse.swt.graphics.Color;
 
13
import org.eclipse.swt.layout.GridData;
 
14
import org.eclipse.swt.layout.GridLayout;
 
15
import org.eclipse.swt.widgets.Button;
 
16
import org.eclipse.swt.widgets.Composite;
 
17
import org.eclipse.swt.widgets.Shell;
 
18
import org.herac.tuxguitar.gui.TuxGuitar;
 
19
import org.herac.tuxguitar.gui.system.icons.IconLoader;
 
20
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
 
21
import org.herac.tuxguitar.gui.util.DialogUtils;
 
22
import org.herac.tuxguitar.util.TGSynchronizer;
 
23
 
 
24
public class TGConverterProcess implements TGConverterListener,LanguageLoader,IconLoader{
 
25
        
 
26
        private static final int SHELL_WIDTH = 650;
 
27
        private static final int SHELL_HEIGHT = 350;
 
28
        
 
29
        protected static final String EOL = ("\n");
 
30
        
 
31
        protected static final Color COLOR_INFO  = TuxGuitar.instance().getDisplay().getSystemColor(SWT.COLOR_BLUE);
 
32
        protected static final Color COLOR_ERROR = TuxGuitar.instance().getDisplay().getSystemColor(SWT.COLOR_RED );
 
33
        
 
34
        protected Shell dialog;
 
35
        protected StyledText output;
 
36
        protected Button buttonCancel;
 
37
        protected Button buttonClose;
 
38
        protected TGConverter converter;
 
39
        protected boolean finished;
 
40
        
 
41
        public void start(final String initFolder,String destFolder,String extension){
 
42
                this.converter = new TGConverter(initFolder,destFolder);
 
43
                this.converter.setExtension(extension);
 
44
                this.converter.setListener(this);
 
45
                
 
46
                this.showProcess();
 
47
                
 
48
                new Thread(new Runnable() {
 
49
                        public void run() {
 
50
                                TGConverterProcess.this.converter.process();
 
51
                        }
 
52
                }).start();
 
53
        }
 
54
        
 
55
        protected void showProcess() {
 
56
                this.finished = false;
 
57
                
 
58
                this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(),SWT.SHELL_TRIM);
 
59
                this.dialog.setLayout(new GridLayout());
 
60
                this.dialog.setSize( SHELL_WIDTH , SHELL_HEIGHT );
 
61
                this.dialog.addDisposeListener(new DisposeListener() {
 
62
                        public void widgetDisposed(DisposeEvent e) {
 
63
                                TuxGuitar.instance().getIconManager().removeLoader( TGConverterProcess.this );
 
64
                                TuxGuitar.instance().getLanguageManager().removeLoader( TGConverterProcess.this );
 
65
                        }
 
66
                });
 
67
                this.dialog.addShellListener(new ShellAdapter() {
 
68
                        public void shellClosed(ShellEvent e) {
 
69
                                e.doit = TGConverterProcess.this.finished;
 
70
                        }
 
71
                });
 
72
                
 
73
                Composite composite = new Composite(this.dialog,SWT.NONE);
 
74
                composite.setLayout(new GridLayout());
 
75
                composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
76
                
 
77
                this.output = new StyledText(composite,SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
 
78
                this.output.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
79
                this.output.setEditable(false);
 
80
                
 
81
                //------------------BUTTONS--------------------------
 
82
                Composite buttons = new Composite(this.dialog, SWT.NONE);
 
83
                buttons.setLayout(new GridLayout(2,false));
 
84
                buttons.setLayoutData(new GridData(SWT.RIGHT,SWT.BOTTOM,true,false));
 
85
                
 
86
                this.buttonCancel = new Button(buttons, SWT.PUSH);
 
87
                this.buttonCancel.setEnabled( false );
 
88
                this.buttonCancel.setLayoutData(getButtonsData());
 
89
                this.buttonCancel.addSelectionListener(new SelectionAdapter() {
 
90
                        public void widgetSelected(SelectionEvent arg0) {
 
91
                                TGConverterProcess.this.converter.setCancelled( true );
 
92
                        }
 
93
                });
 
94
                
 
95
                this.buttonClose = new Button(buttons, SWT.PUSH);
 
96
                this.buttonClose.setEnabled( false );
 
97
                this.buttonClose.setLayoutData(getButtonsData());
 
98
                this.buttonClose.addSelectionListener(new SelectionAdapter() {
 
99
                        public void widgetSelected(SelectionEvent arg0) {
 
100
                                TGConverterProcess.this.dialog.dispose();
 
101
                        }
 
102
                });
 
103
                
 
104
                this.loadIcons(false);
 
105
                this.loadProperties(false);
 
106
                
 
107
                TuxGuitar.instance().getIconManager().addLoader( this );
 
108
                TuxGuitar.instance().getLanguageManager().addLoader( this );
 
109
                
 
110
                DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER);
 
111
        }
 
112
        
 
113
        private GridData getButtonsData(){
 
114
                GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
 
115
                data.minimumWidth = 80;
 
116
                data.minimumHeight = 25;
 
117
                return data;
 
118
        }
 
119
        
 
120
        public boolean isDisposed(){
 
121
                return (this.dialog == null || this.dialog.isDisposed() );
 
122
        }
 
123
        
 
124
        public void loadProperties(){
 
125
                this.loadProperties(true);
 
126
        }
 
127
        
 
128
        public void loadProperties(boolean layout){
 
129
                if(!isDisposed()){
 
130
                        this.dialog.setText(TuxGuitar.getProperty("batch.converter"));
 
131
                        this.buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
132
                        this.buttonClose.setText(TuxGuitar.getProperty("close"));
 
133
                        if(layout){
 
134
                                this.dialog.layout(true, true);
 
135
                        }
 
136
                }
 
137
        }
 
138
        
 
139
        public void loadIcons() {
 
140
                this.loadIcons(true);
 
141
        }
 
142
        
 
143
        public void loadIcons(boolean layout){
 
144
                if(!isDisposed()){
 
145
                        this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
 
146
                        if(layout){
 
147
                                this.dialog.layout(true, true);
 
148
                        }
 
149
                }
 
150
        }
 
151
        
 
152
        //------------------------------------------------------------------------------------------------//
 
153
        //---TGConverterListener Implementation ----------------------------------------------------------//
 
154
        //------------------------------------------------------------------------------------------------//
 
155
        
 
156
        public void notifyFileProcess(final String filename) {
 
157
                if(!isDisposed() ){
 
158
                        try {
 
159
                                TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
160
                                        public void run() {
 
161
                                                if(!isDisposed() ){
 
162
                                                        TGConverterProcess.this.output.append(TuxGuitar.getProperty("batch.converter.messages.converting", new String[] {filename}));
 
163
                                                        TGConverterProcess.this.output.setSelection( TGConverterProcess.this.output.getCharCount() );
 
164
                                                }
 
165
                                        }
 
166
                                });
 
167
                        } catch (Throwable e) {
 
168
                                e.printStackTrace();
 
169
                        }
 
170
                }
 
171
        }
 
172
        
 
173
        public void notifyFileResult(final String filename, final int errorCode) {
 
174
                if(!isDisposed() ){
 
175
                        try {
 
176
                                TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
177
                                        public void run() {
 
178
                                                if(!isDisposed() ){
 
179
                                                        TGConverterProcess.this.appendLogMessage(errorCode, filename);
 
180
                                                        TGConverterProcess.this.output.setSelection( TGConverterProcess.this.output.getCharCount() );
 
181
                                                }
 
182
                                        }
 
183
                                });
 
184
                        } catch (Throwable e) {
 
185
                                e.printStackTrace();
 
186
                        }
 
187
                }
 
188
        }
 
189
        
 
190
        public void notifyStart() {
 
191
                if(!isDisposed() ){
 
192
                        try {
 
193
                                TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
194
                                        public void run() {
 
195
                                                if(!isDisposed() ){
 
196
                                                        TGConverterProcess.this.finished = false;
 
197
                                                        TGConverterProcess.this.buttonClose.setEnabled( TGConverterProcess.this.finished );
 
198
                                                        TGConverterProcess.this.buttonCancel.setEnabled( !TGConverterProcess.this.finished );
 
199
                                                        TGConverterProcess.this.output.setCursor(TGConverterProcess.this.dialog.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
 
200
                                                }
 
201
                                        }
 
202
                                });
 
203
                        } catch (Throwable e) {
 
204
                                e.printStackTrace();
 
205
                        }
 
206
                }
 
207
        }
 
208
        
 
209
        public void notifyFinish() {
 
210
                if(!isDisposed() ){
 
211
                        try {
 
212
                                TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
 
213
                                        public void run() {
 
214
                                                if(!isDisposed() ){
 
215
                                                        TGConverterProcess.this.finished = true;
 
216
                                                        TGConverterProcess.this.buttonClose.setEnabled( TGConverterProcess.this.finished );
 
217
                                                        TGConverterProcess.this.buttonCancel.setEnabled( !TGConverterProcess.this.finished );
 
218
                                                        TGConverterProcess.this.output.setCursor(TGConverterProcess.this.dialog.getDisplay().getSystemCursor(SWT.CURSOR_ARROW));
 
219
                                                }
 
220
                                        }
 
221
                                });
 
222
                        } catch (Throwable e) {
 
223
                                e.printStackTrace();
 
224
                        }
 
225
                }
 
226
        }
 
227
        
 
228
        protected void appendLogMessage(int code, String fileName) {
 
229
                String message = (TuxGuitar.getProperty( "batch.converter.messages." + (code == TGConverter.FILE_OK ? "ok" : "failed") ) + EOL);
 
230
                
 
231
                switch (code) {
 
232
                        case TGConverter.FILE_COULDNT_WRITE :
 
233
                                message += ( TuxGuitar.getProperty("batch.converter.messages.couldnt-write", new String[] {fileName}) + EOL );
 
234
                                break;
 
235
                        case TGConverter.FILE_BAD :
 
236
                                message += ( TuxGuitar.getProperty("batch.converter.messages.bad-file", new String[] {fileName}) + EOL );
 
237
                                break;
 
238
                        case TGConverter.FILE_NOT_FOUND :
 
239
                                message += ( TuxGuitar.getProperty("batch.converter.messages.file-not-found", new String[] {fileName}) + EOL );
 
240
                                break;
 
241
                        case TGConverter.OUT_OF_MEMORY :
 
242
                                message += ( TuxGuitar.getProperty("batch.converter.messages.out-of-memory", new String[] {fileName}) + EOL );
 
243
                                break;
 
244
                        case TGConverter.EXPORTER_NOT_FOUND :
 
245
                                message += ( TuxGuitar.getProperty("batch.converter.messages.exporter-not-found", new String[] {fileName}) + EOL );
 
246
                                break;
 
247
                        case TGConverter.UNKNOWN_ERROR :
 
248
                                message += ( TuxGuitar.getProperty("batch.converter.messages.unknown-error", new String[] {fileName}) + EOL );
 
249
                                break;
 
250
                }
 
251
                
 
252
                StyleRange range = new StyleRange();
 
253
                range.foreground = ( code == TGConverter.FILE_OK ? TGConverterProcess.COLOR_INFO : TGConverterProcess.COLOR_ERROR );
 
254
                range.start = TGConverterProcess.this.output.getCharCount();
 
255
                range.length = message.length();
 
256
                
 
257
                TGConverterProcess.this.output.append( message );
 
258
                TGConverterProcess.this.output.setStyleRange(range);
 
259
        }
 
260
}