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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/config/TGConfigEditor.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
/*
 
2
 * Created on 17-dic-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.system.config;
 
8
 
 
9
import java.util.ArrayList;
 
10
import java.util.Iterator;
 
11
import java.util.List;
 
12
import java.util.Properties;
 
13
 
 
14
import org.eclipse.swt.SWT;
 
15
import org.eclipse.swt.events.SelectionAdapter;
 
16
import org.eclipse.swt.events.SelectionEvent;
 
17
import org.eclipse.swt.graphics.Point;
 
18
import org.eclipse.swt.layout.FormLayout;
 
19
import org.eclipse.swt.layout.GridData;
 
20
import org.eclipse.swt.layout.GridLayout;
 
21
import org.eclipse.swt.widgets.Button;
 
22
import org.eclipse.swt.widgets.Composite;
 
23
import org.eclipse.swt.widgets.Shell;
 
24
import org.eclipse.swt.widgets.ToolBar;
 
25
import org.herac.tuxguitar.gui.TuxGuitar;
 
26
import org.herac.tuxguitar.gui.actions.ActionLock;
 
27
import org.herac.tuxguitar.gui.editors.TablatureEditor;
 
28
import org.herac.tuxguitar.gui.helper.SyncThread;
 
29
import org.herac.tuxguitar.gui.system.config.items.LanguageOption;
 
30
import org.herac.tuxguitar.gui.system.config.items.MainOption;
 
31
import org.herac.tuxguitar.gui.system.config.items.Option;
 
32
import org.herac.tuxguitar.gui.system.config.items.SkinOption;
 
33
import org.herac.tuxguitar.gui.system.config.items.SoundOption;
 
34
import org.herac.tuxguitar.gui.system.config.items.StylesOption;
 
35
import org.herac.tuxguitar.gui.system.config.items.ToolBarsOption;
 
36
import org.herac.tuxguitar.gui.util.ConfirmDialog;
 
37
import org.herac.tuxguitar.gui.util.DialogUtils;
 
38
 
 
39
/**
 
40
 * @author julian
 
41
 *
 
42
 * TODO To change the template for this generated type comment go to
 
43
 * Window - Preferences - Java - Code Style - Code Templates
 
44
 */
 
45
public class TGConfigEditor{
 
46
        
 
47
        protected Shell dialog;
 
48
        protected TGConfigManager config;
 
49
        protected List options;
 
50
        protected Properties defaults;
 
51
        protected boolean accepted;
 
52
        
 
53
        protected List runnables;
 
54
        
 
55
        public TGConfigEditor() {
 
56
                this.config = TuxGuitar.instance().getConfig();
 
57
        }
 
58
        
 
59
        public void showDialog(Shell shell) {
 
60
                this.accepted = false;
 
61
                
 
62
                this.dialog = DialogUtils.newDialog(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
 
63
                this.dialog.setLayout(new GridLayout());
 
64
                this.dialog.setText(TuxGuitar.getProperty("settings.config"));
 
65
                
 
66
                //-------main-------------------------------------
 
67
                Composite mainComposite = new Composite(this.dialog,SWT.NONE);
 
68
                mainComposite.setLayout(new GridLayout(2,false));
 
69
                mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true , true));
 
70
                createComposites(mainComposite);
 
71
                
 
72
                //-------buttons-------------------------------------
 
73
                Composite buttonComposite = new Composite(this.dialog,SWT.NONE);
 
74
                buttonComposite.setLayout(new GridLayout(3,true));
 
75
                buttonComposite.setLayoutData(new GridData(SWT.RIGHT,SWT.FILL,true,true));
 
76
                
 
77
                Button buttonDefaults = new Button(buttonComposite, SWT.PUSH);
 
78
                buttonDefaults.setLayoutData(getButtonData()); 
 
79
                buttonDefaults.setText(TuxGuitar.getProperty("defaults"));
 
80
                buttonDefaults.addSelectionListener(new SelectionAdapter() {
 
81
                        public void widgetSelected(SelectionEvent arg0) {
 
82
                                TGConfigEditor.this.accepted = true;
 
83
                                TuxGuitar.instance().loadCursor(SWT.CURSOR_WAIT);
 
84
                                dispose();
 
85
                                setDefaults();
 
86
                                ConfirmDialog confirm = new ConfirmDialog(TuxGuitar.getProperty("settings.config.apply-changes-question"));
 
87
                                confirm.setDefaultStatus( ConfirmDialog.STATUS_NO );
 
88
                                if(confirm.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO, ConfirmDialog.BUTTON_YES) == ConfirmDialog.STATUS_NO){
 
89
                                        TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
 
90
                                        ActionLock.unlock();
 
91
                                        return;
 
92
                                }
 
93
                                applyConfig(true);
 
94
                        }
 
95
                });
 
