/* $Id: SetScopePanel.java 569 2012-09-12 01:12:33Z shayashi $ */
package smart_gs.spread_selection;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.text.StyleContext;
import javax.swing.JLabel;
import smart_gs.dictionary.DictionaryXML;
import smart_gs.logical.Spread;
import smart_gs.logical.SpreadTree;
/**
 * 検索範囲（spread）の選択のためのパネルを作る。
 * なかのロジックだけを別のクラスに移すことも考えられる。
 * @author Administrator
 *
 */
public class SetScopePanel extends JPanel{
	private JRadioButton all;
	private JRadioButton currentDirectory;	
	private JRadioButton selectSpread;
	private JRadioButton imageDictionary;
//	20101029 shayashi
	private JPanel readSavePanel;
	private JButton readSelectedSpread;
	private JButton saveSelectedSpread;
	private JButton showSpreadSelectionLists;
	private JTextField pageSelectField;
	
	//20081031 North Grid Add
	private static boolean allSelected = true;
	private static boolean currentDirectorySelected = false;
	private static boolean selectSpreadSelected = false;
	private static boolean imageDictionarySelected = false;
	private static String pageSelectFieldText = "";

	private ScopePanelAction scopePanelAction;
	private SetScopePanel mySetScopePanel;
	
	public SetScopePanel(){
		this(false);
	}
	public SetScopePanel(boolean imagemode){
		if(!imagemode){
			allSelected = true;
			imageDictionarySelected = false;
		}

		this.setLayout(new BorderLayout());
		this.setBorder(new TitledBorder("Scope (Search Scope)"));
		this.all = new JRadioButton(imagemode?"All Images":"All Spreads");
		this.all.setSelected(allSelected);
		this.currentDirectory = new JRadioButton("Current Directory");
		this.currentDirectory.setSelected(currentDirectorySelected);
		
		this.selectSpread = new JRadioButton("Spread Selection List");
		this.selectSpread.setSelected(selectSpreadSelected);		
		this.imageDictionary = new JRadioButton("Image Dictionary");
		this.imageDictionary.setSelected(imageDictionarySelected);
		
		ButtonGroup group1 = new ButtonGroup();
		
		group1.add(this.all);
		group1.add(this.currentDirectory);
		group1.add(this.selectSpread);
		// ADD NorthGrid image検索のみ追加
		if(imagemode){
			group1.add(this.imageDictionary);
		}
		
		JPanel leftPanel = new JPanel();
		
		leftPanel.setLayout(new GridLayout(4,1));
		
		leftPanel.add(all);
		leftPanel.add(currentDirectory);
		// ADD NorthGrid image検索のみ追加
		leftPanel.add(imageDictionary);
		if(imagemode){
			imageDictionary.setEnabled(true);
		} else {
			imageDictionary.setEnabled(false);
		}
		leftPanel.add(selectSpread);
		

		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new GridLayout(4,1));
		
		pageSelectField = new JTextField();
		pageSelectField.setFont(new Font(StyleContext.DEFAULT_STYLE,Font.PLAIN,12));
		pageSelectField.setText(pageSelectFieldText);
		
		rightPanel.add(pageSelectField);
		rightPanel.add(new JLabel("e.g.:2,4,6-10"));
				
		this.readSelectedSpread = new JButton("Read");
		this.readSelectedSpread.addActionListener(new ReadSpreadSelectionListAction(this));
		
		this.saveSelectedSpread = new JButton("Save");
		this.saveSelectedSpread.addActionListener(new SaveSpreadSelectionListAction(this));
		
		this.showSpreadSelectionLists = new JButton("Show");
		this.showSpreadSelectionLists.addActionListener(new GUIShowSpreadSelectionListsAction(showSpreadSelectionLists,	this.pageSelectField));
		
		this.readSavePanel = new JPanel();
		
		readSavePanel.add(readSelectedSpread);
		readSavePanel.add(saveSelectedSpread);
		readSavePanel.add(showSpreadSelectionLists);
		
