/* $Id: GUISaveSpreadSelectionListAction.java 343 2011-09-07 14:39:45Z shayashi $ */
package smart_gs.spread_selection;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import smart_gs.logical.SpreadSelectionList;
import smart_gs.logical.SpreadSelectionListManager;
import smart_gs.swingui.WorkspaceWindow;


public class GUISaveSpreadSelectionListAction implements ActionListener {
	
	private SpreadSelectionTree tree;

	public GUISaveSpreadSelectionListAction(SpreadSelectionTree givenTree) {
		super();
		tree = givenTree;
	}

//	@Override
	public void actionPerformed(ActionEvent arg0) {

		ArrayList<Integer> selectionList = tree.selectionList();
		String resultString = NormalizedSelectionListInString.convert(selectionList);
		ArrayList<SpreadSelectionList> SpreadSelectionLists = SpreadSelectionListManager.getInstance().getSpreadSelectionLists();

		String newSpreadSelectionListName = JOptionPane.showInputDialog(tree,"Input SpreadSelectionList name","");
		if (newSpreadSelectionListName != null){
			if (!(new SpreadSelectionListIdentifierGrammarChecker()).checks(newSpreadSelectionListName)){
				JOptionPane.showMessageDialog(tree,"name must be an non-empty string of 0-9,A-z,_-+=.&~:,Japanese zenkaku-moji starting with A-z or Japanese zenkaku-moji","",JOptionPane.PLAIN_MESSAGE);
				return;
			}
			SpreadSelectionListManager.getInstance().addSpreadSelectionList(newSpreadSelectionListName,resultString);
			Integer size = SpreadSelectionLists.size();
			Integer i = 0;
			while (i<size){
				System.out.println((SpreadSelectionLists.get(i)).toString());
				i++;
			}
		} 
	}
}

