//这个进度条是别人写的,呵呵
import java.awt.Point;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class ORProgressBar {
/**
* Logger for this class
*/
public boolean isDispose = false;
private Shell shell = null;
private Display display = null;
private String message = "";
private String title = "";
private ProgressBar pbar = null;
private Object obj = null;
/**
* 通用Eclipse平台进度条
* @param pObj 调用类实例
* @param ptitle 进度条标题
* @param pmsg 进度条显示信息
*/
public ORProgressBar(Object pObj, String ptitle, String pmsg) {
obj = pObj;
message = pmsg;
title = ptitle;
init();
}
private void init() {
try {
display = new Display();
} catch (Exception e) {
try {
display = Display.getDefault();
} catch (Exception e1) {
display = Display.getCurrent();
}
}
shell = new Shell(display);
shell.setSize(335, 100);
Point p = UtilApi.getMsgboxLocate();
shell.setLocation(p.x, p.y);
pbar = new ProgressBar(shell, SWT.INDETERMINATE);
pbar.setBounds(10, 30, 300, 20);
shell.setText(title);
Label label = new Label(shell, SWT.NULL);
label.setText(this.message);
label.setAlignment(SWT.CENTER);
label.setBounds(10, 10, 300, 20);
shell.setEnabled(false);
}
/**
* doProcess:调用实例类progress方法
* @param @return 设定文件
* @return boolean 对象类型
* @throws
* @since CodingExample Ver 1.1
*/
private boolean doProcess() {
try {
Class cls = obj.getClass();
//通过反射机制调用传入类中声明的方法。
Method mth = cls.getDeclaredMethod("progress", null);
mth.invoke(obj, null);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return true;
}
public void run() {
shell.open();
if (display == null)
display = Display.getDefault();
if (display != null && !display.isDisposed()) {
Thread t1 = new Thread() {
public void run() {
isDispose = doProcess();
}
};
t1.start();
System.out.println("111");
display.syncExec(new Runnable() {
public void run() {
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
if (!isDispose) {
try {
shell.forceActive();
display.getThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
display.dispose();
}
}
}
}
});
}
}
public static void main(String[] args) {
ORProgressBar t = new ORProgressBar(null, "", "");
ORProgressBar bar = new ORProgressBar(t, "进度", "正在...,请稍候...");
bar.run();
}
/**
* progress:实例对象中运行内容(必须存在)
* @param 设定文件
* @return void 对象类型
* @throws
* @since CodingExample Ver 1.1
*/
public void progress() {
System.out.println("Progress()----------");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}