/* $Id: GSEditor.java 291 2011-08-04 15:41:48Z ohura $ */
package smart_gs.smleditor.swingui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import smart_gs.all_text_search.SearchAllDialogManager;
import smart_gs.current_text_search.SearchDialogManager;
import smart_gs.drawing_tool.SpreadCanvas;
import smart_gs.drawing_tool.drawing_mode.Mode;
import smart_gs.drawing_tool.state.DefaultState;
import smart_gs.drawing_tool.view.RectangleView;
import smart_gs.drawing_tool.view.View;
import smart_gs.image_search.logical.SearchResult;
import smart_gs.logical.Region;
import smart_gs.logical.Spread;
import smart_gs.logical.TextSegment;
import smart_gs.swingui.AbstractGSWindow;
import smart_gs.swingui.GSTabbedPaneManager;
import smart_gs.swingui.IDATNameInGSTabbedPaneManager;
import smart_gs.swingui.toolbar.GSEditorToolBar;
import smart_gs.swingui.toolbar.HistoryToolBar;
import smart_gs.swingui.toolbar.ImageToolBar;
import smart_gs.swingui.toolbar.MoveTreeToolBar;
import smart_gs.swingui.toolbar.SearchToolBar;
import smart_gs.swingui.toolbar.UtilityToolBar;
import sml_editor.swingui.EditorTextPane;

import com.hexidec.ekit.GSHTMLEditorFoucsActionListner;
import com.hexidec.ekit.GSHTMLEditorSearchActionListner;


@SuppressWarnings("serial")
public class GSEditor extends AbstractGSWindow {

	public GSTabbedPane tabbedPane;
//	private GSEditorToolBar gsEditorToolBar;
	// SHOWを押した時の判別用ID
	private String url;
	protected int open = 0;
	protected AtomicLong id;
	private MoveTreeToolBar moveTreeToolBar;
	private Mode mode;
	private GSTabbedPaneWindowAdapter gsTabbedPaneWindowAdapter;

	//	private GSEditorToolBar editorToolBar;
//	private GSEditorTextPane textPane;
	public GSEditor(Spread spread)  {
		super("Editor: " + spread.getName());
		this.url = spread.getURI();
		this.tabbedPane = this.createTabbedPane(spread);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLayout(new BorderLayout());
		this.add(this.tabbedPane,BorderLayout.CENTER);

		JPanel gsEditorToolBarPanel = new JPanel(new GridLayout(2,1));
		JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		ImageToolBar imageToolBar = new ImageToolBar(this);
		panel1.add(imageToolBar);
		imageToolBar.enableIcons();
		panel1.add(new SearchToolBar(this));
		JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panel2.add(new UtilityToolBar(this));
		panel2.add(new HistoryToolBar(this));
		MoveTreeToolBar moveTreeToolBar = new MoveTreeToolBar(this);
		this.moveTreeToolBar = moveTreeToolBar;
		panel2.add(moveTreeToolBar);
		updateMoveToolBar();

		gsEditorToolBarPanel.add(panel1);
		gsEditorToolBarPanel.add(panel2);
		
		this.add(gsEditorToolBarPanel, BorderLayout.NORTH);
		
		this.setSize(900,700);
		
		id = GSTabbedPaneManager.getControlID();
		gsTabbedPaneWindowAdapter = new GSTabbedPaneWindowAdapter(this.url,String.valueOf(id),getSMLTabbedPane(),this);
		this.addWindowListener(gsTabbedPaneWindowAdapter);
	}

	public GSEditor(Spread spread, Rectangle2D rect) {
		this(spread);
		View view = new RectangleView(rect);
		view.setIsEmphasized(true);
		Point point = new Point((int)rect.getX(),(int)rect.getY());
		this.tabbedPane.getSpreadCanvas().setCenterLocation(point);
		this.tabbedPane.getSpreadCanvas().addTempView(view);
	}
	
	public GSEditor(List<SearchResult> results) {
		this(results.get(0).getSpread());
		for(int i=0;i<results.size();i++){
			Rectangle2D rect = results.get(i).getRectangle();
			View view = new RectangleView(rect);
			view.setIsEmphasized(true);
			this.tabbedPane.getSpreadCanvas().addTempView(view);
		}
		Point point = results.get(0).getRectangle().getLocation();
		this.tabbedPane.getSpreadCanvas().setCenterLocation(point);
	}
	public GSEditor(Region region) {
		this(region.getSpread());
		View view = region.getView();
		view.setIsEmphasized(true);
		Rectangle2D rect = view.getRectangle2D();
		Point point = new Point((int)rect.getX(),(int)rect.getY());
		this.tabbedPane.getSpreadCanvas().setCenterLocation(point);
		this.tabbedPane.getSpreadCanvas().addTempView(view);
		
	}
	public GSEditor(TextSegment segment) {
		super("Editor");
	}

	public void setEditorTextPane(EditorTextPane textPane) {
		textPane.setSearchAction(new SearchAction(getGSEditorToolBar()));
		textPane.setFocusAction(new FoucsAction(this, getGSEditorToolBar()));
		this.tabbedPane.getGSEditorToolBar().setTextPane(textPane);
	}

	public void setEditorToolBar(JToolBar toolbar) {
		
	}
	public Spread getSpread() {
		return this.tabbedPane.getSpread();
	}
	
	public GSTabbedPane getSMLTabbedPane(){
		return this.tabbedPane;
	}

	public void updateMoveToolBar() {
		if(this.tabbedPane.getSpread() != null){
			this.moveTreeToolBar.setPathCount(this.tabbedPane.getSpread().getPageViewIndex());
		}
	}

	@Override
	public void updateGUI(Spread spread) {
		validate();
	}
	@Override
	public SpreadCanvas getSpreadCanvas() {
		return this.tabbedPane.canvas;
	}
	public Frame getFrame() {
		return this;
	}
	@Override
	public GSTabbedPane createTabbedPane(Spread spread) {
		return new GSTabbedPane(this,spread);
	}
	public void emphasize(String id) {
		this.tabbedPane.emphasize(id);
	}
	public void updateText() {
		this.tabbedPane.updateText();
	}

	public String getURL(){
		return url;
	}
	
	public AtomicLong getControlID(){
		return id;
	}
	public 	GSEditorToolBar getGSEditorToolBar(){
		return this.tabbedPane.getGSEditorToolBar();
	}
	
	// 20080916 hashimoto.
	// 現在選択されているタブで、カーソルを与えられたインデックスに移動させるメソッド
	public void setCaret(int index){
		getSMLTabbedPane().setCaret(index);
	}

	public void updateWindowListener() {
		this.removeWindowListener(this.gsTabbedPaneWindowAdapter);
		id = GSTabbedPaneManager.getControlID();
		gsTabbedPaneWindowAdapter = new GSTabbedPaneWindowAdapter(this.url,String.valueOf(id),getSMLTabbedPane(),this);
		this.addWindowListener(gsTabbedPaneWindowAdapter);
	}

	class GSTabbedPaneWindowAdapter extends WindowAdapter{
		protected String key;
		protected String id;
		protected GSTabbedPane gsTabbedPane;
		private AbstractGSWindow parent;
		
		public GSTabbedPaneWindowAdapter(String key,String id,GSTabbedPane gsTabbedPane, AbstractGSWindow parent){
			this.key = key;
			this.id = id;
			this.gsTabbedPane = gsTabbedPane;
			this.parent = parent;
		}
		public void windowClosing(WindowEvent e) {
			IDATNameInGSTabbedPaneManager.unregistorGSEditorTabbedPane(gsTabbedPane);
		}
		public void windowActivated(WindowEvent e) {
			// 検索ウィンドウの設定
			SearchDialogManager.setGSEditorToolBar(getGSEditorToolBar());
			GSTabbedPane newTabbedPane = GSTabbedPaneManager.getGSTabbedPane(key);
        	if(newTabbedPane != null){
        		String windowid = GSTabbedPaneManager.getWindowsID(key);
        		if(windowid == null){
        			return;
        		}
        		if(windowid.equals(key + id)){
        			return;
        		}
        		newTabbedPane.save();
	        	String fstidatSource = newTabbedPane.getFirstIDATSource();
	        	this.gsTabbedPane.updateFirstIDAT(fstidatSource);

	        	String sndidatSource = newTabbedPane.getSecondIDATSource();
	        	this.gsTabbedPane.updateSecondIDAT(sndidatSource);

	        	String thrdidatSource = newTabbedPane.getThirdIDATSource();
	        	this.gsTabbedPane.updateThirdIDAT(thrdidatSource);

	        	this.gsTabbedPane.updateText();
	        	this.gsTabbedPane.save();
	        	GSTabbedPaneManager.removeGSTabbedPane(key);
        	}
        	open = 1;

        	Mode mode = ((GSEditor) parent).getMode();
        	if(mode != null) {
        		DefaultState.getInstance().setMode(mode);
        	}
        }

		public void windowDeactivated(WindowEvent e) {
        	this.gsTabbedPane.save();
        	GSTabbedPaneManager.setGSTabbedPane(key, key + id ,gsTabbedPane);
        }
		public void windowClosed(WindowEvent e) {
        	open = 2;
        	GSTabbedPaneManager.close(key + id);
        }
	}
	
	public int isOpened(){
		return open;
	}

	private class SearchAction implements GSHTMLEditorSearchActionListner{
		protected GSEditorToolBar gsEditorToolBar;
		public SearchAction(GSEditorToolBar gsEditorToolBar) {
			this.gsEditorToolBar = gsEditorToolBar;
		}
		public void actionPerformed() {
			SearchDialogManager.setGSEditorToolBar(gsEditorToolBar);
			SearchDialogManager.visible();
		}
	}

	private class FoucsAction implements GSHTMLEditorFoucsActionListner{
		protected GSEditorToolBar gsEditorToolBar;
		protected GSEditor gsEditor;
		public FoucsAction(GSEditor gsEditor,GSEditorToolBar gsEditorToolBar) {
			this.gsEditor = gsEditor; 
			this.gsEditorToolBar = gsEditorToolBar;
		}
		public void focusGained() {
			SearchAllDialogManager.setEditor(gsEditor, gsEditorToolBar);
			SearchDialogManager.setGSEditorToolBar(gsEditorToolBar);
		}
		public void focusLost() {
			
		}
	}
	
	public void setMode(Mode mode) {
		this.mode = mode;
	}
	
	public Mode getMode() {
		return this.mode;
	}

	public void setURL(String url) {
		this.url = url;
	}

}
