~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/SkinOption.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.system.config.items;
 
2
 
 
3
import java.io.File;
 
4
import java.io.FileInputStream;
 
5
import java.io.FileNotFoundException;
 
6
import java.io.IOException;
 
7
import java.util.ArrayList;
 
8
import java.util.List;
 
9
import java.util.Properties;
 
10
 
 
11
import org.eclipse.swt.SWT;
 
12
import org.eclipse.swt.events.PaintEvent;
 
13
import org.eclipse.swt.events.PaintListener;
 
14
import org.eclipse.swt.events.SelectionAdapter;
 
15
import org.eclipse.swt.events.SelectionEvent;
 
16
import org.eclipse.swt.graphics.Image;
 
17
import org.eclipse.swt.layout.GridData;
 
18
import org.eclipse.swt.layout.GridLayout;
 
19
import org.eclipse.swt.widgets.Combo;
 
20
import org.eclipse.swt.widgets.Composite;
 
21
import org.eclipse.swt.widgets.Label;
 
22
import org.eclipse.swt.widgets.ToolBar;
 
23
import org.herac.tuxguitar.gui.TuxGuitar;
 
24
import org.herac.tuxguitar.gui.helper.SyncThread;
 
25
import org.herac.tuxguitar.gui.system.config.TGConfigEditor;
 
26
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
 
27
import org.herac.tuxguitar.gui.util.TGFileUtils;
 
28
 
 
29
public class SkinOption extends Option{
 
30
        
 
31
        protected boolean initialized;
 
32
        protected List skins;
 
33
        protected Combo combo;
 
34
        protected Label nameLabel;
 
35
        protected Label authorLabel;
 
36
        protected Label versionLabel;
 
37
        protected Label descriptionLabel;
 
38
        protected Image preview;
 
39
        protected Composite previewArea;
 
40
        
 
41
        public SkinOption(TGConfigEditor configEditor,ToolBar toolBar,final Composite parent){
 
42
                super(configEditor,toolBar,parent,TuxGuitar.getProperty("settings.config.skin"), SWT.FILL,SWT.FILL);
 
43
                this.initialized = false;
 
44
        }
 
45
        
 
46
        public void createOption() {
 
47
                getToolItem().setText(TuxGuitar.getProperty("settings.config.skin"));
 
48
                getToolItem().setImage(TuxGuitar.instance().getIconManager().getOptionSkin());
 
49
                getToolItem().addSelectionListener(this);
 
50
                
 
51
                showLabel(getComposite(),SWT.FILL,SWT.TOP,true, false, SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("settings.config.skin.choose"));
 
52
                
 
53
                Composite composite = new Composite(getComposite(),SWT.NONE);
 
54
                composite.setLayout(new GridLayout());
 
55
                composite.setLayoutData(getTabbedData(SWT.FILL, SWT.FILL, true, false));
 
56
                
 
57
                this.combo = new Combo(composite,SWT.DROP_DOWN | SWT.READ_ONLY);
 
58
                this.combo.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
59
                
 
60
                Composite skinInfoComposite = new Composite(getComposite(),SWT.NONE);
 
61
                skinInfoComposite.setLayout(new GridLayout(2,false));
 
62
                skinInfoComposite.setLayoutData(getTabbedData(SWT.FILL, SWT.FILL, true, false));
 
63
                showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,false,true,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("name") + ": ");
 
64
                this.nameLabel = showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.NONE,0,"");
 
65
                showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,false,true,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("author")+": ");
 
66
                this.authorLabel = showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.NONE,0,"");
 
67
                showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,false,true,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("version")+": ");
 
68
                this.versionLabel = showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.NONE,0,"");
 
69
                showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,false,true,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("description")+": ");
 
70
                this.descriptionLabel = showLabel(skinInfoComposite,SWT.FILL,SWT.CENTER,SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.NONE,0,"");
 
71
                
 
72
                Composite skinPreviewComposite = new Composite(getComposite(),SWT.NONE);
 
73
                skinPreviewComposite.setLayout(new GridLayout());
 
74
                skinPreviewComposite.setLayoutData(getTabbedData(SWT.FILL, SWT.FILL ,true, true));
 