96
                
 
97
                Button buttonOK = new Button(buttonComposite, SWT.PUSH);
 
98
                buttonOK.setLayoutData(getButtonData());
 
99
                buttonOK.setText(TuxGuitar.getProperty("ok"));
 
100
                buttonOK.addSelectionListener(new SelectionAdapter() {
 
101
                        public void widgetSelected(SelectionEvent arg0) {
 
102
                                TGConfigEditor.this.accepted = true;
 
103
                                TuxGuitar.instance().loadCursor(SWT.CURSOR_WAIT);
 
104
                                updateOptions();
 
105
                                dispose();
 
106
                                ConfirmDialog confirm = new ConfirmDialog(TuxGuitar.getProperty("settings.config.apply-changes-question"));
 
107
                                confirm.setDefaultStatus( ConfirmDialog.STATUS_NO );
 
108
                                if(confirm.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO, ConfirmDialog.BUTTON_YES) == ConfirmDialog.STATUS_NO){
 
109
                                        TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
 
110
                                        ActionLock.unlock();
 
111
                                        return;
 
112
                                }
 
113
                                applyConfig(false);
 
114
                        }
 
115
                });
 
116
                
 
117
                Button buttonCancel = new Button(buttonComposite, SWT.PUSH);
 
118
                buttonCancel.setLayoutData(getButtonData()); 
 
119
                buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
120
                buttonCancel.addSelectionListener(new SelectionAdapter() {
 
121
                        public void widgetSelected(SelectionEvent arg0) {
 
122
                                TuxGuitar.instance().loadCursor(SWT.CURSOR_WAIT);
 
123
                                dispose();
 
124
                                TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
 
125
                        }
 
126
                });
 
127
                
 
128
                this.dialog.setDefaultButton( buttonOK );
 
129
                
 
130
                DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
 
131
                
 
132
                if(!this.accepted){
 
133
                        ActionLock.unlock();
 
134
                }
 
135
        }
 
136
        
 
137
        private void createComposites(Composite parent) {
 
138
                ToolBar toolBar = new ToolBar(parent, SWT.VERTICAL | SWT.FLAT | SWT.WRAP);
 
139
                toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true , true));
 
140
                
 
141
                Composite option = new Composite(parent,SWT.NONE);
 
142
                option.setLayout(new FormLayout());
 
143
                
 
144
                initOptions(toolBar,option);
 
145
                
 
146
                Point optionSize = computeOptionsSize( 0 , toolBar.computeSize(SWT.DEFAULT,SWT.DEFAULT).y );
 
147
                option.setLayoutData(new GridData(optionSize.x,optionSize.y));
 
148
                
 
149
                if(this.options.size() > 0){
 
150
                        select((Option)this.options.get(0));
 
151
                }
 
152
        }
 
153
        
 
154
        private void initOptions(ToolBar toolBar,Composite parent){
 
155
                this.options = new ArrayList();
 
156
                this.options.add(new MainOption(this,toolBar,parent));
 
157
                this.options.add(new StylesOption(this,toolBar,parent));
 
158
                this.options.add(new LanguageOption(this,toolBar,parent));
 
159
                this.options.add(new ToolBarsOption(this,toolBar,parent));
 
160
                this.options.add(new SkinOption(this,toolBar,parent));
 
161
                this.options.add(new SoundOption(this,toolBar,parent));
 
162
                
 
163
                Iterator it = this.options.iterator();
 
164
                while(it.hasNext()){
 
165
                        Option option = (Option)it.next();
 
166
                        option.createOption();
 
167
                }
 
168
        }
 
169
        
 
170
        private Point computeOptionsSize(int minimumWidth, int minimumHeight){
 
171
                int width = minimumWidth;
 
172
                int height = minimumHeight;
 
173
                
 
174
                Iterator it = this.options.iterator();
 
175
                while(it.hasNext()){
 
176
                        Option option = (Option)it.next();
 
177
                        Point size = option.computeSize();
 
178
                        if(size.x > width){
 
179
                                width = size.x;
 
180
                        }
 
181
                        if(size.y > height){
 
182
                                height = size.y;
 
183
                        }
 
184
                }
 
185
                return new Point(width, height);
 
186
        }
 
