📄 taskmgr.java
字号:
package net.sujee.javatimer;import net.sujee.javatimer.Task;import net.sujee.javatimer.TimerCallback;import net.sujee.javatimer.Timer;import java.util.Vector;import java.util.ArrayList;import java.util.Date;import java.io.File;import java.io.IOException;import java.io.FileNotFoundException;import java.io.EOFException;import java.io.StreamCorruptedException;import java.io.ObjectOutputStream;import java.io.ObjectInputStream;import java.io.FileOutputStream;import java.io.FileInputStream;import javax.swing.*;import java.awt.*;public class TaskMgr implements TimerCallback{ private Vector mTasks; //private ArrayList mTasks; private File mDataFile; private int mActiveTaskIndex = -1; private Timer mTimer; private boolean mStayAlive; private long lastTime = -1; public TaskMgr (String filename) throws IOException { this (new File (filename)); } public TaskMgr (File file) throws IOException { mDataFile = file; mTasks = new Vector (); readFromDisk(); //setupTimer (); } public TaskMgr () { mTasks = new Vector(); //setupTimer (); } public void setFile (File file) throws IOException { if ( file.isFile() && file.exists() && file.canRead() && file.canWrite() ) mDataFile = file; else if ( !file.exists() ) { file.createNewFile(); mDataFile = file; } else throw new IOException ("File is not file or doesn't have right permissions"); } public synchronized void writeToDisk () throws IOException { ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream (mDataFile)); for (int i=0 ; i < mTasks.size() ; ++i) { Task task = (Task) mTasks.get(i); os.writeObject(task); // FIXME : Should I cast the obj into task or can I write it as // 'plain' object } os.flush(); os.close(); } public synchronized void readFromDisk () { if (mDataFile == null) return; try { ObjectInputStream is = new ObjectInputStream (new FileInputStream (mDataFile)); mTasks.clear(); Object obj; while ( (obj = is.readObject()) != null) { Task task = (Task) obj; mTasks.add( task); // when reading, we want all tasks to be 'not running' if (task.isAlive() ) { task.setAlive(false); if ( Global.traceLevel >=3 ) Global.logWriter.log ("** read task : " + task.toString()); } else if ( Global.traceLevel >=3 ) Global.logWriter.log ("read task : " + task.dump()); } } catch (EOFException ex) { // harm less exception } catch (FileNotFoundException ex2) { ex2.printStackTrace(); } catch (StreamCorruptedException ex3) { ex3.printStackTrace(); } catch (ClassNotFoundException ex4) { ex4.printStackTrace(); } catch (IOException ex1) { ex1.printStackTrace(); } } public Vector listTasks () { return new Vector (mTasks); } public void createTask (String taskName, String description) { Task task = new Task (taskName, description); mTasks.add (task); } public synchronized void createTask (Task t) { mTasks.add(t); } public synchronized void deleteTask (int pos) { mTasks.remove(pos); } public void deleteTask (int [] indexArray) { // have to be careful, can't delete the array one by one! // as the vector will change as we delete. // first gather all the tasks.... Task [] taskArray = new Task [indexArray.length]; for (int i=0; i < indexArray.length ; ++i) taskArray [i] = (Task) mTasks.get(indexArray[i]); // now we can blow away the tasks... for (int i=0; i < taskArray.length ; i++) deleteTask(taskArray[i]); } public synchronized void deleteTask (Task task) { mTasks.remove(task); } public synchronized void setTaskAlive (int index, boolean aliveFlag) { if ( (index >=0) && (index <= mTasks.size() ) ) { Task t = (Task) mTasks.get (index); t.setAlive(aliveFlag); if ( (mActiveTaskIndex != -1) && (mActiveTaskIndex != index) ) { // disable the current running flag t = (Task) mTasks.get(mActiveTaskIndex); t.setAlive(!aliveFlag); } mActiveTaskIndex = (aliveFlag) ? index : -1; } } public int getTaskCount () { return mTasks.size(); } public Task getTask (int index) { if ( (index >= 0) && (index < mTasks.size()) ) return (Task) mTasks.get(index); else return null; } public Task getTask (String name) { for (int i=0; i < mTasks.size(); ++i) { Task t = (Task) mTasks.get(i); if (t.getName().equals(name)) return t; } return null; } public synchronized void setTask (int index, Task t) { if ( (index >= 0) && (index < mTasks.size()) ) mTasks.set(index, t); } //public void tick (long msecs) public synchronized void tick () { //Global.logWriter.log(new Date().toString() + " : TaskMgr::tick()"); if (lastTime == -1) lastTime = System.currentTimeMillis(); // timer call back. Update the running tasks with elapsed time // at this version there can be only one running task if ( (mActiveTaskIndex >= 0) && (mActiveTaskIndex < mTasks.size() )) { Task task = (Task) mTasks.get(mActiveTaskIndex); task.addTime( (System.currentTimeMillis() - lastTime) / 1000.0); } lastTime = System.currentTimeMillis(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -