RCP中可以取消的进度条

<pre name="code" class="java">package cn.com.zte.esb.progress;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.jboss.tools.flow.jpdl4.util.ZteMessages;

/**
 * 测试Phoenix连接的进度条
 *
 */
public class TestPhoenixProgress implements IRunnableWithProgress {

	public Map<String, String> params = new HashMap<String, String>();
	public Map<String, String> resultMap = new HashMap<String, String>();
	class Login implements Runnable {
		public Exception exception;

		Login() {

		}
		public void run() {
			resultMap.clear();
			try {
				String url=params.get("url");
				String driver=params.get("driver");
				Class.forName(driver);
				Connection conn = DriverManager.getConnection(url);
				if(conn!=null){
					resultMap.put("success",url);
					conn.close();
				}else{
					resultMap.put("error",url);
				}
			} catch (Exception e) {
				resultMap.put("error",e.getMessage());
				e.printStackTrace();
			} 
			
		}
	}


	public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException {
		monitor.beginTask(ZteMessages.getString("connection")+ "...", IProgressMonitor.UNKNOWN);
		{
			Login login = new Login();
			Thread loginThread = new Thread(login, "LoginThread");
			loginThread.start();
			do {
				Thread.sleep(250L);
				//进度条被取消事件
				if (monitor.isCanceled()) {
					throw new InterruptedException("interrupted");
				}
			} while (loginThread.isAlive());
		}
		//完成
		monitor.done();
		String message_error = resultMap.get("error");
		String message_success = resultMap.get("success");
		//根据返回值抛出异常,调用出根据异常信息判断连接是否成功
		if (message_success != null) {
			throw new cn.com.zte.esb.exception.LoginException("success#"+message_success);
		}
		if (message_error != null) {
			throw new cn.com.zte.esb.exception.LoginException("error#"+message_error);
		}
	}

}



//调用

                    		final Map<String, String> map=new HashMap<String, String>();
                    		map.put("url",connection.getText());
                    		map.put("driver",driver);
                    		ProgressMonitorDialog pmd = new ProgressMonitorDialog(Display.getDefault().getActiveShell());  
                    		TestPhoenixProgress progress=new TestPhoenixProgress();
							progress.params=map;
							try {
								pmd.run(true, true, progress);
							} catch (InvocationTargetException e1) {
								Throwable targetException = e1.getTargetException();
								if(targetException!=null){
									String message=targetException.getMessage();
									if(message!=null){
										message=message.trim();
										if(message.startsWith("success")){
											 MessageDialog.openInformation(Display.getDefault().getActiveShell(), ZteMessages
								                        .getString("ConnectionTest"),  ZteMessages.getString("ConnectionSuccess"));
										}else if(message.startsWith("error")){
											MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),"\n"+message.substring(6));
										}else{
											if(!"interrupted".equals(e1.getMessage()))
												MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),e1.getMessage());
										}
									}
								}
							} catch (InterruptedException e1) {
								String message = e1.getMessage();
								if(message!=null){
									message=message.trim();
									if(message.startsWith("success")){
										 MessageDialog.openInformation(Display.getDefault().getActiveShell(), ZteMessages
							                        .getString("ConnectionTest"),  ZteMessages.getString("ConnectionSuccess"));
									}else if(message.startsWith("error")){
										MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),"\n"+message);
									}else{
										if(!"interrupted".equals(e1.getMessage()))
											MessageDialog.openError(Display.getDefault().getActiveShell(), ZteMessages.getString("ConnectionTest"),e1.getMessage());
									}
								}
							}
                    		
                    		return;
                    	



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值