1 /*
2  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details ( see the LICENSE file ).
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 package org.gudy.azureus2.ui.swt.speedtest;
20 
21 
22 
23 import java.util.HashMap;
24 
25 import org.eclipse.swt.SWT;
26 
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.*;
30 
31 import org.gudy.azureus2.core3.config.COConfigurationManager;
32 import org.gudy.azureus2.core3.internat.MessageText;
33 import org.gudy.azureus2.core3.util.Debug;
34 import org.gudy.azureus2.plugins.PluginInterface;
35 import org.gudy.azureus2.plugins.ipc.IPCException;
36 import org.gudy.azureus2.plugins.ipc.IPCInterface;
37 import org.gudy.azureus2.ui.swt.Messages;
38 import org.gudy.azureus2.ui.swt.config.wizard.ConfigureWizard;
39 import org.gudy.azureus2.ui.swt.shells.CoreWaiterSWT;
40 import org.gudy.azureus2.ui.swt.shells.MessageBoxShell;
41 import org.gudy.azureus2.ui.swt.wizard.AbstractWizardPanel;
42 import org.gudy.azureus2.ui.swt.wizard.IWizardPanel;
43 
44 import com.aelitis.azureus.core.AzureusCore;
45 import com.aelitis.azureus.core.AzureusCoreFactory;
46 import com.aelitis.azureus.core.AzureusCoreRunningListener;
47 import com.aelitis.azureus.ui.UIFunctions;
48 import com.aelitis.azureus.ui.UIFunctionsManager;
49 import com.aelitis.azureus.ui.UserPrompterResultListener;
50 
51 public class
52 SpeedTestSelector
53 	extends AbstractWizardPanel<SpeedTestWizard>
54 {
55 	private boolean	mlab_test = true;
56 
SpeedTestSelector(SpeedTestWizard wizard, IWizardPanel previous)57 	public SpeedTestSelector(SpeedTestWizard wizard, IWizardPanel previous) {
58 		super(wizard, previous);
59 	}
60 
show()61 	public void show() {
62 		wizard.setTitle(MessageText.getString("speedtest.wizard.select.title"));
63 		wizard.setCurrentInfo( "" );
64 		final Composite rootPanel = wizard.getPanel();
65 		GridLayout layout = new GridLayout();
66 		layout.numColumns = 1;
67 		rootPanel.setLayout(layout);
68 
69 		Composite panel = new Composite(rootPanel, SWT.NULL);
70 		GridData gridData = new GridData(GridData.FILL_BOTH);
71 		panel.setLayoutData(gridData);
72 		layout = new GridLayout();
73 		layout.numColumns = 1;
74 		panel.setLayout(layout);
75 
76 		final Group gRadio = new Group(panel, SWT.NULL);
77 		Messages.setLanguageText(gRadio, "speedtest.wizard.select.group");
78 		gRadio.setLayoutData(gridData);
79 		layout = new GridLayout();
80 		layout.numColumns = 1;
81 		gRadio.setLayout( layout );
82 		gridData = new GridData(GridData.FILL_HORIZONTAL);
83 		gRadio.setLayoutData(gridData);
84 
85 
86 		// general test
87 
88 		Button auto_button = new Button (gRadio, SWT.RADIO);
89 		Messages.setLanguageText(auto_button, "speedtest.wizard.select.general");
90 		auto_button.setSelection( true );
91 
92 		// BT
93 
94 		final Button manual_button = new Button( gRadio, SWT.RADIO );
95 		Messages.setLanguageText(manual_button, "speedtest.wizard.select.bt");
96 
97 		manual_button.addListener(
98 				SWT.Selection,
99 				new Listener()
100 				{
101 					public void
102 					handleEvent(
103 							Event arg0 )
104 					{
105 						mlab_test = !manual_button.getSelection();
106 					}
107 				});
108 	}
109 
110 
111 
112 	public boolean
isNextEnabled()113 	isNextEnabled()
114 	{
115 		return( true );
116 	}
117 
118 	public boolean
isPreviousEnabled()119 	isPreviousEnabled()
120 	{
121 		return( false );
122 	}
123 
124 	public IWizardPanel
getNextPanel()125 	getNextPanel()
126 	{
127 		if ( mlab_test ){
128 
129 			wizard.close();
130 
131 			runMLABTest( null);
132 
133 			//new ConfigureWizard( false, ConfigureWizard.WIZARD_MODE_SPEED_TEST_AUTO );
134 
135 			return( null );
136 
137 		}else{
138 
139 			return( new SpeedTestPanel( wizard, null ));
140 		}
141 	}
142 
runMLABTest(final Runnable runWhenClosed)143 	public static void runMLABTest(final Runnable runWhenClosed) {
144 		CoreWaiterSWT.waitForCoreRunning(new AzureusCoreRunningListener() {
145 			public void azureusCoreRunning(AzureusCore core) {
146 				UIFunctionsManager.getUIFunctions().installPlugin("mlab",
147 						"dlg.install.mlab", new UIFunctions.actionListener() {
148 							public void actionComplete(Object result) {
149 								if (result instanceof Boolean) {
150 									_runMLABTest(runWhenClosed);
151 								} else {
152 
153 									try {
154 										Throwable error = (Throwable) result;
155 
156 										Debug.out(error);
157 
158 									} finally {
159 										if (runWhenClosed != null) {
160 											runWhenClosed.run();
161 										}
162 									}
163 								}
164 							}
165 						});
166 			}
167 		});
168 	}
169 
_runMLABTest(final Runnable runWhenClosed)170 	private static void _runMLABTest(final Runnable runWhenClosed) {
171 		PluginInterface pi = AzureusCoreFactory.getSingleton().getPluginManager().getPluginInterfaceByID(
172 				"mlab");
173 
174 		if ( pi == null ){
175 			Debug.out("mlab plugin not available");
176 			if (runWhenClosed != null) {
177 				runWhenClosed.run();
178 			}
179 		}else{
180 			try {
181 				HashMap<String, Object> map = new HashMap<String, Object>();
182 
183 				pi.getIPC().invoke("runTest", new Object[] {
184 					map,
185 					new IPCInterface() {
186 						public Object invoke(String methodName, Object[] params)
187 								throws IPCException {
188 							// we could set SpeedTest Completed when methodName == "results"
189 							// or ask user if they want to be prompted again if it isn't
190 							// But, we'd have to pass a param into runMLABTest (so we don't
191 							// get prompt on menu invocation).
192 
193 							// For now, show only once, with no reprompt (even if they cancel).
194 							// They can use the menu
195 							COConfigurationManager.setParameter("SpeedTest Completed", true);
196 
197 							if (runWhenClosed != null) {
198 								runWhenClosed.run();
199 							}
200 							return null;
201 						}
202 
203 						public boolean canInvoke(String methodName, Object[] params) {
204 							return true;
205 						}
206 					},
207 					true
208 				});
209 
210 			} catch (Throwable e) {
211 
212 				Debug.out(e);
213 				if (runWhenClosed != null) {
214 					runWhenClosed.run();
215 				}
216 			}
217 		}
218 	}
219 }
220