187
        
 
188
        public void pack(){
 
189
                this.dialog.pack();
 
190
        }
 
191
        
 
192
        protected GridData getButtonData(){
 
193
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
 
194
                data.minimumWidth = 80;
 
195
                data.minimumHeight = 25;
 
196
                return data;
 
197
        }
 
198
        
 
199
        public GridData makeGridData(int with,int height,int minWith,int minHeight){
 
200
                GridData data = new GridData();
 
201
                data.minimumWidth = minWith;
 
202
                data.minimumHeight = minHeight;
 
203
                if(with > 0){
 
204
                        data.widthHint = with;
 
205
                }else{
 
206
                        data.horizontalAlignment = SWT.FILL;
 
207
                        data.grabExcessHorizontalSpace = true;
 
208
                }
 
209
                if(height > 0){
 
210
                        data.heightHint = with;
 
211
                }else{
 
212
                        data.verticalAlignment = SWT.FILL;
 
213
                        data.grabExcessVerticalSpace = true;
 
214
                }
 
215
                
 
216
                return data;
 
217
        }
 
218
        
 
219
        public void select(Option option){
 
220
                hideAll();
 
221
                option.setVisible(true);
 
222
                //this.dialog.layout();
 
223
                this.dialog.redraw();
 
224
        }
 
225
        
 
226
        private void hideAll(){
 
227
                Iterator it = this.options.iterator();
 
228
                while(it.hasNext()){
 
229
                        Option option = (Option)it.next();
 
230
                        option.setVisible(false);
 
231
                }
 
232
        }
 
233
        
 
234
        protected void updateOptions(){
 
235
                Iterator it = this.options.iterator();
 
236
                while(it.hasNext()){
 
237
                        Option option = (Option)it.next();
 
238
                        option.updateConfig();
 
239
                }
 
240
                this.config.save();
 
241
        }
 
242
        
 
243
        protected void setDefaults(){
 
244
                Iterator it = this.options.iterator();
 
245
                while(it.hasNext()){
 
246
                        Option option = (Option)it.next();
 
247
                        option.updateDefaults();
 
248
                }
 
249
                this.config.save();
 
250
        }
 
251
        
 
252
        protected void applyConfig(final boolean force){
 
253
                new Thread(new Runnable() {
 
254
                        public void run() {
 
255
                                TGConfigEditor.this.runnables = new ArrayList();
 
256
                                
 
257
                                Iterator it = TGConfigEditor.this.options.iterator();
 
258
                                while(it.hasNext()){
 
259
                                        Option option = (Option)it.next();
 
260
                                        option.applyConfig(force);
 
261
                                }
 
262
                                
 
263
                                new SyncThread( new Runnable() {
 
264
                                        public void run() {
 
265
                                                Iterator it = TGConfigEditor.this.runnables.iterator();
 
266
                                                while(it.hasNext()){
 
267
                                                        Runnable current = (Runnable)it.next();
 
268
                                                        current.run();
 
269
                                                }
 
270
                                                new Thread(new Runnable() {
 
271
                                                        public void run() {
 
272
                                                                TuxGuitar.instance().fireUpdate();
 
273
                                                                TuxGuitar.instance().updateCache(true);
 
274
                                                                TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
 
275
                                                                ActionLock.unlock();
 
276
                                                        }
 
277
                                                }).start();
 
278
                                        }
 
279
                                }).start();
 
280
                        }
 
281
                }).start();
 
282
        }
 
283
        
 
284
        protected void dispose(){
 
285
                Iterator it = this.options.iterator();
 
286
                while(it.hasNext()){
 
287
                        Option option = (Option)it.next();
 
288
                        option.dispose();
 
289
                }
 
290
                getDialog().dispose();
 
291
        }
 
292
        
 
293
        public Properties getDefaults(){
 
294
                if(this.defaults == null){
 
295
                        this.defaults = new TGConfigDefaults().getProperties();
 
296
                }
 
297
                return this.defaults;
 
298
        }
 
299
        
 
300
        public TGConfigManager getConfig(){
 
301
                return this.config;
 
302
        }
 
303
        
 
304
        public TablatureEditor getEditor(){
 
305
                return TuxGuitar.instance().getTablatureEditor();
 
306
        }
 
307
        
 
308
        public Shell getDialog(){
 
309
                return this.dialog;
 
310
        }
 
311
        
 
312
        public void addSyncThread(Runnable runnable){
 
313
                this.runnables.add( runnable );
 
314
        }
 
315
}