		rightPanel.add(readSavePanel);
		
		this.mySetScopePanel = this;
		
		this.add(leftPanel,BorderLayout.WEST);
		this.add(rightPanel,BorderLayout.EAST);
		
		
		this.all.addActionListener(new ScopeSelectAction());
		this.selectSpread.addActionListener(new ScopeSelectAction());
		this.currentDirectory.addActionListener(new ScopeSelectAction());
		this.imageDictionary.addActionListener(new ScopeSelectAction());
		
		new ScopeSelectAction().actionPerformed(null);
	}

	public JTextField getPageSelectField () { return pageSelectField; }

	public List<Spread> getSpreadsToSearch(){
		if(this.all.isSelected()){
			return SpreadTree.getInstance().getSpreads();
		}
		else if(this.currentDirectory.isSelected()){
			return SpreadTree.getInstance().getCurrentDirectory().getSpreads();
		}
		else if(this.selectSpread.isSelected()){
			String spreadSelection = this.pageSelectField.getText();
//			shayashi 20101028
			if (!(new SpreadSelectionListGrammarChecker()).checks(spreadSelection)) {
				JOptionPane.showMessageDialog(this, spreadSelection + ": ill-formed selection list", "Error", JOptionPane.ERROR_MESSAGE);			
				return null;	
			}
			SpreadSelector selector = new SpreadSelector( SpreadTree.getInstance().getSpreads(), spreadSelection);
			return selector.select();
		}else if(this.imageDictionary.isSelected()){
			DictionaryXML dictionaryXML = DictionaryXML.getInstance();
			return dictionaryXML.getLoadSpreads();
		}
		return null;
	}
	public List<Spread> getDSCSpreads(List<Spread> spreads){
		List<Spread> results = new ArrayList<Spread>();
		for(int i=0;i<spreads.size();i++){
			if(spreads.get(i).hasDscFile()){
				results.add(spreads.get(i));
			}
		}
		return results;
	}
	public String getSelectedPage(){
		pageSelectFieldText = pageSelectField.getText();
		return this.pageSelectField.getText();
	}
	public class ScopeSelectAction implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(selectSpread.isSelected()){
				pageSelectField.setEnabled(true);
				readSelectedSpread.setEnabled(true);
				saveSelectedSpread.setEnabled(true);
			}
			else if(all.isSelected() || currentDirectory.isSelected() || imageDictionary.isSelected()){
				pageSelectField.setEnabled(false);
				readSelectedSpread.setEnabled(false);
				saveSelectedSpread.setEnabled(false);
			}
			allSelected = all.isSelected();
			currentDirectorySelected = currentDirectory.isSelected();
			selectSpreadSelected = selectSpread.isSelected();
			imageDictionarySelected = imageDictionary.isSelected();
			if(scopePanelAction != null){
				scopePanelAction.actionPerformed(mySetScopePanel);
			}
		}
	}
	/*
	 * 2008,0901. 
	 * パネル操作を有効化・無効化する 
	 */
	public void setEnabled(boolean flag){
		all.setEnabled(flag);
		currentDirectory.setEnabled(flag);
		selectSpread.setEnabled(flag);
		pageSelectField.setEnabled(flag);
		imageDictionary.setEnabled(flag);
	}
	
	public boolean isAll(){
		return this.all.isSelected();
	}
	public boolean isCurrentDirectory(){
		return this.currentDirectory.isSelected();
	}
	public boolean isSelectSpread(){
		return this.selectSpread.isSelected();
	}
	public boolean isImageDictionary(){
		return this.imageDictionary.isSelected();
	}
	public String  getPageSelectFieldString(){
		return this.pageSelectField.getText();
	}
	public void setScopePanelAction(ScopePanelAction scopePanelAction) {
		this.scopePanelAction = scopePanelAction;
	}
	public void execScopePanelAction(){
		if(scopePanelAction != null){
			scopePanelAction.actionPerformed(mySetScopePanel);
		}
	}
	
}
