package com.interpress_project.modernshare.client.controller.actions;

import com.interpress_project.modernshare.client.controller.command.ISVNCommand;
import com.interpress_project.modernshare.client.controller.command.SVNCommandFactory;
import com.interpress_project.modernshare.client.controller.command.SVNCommandManager;
import com.interpress_project.modernshare.client.controller.command.TaskManager;
import com.interpress_project.modernshare.client.controller.model.RootStatus;
import com.interpress_project.modernshare.client.events.GUIEvent;
import com.interpress_project.modernshare.client.events.GUIEventType;
import com.interpress_project.modernshare.client.view.ViewManager;

/**
 *@stereotype action
 */
public final class DeActivateTargetAction extends ActionAdaptor {
	private SVNCommandManager cmdmgr = SVNCommandManager.getInstance();
	private SVNCommandFactory factory = SVNCommandFactory.getInstance();
	private ViewManager vmgr;
	private String path;

	/**
	 * DeActivateTargetAction
	 * @param vmgr
	 * @param path
	 */
	public DeActivateTargetAction(ViewManager vmgr, String path) {
		setText("自動監視を停止(&T)");
		setToolTipText("対象となるホットショットフォルダに対する自動監視を停止します。");
		try {
			setImageDescriptor(ImageFactory.getInstance().getImageDescriptor("unconnected"));
		}
		catch (Exception ex) {
			sb.getLogger().fatal("リソース設定のエラーが発生しました。", ex);
			System.exit(-1);
		}
		this.vmgr = vmgr;
		this.path = path;
	}

	/**
	 * setDeActive
	 * @param path
	 */
	private void setDeActive(String path) {
		ISVNCommand command = factory.createActiveTargetCommand(path, RootStatus.deactive);
		cmdmgr.addCommand(command);
	}

	/**
	 * run
	 */
	public void run() {
		TaskManager taskmgr = TaskManager.getInstance();
		try {
			taskmgr.stop();

			boolean bAnswer = vmgr.getDialogManager().createYesNoMessageDialog("自動監視の停止確認",
			  "ホットショットフォルダ" + path + "に対する自動監視を停止します。");
			if (bAnswer == false) {
				return;
			}
			setDeActive(path);
		}
		finally {
			guiEventSource.fireEvent(new GUIEvent(this, "ホットショットフォルダ" + path + "に対する自動監視を停止しました。", GUIEventType.FINISH));
			taskmgr.start();
		}
	}
}
