1
package org.herac.tuxguitar.gui.system.config.items;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7
import java.util.ArrayList;
9
import java.util.Properties;
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;
29
public class SkinOption extends Option{
31
protected boolean initialized;
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;
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;
46
public void createOption() {
47
getToolItem().setText(TuxGuitar.getProperty("settings.config.skin"));
48
getToolItem().setImage(TuxGuitar.instance().getIconManager().getOptionSkin());
49
getToolItem().addSelectionListener(this);
51
showLabel(getComposite(),SWT.FILL,SWT.TOP,true, false, SWT.TOP | SWT.LEFT | SWT.WRAP,SWT.BOLD,0,TuxGuitar.getProperty("settings.config.skin.choose"));
53
Composite composite = new Composite(getComposite(),SWT.NONE);
54
composite.setLayout(new GridLayout());
55
composite.setLayoutData(getTabbedData(SWT.FILL, SWT.FILL, true, false));
57
this.combo = new Combo(composite,SWT.DROP_DOWN | SWT.READ_ONLY);
58
this.combo.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
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,"");
72
Composite skinPreviewComposite = new Composite(getComposite(),SWT.NONE);
73
skinPreviewComposite.setLayout(new GridLayout());
74
skinPreviewComposite.setLayoutData(getTabbedData(SWT.FILL, SWT.FILL ,true, true));
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);
89
protected void loadConfig(){
90
new Thread(new Runnable() {
92
SkinOption.this.skins = new ArrayList();
93
String skinPath = TGFileUtils.PATH_SKINS;
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();
101
File skinInfoFile = new File(skinPath + File.separator + skinNames[i] + File.separator + "skin.properties");
102
if(skinInfoFile.exists()){
103
properties.load(new FileInputStream(skinInfoFile));
105
}catch (FileNotFoundException e) {
107
} catch (IOException e) {
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);
119
new SyncThread(new Runnable() {
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);
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));
138
int selection = SkinOption.this.combo.getSelectionIndex();
139
if(selection >= 0 && selection < SkinOption.this.skins.size()){
140
showSkinInfo((SkinInfo)SkinOption.this.skins.get(selection));
142
SkinOption.this.initialized = true;
143
SkinOption.this.pack();
153
protected void showSkinInfo(final SkinInfo info){
154
loadCursor(SWT.CURSOR_WAIT);
155
new SyncThread(new Runnable() {
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());
166
SkinOption.this.previewArea.redraw();
167
loadCursor(SWT.CURSOR_ARROW);
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());
183
public void updateDefaults(){
184
if(this.initialized){
185
getConfig().setProperty(TGConfigKeys.SKIN,getDefaults().getProperty(TGConfigKeys.SKIN));
189
public void applyConfig(boolean force){
190
if(force || (this.initialized && TuxGuitar.instance().getIconManager().shouldReload())){
191
addSyncThread(new Runnable() {
193
TuxGuitar.instance().loadSkin();
199
public void dispose(){
200
this.disposePreview();
203
public void disposePreview(){
204
if(this.preview != null && !this.preview.isDisposed()){
205
this.preview.dispose();
209
private class SkinInfo{
213
private String author;
214
private String version;
215
private String description;
216
private String preview;
218
public SkinInfo(String skin){
222
public String getAuthor() {
226
public void setAuthor(String author) {
227
this.author = author;
230
public String getDate() {
234
public void setDate(String date) {
238
public String getDescription() {
239
return this.description;
242
public void setDescription(String description) {
243
this.description = description;
246
public String getName() {
250
public void setName(String name) {
254
public String getVersion() {
258
public void setVersion(String version) {
259
this.version = version;
262
public String getSkin() {
266
public String getPreview() {
270
public void setPreview(String preview) {
271
this.preview = preview;