package wwww;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import jsoup.ConnectionManager;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.GUITest01;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class InfoqUtil {
/*public static void main(String[] args) {
InfoqUtil util = new InfoqUtil();
try {
util.readDataLink();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}*/
private GUITest01 guitest;//前台界面对象
private boolean stopFlag;//是否停止的标识
/**
* 取得前台界面兑现
* @param taskGUI
*/
public InfoqUtil(GUITest01 taskGUI){
this.guitest = taskGUI;
}
/**
* 停止执行
*/
public void stop(){
stopFlag = true;
}
/**
* 开始执行
* @param taskCount
*/
public void start(int taskCount){
stopFlag = false;//将执行状态初始化成执行
intsertConsoleText("------后台线程开始执行任务:获取infoQ新闻------\n");
try {
readDataLink();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (ExecutionException e1) {
e1.printStackTrace();
}
/*for (int i = 0; i < taskCount; i++) {
if(stopFlag)//如果发现执行状态为停止,则退出次循环
break;
try {
Thread.sleep(1);//0.5秒一次循环
} catch (InterruptedException e) {
e.printStackTrace();
}
//intsertConsoleText("开始处理第"+(i+1)+"个任务...\n");//设置文本内容
try {
Thread.sleep(1);//休眠0.5秒
} catch (InterruptedException e) {
e.printStackTrace();
}
//intsertConsoleText("任务"+(i+1)+"处理完毕\n");
moveProgressBar(i);//设置进度条移动
}*/
intsertConsoleText("------后台线程结束执行任务------\n");
setTaskGUIButtonState(true);//刷新界面按钮状态
}
/**
* 界面按钮状态
* @param bFlag
*/
private void setTaskGUIButtonState(final boolean bFlag){
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
guitest.setButtonState(bFlag);
}
});
}
/**
* 显示进度条
* @param progress
*/
private void moveProgressBar(final int progress){
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
guitest.getProgressBar().setSelection(progress);
}
});
}
/**
* 文本插入
* @param str
*/
private void intsertConsoleText(final String str) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
guitest.getConsoleText().insert(str);
}
});
}
public void readDataLink() throws InterruptedException, ExecutionException{
ForkJoinPool forkJoinPool = new ForkJoinPool(10);
ForkJoinFirst firstTask = new ForkJoinFirst(1,150,this);
Future<List<String>> result = forkJoinPool.submit(firstTask);
System.out.println(result.get().size());
forkJoinPool.shutdown();
}
public List<String> readData(Integer pageNo) {
String url = "http://www.infoq.com/cn/articles/"+12*(pageNo-1);
List<String> resList = new ArrayList<String>();
Document doc;
try {
doc = ConnectionManager.getInstance().getDocument(url);
} catch (Exception e) {
e.printStackTrace();
return resList;
}
Elements div = doc.getElementsByAttributeValue("class", "news_type2 full_screen");
for (Element element : div) {
resList.add(element.child(0).html());
//System.out.println(element.child(0).html());
intsertConsoleText(element.child(0).html()+"\n");
}
return resList;
}
}