activator.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 102 行

JAVA
102
字号
package net.sf.pim.scheduler;

import java.util.List;

import net.sf.util.persistence.DMFactory;
import net.sf.util.persistence.IDataManager;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.jcrontab.Crontab;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "net.sf.pim.scheduler";

	// The shared instance
	private static Activator plugin;

	// This variable defines the Crontab
	static private Crontab crontab = null;

	/**
	 * The constructor
	 */
	public Activator() {
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		crontab = Crontab.getInstance();
		try {
			ShutdownHook();
			crontab.getInstance().init();
			System.out.println("Working...");
			
			//初始判断如果没有设置scheduler,则增加三条默认值:下班了、周报、注意眼睛
			IDataManager dataManager = DMFactory.getDataManager(Scheduler.class);
			List list = dataManager.readList("");
			if(list.isEmpty()){
				Scheduler sd=new Scheduler();
				sd.title="下班了"; sd.minute="30"; sd.hour="18"; sd.day="*"; sd.month="*"; sd.week="*"; sd.command="scheduler.Alert"; sd.param="下班了 身体是革命的本钱";
				list.add(sd);
				sd=new Scheduler();
				sd.title="周报"; sd.minute="15"; sd.hour="15"; sd.day="*"; sd.month="*"; sd.week="5"; sd.command="scheduler.Alert"; sd.param="周报 该写周报了";
				list.add(sd);
				sd=new Scheduler();
				sd.title="注意眼睛"; sd.minute="55"; sd.hour="9,10,13,14,15,16"; sd.day="*"; sd.month="*"; sd.week="*"; sd.command="scheduler.Alert"; sd.param="EYE 心灵的窗口需要保护";
				list.add(sd);
				dataManager.createList(list);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	public static void ShutdownHook() throws Exception {
		try {
			Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {
					System.out.println("Shutting down...");
					// stops the system in 200 miliseconds :-)
					crontab.uninit(200);
					System.out.println("Stoped");
				}
			});
		} catch (Exception e) {
			throw new Exception(e.toString());
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?