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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/system/config/items/LanguageOption.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080619003030-agens2gvd5m4dacu
New upstream release (Closes: #481728) also (LP: #176979, #212207)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.system.config.items;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Collections;
 
5
import java.util.Comparator;
 
6
import java.util.List;
 
7
 
 
8
import org.eclipse.swt.SWT;
 
9
import org.eclipse.swt.graphics.Point;
 
10
import org.eclipse.swt.layout.GridData;
 
11
import org.eclipse.swt.layout.GridLayout;
 
12
import org.eclipse.swt.widgets.Composite;
 
13
import org.eclipse.swt.widgets.Table;
 
14
import org.eclipse.swt.widgets.TableColumn;
 
15
import org.eclipse.swt.widgets.TableItem;
 
16
import org.eclipse.swt.widgets.ToolBar;
 
17
import org.herac.tuxguitar.gui.TuxGuitar;
 
18
import org.herac.tuxguitar.gui.helper.SyncThread;
 
19
import org.herac.tuxguitar.gui.system.config.TGConfigEditor;
 
20
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
 
21
 
 
22
public class LanguageOption extends Option{
 
23
        protected boolean initialized;
 
24
        protected Table table;
 
25
        protected TableColumn column;
 
26
        
 
27
        public LanguageOption(TGConfigEditor configEditor,ToolBar toolBar,final Composite parent){
 
28
                super(configEditor,toolBar,parent,TuxGuitar.getProperty("settings.config.language"), SWT.FILL, SWT.FILL);
 
29
                this.initialized = false;
 
30
        }
 
31
        
 
32
        public void createOption(){
 
33
                getToolItem().setText(TuxGuitar.getProperty("settings.config.language"));
 
34
                getToolItem().setImage(TuxGuitar.instance().getIconManager().getOptionLanguage());
 
35
                getToolItem().addSelectionListener(this);
 
36
                
 
37
                showLabel(getComposite(),SWT.FILL,SWT.TOP, true, false,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("settings.config.language.choose"));
 
38
                
 
39
                Composite composite = new Composite(getComposite(),SWT.NONE);
 
40
                composite.setLayout(new GridLayout());
 
41
                composite.setLayoutData(getTabbedData(SWT.FILL, SWT.FILL));
 
42
                
 
43
                this.table = new Table(composite, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
 
44
                this.table.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
45
                this.table.setHeaderVisible(true);
 
46
                this.table.setLinesVisible(false);
 
47
                
 
48
                this.column = new TableColumn(this.table, SWT.LEFT);
 
49
                this.column.setText(TuxGuitar.getProperty("settings.config.language.choose"));
 
50
                this.column.pack();
 
51
                
 
52
                this.loadConfig();
 
53
        }
 
54
        
 
55
        protected void loadTableItem(String text, String data, boolean selected){
 
56
                TableItem item = new TableItem(this.table, SWT.NONE);
 
57
                item.setText(text);
 
58
                item.setData(data);
 
59
                if( selected ){
 
60
                        this.table.setSelection(item);
 
61
                }
 
62
        }
 
63
        
 
64
        protected List getLanguageItems(String[] languages){
 
65
                List list = new ArrayList();
 
66
                for(int i = 0;i < languages.length; i ++){
 
67
                        list.add( new LanguageItem(languages[i],TuxGuitar.getProperty("locale." + languages[i] ) ) );
 
68
                }
 
69
                Collections.sort(list, new Comparator() {
 
70
                        public int compare(Object o1, Object o2) {
 
71
                                if( o1 instanceof LanguageItem && o2 instanceof LanguageItem){
 
72
                                        LanguageItem l1 = (LanguageItem)o1;
 
73
                                        LanguageItem l2 = (LanguageItem)o2;
 
74
                                        return l1.getValue().compareTo( l2.getValue() );
 
75
                                }
 
76
                                return 0;
 
77
                        }
 
78
                } );
 
79
                return list;
 
80
        }
 
81
        
 
82
        protected void loadConfig(){
 
83
                new Thread(new Runnable() {
 
84
                        public void run() {
 
85
                                final String language = getConfig().getStringConfigValue(TGConfigKeys.LANGUAGE);
 
86
                                final List languages = getLanguageItems( TuxGuitar.instance().getLanguageManager().getLanguages() );
 
87
                                new SyncThread(new Runnable() {
 
88
                                        public void run() {
 
89
                                                if(!isDisposed()){
 
90
                                                        // Load default item
 
91
                                                        loadTableItem(TuxGuitar.getProperty("locale.default"), new String(), true);
 
92
                                                        
 
93
                                                        for(int i = 0;i < languages.size(); i ++){
 
94
                                                                LanguageItem item = (LanguageItem)languages.get( i );
 
95
                                                                loadTableItem(item.getValue(),item.getKey(),(language != null && item.getKey().equals( language )));
 
96
                                                        }
 
97
                                                        
 
98
                                                        LanguageOption.this.initialized = true;
 
99
                                                        LanguageOption.this.column.pack();
 
100
                                                        LanguageOption.this.pack();
 
101
                                                }
 
102
                                        }
 
103
                                }).start();
 
104
                        }
 
105
                }).start();
 
106
        }
 
107
        
 
108
        public void updateConfig(){
 
109
                if(this.initialized){
 
110
                        String language = null;
 
111
                        if(this.table != null && !this.table.isDisposed()){
 
112
                                int index = this.table.getSelectionIndex();
 
113
                                if(index >= 0 && index < this.table.getItemCount() ){
 
114
                                        language = (String)this.table.getItem(index).getData();
 
115
                                }
 
116
                        }
 
117
                        getConfig().setProperty(TGConfigKeys.LANGUAGE, language );
 
118
                }
 
119
        }
 
120
        
 
121
        public void updateDefaults(){
 
122
                if(this.initialized){
 
123
                        getConfig().setProperty(TGConfigKeys.LANGUAGE,getDefaults().getProperty(TGConfigKeys.LANGUAGE));
 
124
                }
 
125
        }
 
126
        
 
127
        public void applyConfig(boolean force){
 
128
                if(force || this.initialized){
 
129
                        boolean changed = force;
 
130
                        
 
131
                        if(!changed){
 
132
                                String languageLoaded = TuxGuitar.instance().getLanguageManager().getLanguage();
 
133
                                String languageConfigured = getConfig().getStringConfigValue(TGConfigKeys.LANGUAGE);
 
134
                                if(languageLoaded == null && languageConfigured == null){
 
135
                                        changed = false;
 
136
                                }
 
137
                                else if(languageLoaded != null && languageConfigured != null){
 
138
                                        changed = ( !languageLoaded.equals( languageConfigured ) );
 
139
                                }
 
140
                                else {
 
141
                                        changed = true;
 
142
                                }
 
143
                        }
 
144
                        
 
145
                        if(changed){
 
146
                                addSyncThread(new Runnable() {
 
147
                                        public void run() {
 
148
                                                TuxGuitar.instance().loadLanguage();
 
149
                                        }
 
150
                                });
 
151
                        }
 
152
                }
 
153
        }
 
154
        
 
155
        public Point computeSize(){
 
156
                return this.computeSize(SWT.DEFAULT,SWT.NONE);
 
157
        }
 
158
        
 
159
        private class LanguageItem {
 
160
                private String key;
 
161
                private String value;
 
162
                
 
163
                public LanguageItem(String key, String value){
 
164
                        this.key = key;
 
165
                        this.value = value;
 
166
                }
 
167
                
 
168
                public String getKey(){
 
169
                        return this.key;
 
170
                }
 
171
                
 
172
                public String getValue(){
 
173
                        return this.value;
 
174
                }
 
175
        }
 
176
}
 
 
b'\\ No newline at end of file'