1 package org.herac.tuxguitar.player.impl.jsa.assistant;
2 
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.events.SelectionAdapter;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Group;
14 import org.eclipse.swt.widgets.Shell;
15 import org.herac.tuxguitar.gui.TuxGuitar;
16 import org.herac.tuxguitar.gui.util.ConfirmDialog;
17 import org.herac.tuxguitar.gui.util.DialogUtils;
18 import org.herac.tuxguitar.player.impl.jsa.midiport.MidiPortSynthesizer;
19 import org.herac.tuxguitar.util.TGSynchronizer;
20 
21 public class SBAssistant {
22 
23 	public static final SBUrl[] URLS = new SBUrl[]{
24 		new SBUrl(toURL("http://java.sun.com/products/java-media/sound/soundbank-min.gm.zip"),TuxGuitar.getProperty("jsa.soundbank-assistant.minimal")),
25 		new SBUrl(toURL("http://java.sun.com/products/java-media/sound/soundbank-mid.gm.zip"),TuxGuitar.getProperty("jsa.soundbank-assistant.medium")),
26 		new SBUrl(toURL("http://java.sun.com/products/java-media/sound/soundbank-deluxe.gm.zip"),TuxGuitar.getProperty("jsa.soundbank-assistant.deluxe")),
27 	};
28 
29 	private MidiPortSynthesizer synthesizer;
30 
SBAssistant(MidiPortSynthesizer synthesizer)31 	public SBAssistant(MidiPortSynthesizer synthesizer){
32 		this.synthesizer = synthesizer;
33 	}
34 
process()35 	public void process(){
36 		new Thread(new Runnable() {
37 			public void run() {
38 				try {
39 					TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
40 						public void run() {
41 							ConfirmDialog dialog = new ConfirmDialog(TuxGuitar.getProperty("jsa.soundbank-assistant.confirm-message"));
42 							dialog.setDefaultStatus( ConfirmDialog.STATUS_NO );
43 							if (dialog.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO , ConfirmDialog.BUTTON_YES) == ConfirmDialog.STATUS_YES){
44 								open();
45 							}
46 						}
47 					});
48 				} catch (Throwable e) {
49 					e.printStackTrace();
50 				}
51 			}
52 		}).start();
53 	}
54 
open()55 	protected void open(){
56 		final Shell dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
57 		dialog.setLayout(new GridLayout());
58 
59 		//------------------------------------------------------------------------------
60         Group group = new Group(dialog,SWT.SHADOW_ETCHED_IN);
61         group.setLayout(new GridLayout());
62         group.setLayoutData(getGroupData());
63         group.setText(TuxGuitar.getProperty("jsa.soundbank-assistant.select"));
64 
65 
66         final Button urls[] = new Button[ URLS.length ];
67         for(int i = 0; i < URLS.length ; i ++){
68 	        urls[i] = new Button(group, SWT.RADIO);
69 	        urls[i].setText(URLS[i].getName());
70 	        urls[i].setData(URLS[i]);
71 	        urls[i].setSelection(i == 0);
72         }
73 
74         //------------------BUTTONS--------------------------
75         Composite buttons = new Composite(dialog, SWT.NONE);
76         buttons.setLayout(new GridLayout(2,false));
77         buttons.setLayoutData(new GridData(SWT.RIGHT,SWT.BOTTOM,true,false));
78 
79         Button buttonOk = new Button(buttons, SWT.PUSH);
80         buttonOk.setText(TuxGuitar.getProperty("ok"));
81         buttonOk.setLayoutData(getButtonsData());
82         buttonOk.addSelectionListener(new SelectionAdapter() {
83             public void widgetSelected(SelectionEvent arg0) {
84             	URL url = getSelection(urls);
85 
86             	dialog.dispose();
87 
88             	if(url != null ){
89             		install(url);
90             	}
91             }
92         });
93 
94         Button buttonCancel = new Button(buttons, SWT.PUSH);
95         buttonCancel.setText(TuxGuitar.getProperty("cancel"));
96         buttonCancel.setLayoutData(getButtonsData());
97         buttonCancel.addSelectionListener(new SelectionAdapter() {
98             public void widgetSelected(SelectionEvent arg0) {
99                 dialog.dispose();
100             }
101         });
102 
103         dialog.setDefaultButton( buttonOk );
104 
105         DialogUtils.openDialog(dialog, DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK);
106 	}
107 
getGroupData()108 	protected GridData getGroupData(){
109 		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
110 		data.minimumWidth = 250;
111 		return data;
112 	}
113 
getButtonsData()114 	protected GridData getButtonsData(){
115 		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
116 		data.minimumWidth = 80;
117 		data.minimumHeight = 25;
118 		return data;
119 	}
120 
getSelection(Button[] buttons)121 	protected URL getSelection(Button[] buttons){
122     	for(int i = 0; i < buttons.length ; i ++){
123     		if( buttons[i].getSelection() && buttons[i].getData() instanceof SBUrl ){
124     			return ((SBUrl)buttons[i].getData()).getUrl();
125     		}
126     	}
127     	return null;
128 	}
129 
install(URL url )130 	protected void install(URL url ){
131 		new SBInstallerGui(url,this.synthesizer).open();
132 	}
133 
toURL(String s)134 	private static URL toURL(String s){
135 		try {
136 			return new URL(s);
137 		} catch (MalformedURLException e) {
138 			e.printStackTrace();
139 		}
140 		return null;
141 	}
142 
143 }
144