75
                
 
76
                this.previewArea = new Composite(skinPreviewComposite,SWT.DOUBLE_BUFFERED);
 
77
                this.previewArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
78
                this.previewArea.addPaintListener(new PaintListener() {
 
79
                        public void paintControl(PaintEvent e) {
 
80
                                if(SkinOption.this.preview != null && !SkinOption.this.preview.isDisposed()){
 
81
                                        e.gc.drawImage(SkinOption.this.preview, 0, 0);
 
82
                                }
 
83
                        }
 
84
                });
 
85
                
 
86
                this.loadConfig();
 
87
        }
 
88
        
 
89
        protected void loadConfig(){
 
90
                new Thread(new Runnable() {
 
91
                        public void run() {
 
92
                                SkinOption.this.skins = new ArrayList();
 
93
                                String skinPath = TGFileUtils.PATH_SKINS;
 
94
                                if(skinPath != null){
 
95
                                        File skinfolder = new File(skinPath);
 
96
                                        if(skinfolder.exists() && skinfolder.isDirectory()){
 
97
                                                String[] skinNames = skinfolder.list();
 
98
                                                for(int i = 0;i < skinNames.length;i++){
 
99
                                                        Properties properties = new Properties();
 
100
                                                        try {
 
101
                                                                File skinInfoFile = new File(skinPath + File.separator + skinNames[i] + File.separator + "skin.properties");
 
102
                                                                if(skinInfoFile.exists()){
 
103
                                                                        properties.load(new FileInputStream(skinInfoFile));
 
104
                                                                }
 
105
                                                        }catch (FileNotFoundException e) {
 
106
                                                                e.printStackTrace();
 
107
                                                        } catch (IOException e) {
 
108
                                                                e.printStackTrace();
 
109
                                                        }
 
110
                                                        SkinInfo info = new SkinInfo(skinNames[i]);
 
111
                                                        info.setName(properties.getProperty("name",info.getSkin()));
 
112
                                                        info.setAuthor(properties.getProperty("author","Not available."));
 
113
                                                        info.setVersion(properties.getProperty("version","Not available."));
 
114
                                                        info.setDescription(properties.getProperty("description","Not available."));
 
115
                                                        info.setDate(properties.getProperty("date",null));
 
116
                                                        info.setPreview(properties.getProperty("preview",null));
 
117
                                                        SkinOption.this.skins.add(info);
 
118
                                                }
 
119
                                                new SyncThread(new Runnable() {
 
120
                                                        public void run() {
 
121
                                                                if(!isDisposed()){
 
122
                                                                        for(int i = 0;i < SkinOption.this.skins.size();i++){
 
123
                                                                                SkinInfo info = (SkinInfo)SkinOption.this.skins.get(i);
 
124
                                                                                SkinOption.this.combo.add(info.getName());
 
125
                                                                                if(info.getSkin().equals(getConfig().getStringConfigValue(TGConfigKeys.SKIN))){
 
126
                                                                                        SkinOption.this.combo.select(i);
 
127
                                                                                }
 
128
                                                                        }
 
129
                                                                        SkinOption.this.combo.addSelectionListener(new SelectionAdapter() {
 
130
                                                                                public void widgetSelected(SelectionEvent e) {
 
131
                                                                                        int selection = SkinOption.this.combo.getSelectionIndex();
 
132
                                                                                        if(selection >= 0 && selection < SkinOption.this.skins.size()){
 
133
                                                                                                showSkinInfo((SkinInfo)SkinOption.this.skins.get(selection));
 
134
                                                                                        }
 
135
                                                                                }
 
136
                                                                        });
 
137
                                                                        
 
138
                                                                        int selection = SkinOption.this.combo.getSelectionIndex();
 
139
                                                                        if(selection >= 0 && selection < SkinOption.this.skins.size()){
 
140
                                                                                showSkinInfo((SkinInfo)SkinOption.this.skins.get(selection));
 
141
                                                                        }
 
142
                                                                        SkinOption.this.initialized = true;
 
143
                                                                        SkinOption.this.pack();
 
144
                                                                }
 
145
                                                        }
 
146
                                                }).start();
 
147
                                        }
 
148
                                }
 
149
                        }
 
150
                }).start();
 
151
        }
 
