/*  
 * Copyright 2004-2009 unitarou <boss@unitarou.org>. 
 * All rights reserved.
 * 
 * This program and the accompanying materials are made available under the terms of 
 * the Common Public License v1.0 which accompanies this distribution, 
 * and is available at http://opensource.org/licenses/cpl.php
 * 
 * Contributors:
 *     unitarou - initial API and implementation
 */
package org.unitarou.yukinoshita.view.jface.act;

import org.eclipse.jface.action.Action;

import org.unitarou.lang.ArgumentChecker;
import org.unitarou.sgf.RootGameTree;
import org.unitarou.sgf.SgfId;
import org.unitarou.sgf.util.BasicFinder;
import org.unitarou.yukinoshita.view.WindowController;


/**
 * template.sgft@C©çVKÉûEléðì¬µÜ·B
 * @author UNITAROU &lt;boss@unitarou.org&gt;
 */
public class NewRgtFromTemplate extends Action {

	private final RootGameTree rgt_;
	
	private final WindowController controller_;
	/**
	 * @param rgt 
	 * @param controller
	 */
	public NewRgtFromTemplate(RootGameTree rgt, WindowController controller) {
		super();
		ArgumentChecker.throwIfNull(rgt, controller);
		rgt_ = rgt;
		controller_ = controller;
		setText();
	}
	
	/**
	 * ±ÌqfsÉ í¹½xðì¬µÜ·B 
	 */
	private void setText() {
		StringBuilder sb = new StringBuilder();
		switch (rgt_.getGameType()) {
		case GAME:
			sb.append(BasicFinder.findDatum(rgt_.getSequence(), SgfId.EVENT))
			  .append('/')
			  .append(BasicFinder.findDatum(rgt_.getSequence(), SgfId.GAME_NAME));
			break;

		case PROBLEM:
			sb.append(BasicFinder.findDatum(rgt_.getSequence(), SgfId.SOURCE))
			  .append('/')
			  .append(BasicFinder.findDatum(rgt_.getSequence(), SgfId.GAME_NAME));
			break;
			
		case DRILL:
			sb.append(BasicFinder.findDatum(rgt_.getSequence(), SgfId.GAME_NAME));
			break;
		}
		setText(sb.toString());
	}
	

	/* 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	@Override
	public void run() {
		switch (rgt_.getGameType()) {
		case GAME: 
			NewGameFileAction.execute(controller_, rgt_); 
			break;
			
		case PROBLEM:
			NewProblemFileAction.execute(controller_, rgt_);
			break;
			
		case DRILL:
			NewDrillFileAction.execute(controller_, rgt_);
			break;
		}
	}
}
