www.pudn.com > talk-plugins.rar > StartThreadAction.java


/** 
 * StartThreadAction.java 
 */ 
package talkServer; 
 
import org.eclipse.jface.action.Action; 
import org.eclipse.jface.dialogs.MessageDialog; 
import org.eclipse.ui.IWorkbenchPage; 
import org.eclipse.ui.IWorkbenchWindow; 
import org.eclipse.ui.PartInitException; 
 
import talkServer.business.ListeningThread; 
 
/** 
 * @author Xiang Zhou 
 * @date 2006-4-29 
 */ 
public class StartThreadAction extends Action { 
	private final IWorkbenchWindow window; 
 
	private final String viewId; 
 
	public StartThreadAction(IWorkbenchWindow window, String label, 
			String viewId) { 
		this.window = window; 
		this.viewId = viewId; 
		setText(label); 
		// The id is used to refer to the action in a menu or toolbar 
		setId(ICommandIds.CMD_START_THREAD); 
		// Associate the action with a pre-defined command, to allow key 
		// bindings. 
		setActionDefinitionId(ICommandIds.CMD_START_THREAD); 
		setImageDescriptor(talkServer.TalkServerPlugin 
				.getImageDescriptor("/icons/sample.gif")); 
	} 
 
	public void run() { 
		if (window != null) { 
			try { 
				window.getActivePage().showView(viewId, null, 
						IWorkbenchPage.VIEW_ACTIVATE); 
 
				// START LISTENING 
				startListening(); 
 
			} catch (PartInitException e) { 
				MessageDialog.openError(window.getShell(), "Error", 
						"Error opening view:" + e.getMessage()); 
			} 
		} 
	} 
 
	public void startListening() { 
		if (((ThreadView) window.getActivePage().findView(viewId)).listeningThread == null) { 
			ListeningThread listeningThread = new ListeningThread(((ThreadView) window.getActivePage().findView(viewId)).text); 
			// SET FOR REFERENCE 
			((ThreadView) window.getActivePage().findView(viewId)).listeningThread = listeningThread; 
		} 
 
		if (!((ThreadView) window.getActivePage().findView(viewId)).listeningThread 
				.isAlive()) 
			((ThreadView) window.getActivePage().findView(viewId)).listeningThread 
					.start(); 
 
		((ThreadView) window.getActivePage().findView(viewId)).text.setText("Server started."); 
		 
		System.out.println("Listening start."); 
	} 
}