152
        
 
153
        protected void showSkinInfo(final SkinInfo info){
 
154
                loadCursor(SWT.CURSOR_WAIT);
 
155
                new SyncThread(new Runnable() {
 
156
                        public void run() {
 
157
                                if(!isDisposed()){
 
158
                                        disposePreview();
 
159
                                        SkinOption.this.nameLabel.setText(info.getName());
 
160
                                        SkinOption.this.authorLabel.setText(info.getAuthor());
 
161
                                        SkinOption.this.descriptionLabel.setText(info.getDescription());
 
162
                                        SkinOption.this.versionLabel.setText((info.getDate() == null)?info.getVersion():info.getVersion() + " (" + info.getDate() + ")");
 
163
                                        if(info.getPreview() != null){
 
164
                                                SkinOption.this.preview = TGFileUtils.loadImage(info.getSkin(),info.getPreview());
 
165
                                        }
 
166
                                        SkinOption.this.previewArea.redraw();
 
167
                                        loadCursor(SWT.CURSOR_ARROW);
 
168
                                }
 
169
                        }
 
170
                }).start();
 
171
        }
 
172
        
 
173
        public void updateConfig() {
 
174
                if(this.initialized){
 
175
                        int selection = this.combo.getSelectionIndex();
 
176
                        if(selection >= 0 && selection < this.skins.size()){
 
177
                                SkinInfo info = (SkinInfo)this.skins.get(selection);
 
178
                                getConfig().setProperty(TGConfigKeys.SKIN,info.getSkin());
 
179
                        }
 
180
                }
 
181
        }
 
182
        
 
183
        public void updateDefaults(){
 
184
                if(this.initialized){
 
185
                        getConfig().setProperty(TGConfigKeys.SKIN,getDefaults().getProperty(TGConfigKeys.SKIN));
 
186
                }
 
187
        }
 
188
        
 
189
        public void applyConfig(boolean force){
 
190
                if(force || (this.initialized && TuxGuitar.instance().getIconManager().shouldReload())){
 
191
                        addSyncThread(new Runnable() {
 
192
                                public void run() {
 
193
                                        TuxGuitar.instance().loadSkin();
 
194
                                }
 
195
                        });
 
196
                }
 
197
        }
 
198
        
 
199
        public void dispose(){
 
200
                this.disposePreview();
 
201
        }
 
202
        
 
203
        public void disposePreview(){
 
204
                if(this.preview != null && !this.preview.isDisposed()){
 
205
                        this.preview.dispose();
 
206
                }
 
207
        }
 
208
        
 
209
        private class SkinInfo{
 
210
                private String skin;
 
211
                private String name;
 
212
                private String date;
 
213
                private String author;
 
214
                private String version;
 
215
                private String description;
 
216
                private String preview;
 
217
                
 
218
                public SkinInfo(String skin){
 
219
                        this.skin = skin;
 
220
                }
 
221
                
 
222
                public String getAuthor() {
 
223
                        return this.author;
 
224
                }
 
225
                
 
226
                public void setAuthor(String author) {
 
227
                        this.author = author;
 
228
                }
 
229
                
 
230
                public String getDate() {
 
231
                        return this.date;
 
232
                }
 
233
                
 
234
                public void setDate(String date) {
 
235
                        this.date = date;
 
236
                }
 
237
                
 
238
                public String getDescription() {
 
239
                        return this.description;
 
240
                }
 
241
                
 
242
                public void setDescription(String description) {
 
243
                        this.description = description;
 
244
                }
 
245
                
 
246
                public String getName() {
 
247
                        return this.name;
 
248
                }
 
249
                
 
250
                public void setName(String name) {
 
251
                        this.name = name;
 
252
                }
 
253
                
 
254
                public String getVersion() {
 
255
                        return this.version;
 
256
                }
 
257
                
 
258
                public void setVersion(String version) {
 
259
                        this.version = version;
 
260
                }
 
261
                
 
262
                public String getSkin() {
 
263
                        return this.skin;
 
264
                }
 
265
                
 
266
                public String getPreview() {
 
267
                        return this.preview;
 
268
                }
 
269
                
 
270
                public void setPreview(String preview) {
 
271
                        this.preview = preview;
 
272
                }
 
273
        }
 
274
}