?? taskproxy.java
字號(hào):
if (stopTask(ID)) {
message = "<<"+taskName+">> 任務(wù)已經(jīng)中止!";
insertInstantInfo(true, message);
JOptionPane.showMessageDialog(mainFrame, message);
result = true;
} else {
model.setValueAt(new Boolean(true), rowNo, runColumn);
}
} else {
model.setValueAt(new Boolean(true), rowNo, runColumn);
}
}
return result;
}
public void runAllTask(){
int rows = table.getRowCount();
if (rows == 0) {
JOptionPane.showMessageDialog(mainFrame, "任務(wù)列表中未定義任何任務(wù)!");
return;
}
int n = JOptionPane.showConfirmDialog(
mainFrame, "即將啟動(dòng)任務(wù)列表中的所有未啟動(dòng)任務(wù),是否繼續(xù)?",
"啟動(dòng)所有任務(wù)",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
// 啟動(dòng)任務(wù)列表所有未啟動(dòng)的任務(wù)
int cols = table.getColumnCount();
int count = 0;
String message;
errorMsg = "";
for(int i=0;i<rows;i++){
Integer ID = (Integer)table.getValueAt(i, 0)-1;
String taskName = (String)table.getValueAt(i, 1);
boolean boo = ((Boolean)table.getValueAt(i, cols-1)).booleanValue();
if (!boo) {
if(runTask(ID, taskName)){
message = "任務(wù)<<"+taskName+">>已經(jīng)啟動(dòng)!";
insertInstantInfo(true, message);
errorMsg += taskName+" 任務(wù)已經(jīng)啟動(dòng)!\r\n";
table.setValueAt(new Boolean(true), i, cols-1);
count++;
}
else{
message = "任務(wù)<<"+taskName+">>無法啟動(dòng)!";
insertInstantInfo(true, message);
errorMsg += "無法啟動(dòng)任務(wù):"+taskName+"\r\n";
table.setValueAt(new Boolean(false), i, cols-1);
/*if (errorMsg.indexOf("步驟") != -1) {
editTask(i, 1);
}
else if (errorMsg.indexOf("調(diào)度") != -1
|| errorMsg.indexOf("已到結(jié)束時(shí)間") != -1
|| errorMsg.indexOf("已過執(zhí)行時(shí)間") != -1) {
editTask(i, 2);
}*/
}
}
}
JOptionPane.showMessageDialog(mainFrame, errorMsg);
/*if(count < rows && count > 0) {
JOptionPane.showMessageDialog(mainFrame, errorMsg+"本次操作啟動(dòng)了部分任務(wù)!");
}
else if(count == 0 || count == rows) {
JOptionPane.showMessageDialog(mainFrame, errorMsg+"任務(wù)列表中的任務(wù)已經(jīng)全部啟動(dòng)!");
}*/
}
}
public void stopAllTask(){
int rows = table.getRowCount();
if (rows == 0) {
JOptionPane.showMessageDialog(mainFrame, "任務(wù)列表中未定義任何任務(wù)!");
return;
}
int n = JOptionPane.showConfirmDialog(
mainFrame, "即將中止任務(wù)列表中的所有正在運(yùn)行的任務(wù),是否繼續(xù)?",
"中止所有任務(wù)",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
// 停止任務(wù)列表所有執(zhí)行中的任務(wù)
int cols = table.getColumnCount();
int count=0;
String message;
for(int i=0;i<rows;i++){
String taskName = (String)table.getValueAt(i, 1);
boolean boo = ((Boolean)table.getValueAt(i, cols-1)).booleanValue();
if (boo) {
if (stopTask((Integer)table.getValueAt(i, 0)-1)) {
message = "任務(wù)<<"+taskName+">>已經(jīng)中止!";
insertInstantInfo(true, message);
model.setValueAt(new Boolean(false), i, cols-1);
}
count++;
}
}
if (count > 0){
JOptionPane.showMessageDialog(mainFrame, "任務(wù)列表中的任務(wù)已經(jīng)全部中止!");
}
else {
JOptionPane.showMessageDialog(mainFrame, "任務(wù)列表中沒有正在執(zhí)行中的任務(wù)!");
}
}
}
public void insertInstantInfo(boolean isSuccess, String text) {
JTextPane loginfo = mainFrame.getLogInfo();
javax.swing.text.Document doc = loginfo.getDocument();
try {
AttributeSet atts = null;
if (!isSuccess) {
atts = mainFrame.getLogInfo().getStyle("Red");
}
doc.insertString(doc.getLength(), text+"\r\n", atts);
} catch (BadLocationException e) {
e.printStackTrace();
}
loginfo.setDocument(doc);
loginfo.setCaretPosition(doc.getLength());
}
public int getTasksSize(){
return tasks.size();
}
private boolean runTask(Integer ID, String taskName) {
boolean result = false;
this.ID = ID;
obj = taskMap.get(ID);
ArrayList stepData = (ArrayList)((Object[])obj)[1];
if (stepData.size() == 0) {
errorMsg += "未定義步驟!\r\n";
return false;
}
ArrayList attempData = (ArrayList)((Object[])obj)[2];
if (attempData.size() == 0 && runType == 0) {
errorMsg += "未定義調(diào)度!\r\n";
return false;
}
if (timer == null) {
timer = new Timer();
}
String[] notifyData = (String[])((Object[])obj)[3];
managerMail = notifyData[2];
ccMail = notifyData[3];
task = new Task(mainFrame, stepData);
task.setID(ID);
task.setTaskName(taskName);
task.setManagerMail(managerMail);
task.setCcMail(ccMail);
if (runType == 1 && (getSubTask(null) != null)) {
return true;
}
Task[] subTasks = new Task[attempData.size()];
ArrayList<Task[]> attempInnerTasks = new ArrayList<Task[]>();
for(int i=0;i<attempData.size();i++){
String[] oneAttemp = (String[]) attempData.get(i);
setOneAttemp(oneAttemp);
subTasks[i] = getSubTask(attempInnerTasks);
if(subTasks[i] != null) {
result = true;
}
}
tasks.put(ID, subTasks);
innerTasks.put(ID, attempInnerTasks);
return result;
}
private Task getSubTask(ArrayList<Task[]> attempInnerTasks) {
Task result = null;
Date date = null;
int execCode = 0;
if(runType == 0) {
execCode = Integer.parseInt(oneAttemp[2]); // 調(diào)度類型代碼
}
else {
execCode = 1;
}
task.setExecCode(execCode);
// 立即執(zhí)行任務(wù)
if (execCode == 1) {
result = task.clone();
timer.schedule(result, 0);
}
// 一次性運(yùn)行任務(wù)
else if (execCode == 2) {
date = getDate(oneAttemp[6], oneAttemp[7], oneAttemp[8]);
if (date != null) {
Date nowDate = new Date();
if (date.after(nowDate)) {
result = task.clone();
timer.schedule(result, date);
}
else {
errorMsg += "當(dāng)前時(shí)間:"+Common.getDateString(nowDate)
+" 已過執(zhí)行時(shí)間:"+Common.getDateString(date)
+",無法啟動(dòng)調(diào)度:"+oneAttemp[1]+"\r\n";
}
}
}
// 重復(fù)運(yùn)行任務(wù)
else if (execCode == 3) {
date = getDate(oneAttemp[23], oneAttemp[24], oneAttemp[25]);
if (date !=null) {
int endTypeCode = Integer.parseInt(oneAttemp[18]);
if (endTypeCode == 1) {
endDate = getDate("", oneAttemp[19], oneAttemp[20]);
}
else {
int periodHour = Integer.parseInt(oneAttemp[21]);
int periodMinute = Integer.parseInt(oneAttemp[22]);
now.setTime(date);
now.add(Calendar.HOUR_OF_DAY, periodHour);
now.add(Calendar.MINUTE, periodMinute);
endDate = now.getTime();
}
Date current = new Date();
if (current.after(endDate)) {
String currentStr = Common.getDateString(current);
String endDateStr = Common.getDateString(endDate);
errorMsg += "當(dāng)前時(shí)間:"+currentStr+"已到結(jié)束時(shí)間:"+endDateStr+"\r\n";
return null;
}
task.setEndDate(endDate);
intervalCode = Integer.parseInt(oneAttemp[9]);
int period = 0;
if (intervalCode == 1) { // 每天
period = Integer.parseInt(oneAttemp[10]);
}
else if (intervalCode == 2) { // 每周
period = Integer.parseInt(oneAttemp[11]);
}
else if (intervalCode == 3) { // 每月
}
result = scheduleTask(date, period, attempInnerTasks);
}
}
return result;
}
private Task scheduleTask(
Date date,
final int period,
final ArrayList<Task[]> attempInnerTasks) {
Task outerTask = null;
int hourOrMinuteJI = Integer.parseInt(oneAttemp[16]);
int hourOrMinute = Integer.parseInt(oneAttemp[17]);
final int persistTime = hourOrMinute == 1 ?
hourOrMinuteJI*60 : hourOrMinuteJI;
if (persistTime == 0) {
JOptionPane.showMessageDialog(mainFrame, "小時(shí)/分鐘數(shù)必須大于零!");
setRunable(false);
}
else {
now.setTime(date);
final int theYear = now.get(Calendar.YEAR);
final int theMonth = now.get(Calendar.MONTH);
final int today = now.get(Calendar.DATE);
final int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
final Calendar c = Calendar.getInstance();
task.setExecCode(3);
task.setEndDate(endDate);
if (intervalCode == 1) { // 每天
if (period > 0) {
final Task subTask = task.clone();
outerTask = new Task(){
public void run() {
timer.schedule(subTask, 0, persistTime * 1000);
}
};
timer.schedule(outerTask, date, period * 24 * 60 * 60 * 1000);
attempInnerTasks.add(new Task[]{subTask});
}
else {
JOptionPane.showMessageDialog(mainFrame, "天數(shù)必須大于零!");
setRunable(false);
}
}
else if (intervalCode == 2) { // 每周
if (period > 0) {
String[] days = {};
if (oneAttemp[28] != null) {
days = oneAttemp[28].split(",");
}
final String[] daysClone = days.clone();
final int taskLen = days.length;
outerTask = new Task() {
int count;
public void run() {
Task[] innerWeekTasksClone = new Task[taskLen];
Date[] scheduleDatesClone = new Date[taskLen];
for (int i = 0; i < taskLen; i++) {
innerWeekTasksClone[i] = task.clone();
int day = Integer.parseInt(daysClone[i]) + 1;
int diffDay = dayOfWeek <= day ? (day - dayOfWeek)
: (7 + day - dayOfWeek);
c.set(Calendar.YEAR, theYear);
c.set(Calendar.MONTH, theMonth);
c.set(Calendar.DATE, today);
c.add(Calendar.DATE, count * period * 7);
c.add(Calendar.DAY_OF_WEEK, diffDay);
scheduleDatesClone[i] = c.getTime();
timer.schedule(
innerWeekTasksClone[i],
scheduleDatesClone[i],
persistTime * 60 * 1000
);
}
attempInnerTasks.add(innerWeekTasksClone);
count++;
}
};
timer.schedule(
outerTask, date, period * 7 * 24 * 60 * 60 * 1000);
}
else {
JOptionPane.showMessageDialog(mainFrame, "周數(shù)必須大于零!");
setRunable(false);
}
}
else if (intervalCode == 3) { // 每月
String[] months = {};
if (oneAttemp[29] != null) {
months = oneAttemp[29].split(",");
}
final String[] monthsClone = months.clone();
final int taskLen = months.length;
outerTask = new Task() {
int count;
public void run() {
Task[] innerMonthTasksClone = new Task[taskLen];
Date[] scheduleDatesClone = new Date[taskLen];
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
for (int i = 0; i < taskLen; i++) {
innerMonthTasksClone[i] = task.clone();
int month = Integer.parseInt(monthsClone[i]);
int diffMonth = theMonth <= month ? (month - theMonth)
: (12 + month - theMonth);
c.set(Calendar.YEAR, theYear);
c.set(Calendar.MONTH, month);
c.set(Calendar.DATE, today);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -