/* $Id: SearchAllDialogManager.java 125 2011-03-09 09:49:51Z ohura $ */
package smart_gs.all_text_search;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;

import smart_gs.logical.UsersNote;
import smart_gs.logical.UsersNoteTree;
import smart_gs.logical.Spread;
import smart_gs.logical.SpreadTree;
import smart_gs.smleditor.swingui.GSEditor;
import smart_gs.smleditor.swingui.GSTabbedPane;
import smart_gs.smleditor.swingui.GSTreeTextEditor;
import smart_gs.swingui.toolbar.GSEditorToolBar;

/**
 * 
 * 全体の検索ダイアログの管理クラス.
 *
 */
public class SearchAllDialogManager {
	/**
	 * 操作ダイアログ.
	 */
	protected static SearchTextAllDialog searchallTextDialog;
	/**
	 * 検索対象特定クラス.
	 */
	protected static SearchAllUtil searchAllUtil;
	/**
	 * 現在操作しているダイアログ.
	 */
	protected static JFrame currenteditor;

	/**
	 * 操作対象のエディタを設定する.
	 * @param editor
	 * @param gsEditorToolBar
	 */
	public static synchronized void setEditor(JFrame editor,GSEditorToolBar gsEditorToolBar){
		if(searchallTextDialog == null){
			searchallTextDialog = new SearchTextAllDialog(new JFrame(),"Search All Dialog");
			searchallTextDialog.setLocationRelativeTo(null);
		}
		currenteditor = editor;
		searchallTextDialog.setEditor(editor,gsEditorToolBar);
//		if(searchAllUtil == null){
//			searchAllUtil = new SearchAllUtil();
//		}
	}
	/**
	 * ウィンドウを表示する.
	 */
	public static synchronized void visible(){
		if(searchallTextDialog != null){
			if(!searchallTextDialog.isVisible()){
				searchallTextDialog.setAlwaysOnTop(true);
				searchallTextDialog.setVisible(true);
			}
		}
	}
	public static synchronized boolean isVisible(){
		return searchallTextDialog.isVisible();
	}
	public static synchronized void setting(String searchText,String rplaceText,boolean isRebular,boolean isCase){
		if(searchallTextDialog == null){
			searchallTextDialog = new SearchTextAllDialog(new JFrame(),"Search All Dialog");
			searchallTextDialog.setLocationRelativeTo(null);
		}
		searchallTextDialog.setSearchTextField(searchText);
		searchallTextDialog.setReplaceTextField(rplaceText);
		searchallTextDialog.setRegularCheckBox(isRebular);
		searchallTextDialog.setCaseCheckBox(isCase);
		
		
	}
	public static synchronized void clearselect(){
		searchallTextDialog.clearselect();
	}
	public static synchronized void search(final String relace){
		boolean searchway = true;
		if(searchallTextDialog.isForwardButton()){
			searchway = true;
		}
		if(searchallTextDialog.isBackwordButton()){
			searchway = false;
		}
		searchallTextDialog.search(searchway, relace, false);
	}
	public static String getSearchTextField(){
		return searchallTextDialog.getSearchTextField();
	}
	public static boolean isCaseCheckBox(){
		return searchallTextDialog.isCaseCheckBox();
	}
	public static boolean isRegularCheckBox(){
		return searchallTextDialog.isRegularCheckBox();
	}
	
	public static synchronized void findfirst(int documentType_,int scope,String spreadSelection){
		if(searchAllUtil == null){
			searchAllUtil = new SearchAllUtil(scope,spreadSelection);
		}
		searchAllUtil.findfirst(documentType_);
	}

	public static synchronized boolean next(final String replace){
		if(currenteditor != null){
			if(currenteditor instanceof GSEditor){
				synchronized (currenteditor) {
					GSEditor gsEditor = (GSEditor)currenteditor;
					String url = gsEditor.getURL();
					List<Spread> list = SpreadTree.getInstance().getSpreads();
					for(int i=0;i<list.size();i++){
						Spread spread = list.get(i);
						if(spread.getURI().equals(url)){
							searchAllUtil.setSpreadnumber(i);
							GSTabbedPane gsTabbedPane = gsEditor.getSMLTabbedPane();
							searchAllUtil.setTabnumber(gsTabbedPane.getSelectedIndex());
							currenteditor = null;
							break;
						}
					}
				}
			}else if(currenteditor instanceof GSTreeTextEditor){
				synchronized (currenteditor) {
					GSTreeTextEditor gsEditor = (GSTreeTextEditor)currenteditor;
					int id = gsEditor.getDocumentID();
					ArrayList<UsersNote> arrList = (ArrayList<UsersNote>) UsersNoteTree.getInstance().getUsersNotes();
					for(int i=0;i<arrList.size();i++){
						UsersNote spread = arrList.get(i);
						if(spread.getId() == id){
							searchAllUtil.setSpreadnumber(Integer.MAX_VALUE);
							searchAllUtil.setTextNumber(i+1);
							currenteditor = null;
							break;
						}
					}
				}
			}
		}
		return searchAllUtil.next(replace);
	}
	public static synchronized boolean prev(final String replace){
		if(currenteditor != null){
			if(currenteditor instanceof GSEditor){
				synchronized (currenteditor) {
					GSEditor gsEditor = (GSEditor)currenteditor;
					String url = gsEditor.getURL();
					List<Spread> list = SpreadTree.getInstance().getSpreads();
					for(int i=0;i<list.size();i++){
						Spread spread = list.get(i);
						if(spread.getURI().equals(url)){
							searchAllUtil.setSpreadnumber(i);
							GSTabbedPane gsTabbedPane = gsEditor.getSMLTabbedPane();
							searchAllUtil.setTabnumber(gsTabbedPane.getSelectedIndex());
							currenteditor = null;
							break;
						}
					}
				}
			}else if(currenteditor instanceof GSTreeTextEditor){
				synchronized (currenteditor) {
					GSTreeTextEditor gsEditor = (GSTreeTextEditor)currenteditor;
					int id = gsEditor.getDocumentID();
					ArrayList<UsersNote> arrList = (ArrayList<UsersNote>) UsersNoteTree.getInstance().getUsersNotes();
					for(int i=0;i<arrList.size();i++){
						UsersNote spread = arrList.get(i);
						if(spread.getId() == id){
							searchAllUtil.setSpreadnumber(Integer.MAX_VALUE);
							searchAllUtil.setTextNumber(i-1);
							currenteditor = null;
							break;
						}
					}
				}
			}
		}
		return searchAllUtil.prev(replace);
	}
	public static synchronized boolean nextrelaceall(){
		return searchAllUtil.nextrelaceall();
	}
	public static synchronized boolean relaceallnonewwindows(final String replace){
		return searchAllUtil.relaceallnonewwindows(replace);
	}
	public static synchronized  void setstart(){
		searchAllUtil.setstart();
	}
	
	public static JDialog getDialog(){
		return searchallTextDialog;
	}
}
